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