Changeset View
Changeset View
Standalone View
Standalone View
src/util/strencodings.cpp
Show First 20 Lines • Show All 340 Lines • ▼ Show 20 Lines | bool ParseInt64(const std::string &str, int64_t *out) { | ||||
// Note that strtoll returns a *long long int*, so even if strtol doesn't | // Note that strtoll returns a *long long int*, so even if strtol doesn't | ||||
// report an over/underflow we still have to check that the returned value | // report an over/underflow we still have to check that the returned value | ||||
// is within the range of an *int64_t*. | // is within the range of an *int64_t*. | ||||
return endp && *endp == 0 && !errno && | return endp && *endp == 0 && !errno && | ||||
n >= std::numeric_limits<int64_t>::min() && | n >= std::numeric_limits<int64_t>::min() && | ||||
n <= std::numeric_limits<int64_t>::max(); | n <= std::numeric_limits<int64_t>::max(); | ||||
} | } | ||||
bool ParseUInt8(const std::string &str, uint8_t *out) { | |||||
uint32_t u32; | |||||
if (!ParseUInt32(str, &u32) || u32 > std::numeric_limits<uint8_t>::max()) { | |||||
return false; | |||||
} | |||||
if (out != nullptr) { | |||||
*out = static_cast<uint8_t>(u32); | |||||
} | |||||
return true; | |||||
} | |||||
bool ParseUInt32(const std::string &str, uint32_t *out) { | bool ParseUInt32(const std::string &str, uint32_t *out) { | ||||
if (!ParsePrechecks(str)) { | if (!ParsePrechecks(str)) { | ||||
return false; | return false; | ||||
} | } | ||||
// Reject negative values, unfortunately strtoul accepts these by default if | // Reject negative values, unfortunately strtoul accepts these by default if | ||||
// they fit in the range | // they fit in the range | ||||
if (str.size() >= 1 && str[0] == '-') { | if (str.size() >= 1 && str[0] == '-') { | ||||
return false; | return false; | ||||
▲ Show 20 Lines • Show All 283 Lines • Show Last 20 Lines |