Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/network/MACAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ bool StringToEther(const string &address, ether_addr *target) {
}

for (unsigned int i = 0; i < MACAddress::LENGTH; i++) {
if (!ola::HexStringToInt(tokens[i], target->ether_addr_octet + i)) {
if (!ola::StringToInt(tokens[i], target->ether_addr_octet + i, true, 16)) {
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions common/rdm/UID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ UID* UID::FromString(const string &uid) {

uint16_t esta_id;
unsigned int device_id;
if (!ola::HexStringToInt(tokens[0], &esta_id)) {
if (!ola::StringToInt(tokens[0], &esta_id, true, 16)) {
return NULL;
}
if (!ola::HexStringToInt(tokens[1], &device_id)) {
if (!ola::StringToInt(tokens[1], &device_id, true, 16)) {
return NULL;
}

Expand Down
178 changes: 77 additions & 101 deletions common/utils/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,17 @@ bool StringToBoolTolerant(const string &value, bool *output) {
return false;
}

bool StringToInt(const string &value, unsigned int *output, bool strict) {
bool StringToInt(const string &value,
uint32_t *output,
bool strict,
uint8_t base) {
if (value.empty()) {
return false;
}
char *end_ptr;
errno = 0;
long long l = strtoll(value.data(), &end_ptr, 10); // NOLINT(runtime/int)
if (l < 0 || (l == 0 && errno != 0)) {
unsigned long l = strtoul(value.data(), &end_ptr, base); // NOLINT(runtime/int)
if (l == 0 && errno != 0) {
return false;
}
if (value == end_ptr) {
Expand All @@ -168,44 +171,56 @@ bool StringToInt(const string &value, unsigned int *output, bool strict) {
if (strict && *end_ptr != 0) {
return false;
}
if (l > static_cast<long long>(UINT32_MAX)) { // NOLINT(runtime/int)
if (l != static_cast<uint32_t>(l)) { // NOLINT(runtime/int)
return false;
}
*output = static_cast<unsigned int>(l);
*output = static_cast<uint32_t>(l);
return true;
}

bool StringToInt(const string &value, uint16_t *output, bool strict) {
unsigned int v;
if (!StringToInt(value, &v, strict)) {
bool StringToInt(const string &value,
uint16_t *output,
bool strict,
uint8_t base) {
uint32_t v;
if (!StringToInt(value, &v, strict, base)) {
return false;
}
if (v > UINT16_MAX) {
if (v != static_cast<uint16_t>(v)) {
return false;
}
*output = static_cast<uint16_t>(v);
return true;
}

bool StringToInt(const string &value, uint8_t *output, bool strict) {
unsigned int v;
if (!StringToInt(value, &v, strict)) {
bool StringToInt(const string &value,
uint8_t *output,
bool strict,
uint8_t base) {
uint32_t v;
if (!StringToInt(value, &v, strict, base)) {
return false;
}
if (v > UINT8_MAX) {
if (v != static_cast<uint8_t>(v)) {
return false;
}
*output = static_cast<uint8_t>(v);
return true;
}

bool StringToInt(const string &value, int *output, bool strict) {
bool StringToInt(const string &value,
int32_t *output,
bool strict,
uint8_t base) {
if (value.empty()) {
return false;
}
char *end_ptr;
errno = 0;
long long l = strtoll(value.data(), &end_ptr, 10); // NOLINT(runtime/int)

long l = strtol(value.data(), &end_ptr, base); // NOLINT(runtime/int)
int32_t i = static_cast<int32_t>(l);

if (l == 0 && errno != 0) {
return false;
}
Expand All @@ -215,34 +230,69 @@ bool StringToInt(const string &value, int *output, bool strict) {
if (strict && *end_ptr != 0) {
return false;
}
if (l < INT32_MIN || l > INT32_MAX) {
if (i < 0 && base != 10) {
if (l > 0 && l != static_cast<uint32_t>(l)) {
return false;
} else if(l < 0 && i != l) {
return false;
}
} else if (l != static_cast<int32_t>(l)) {
return false;
}
*output = static_cast<unsigned int>(l);

*output = static_cast<int32_t>(l);
return true;
}

bool StringToInt(const string &value, int16_t *output, bool strict) {
int v;
if (!StringToInt(value, &v, strict)) {
bool StringToInt(const string &value,
int16_t *output,
bool strict,
uint8_t base) {
int32_t v;
int16_t i;

if (!StringToInt(value, &v, strict, base)) {
return false;
}
if (v < INT16_MIN || v > INT16_MAX) {
i = static_cast<int16_t>(v);

if (i < 0 && base != 10) {
if (v > 0 && v != static_cast<uint16_t>(v)) {
return false;
} else if(v < 0 && i != v) {
return false;
}
} else if (v != i) {
return false;
}
*output = static_cast<int16_t>(v);

*output = i;
return true;
}

bool StringToInt(const string &value, int8_t *output, bool strict) {
int v;
if (!StringToInt(value, &v, strict)) {
bool StringToInt(const string &value,
int8_t *output,
bool strict,
uint8_t base) {
int32_t v;
int8_t i;

if (!StringToInt(value, &v, strict, base)) {
return false;
}
if (v < INT8_MIN || v > INT8_MAX) {
i = static_cast<int8_t>(v);

if (i < 0 && base != 10) {
if (v > 0 && v != static_cast<uint8_t>(v)) {
return false;
} else if(v < 0 && i != v) {
return false;
}
} else if (v != i) {
return false;
}
*output = static_cast<int8_t>(v);

*output = i;
return true;
}

Expand Down Expand Up @@ -327,80 +377,6 @@ void ReplaceAll(string *original, const string &find, const string &replace) {
}
}

bool HexStringToInt(const string &value, uint8_t *output) {
uint32_t temp;
if (!HexStringToInt(value, &temp)) {
return false;
}
if (temp > UINT8_MAX) {
return false;
}
*output = static_cast<uint8_t>(temp);
return true;
}

bool HexStringToInt(const string &value, uint16_t *output) {
uint32_t temp;
if (!HexStringToInt(value, &temp)) {
return false;
}
if (temp > UINT16_MAX) {
return false;
}
*output = static_cast<uint16_t>(temp);
return true;
}

bool HexStringToInt(const string &value, uint32_t *output) {
if (value.empty()) {
return false;
}

size_t found = value.find_first_not_of("ABCDEFabcdef0123456789");
if (found != string::npos) {
return false;
}
*output = strtoul(value.data(), NULL, 16);
return true;
}

bool HexStringToInt(const string &value, int8_t *output) {
int32_t temp;
if (!HexStringToInt(value, &temp)) {
return false;
}
if (temp < 0 || temp > static_cast<int32_t>(UINT8_MAX)) {
return false;
}
*output = static_cast<int8_t>(temp);
return true;
}

bool HexStringToInt(const string &value, int16_t *output) {
int32_t temp;
if (!HexStringToInt(value, &temp)) {
return false;
}
if (temp < 0 || temp > static_cast<int32_t>(UINT16_MAX)) {
return false;
}
*output = static_cast<int16_t>(temp);
return true;
}

bool HexStringToInt(const string &value, int32_t *output) {
if (value.empty()) {
return false;
}

size_t found = value.find_first_not_of("ABCDEFabcdef0123456789");
if (found != string::npos) {
return false;
}
*output = strtoll(value.data(), NULL, 16);
return true;
}

void ToLower(string *s) {
std::transform(s->begin(), s->end(), s->begin(),
std::ptr_fun<int, int>(std::tolower));
Expand Down
Loading