2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #define FOLLY_CONV_INTERNAL
17 #include <folly/Conv.h>
22 extern const char digit1[101] =
23 "00000000001111111111222222222233333333334444444444"
24 "55555555556666666666777777777788888888889999999999";
25 extern const char digit2[101] =
26 "01234567890123456789012345678901234567890123456789"
27 "01234567890123456789012345678901234567890123456789";
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 =
37 template <> const char *const MaxString<unsigned long>::value =
38 "18446744073709551615";
40 static_assert(sizeof(unsigned long) >= 4,
41 "Wrong value for MaxString<unsigned long>::value,"
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"
49 /* Test for GCC >= 3.6.0 */
50 #if __GNUC__ > 3 || (__GNUC__ == 3 && (__GNUC_MINOR__ >= 6))
51 template <> const char *const MaxString<__uint128_t>::value =
52 "340282366920938463463374607431768211455";
55 inline bool bool_str_cmp(const char** b, size_t len, const char* value) {
56 // Can't use strncasecmp, since we want to ensure that the full value matches
58 const char* e = *b + len;
59 const char* v = value;
61 if (p == e || tolower(*p) != *v) { // value is already lowercase
72 bool str_to_bool(StringPiece* src) {
73 auto b = src->begin(), e = src->end();
75 FOLLY_RANGE_CHECK(b < e,
76 "No non-whitespace characters found in input string");
77 if (!isspace(*b)) break;
85 // Attempt to parse the value as an integer
86 StringPiece tmp(*src);
87 uint8_t value = to<uint8_t>(&tmp);
89 FOLLY_RANGE_CHECK(value <= 1,
90 "Integer overflow when parsing bool: must be 0 or 1");
92 result = (value == 1);
98 if (!bool_str_cmp(&b, len, "yes")) {
99 ++b; // accept the single 'y' character
105 if (!bool_str_cmp(&b, len, "no")) {
112 if (!bool_str_cmp(&b, len, "true")) {
119 if (!bool_str_cmp(&b, len, "false")) {
125 if (bool_str_cmp(&b, len, "on")) {
127 } else if (bool_str_cmp(&b, len, "off")) {
130 FOLLY_RANGE_CHECK(false, "Invalid value for bool");
134 FOLLY_RANGE_CHECK(false, "Invalid value for bool");
141 } // namespace detail