fix flaky ConnectTFOTimeout and ConnectTFOFallbackTimeout tests
[folly.git] / folly / Conv.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #define FOLLY_CONV_INTERNAL
17 #include <folly/Conv.h>
18
19 namespace folly {
20 namespace detail {
21
22 namespace {
23
24 // Maximum value of number when represented as a string
25 template <class T>
26 struct MaxString {
27   static const char* const value;
28 };
29
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 =
35   "4294967295";
36 #else
37 template <> const char *const MaxString<unsigned long>::value =
38   "18446744073709551615";
39 #endif
40 static_assert(sizeof(unsigned long) >= 4,
41               "Wrong value for MaxString<unsigned long>::value,"
42               " please update.");
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"
47               ", please update.");
48
49 #ifdef FOLLY_HAVE_INT128_T
50 template <> const char *const MaxString<__uint128_t>::value =
51   "340282366920938463463374607431768211455";
52 #endif
53
54 /*
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.
60  */
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;
65
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
93 };
94
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
122 };
123
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
151 };
152
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
180 };
181 }
182
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
185   const char* p = *b;
186   const char* e = *b + len;
187   const char* v = value;
188   while (*v != '\0') {
189     if (p == e || tolower(*p) != *v) { // value is already lowercase
190       return false;
191     }
192     ++p;
193     ++v;
194   }
195
196   *b = p;
197   return true;
198 }
199
200 bool str_to_bool(StringPiece* src) {
201   auto b = src->begin(), e = src->end();
202   for (;; ++b) {
203     FOLLY_RANGE_CHECK_STRINGPIECE(
204       b < e, "No non-whitespace characters found in input string", *src);
205     if (!isspace(*b)) break;
206   }
207
208   bool result;
209   size_t len = e - b;
210   switch (*b) {
211     case '0':
212     case '1': {
213       result = false;
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');
219       }
220       break;
221     }
222     case 'y':
223     case 'Y':
224       result = true;
225       if (!bool_str_cmp(&b, len, "yes")) {
226         ++b;  // accept the single 'y' character
227       }
228       break;
229     case 'n':
230     case 'N':
231       result = false;
232       if (!bool_str_cmp(&b, len, "no")) {
233         ++b;
234       }
235       break;
236     case 't':
237     case 'T':
238       result = true;
239       if (!bool_str_cmp(&b, len, "true")) {
240         ++b;
241       }
242       break;
243     case 'f':
244     case 'F':
245       result = false;
246       if (!bool_str_cmp(&b, len, "false")) {
247         ++b;
248       }
249       break;
250     case 'o':
251     case 'O':
252       if (bool_str_cmp(&b, len, "on")) {
253         result = true;
254       } else if (bool_str_cmp(&b, len, "off")) {
255         result = false;
256       } else {
257         FOLLY_RANGE_CHECK_STRINGPIECE(false, "Invalid value for bool", *src);
258       }
259       break;
260     default:
261       FOLLY_RANGE_CHECK_STRINGPIECE(false, "Invalid value for bool", *src);
262   }
263
264   src->assign(b, e);
265   return result;
266 }
267
268 namespace {
269 /**
270  * StringPiece to double, with progress information. Alters the
271  * StringPiece parameter to munch the already-parsed characters.
272  */
273 template <class Tgt>
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,
279          0.0,
280          // return this for junk input string
281          std::numeric_limits<double>::quiet_NaN(),
282          nullptr, nullptr);
283
284   FOLLY_RANGE_CHECK_STRINGPIECE(!src->empty(),
285                                 "No digits found in input string", *src);
286
287   int length;
288   auto result = conv.StringToDouble(src->data(),
289                                     static_cast<int>(src->size()),
290                                     &length); // processed char count
291
292   if (!std::isnan(result)) {
293     src->advance(length);
294     return result;
295   }
296
297   for (;; src->advance(1)) {
298     if (src->empty()) {
299       throw std::range_error("Unable to convert an empty string"
300                              " to a floating point value.");
301     }
302     if (!isspace(src->front())) {
303       break;
304     }
305   }
306
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') {
316       src->advance(8);
317     } else {
318       src->advance(3);
319     }
320     return std::numeric_limits<Tgt>::infinity();
321   }
322
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') {
333       src->advance(9);
334     } else {
335       src->advance(4);
336     }
337     return -std::numeric_limits<Tgt>::infinity();
338   }
339
340   // "nan"?
341   if (src->size() >= 3 && toupper((*src)[0]) == 'N'
342         && toupper((*src)[1]) == 'A' && toupper((*src)[2]) == 'N') {
343     src->advance(3);
344     return std::numeric_limits<Tgt>::quiet_NaN();
345   }
346
347   // "-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') {
353     src->advance(4);
354     return -std::numeric_limits<Tgt>::quiet_NaN();
355   }
356
357   // All bets are off
358   throw std::range_error("Unable to convert \"" + src->toString()
359                          + "\" to a floating point value.");
360 }
361
362 }
363
364 float str_to_float(StringPiece* src) {
365   return str_to_floating<float>(src);
366 }
367
368 double str_to_double(StringPiece* src) {
369   return str_to_floating<double>(src);
370 }
371
372 /**
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.
378  */
379 template <class Tgt>
380 Tgt digits_to(const char* b, const char* e) {
381
382   static_assert(!std::is_signed<Tgt>::value, "Unsigned type expected");
383   assert(b <= e);
384
385   const size_t size = e - b;
386
387   /* Although the string is entirely made of digits, we still need to
388    * check for overflow.
389    */
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') {
393       for (++b;; ++b) {
394         if (b == e)
395           return 0; // just zeros, e.g. "0000"
396         if (*b != '0')
397           return digits_to<Tgt>(b, e);
398       }
399     }
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",
404         b,
405         e);
406   }
407
408   // Here we know that the number won't overflow when
409   // converted. Proceed without checks.
410
411   Tgt result = 0;
412
413   for (; e - b >= 4; b += 4) {
414     result *= 10000;
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");
421     result += sum;
422   }
423
424   switch (e - b) {
425   case 3: {
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;
432   }
433   case 2: {
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;
439   }
440   case 1: {
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;
444   }
445   }
446
447   assert(b == e);
448   FOLLY_RANGE_CHECK_BEGIN_END(
449       size > 0, "Found no digits to convert in input", b, e);
450   return result;
451 }
452
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,
458                                                           const char* e);
459 #if FOLLY_HAVE_INT128_T
460 template unsigned __int128 digits_to<unsigned __int128>(const char* b,
461                                                         const char* e);
462 #endif
463
464 } // namespace detail
465 } // namespace folly