2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #define FOLLY_CONV_INTERNAL
17 #include <folly/Conv.h>
24 // Maximum value of number when represented as a string
27 static const char* const value;
30 template <> const char *const MaxString<uint8_t>::value = "255";
31 template <> const char *const MaxString<uint16_t>::value = "65535";
32 template <> const char *const MaxString<uint32_t>::value = "4294967295";
33 #if __SIZEOF_LONG__ == 4
34 template <> const char *const MaxString<unsigned long>::value =
37 template <> const char *const MaxString<unsigned long>::value =
38 "18446744073709551615";
40 static_assert(sizeof(unsigned long) >= 4,
41 "Wrong value for MaxString<unsigned long>::value,"
43 template <> const char *const MaxString<unsigned long long>::value =
44 "18446744073709551615";
45 static_assert(sizeof(unsigned long long) >= 8,
46 "Wrong value for MaxString<unsigned long long>::value"
49 #ifdef FOLLY_HAVE_INT128_T
50 template <> const char *const MaxString<__uint128_t>::value =
51 "340282366920938463463374607431768211455";
55 * Lookup tables that converts from a decimal character value to an integral
56 * binary value, shifted by a decimal "shift" multiplier.
57 * For all character values in the range '0'..'9', the table at those
58 * index locations returns the actual decimal value shifted by the multiplier.
59 * For all other values, the lookup table returns an invalid OOR value.
61 // Out-of-range flag value, larger than the largest value that can fit in
62 // four decimal bytes (9999), but four of these added up together should
63 // still not overflow uint16_t.
64 constexpr int32_t OOR = 10000;
66 FOLLY_ALIGNED(16) constexpr uint16_t shift1[] = {
67 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
68 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
69 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
70 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
71 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
72 1, 2, 3, 4, 5, 6, 7, 8, 9, OOR, OOR,
73 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
74 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
75 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
76 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
77 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
78 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
79 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
80 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
81 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
82 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
83 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
84 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
85 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
86 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
87 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
88 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
89 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
90 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
91 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
92 OOR, OOR, OOR, OOR, OOR, OOR // 250
95 FOLLY_ALIGNED(16) constexpr uint16_t shift10[] = {
96 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
97 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
98 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
99 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
100 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
101 10, 20, 30, 40, 50, 60, 70, 80, 90, OOR, OOR,
102 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
103 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
104 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
105 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
106 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
107 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
108 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
109 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
110 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
111 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
112 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
113 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
114 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
115 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
116 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
117 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
118 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
119 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
120 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
121 OOR, OOR, OOR, OOR, OOR, OOR // 250
124 FOLLY_ALIGNED(16) constexpr uint16_t shift100[] = {
125 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
126 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
127 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
128 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
129 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
130 100, 200, 300, 400, 500, 600, 700, 800, 900, OOR, OOR,
131 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
132 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
133 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
134 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
135 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
136 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
137 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
138 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
139 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
140 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
141 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
142 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
143 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
144 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
145 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
146 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
147 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
148 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
149 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
150 OOR, OOR, OOR, OOR, OOR, OOR // 250
153 FOLLY_ALIGNED(16) constexpr uint16_t shift1000[] = {
154 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
155 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
156 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
157 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
158 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
159 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, OOR, OOR,
160 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
161 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
162 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
163 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
164 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
165 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
166 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
167 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
168 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
169 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
170 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
171 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
172 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
173 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
174 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
175 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
176 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
177 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
178 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
179 OOR, OOR, OOR, OOR, OOR, OOR // 250
183 inline bool bool_str_cmp(const char** b, size_t len, const char* value) {
184 // Can't use strncasecmp, since we want to ensure that the full value matches
186 const char* e = *b + len;
187 const char* v = value;
189 if (p == e || tolower(*p) != *v) { // value is already lowercase
200 bool str_to_bool(StringPiece* src) {
201 auto b = src->begin(), e = src->end();
203 FOLLY_RANGE_CHECK_STRINGPIECE(
204 b < e, "No non-whitespace characters found in input string", *src);
205 if (!isspace(*b)) break;
214 for (; b < e && isdigit(*b); ++b) {
215 FOLLY_RANGE_CHECK_STRINGPIECE(
216 !result && (*b == '0' || *b == '1'),
217 "Integer overflow when parsing bool: must be 0 or 1", *src);
218 result = (*b == '1');
225 if (!bool_str_cmp(&b, len, "yes")) {
226 ++b; // accept the single 'y' character
232 if (!bool_str_cmp(&b, len, "no")) {
239 if (!bool_str_cmp(&b, len, "true")) {
246 if (!bool_str_cmp(&b, len, "false")) {
252 if (bool_str_cmp(&b, len, "on")) {
254 } else if (bool_str_cmp(&b, len, "off")) {
257 FOLLY_RANGE_CHECK_STRINGPIECE(false, "Invalid value for bool", *src);
261 FOLLY_RANGE_CHECK_STRINGPIECE(false, "Invalid value for bool", *src);
270 * StringPiece to double, with progress information. Alters the
271 * StringPiece parameter to munch the already-parsed characters.
274 Tgt str_to_floating(StringPiece* src) {
275 using namespace double_conversion;
276 static StringToDoubleConverter
277 conv(StringToDoubleConverter::ALLOW_TRAILING_JUNK
278 | StringToDoubleConverter::ALLOW_LEADING_SPACES,
280 // return this for junk input string
281 std::numeric_limits<double>::quiet_NaN(),
284 FOLLY_RANGE_CHECK_STRINGPIECE(!src->empty(),
285 "No digits found in input string", *src);
288 auto result = conv.StringToDouble(src->data(),
289 static_cast<int>(src->size()),
290 &length); // processed char count
292 if (!std::isnan(result)) {
293 src->advance(length);
297 for (;; src->advance(1)) {
299 throw std::range_error("Unable to convert an empty string"
300 " to a floating point value.");
302 if (!isspace(src->front())) {
307 // Was that "inf[inity]"?
308 if (src->size() >= 3 && toupper((*src)[0]) == 'I'
309 && toupper((*src)[1]) == 'N' && toupper((*src)[2]) == 'F') {
310 if (src->size() >= 8 &&
311 toupper((*src)[3]) == 'I' &&
312 toupper((*src)[4]) == 'N' &&
313 toupper((*src)[5]) == 'I' &&
314 toupper((*src)[6]) == 'T' &&
315 toupper((*src)[7]) == 'Y') {
320 return std::numeric_limits<Tgt>::infinity();
323 // Was that "-inf[inity]"?
324 if (src->size() >= 4 && toupper((*src)[0]) == '-'
325 && toupper((*src)[1]) == 'I' && toupper((*src)[2]) == 'N'
326 && toupper((*src)[3]) == 'F') {
327 if (src->size() >= 9 &&
328 toupper((*src)[4]) == 'I' &&
329 toupper((*src)[5]) == 'N' &&
330 toupper((*src)[6]) == 'I' &&
331 toupper((*src)[7]) == 'T' &&
332 toupper((*src)[8]) == 'Y') {
337 return -std::numeric_limits<Tgt>::infinity();
341 if (src->size() >= 3 && toupper((*src)[0]) == 'N'
342 && toupper((*src)[1]) == 'A' && toupper((*src)[2]) == 'N') {
344 return std::numeric_limits<Tgt>::quiet_NaN();
348 if (src->size() >= 4 &&
349 toupper((*src)[0]) == '-' &&
350 toupper((*src)[1]) == 'N' &&
351 toupper((*src)[2]) == 'A' &&
352 toupper((*src)[3]) == 'N') {
354 return -std::numeric_limits<Tgt>::quiet_NaN();
358 throw std::range_error("Unable to convert \"" + src->toString()
359 + "\" to a floating point value.");
364 float str_to_float(StringPiece* src) {
365 return str_to_floating<float>(src);
368 double str_to_double(StringPiece* src) {
369 return str_to_floating<double>(src);
373 * String represented as a pair of pointers to char to unsigned
374 * integrals. Assumes NO whitespace before or after, and also that the
375 * string is composed entirely of digits. Tgt must be unsigned, and no
376 * sign is allowed in the string (even it's '+'). String may be empty,
377 * in which case digits_to throws.
380 Tgt digits_to(const char* b, const char* e) {
382 static_assert(!std::is_signed<Tgt>::value, "Unsigned type expected");
385 const size_t size = e - b;
387 /* Although the string is entirely made of digits, we still need to
388 * check for overflow.
390 if (size >= std::numeric_limits<Tgt>::digits10 + 1) {
391 // Leading zeros? If so, recurse to keep things simple
392 if (b < e && *b == '0') {
395 return 0; // just zeros, e.g. "0000"
397 return digits_to<Tgt>(b, e);
400 FOLLY_RANGE_CHECK_BEGIN_END(
401 size == std::numeric_limits<Tgt>::digits10 + 1 &&
402 strncmp(b, detail::MaxString<Tgt>::value, size) <= 0,
403 "Numeric overflow upon conversion",
408 // Here we know that the number won't overflow when
409 // converted. Proceed without checks.
413 for (; e - b >= 4; b += 4) {
415 const int32_t r0 = shift1000[static_cast<size_t>(b[0])];
416 const int32_t r1 = shift100[static_cast<size_t>(b[1])];
417 const int32_t r2 = shift10[static_cast<size_t>(b[2])];
418 const int32_t r3 = shift1[static_cast<size_t>(b[3])];
419 const auto sum = r0 + r1 + r2 + r3;
420 assert(sum < OOR && "Assumption: string only has digits");
426 const int32_t r0 = shift100[static_cast<size_t>(b[0])];
427 const int32_t r1 = shift10[static_cast<size_t>(b[1])];
428 const int32_t r2 = shift1[static_cast<size_t>(b[2])];
429 const auto sum = r0 + r1 + r2;
430 assert(sum < OOR && "Assumption: string only has digits");
431 return result * 1000 + sum;
434 const int32_t r0 = shift10[static_cast<size_t>(b[0])];
435 const int32_t r1 = shift1[static_cast<size_t>(b[1])];
436 const auto sum = r0 + r1;
437 assert(sum < OOR && "Assumption: string only has digits");
438 return result * 100 + sum;
441 const int32_t sum = shift1[static_cast<size_t>(b[0])];
442 assert(sum < OOR && "Assumption: string only has digits");
443 return result * 10 + sum;
448 FOLLY_RANGE_CHECK_BEGIN_END(
449 size > 0, "Found no digits to convert in input", b, e);
453 template unsigned char digits_to<unsigned char>(const char* b, const char* e);
454 template unsigned short digits_to<unsigned short>(const char* b, const char* e);
455 template unsigned int digits_to<unsigned int>(const char* b, const char* e);
456 template unsigned long digits_to<unsigned long>(const char* b, const char* e);
457 template unsigned long long digits_to<unsigned long long>(const char* b,
459 #if FOLLY_HAVE_INT128_T
460 template unsigned __int128 digits_to<unsigned __int128>(const char* b,
464 } // namespace detail