folly: to: make exceptions more informative
[folly.git] / folly / Conv.cpp
1 /*
2  * Copyright 2014 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       // Attempt to parse the value as an integer
85       StringPiece tmp(*src);
86       uint8_t value = to<uint8_t>(&tmp);
87       // Only accept 0 or 1
88       FOLLY_RANGE_CHECK_STRINGPIECE(
89         value <= 1, "Integer overflow when parsing bool: must be 0 or 1", *src);
90       b = tmp.begin();
91       result = (value == 1);
92       break;
93     }
94     case 'y':
95     case 'Y':
96       result = true;
97       if (!bool_str_cmp(&b, len, "yes")) {
98         ++b;  // accept the single 'y' character
99       }
100       break;
101     case 'n':
102     case 'N':
103       result = false;
104       if (!bool_str_cmp(&b, len, "no")) {
105         ++b;
106       }
107       break;
108     case 't':
109     case 'T':
110       result = true;
111       if (!bool_str_cmp(&b, len, "true")) {
112         ++b;
113       }
114       break;
115     case 'f':
116     case 'F':
117       result = false;
118       if (!bool_str_cmp(&b, len, "false")) {
119         ++b;
120       }
121       break;
122     case 'o':
123     case 'O':
124       if (bool_str_cmp(&b, len, "on")) {
125         result = true;
126       } else if (bool_str_cmp(&b, len, "off")) {
127         result = false;
128       } else {
129         FOLLY_RANGE_CHECK_STRINGPIECE(false, "Invalid value for bool", *src);
130       }
131       break;
132     default:
133       FOLLY_RANGE_CHECK_STRINGPIECE(false, "Invalid value for bool", *src);
134   }
135
136   src->assign(b, e);
137   return result;
138 }
139
140 } // namespace detail
141 } // namespace folly