Adding a unit test for HHWheelTimer exercising the default timeout functionality.
[folly.git] / folly / Conv.cpp
1 /*
2  * Copyright 2015 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 extern const char digit1[101] =
23   "00000000001111111111222222222233333333334444444444"
24   "55555555556666666666777777777788888888889999999999";
25 extern const char digit2[101] =
26   "01234567890123456789012345678901234567890123456789"
27   "01234567890123456789012345678901234567890123456789";
28
29 template <> const char *const MaxString<bool>::value = "true";
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 inline bool bool_str_cmp(const char** b, size_t len, const char* value) {
55   // Can't use strncasecmp, since we want to ensure that the full value matches
56   const char* p = *b;
57   const char* e = *b + len;
58   const char* v = value;
59   while (*v != '\0') {
60     if (p == e || tolower(*p) != *v) { // value is already lowercase
61       return false;
62     }
63     ++p;
64     ++v;
65   }
66
67   *b = p;
68   return true;
69 }
70
71 bool str_to_bool(StringPiece* src) {
72   auto b = src->begin(), e = src->end();
73   for (;; ++b) {
74     FOLLY_RANGE_CHECK_STRINGPIECE(
75       b < e, "No non-whitespace characters found in input string", *src);
76     if (!isspace(*b)) break;
77   }
78
79   bool result;
80   size_t len = e - b;
81   switch (*b) {
82     case '0':
83     case '1': {
84       result = false;
85       for (; b < e && isdigit(*b); ++b) {
86         FOLLY_RANGE_CHECK_STRINGPIECE(
87           !result && (*b == '0' || *b == '1'),
88           "Integer overflow when parsing bool: must be 0 or 1", *src);
89         result = (*b == '1');
90       }
91       break;
92     }
93     case 'y':
94     case 'Y':
95       result = true;
96       if (!bool_str_cmp(&b, len, "yes")) {
97         ++b;  // accept the single 'y' character
98       }
99       break;
100     case 'n':
101     case 'N':
102       result = false;
103       if (!bool_str_cmp(&b, len, "no")) {
104         ++b;
105       }
106       break;
107     case 't':
108     case 'T':
109       result = true;
110       if (!bool_str_cmp(&b, len, "true")) {
111         ++b;
112       }
113       break;
114     case 'f':
115     case 'F':
116       result = false;
117       if (!bool_str_cmp(&b, len, "false")) {
118         ++b;
119       }
120       break;
121     case 'o':
122     case 'O':
123       if (bool_str_cmp(&b, len, "on")) {
124         result = true;
125       } else if (bool_str_cmp(&b, len, "off")) {
126         result = false;
127       } else {
128         FOLLY_RANGE_CHECK_STRINGPIECE(false, "Invalid value for bool", *src);
129       }
130       break;
131     default:
132       FOLLY_RANGE_CHECK_STRINGPIECE(false, "Invalid value for bool", *src);
133   }
134
135   src->assign(b, e);
136   return result;
137 }
138
139 } // namespace detail
140 } // namespace folly