[Conv] MaxString specialization for __int128
[folly.git] / folly / Conv.cpp
1 /*
2  * Copyright 2012 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 template <> const char *const MaxString<unsigned __int128>::value =
46   "340282366920938463463374607431768211455";
47 static_assert(sizeof(unsigned long long) >= 8,
48               "Wrong value for MaxString<unsigned long long>::value"
49               ", please update.");
50
51 inline bool bool_str_cmp(const char** b, size_t len, const char* value) {
52   // Can't use strncasecmp, since we want to ensure that the full value matches
53   const char* p = *b;
54   const char* e = *b + len;
55   const char* v = value;
56   while (*v != '\0') {
57     if (p == e || tolower(*p) != *v) { // value is already lowercase
58       return false;
59     }
60     ++p;
61     ++v;
62   }
63
64   *b = p;
65   return true;
66 }
67
68 bool str_to_bool(StringPiece* src) {
69   auto b = src->begin(), e = src->end();
70   for (;; ++b) {
71     FOLLY_RANGE_CHECK(b < e,
72                       "No non-whitespace characters found in input string");
73     if (!isspace(*b)) break;
74   }
75
76   bool result;
77   size_t len = e - b;
78   switch (*b) {
79     case '0':
80     case '1': {
81       // Attempt to parse the value as an integer
82       StringPiece tmp(*src);
83       uint8_t value = to<uint8_t>(&tmp);
84       // Only accept 0 or 1
85       FOLLY_RANGE_CHECK(value <= 1,
86                         "Integer overflow when parsing bool: must be 0 or 1");
87       b = tmp.begin();
88       result = (value == 1);
89       break;
90     }
91     case 'y':
92     case 'Y':
93       result = true;
94       if (!bool_str_cmp(&b, len, "yes")) {
95         ++b;  // accept the single 'y' character
96       }
97       break;
98     case 'n':
99     case 'N':
100       result = false;
101       if (!bool_str_cmp(&b, len, "no")) {
102         ++b;
103       }
104       break;
105     case 't':
106     case 'T':
107       result = true;
108       if (!bool_str_cmp(&b, len, "true")) {
109         ++b;
110       }
111       break;
112     case 'f':
113     case 'F':
114       result = false;
115       if (!bool_str_cmp(&b, len, "false")) {
116         ++b;
117       }
118       break;
119     case 'o':
120     case 'O':
121       if (bool_str_cmp(&b, len, "on")) {
122         result = true;
123       } else if (bool_str_cmp(&b, len, "off")) {
124         result = false;
125       } else {
126         FOLLY_RANGE_CHECK(false, "Invalid value for bool");
127       }
128       break;
129     default:
130       FOLLY_RANGE_CHECK(false, "Invalid value for bool");
131   }
132
133   src->assign(b, e);
134   return result;
135 }
136
137 } // namespace detail
138 } // namespace folly