2 * Copyright 2013 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 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
52 const char* e = *b + len;
53 const char* v = value;
55 if (p == e || tolower(*p) != *v) { // value is already lowercase
66 bool str_to_bool(StringPiece* src) {
67 auto b = src->begin(), e = src->end();
69 FOLLY_RANGE_CHECK(b < e,
70 "No non-whitespace characters found in input string");
71 if (!isspace(*b)) break;
79 // Attempt to parse the value as an integer
80 StringPiece tmp(*src);
81 uint8_t value = to<uint8_t>(&tmp);
83 FOLLY_RANGE_CHECK(value <= 1,
84 "Integer overflow when parsing bool: must be 0 or 1");
86 result = (value == 1);
92 if (!bool_str_cmp(&b, len, "yes")) {
93 ++b; // accept the single 'y' character
99 if (!bool_str_cmp(&b, len, "no")) {
106 if (!bool_str_cmp(&b, len, "true")) {
113 if (!bool_str_cmp(&b, len, "false")) {
119 if (bool_str_cmp(&b, len, "on")) {
121 } else if (bool_str_cmp(&b, len, "off")) {
124 FOLLY_RANGE_CHECK(false, "Invalid value for bool");
128 FOLLY_RANGE_CHECK(false, "Invalid value for bool");
135 } // namespace detail