Changeset View
Changeset View
Standalone View
Standalone View
src/utilstrencodings.cpp
Show First 20 Lines • Show All 483 Lines • ▼ Show 20 Lines | bool ParseInt32(const std::string &str, int32_t *out) { | ||||
} | } | ||||
char *endp = nullptr; | char *endp = nullptr; | ||||
// strtol will not set errno if valid | // strtol will not set errno if valid | ||||
errno = 0; | errno = 0; | ||||
long int n = strtol(str.c_str(), &endp, 10); | long int n = strtol(str.c_str(), &endp, 10); | ||||
if (out) { | if (out) { | ||||
*out = (int32_t)n; | *out = (int32_t)n; | ||||
} | } | ||||
// Note that strtol returns a *long int*, so even if strtol doesn't report a | // Note that strtol returns a *long int*, so even if strtol doesn't report | ||||
// over/underflow we still have to check that the returned value is within | // an over/underflow we still have to check that the returned value is | ||||
// the range of an *int32_t*. On 64-bit platforms the size of these types | // within the range of an *int32_t*. On 64-bit platforms the size of these | ||||
// may be different. | // types may be different. | ||||
return endp && *endp == 0 && !errno && | return endp && *endp == 0 && !errno && | ||||
n >= std::numeric_limits<int32_t>::min() && | n >= std::numeric_limits<int32_t>::min() && | ||||
n <= std::numeric_limits<int32_t>::max(); | n <= std::numeric_limits<int32_t>::max(); | ||||
} | } | ||||
bool ParseInt64(const std::string &str, int64_t *out) { | bool ParseInt64(const std::string &str, int64_t *out) { | ||||
if (!ParsePrechecks(str)) { | if (!ParsePrechecks(str)) { | ||||
return false; | return false; | ||||
} | } | ||||
char *endp = nullptr; | char *endp = nullptr; | ||||
// strtoll will not set errno if valid | // strtoll will not set errno if valid | ||||
errno = 0; | errno = 0; | ||||
long long int n = strtoll(str.c_str(), &endp, 10); | long long int n = strtoll(str.c_str(), &endp, 10); | ||||
if (out) { | if (out) { | ||||
*out = (int64_t)n; | *out = (int64_t)n; | ||||
} | } | ||||
// 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 a over/underflow we still have to check that the returned value is | // report an over/underflow we still have to check that the returned value | ||||
// 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 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; | ||||
} | } | ||||
char *endp = nullptr; | char *endp = nullptr; | ||||
// strtoul will not set errno if valid | // strtoul will not set errno if valid | ||||
errno = 0; | errno = 0; | ||||
unsigned long int n = strtoul(str.c_str(), &endp, 10); | unsigned long int n = strtoul(str.c_str(), &endp, 10); | ||||
if (out) { | if (out) { | ||||
*out = (uint32_t)n; | *out = (uint32_t)n; | ||||
} | } | ||||
// Note that strtoul returns a *unsigned long int*, so even if it doesn't | // Note that strtoul returns a *unsigned long int*, so even if it doesn't | ||||
// report a over/underflow we still have to check that the returned value is | // report an over/underflow we still have to check that the returned value | ||||
// within the range of an *uint32_t*. On 64-bit platforms the size of these | // is within the range of an *uint32_t*. On 64-bit platforms the size of | ||||
// types may be different. | // these types may be different. | ||||
return endp && *endp == 0 && !errno && | return endp && *endp == 0 && !errno && | ||||
n <= std::numeric_limits<uint32_t>::max(); | n <= std::numeric_limits<uint32_t>::max(); | ||||
} | } | ||||
bool ParseUInt64(const std::string &str, uint64_t *out) { | bool ParseUInt64(const std::string &str, uint64_t *out) { | ||||
if (!ParsePrechecks(str)) { | if (!ParsePrechecks(str)) { | ||||
return false; | return false; | ||||
} | } | ||||
// Reject negative values, unfortunately strtoull accepts these by default | // Reject negative values, unfortunately strtoull accepts these by default | ||||
// if they fit in the range | // if they fit in the range | ||||
if (str.size() >= 1 && str[0] == '-') { | if (str.size() >= 1 && str[0] == '-') { | ||||
return false; | return false; | ||||
} | } | ||||
char *endp = nullptr; | char *endp = nullptr; | ||||
// strtoull will not set errno if valid | // strtoull will not set errno if valid | ||||
errno = 0; | errno = 0; | ||||
unsigned long long int n = strtoull(str.c_str(), &endp, 10); | unsigned long long int n = strtoull(str.c_str(), &endp, 10); | ||||
if (out) { | if (out) { | ||||
*out = (uint64_t)n; | *out = (uint64_t)n; | ||||
} | } | ||||
// Note that strtoull returns a *unsigned long long int*, so even if it | // Note that strtoull returns a *unsigned long long int*, so even if it | ||||
// doesn't report a over/underflow we still have to check that the returned | // doesn't report an over/underflow we still have to check that the returned | ||||
// value is within the range of an *uint64_t*. | // value is within the range of an *uint64_t*. | ||||
return endp && *endp == 0 && !errno && | return endp && *endp == 0 && !errno && | ||||
n <= std::numeric_limits<uint64_t>::max(); | n <= std::numeric_limits<uint64_t>::max(); | ||||
} | } | ||||
bool ParseDouble(const std::string &str, double *out) { | bool ParseDouble(const std::string &str, double *out) { | ||||
if (!ParsePrechecks(str)) { | if (!ParsePrechecks(str)) { | ||||
return false; | return false; | ||||
▲ Show 20 Lines • Show All 244 Lines • Show Last 20 Lines |