25#include "absl/strings/ascii.h"
29inline int64_t
strtoint64(
const char* nptr,
char** endptr,
int base) {
30 return std::strtoll(nptr, endptr, base);
33inline uint64_t
strtouint64(
const char* nptr,
char** endptr,
int base) {
34 return std::strtoull(nptr, endptr, base);
37inline int32_t
strtoint32(
const char* nptr,
char** endptr,
int base) {
38 return std::strtol(nptr, endptr, base);
41inline uint32_t
strtouint32(
const char* nptr,
char** endptr,
int base) {
42 return std::strtoul(nptr, endptr, base);
54 using std::numeric_limits;
56 char* error =
nullptr;
57 long value = strtol(str, &error, 0);
59 if (value > numeric_limits<int32_t>::max()) {
60 value = numeric_limits<int32_t>::max();
61 }
else if (value < numeric_limits<int32_t>::min()) {
62 value = numeric_limits<int32_t>::min();
64 return (error == str) ? deflt : value;
68 using std::numeric_limits;
70 if (numeric_limits<unsigned long>::max() ==
71 numeric_limits<uint32_t>::max()) {
73 char* error =
nullptr;
74 const uint32_t value = strtoul(str, &error, 0);
75 return (error == str) ? deflt : value;
82 char* error =
nullptr;
84 if (value > numeric_limits<uint32_t>::max() ||
85 value < -
static_cast<int64_t
>(numeric_limits<uint32_t>::max())) {
86 value = numeric_limits<uint32_t>::max();
89 return (error == str) ? deflt : value;
103 using std::numeric_limits;
105 char* error =
nullptr;
106 long value = strtol(str, &error, 10);
108 if (value > numeric_limits<int32_t>::max()) {
109 value = numeric_limits<int32_t>::max();
110 }
else if (value < numeric_limits<int32_t>::min()) {
111 value = numeric_limits<int32_t>::min();
113 return (error == str) ? deflt : value;
117 using std::numeric_limits;
119 if (numeric_limits<unsigned long>::max() ==
120 numeric_limits<uint32_t>::max()) {
122 char* error =
nullptr;
123 const uint32_t value = strtoul(str, &error, 10);
124 return (error == str) ? deflt : value;
131 char* error =
nullptr;
133 if (value > numeric_limits<uint32_t>::max() ||
134 value < -
static_cast<int64_t
>(numeric_limits<uint32_t>::max())) {
135 value = numeric_limits<uint32_t>::max();
138 return (error == str) ? deflt : value;
151 char* error =
nullptr;
152 const uint64_t value =
strtouint64(str, &error, 0);
153 return (error == str) ? deflt : value;
157 char* error =
nullptr;
158 const int64_t value =
strtoint64(str, &error, 0);
159 return (error == str) ? deflt : value;
163 char* error =
nullptr;
164 const uint64_t value =
strtouint64(str, &error, 16);
165 return (error == str) ? deflt : value;
178 char* error =
nullptr;
179 const int64_t value =
strtoint64(str, &error, 10);
180 return (error == str) ? deflt : value;
184 char* error =
nullptr;
186 return (error == str) ? deflt : value;
196 char* error =
nullptr;
198 const double value = strtod(str, &error);
215 static const int kMaxLen = 5;
216 char value[kMaxLen + 1];
218 while (absl::ascii_isspace(*str)) {
222 for (; len <= kMaxLen && absl::ascii_isalnum(*str); ++str) {
223 value[len++] = absl::ascii_tolower(*str);
225 if (len == 0 || len > kMaxLen)
return deflt;
229 if (value[0] ==
'0' || value[0] ==
'n')
return false;
230 if (value[0] ==
'1' || value[0] ==
'y')
return true;
233 if (!strcmp(value,
"no"))
return false;
236 if (!strcmp(value,
"yes"))
return true;
239 if (!strcmp(value,
"true"))
return true;
242 if (!strcmp(value,
"false"))
return false;
Convert strings to numbers or numbers to strings.
uint64_t ParseLeadingUInt64Value(const char *str, uint64_t deflt)
uint64_t ParseLeadingUDec64Value(const char *str, uint64_t deflt)
double ParseLeadingDoubleValue(const char *str, double deflt)
int32_t ParseLeadingDec32Value(const char *str, int32_t deflt)
int64_t ParseLeadingDec64Value(const char *str, int64_t deflt)
int64_t strtoint64(const char *nptr, char **endptr, int base)
uint32_t ParseLeadingUDec32Value(const char *str, uint32_t deflt)
uint64_t strtouint64(const char *nptr, char **endptr, int base)
uint32_t strtouint32(const char *nptr, char **endptr, int base)
int32_t ParseLeadingInt32Value(const char *str, int32_t deflt)
false no n y bool ParseLeadingBoolValue(const char *str, bool deflt)
uint64_t ParseLeadingHex64Value(const char *str, uint64_t deflt)
int32_t strtoint32(const char *nptr, char **endptr, int base)
uint32_t ParseLeadingUInt32Value(const char *str, uint32_t deflt)
int64_t ParseLeadingInt64Value(const char *str, int64_t deflt)