2 * Copyright 2016 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 #include <folly/Conv.h>
25 * Finds the first non-digit in a string. The number of digits
26 * searched depends on the precision of the Tgt integral. Assumes the
27 * string starts with NO whitespace and NO sign.
29 * The semantics of the routine is:
31 * if (b >= e || !isdigit(*b)) return b;
34 * Complete unrolling marks bottom-line (i.e. entire conversion)
35 * improvements of 20%.
37 inline const char* findFirstNonDigit(const char* b, const char* e) {
39 auto const c = static_cast<unsigned>(*b) - '0';
47 // Maximum value of number when represented as a string
50 static const char* const value;
53 template <> const char *const MaxString<uint8_t>::value = "255";
54 template <> const char *const MaxString<uint16_t>::value = "65535";
55 template <> const char *const MaxString<uint32_t>::value = "4294967295";
56 #if __SIZEOF_LONG__ == 4
57 template <> const char *const MaxString<unsigned long>::value =
60 template <> const char *const MaxString<unsigned long>::value =
61 "18446744073709551615";
63 static_assert(sizeof(unsigned long) >= 4,
64 "Wrong value for MaxString<unsigned long>::value,"
66 template <> const char *const MaxString<unsigned long long>::value =
67 "18446744073709551615";
68 static_assert(sizeof(unsigned long long) >= 8,
69 "Wrong value for MaxString<unsigned long long>::value"
72 #if FOLLY_HAVE_INT128_T
73 template <> const char *const MaxString<__uint128_t>::value =
74 "340282366920938463463374607431768211455";
78 * Lookup tables that converts from a decimal character value to an integral
79 * binary value, shifted by a decimal "shift" multiplier.
80 * For all character values in the range '0'..'9', the table at those
81 * index locations returns the actual decimal value shifted by the multiplier.
82 * For all other values, the lookup table returns an invalid OOR value.
84 // Out-of-range flag value, larger than the largest value that can fit in
85 // four decimal bytes (9999), but four of these added up together should
86 // still not overflow uint16_t.
87 constexpr int32_t OOR = 10000;
89 FOLLY_ALIGNED(16) constexpr uint16_t shift1[] = {
90 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
91 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
92 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
93 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
94 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
95 1, 2, 3, 4, 5, 6, 7, 8, 9, OOR, OOR,
96 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
97 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
98 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
99 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
100 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
101 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
102 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
103 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
104 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
105 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
106 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
107 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
108 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
109 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
110 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
111 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
112 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
113 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
114 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
115 OOR, OOR, OOR, OOR, OOR, OOR // 250
118 FOLLY_ALIGNED(16) constexpr uint16_t shift10[] = {
119 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
120 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
121 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
122 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
123 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
124 10, 20, 30, 40, 50, 60, 70, 80, 90, OOR, OOR,
125 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
126 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
127 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
128 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
129 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
130 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
131 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
132 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
133 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
134 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
135 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
136 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
137 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
138 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
139 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
140 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
141 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
142 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
143 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
144 OOR, OOR, OOR, OOR, OOR, OOR // 250
147 FOLLY_ALIGNED(16) constexpr uint16_t shift100[] = {
148 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
149 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
150 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
151 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
152 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
153 100, 200, 300, 400, 500, 600, 700, 800, 900, OOR, OOR,
154 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
155 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
156 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
157 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
158 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
159 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
160 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
161 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
162 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
163 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
164 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
165 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
166 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
167 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
168 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
169 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
170 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
171 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
172 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
173 OOR, OOR, OOR, OOR, OOR, OOR // 250
176 FOLLY_ALIGNED(16) constexpr uint16_t shift1000[] = {
177 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
178 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
179 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
180 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
181 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
182 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, OOR, OOR,
183 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
184 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
185 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
186 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
187 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
188 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
189 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
190 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
191 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
192 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
193 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
194 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
195 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
196 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
197 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
198 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
199 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
200 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
201 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
202 OOR, OOR, OOR, OOR, OOR, OOR // 250
210 // Keep this in sync with ConversionCode in Conv.h
211 constexpr const std::array<
213 static_cast<std::size_t>(ConversionCode::NUM_ERROR_CODES)>
216 {"Empty input string", true},
217 {"No digits found in input string", true},
218 {"Integer overflow when parsing bool (must be 0 or 1)", true},
219 {"Invalid value for bool", true},
220 {"Non-digit character found", true},
221 {"Invalid leading character", true},
222 {"Overflow during conversion", true},
223 {"Negative overflow during conversion", true},
224 {"Unable to convert string to floating point value", true},
225 {"Non-whitespace character found after end of conversion", true},
226 {"Overflow during arithmetic conversion", false},
227 {"Negative overflow during arithmetic conversion", false},
228 {"Loss of precision during arithmetic conversion", false},
231 // Check if ASCII is really ASCII
232 using IsAscii = std::
233 integral_constant<bool, 'A' == 65 && 'Z' == 90 && 'a' == 97 && 'z' == 122>;
235 // The code in this file that uses tolower() really only cares about
236 // 7-bit ASCII characters, so we can take a nice shortcut here.
237 inline char tolower_ascii(char in) {
238 return IsAscii::value ? in | 0x20 : std::tolower(in);
241 inline bool bool_str_cmp(const char** b, size_t len, const char* value) {
242 // Can't use strncasecmp, since we want to ensure that the full value matches
244 const char* e = *b + len;
245 const char* v = value;
247 if (p == e || tolower_ascii(*p) != *v) { // value is already lowercase
258 } // anonymous namespace
260 Expected<bool, ConversionCode> str_to_bool(StringPiece* src) noexcept {
261 auto b = src->begin(), e = src->end();
264 return makeUnexpected(ConversionCode::EMPTY_INPUT_STRING);
266 if (!std::isspace(*b)) {
277 for (; b < e && isdigit(*b); ++b) {
278 if (result || (*b != '0' && *b != '1')) {
279 return makeUnexpected(ConversionCode::BOOL_OVERFLOW);
281 result = (*b == '1');
288 if (!bool_str_cmp(&b, len, "yes")) {
289 ++b; // accept the single 'y' character
295 if (!bool_str_cmp(&b, len, "no")) {
302 if (!bool_str_cmp(&b, len, "true")) {
309 if (!bool_str_cmp(&b, len, "false")) {
315 if (bool_str_cmp(&b, len, "on")) {
317 } else if (bool_str_cmp(&b, len, "off")) {
320 return makeUnexpected(ConversionCode::BOOL_INVALID_VALUE);
324 return makeUnexpected(ConversionCode::BOOL_INVALID_VALUE);
333 * StringPiece to double, with progress information. Alters the
334 * StringPiece parameter to munch the already-parsed characters.
337 Expected<Tgt, ConversionCode> str_to_floating(StringPiece* src) noexcept {
338 using namespace double_conversion;
339 static StringToDoubleConverter
340 conv(StringToDoubleConverter::ALLOW_TRAILING_JUNK
341 | StringToDoubleConverter::ALLOW_LEADING_SPACES,
343 // return this for junk input string
344 std::numeric_limits<double>::quiet_NaN(),
348 return makeUnexpected(ConversionCode::EMPTY_INPUT_STRING);
352 auto result = conv.StringToDouble(src->data(),
353 static_cast<int>(src->size()),
354 &length); // processed char count
356 if (!std::isnan(result)) {
357 // If we get here with length = 0, the input string is empty.
358 // If we get here with result = 0.0, it's either because the string
359 // contained only whitespace, or because we had an actual zero value
360 // (with potential trailing junk). If it was only whitespace, we
361 // want to raise an error; length will point past the last character
362 // that was processed, so we need to check if that character was
363 // whitespace or not.
364 if (length == 0 || (result == 0.0 && std::isspace((*src)[length - 1]))) {
365 return makeUnexpected(ConversionCode::EMPTY_INPUT_STRING);
367 src->advance(length);
371 auto* e = src->end();
373 std::find_if_not(src->begin(), e, [](char c) { return std::isspace(c); });
375 // There must be non-whitespace, otherwise we would have caught this above
379 bool negative = false;
388 switch (tolower_ascii(*b)) {
390 if (size >= 3 && tolower_ascii(b[1]) == 'n' &&
391 tolower_ascii(b[2]) == 'f') {
392 if (size >= 8 && tolower_ascii(b[3]) == 'i' &&
393 tolower_ascii(b[4]) == 'n' && tolower_ascii(b[5]) == 'i' &&
394 tolower_ascii(b[6]) == 't' && tolower_ascii(b[7]) == 'y') {
399 result = std::numeric_limits<Tgt>::infinity();
404 if (size >= 3 && tolower_ascii(b[1]) == 'a' &&
405 tolower_ascii(b[2]) == 'n') {
407 result = std::numeric_limits<Tgt>::quiet_NaN();
417 return makeUnexpected(ConversionCode::STRING_TO_FLOAT_ERROR);
429 template Expected<float, ConversionCode> str_to_floating<float>(
430 StringPiece* src) noexcept;
431 template Expected<double, ConversionCode> str_to_floating<double>(
432 StringPiece* src) noexcept;
435 * This class takes care of additional processing needed for signed values,
436 * like leading sign character and overflow checks.
438 template <typename T, bool IsSigned = std::is_signed<T>::value>
439 class SignedValueHandler;
441 template <typename T>
442 class SignedValueHandler<T, true> {
444 ConversionCode init(const char*& b) {
446 if (!std::isdigit(*b)) {
449 } else if (UNLIKELY(*b != '+')) {
450 return ConversionCode::INVALID_LEADING_CHAR;
454 return ConversionCode::SUCCESS;
457 ConversionCode overflow() {
458 return negative_ ? ConversionCode::NEGATIVE_OVERFLOW
459 : ConversionCode::POSITIVE_OVERFLOW;
462 template <typename U>
463 Expected<T, ConversionCode> finalize(U value) {
467 if (UNLIKELY(rv > 0)) {
468 return makeUnexpected(ConversionCode::NEGATIVE_OVERFLOW);
472 if (UNLIKELY(rv < 0)) {
473 return makeUnexpected(ConversionCode::POSITIVE_OVERFLOW);
483 // For unsigned types, we don't need any extra processing
484 template <typename T>
485 class SignedValueHandler<T, false> {
487 ConversionCode init(const char*&) {
488 return ConversionCode::SUCCESS;
491 ConversionCode overflow() {
492 return ConversionCode::POSITIVE_OVERFLOW;
495 Expected<T, ConversionCode> finalize(T value) {
501 * String represented as a pair of pointers to char to signed/unsigned
502 * integrals. Assumes NO whitespace before or after, and also that the
503 * string is composed entirely of digits (and an optional sign only for
504 * signed types). String may be empty, in which case digits_to returns
505 * an appropriate error.
508 inline Expected<Tgt, ConversionCode> digits_to(
510 const char* const e) noexcept {
511 using UT = typename std::make_unsigned<Tgt>::type;
514 SignedValueHandler<Tgt> sgn;
516 auto err = sgn.init(b);
517 if (UNLIKELY(err != ConversionCode::SUCCESS)) {
518 return makeUnexpected(err);
523 /* Although the string is entirely made of digits, we still need to
524 * check for overflow.
526 if (size > std::numeric_limits<UT>::digits10) {
528 if (b < e && *b == '0') {
531 return Tgt(0); // just zeros, e.g. "0000"
539 if (size > std::numeric_limits<UT>::digits10 &&
540 (size != std::numeric_limits<UT>::digits10 + 1 ||
541 strncmp(b, MaxString<UT>::value, size) > 0)) {
542 return makeUnexpected(sgn.overflow());
546 // Here we know that the number won't overflow when
547 // converted. Proceed without checks.
551 for (; e - b >= 4; b += 4) {
553 const int32_t r0 = shift1000[static_cast<size_t>(b[0])];
554 const int32_t r1 = shift100[static_cast<size_t>(b[1])];
555 const int32_t r2 = shift10[static_cast<size_t>(b[2])];
556 const int32_t r3 = shift1[static_cast<size_t>(b[3])];
557 const auto sum = r0 + r1 + r2 + r3;
566 const int32_t r0 = shift100[static_cast<size_t>(b[0])];
567 const int32_t r1 = shift10[static_cast<size_t>(b[1])];
568 const int32_t r2 = shift1[static_cast<size_t>(b[2])];
569 const auto sum = r0 + r1 + r2;
573 result = 1000 * result + sum;
577 const int32_t r0 = shift10[static_cast<size_t>(b[0])];
578 const int32_t r1 = shift1[static_cast<size_t>(b[1])];
579 const auto sum = r0 + r1;
583 result = 100 * result + sum;
587 const int32_t sum = shift1[static_cast<size_t>(b[0])];
591 result = 10 * result + sum;
597 return makeUnexpected(ConversionCode::NO_DIGITS);
602 return sgn.finalize(result);
605 return makeUnexpected(ConversionCode::NON_DIGIT_CHAR);
608 template Expected<char, ConversionCode> digits_to<char>(
610 const char*) noexcept;
611 template Expected<signed char, ConversionCode> digits_to<signed char>(
613 const char*) noexcept;
614 template Expected<unsigned char, ConversionCode> digits_to<unsigned char>(
616 const char*) noexcept;
618 template Expected<short, ConversionCode> digits_to<short>(
620 const char*) noexcept;
621 template Expected<unsigned short, ConversionCode> digits_to<unsigned short>(
623 const char*) noexcept;
625 template Expected<int, ConversionCode> digits_to<int>(
627 const char*) noexcept;
628 template Expected<unsigned int, ConversionCode> digits_to<unsigned int>(
630 const char*) noexcept;
632 template Expected<long, ConversionCode> digits_to<long>(
634 const char*) noexcept;
635 template Expected<unsigned long, ConversionCode> digits_to<unsigned long>(
637 const char*) noexcept;
639 template Expected<long long, ConversionCode> digits_to<long long>(
641 const char*) noexcept;
642 template Expected<unsigned long long, ConversionCode>
643 digits_to<unsigned long long>(const char*, const char*) noexcept;
645 #if FOLLY_HAVE_INT128_T
646 template Expected<__int128, ConversionCode> digits_to<__int128>(
648 const char*) noexcept;
649 template Expected<unsigned __int128, ConversionCode>
650 digits_to<unsigned __int128>(const char*, const char*) noexcept;
654 * StringPiece to integrals, with progress information. Alters the
655 * StringPiece parameter to munch the already-parsed characters.
658 Expected<Tgt, ConversionCode> str_to_integral(StringPiece* src) noexcept {
659 using UT = typename std::make_unsigned<Tgt>::type;
661 auto b = src->data(), past = src->data() + src->size();
664 if (UNLIKELY(b >= past)) {
665 return makeUnexpected(ConversionCode::EMPTY_INPUT_STRING);
667 if (!std::isspace(*b)) {
672 SignedValueHandler<Tgt> sgn;
673 auto err = sgn.init(b);
675 if (UNLIKELY(err != ConversionCode::SUCCESS)) {
676 return makeUnexpected(err);
678 if (std::is_signed<Tgt>::value && UNLIKELY(b >= past)) {
679 return makeUnexpected(ConversionCode::NO_DIGITS);
681 if (UNLIKELY(!isdigit(*b))) {
682 return makeUnexpected(ConversionCode::NON_DIGIT_CHAR);
685 auto m = findFirstNonDigit(b + 1, past);
687 auto tmp = digits_to<UT>(b, m);
689 if (UNLIKELY(!tmp.hasValue())) {
690 return makeUnexpected(
691 tmp.error() == ConversionCode::POSITIVE_OVERFLOW ? sgn.overflow()
695 auto res = sgn.finalize(tmp.value());
697 if (res.hasValue()) {
698 src->advance(m - src->data());
704 template Expected<char, ConversionCode> str_to_integral<char>(
705 StringPiece* src) noexcept;
706 template Expected<signed char, ConversionCode> str_to_integral<signed char>(
707 StringPiece* src) noexcept;
708 template Expected<unsigned char, ConversionCode> str_to_integral<unsigned char>(
709 StringPiece* src) noexcept;
711 template Expected<short, ConversionCode> str_to_integral<short>(
712 StringPiece* src) noexcept;
713 template Expected<unsigned short, ConversionCode>
714 str_to_integral<unsigned short>(StringPiece* src) noexcept;
716 template Expected<int, ConversionCode> str_to_integral<int>(
717 StringPiece* src) noexcept;
718 template Expected<unsigned int, ConversionCode> str_to_integral<unsigned int>(
719 StringPiece* src) noexcept;
721 template Expected<long, ConversionCode> str_to_integral<long>(
722 StringPiece* src) noexcept;
723 template Expected<unsigned long, ConversionCode> str_to_integral<unsigned long>(
724 StringPiece* src) noexcept;
726 template Expected<long long, ConversionCode> str_to_integral<long long>(
727 StringPiece* src) noexcept;
728 template Expected<unsigned long long, ConversionCode>
729 str_to_integral<unsigned long long>(StringPiece* src) noexcept;
731 #if FOLLY_HAVE_INT128_T
732 template Expected<__int128, ConversionCode> str_to_integral<__int128>(
733 StringPiece* src) noexcept;
734 template Expected<unsigned __int128, ConversionCode>
735 str_to_integral<unsigned __int128>(StringPiece* src) noexcept;
738 } // namespace detail
740 ConversionError makeConversionError(ConversionCode code, StringPiece input) {
741 using namespace detail;
743 std::is_unsigned<std::underlying_type<ConversionCode>::type>::value,
744 "ConversionCode should be unsigned");
745 assert((std::size_t)code < kErrorStrings.size());
746 const ErrorString& err = kErrorStrings[(std::size_t)code];
747 if (code == ConversionCode::EMPTY_INPUT_STRING && input.empty()) {
748 return {err.string, code};
750 std::string tmp(err.string);
755 if (input.size() > 0) {
756 tmp.append(input.data(), input.size());