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.
18 * Converts anything to anything, with an emphasis on performance and
21 * @author Andrei Alexandrescu (andrei.alexandrescu@fb.com)
24 #ifndef FOLLY_BASE_CONV_H_
25 #define FOLLY_BASE_CONV_H_
27 #include "folly/FBString.h"
28 #include "folly/Likely.h"
29 #include "folly/Preprocessor.h"
30 #include "folly/Range.h"
32 #include <boost/implicit_cast.hpp>
33 #include <type_traits>
42 #include "double-conversion.h" // V8 JavaScript implementation
44 #define FOLLY_RANGE_CHECK(condition, message) \
45 ((condition) ? (void)0 : throw std::range_error( \
46 (__FILE__ "(" + std::to_string((long long int) __LINE__) + "): " \
47 + (message)).c_str()))
51 /*******************************************************************************
52 * Integral to integral
53 ******************************************************************************/
56 * Checked conversion from integral to integral. The checks are only
57 * performed when meaningful, e.g. conversion from int to long goes
60 template <class Tgt, class Src>
61 typename std::enable_if<
62 std::is_integral<Src>::value && std::is_integral<Tgt>::value,
64 to(const Src & value) {
65 /* static */ if (std::numeric_limits<Tgt>::max()
66 < std::numeric_limits<Src>::max()) {
67 FOLLY_RANGE_CHECK(value <= std::numeric_limits<Tgt>::max(),
70 /* static */ if (std::is_signed<Src>::value &&
71 (!std::is_signed<Tgt>::value || sizeof(Src) > sizeof(Tgt))) {
72 FOLLY_RANGE_CHECK(value >= std::numeric_limits<Tgt>::min(),
75 return static_cast<Tgt>(value);
78 /*******************************************************************************
79 * Floating point to floating point
80 ******************************************************************************/
82 template <class Tgt, class Src>
83 typename std::enable_if<
84 std::is_floating_point<Tgt>::value && std::is_floating_point<Src>::value,
86 to(const Src & value) {
87 /* static */ if (std::numeric_limits<Tgt>::max() <
88 std::numeric_limits<Src>::max()) {
89 FOLLY_RANGE_CHECK(value <= std::numeric_limits<Tgt>::max(),
91 FOLLY_RANGE_CHECK(value >= -std::numeric_limits<Tgt>::max(),
94 return boost::implicit_cast<Tgt>(value);
97 /*******************************************************************************
99 ******************************************************************************/
103 template <class T> struct IsSomeString {
104 enum { value = std::is_same<T, std::string>::value
105 || std::is_same<T, fbstring>::value };
109 const T& getLastElement(const T & v) {
113 template <class T, class... Ts>
114 typename std::tuple_element<
116 std::tuple<T, Ts...> >::type const&
117 getLastElement(const T& v, const Ts&... vs) {
118 return getLastElement(vs...);
121 } // namespace detail
123 /*******************************************************************************
124 * Conversions from integral types to string types.
125 ******************************************************************************/
127 #if FOLLY_HAVE_INT128_T
130 template <typename IntegerType>
131 constexpr unsigned int
133 return ceil((double(sizeof(IntegerType) * CHAR_BIT) * M_LN2) / M_LN10);
137 unsafeTelescope128(char * buffer, unsigned int room, unsigned __int128 x) {
138 typedef unsigned __int128 Usrc;
139 unsigned int p = room - 1;
141 while (x >= (Usrc(1) << 64)) { // Using 128-bit division while needed
142 const auto y = x / 10;
143 const auto digit = x % 10;
145 buffer[p--] = '0' + digit;
149 uint64_t xx = x; // Moving to faster 64-bit division thereafter
152 const auto y = xx / 10ULL;
153 const auto digit = xx % 10ULL;
155 buffer[p--] = '0' + digit;
159 buffer[p] = '0' + xx;
168 * Returns the number of digits in the base 10 representation of an
169 * uint64_t. Useful for preallocating buffers and such. It's also used
170 * internally, see below. Measurements suggest that defining a
171 * separate overload for 32-bit integers is not worthwhile.
174 inline uint32_t digits10(uint64_t v) {
177 if (LIKELY(v < 10)) return result;
178 if (LIKELY(v < 100)) return result + 1;
179 if (LIKELY(v < 1000)) return result + 2;
180 if (LIKELY(v < 10000)) return result + 3;
181 // Skip ahead by 4 orders of magnitude
188 * Copies the ASCII base 10 representation of v into buffer and
189 * returns the number of bytes written. Does NOT append a \0. Assumes
190 * the buffer points to digits10(v) bytes of valid memory. Note that
191 * uint64 needs at most 20 bytes, uint32_t needs at most 10 bytes,
192 * uint16_t needs at most 5 bytes, and so on. Measurements suggest
193 * that defining a separate overload for 32-bit integers is not
196 * This primitive is unsafe because it makes the size assumption and
197 * because it does not add a terminating \0.
200 inline uint32_t uint64ToBufferUnsafe(uint64_t v, char *const buffer) {
201 auto const result = digits10(v);
202 // WARNING: using size_t or pointer arithmetic for pos slows down
203 // the loop below 20x. This is because several 32-bit ops can be
204 // done in parallel, but only fewer 64-bit ones.
205 uint32_t pos = result - 1;
207 // Keep these together so a peephole optimization "sees" them and
208 // computes them in one shot.
209 auto const q = v / 10;
210 auto const r = static_cast<uint32_t>(v % 10);
211 buffer[pos--] = '0' + r;
214 // Last digit is trivial to handle
215 buffer[pos] = static_cast<uint32_t>(v) + '0';
220 * A single char gets appended.
223 void toAppend(char value, Tgt * result) {
228 * Everything implicitly convertible to const char* gets appended.
230 template <class Tgt, class Src>
231 typename std::enable_if<
232 std::is_convertible<Src, const char*>::value
233 && detail::IsSomeString<Tgt>::value>::type
234 toAppend(Src value, Tgt * result) {
235 // Treat null pointers like an empty string, as in:
236 // operator<<(std::ostream&, const char*).
237 const char* c = value;
239 result->append(value);
244 * Strings get appended, too.
246 template <class Tgt, class Src>
247 typename std::enable_if<
248 detail::IsSomeString<Src>::value && detail::IsSomeString<Tgt>::value>::type
249 toAppend(const Src& value, Tgt * result) {
250 result->append(value);
254 * and StringPiece objects too
257 typename std::enable_if<
258 detail::IsSomeString<Tgt>::value>::type
259 toAppend(StringPiece value, Tgt * result) {
260 result->append(value.data(), value.size());
264 * There's no implicit conversion from fbstring to other string types,
265 * so make a specialization.
268 typename std::enable_if<
269 detail::IsSomeString<Tgt>::value>::type
270 toAppend(const fbstring& value, Tgt * result) {
271 result->append(value.data(), value.size());
274 #if FOLLY_HAVE_INT128_T
276 * Special handling for 128 bit integers.
281 toAppend(__int128 value, Tgt * result) {
282 typedef unsigned __int128 Usrc;
283 char buffer[detail::digitsEnough<unsigned __int128>() + 1];
287 p = detail::unsafeTelescope128(buffer, sizeof(buffer), Usrc(-value));
290 p = detail::unsafeTelescope128(buffer, sizeof(buffer), value);
293 result->append(buffer + p, buffer + sizeof(buffer));
298 toAppend(unsigned __int128 value, Tgt * result) {
299 char buffer[detail::digitsEnough<unsigned __int128>()];
302 p = detail::unsafeTelescope128(buffer, sizeof(buffer), value);
304 result->append(buffer + p, buffer + sizeof(buffer));
310 * int32_t and int64_t to string (by appending) go through here. The
311 * result is APPENDED to a preexisting string passed as the second
312 * parameter. This should be efficient with fbstring because fbstring
313 * incurs no dynamic allocation below 23 bytes and no number has more
314 * than 22 bytes in its textual representation (20 for digits, one for
315 * sign, one for the terminating 0).
317 template <class Tgt, class Src>
318 typename std::enable_if<
319 std::is_integral<Src>::value && std::is_signed<Src>::value &&
320 detail::IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
321 toAppend(Src value, Tgt * result) {
322 typedef typename std::make_unsigned<Src>::type Usrc;
325 result->push_back('-');
326 result->append(buffer, uint64ToBufferUnsafe(-uint64_t(value), buffer));
328 result->append(buffer, uint64ToBufferUnsafe(value, buffer));
333 * As above, but for uint32_t and uint64_t.
335 template <class Tgt, class Src>
336 typename std::enable_if<
337 std::is_integral<Src>::value && !std::is_signed<Src>::value
338 && detail::IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
339 toAppend(Src value, Tgt * result) {
341 result->append(buffer, buffer + uint64ToBufferUnsafe(value, buffer));
345 * All small signed and unsigned integers to string go through 32-bit
346 * types int32_t and uint32_t, respectively.
348 template <class Tgt, class Src>
349 typename std::enable_if<
350 std::is_integral<Src>::value
351 && detail::IsSomeString<Tgt>::value && sizeof(Src) < 4>::type
352 toAppend(Src value, Tgt * result) {
354 std::conditional<std::is_signed<Src>::value, int64_t, uint64_t>::type
356 toAppend<Tgt>(static_cast<Intermediate>(value), result);
359 #if defined(__GNUC__) && __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
360 // std::underlying_type became available by gcc 4.7.0
363 * Enumerated values get appended as integers.
365 template <class Tgt, class Src>
366 typename std::enable_if<
367 std::is_enum<Src>::value && detail::IsSomeString<Tgt>::value>::type
368 toAppend(Src value, Tgt * result) {
370 static_cast<typename std::underlying_type<Src>::type>(value), result);
376 * Enumerated values get appended as integers.
378 template <class Tgt, class Src>
379 typename std::enable_if<
380 std::is_enum<Src>::value && detail::IsSomeString<Tgt>::value>::type
381 toAppend(Src value, Tgt * result) {
382 /* static */ if (Src(-1) < 0) {
383 /* static */ if (sizeof(Src) <= sizeof(int)) {
384 toAppend(static_cast<int>(value), result);
386 toAppend(static_cast<long>(value), result);
389 /* static */ if (sizeof(Src) <= sizeof(int)) {
390 toAppend(static_cast<unsigned int>(value), result);
392 toAppend(static_cast<unsigned long>(value), result);
397 #endif // gcc 4.7 onwards
399 /*******************************************************************************
400 * Conversions from floating-point types to string types.
401 ******************************************************************************/
403 /** Wrapper around DoubleToStringConverter **/
404 template <class Tgt, class Src>
405 typename std::enable_if<
406 std::is_floating_point<Src>::value
407 && detail::IsSomeString<Tgt>::value>::type
411 double_conversion::DoubleToStringConverter::DtoaMode mode,
412 unsigned int numDigits) {
413 using namespace double_conversion;
414 DoubleToStringConverter
415 conv(DoubleToStringConverter::NO_FLAGS,
416 "infinity", "NaN", 'E',
417 -6, // decimal in shortest low
418 21, // decimal in shortest high
419 6, // max leading padding zeros
420 1); // max trailing padding zeros
422 StringBuilder builder(buffer, sizeof(buffer));
424 case DoubleToStringConverter::SHORTEST:
425 conv.ToShortest(value, &builder);
427 case DoubleToStringConverter::FIXED:
428 conv.ToFixed(value, numDigits, &builder);
431 CHECK(mode == DoubleToStringConverter::PRECISION);
432 conv.ToPrecision(value, numDigits, &builder);
435 const size_t length = builder.position();
437 result->append(buffer, length);
441 * As above, but for floating point
443 template <class Tgt, class Src>
444 typename std::enable_if<
445 std::is_floating_point<Src>::value
446 && detail::IsSomeString<Tgt>::value>::type
447 toAppend(Src value, Tgt * result) {
449 value, result, double_conversion::DoubleToStringConverter::SHORTEST, 0);
453 * Variadic conversion to string. Appends each element in turn.
455 template <class T, class... Ts>
456 typename std::enable_if<sizeof...(Ts) >= 2
457 && detail::IsSomeString<
458 typename std::remove_pointer<
459 typename std::tuple_element<
460 sizeof...(Ts) - 1, std::tuple<Ts...>
461 >::type>::type>::value>::type
462 toAppend(const T& v, const Ts&... vs) {
463 toAppend(v, detail::getLastElement(vs...));
468 * Variadic base case: do nothing.
471 typename std::enable_if<detail::IsSomeString<Tgt>::value>::type
472 toAppend(Tgt* result) {
476 * to<SomeString>(v1, v2, ...) uses toAppend() (see below) as back-end
479 template <class Tgt, class... Ts>
480 typename std::enable_if<detail::IsSomeString<Tgt>::value, Tgt>::type
481 to(const Ts&... vs) {
483 toAppend(vs..., &result);
487 /*******************************************************************************
488 * Conversions from string types to integral types.
489 ******************************************************************************/
494 * Finds the first non-digit in a string. The number of digits
495 * searched depends on the precision of the Tgt integral. Assumes the
496 * string starts with NO whitespace and NO sign.
498 * The semantics of the routine is:
500 * if (b >= e || !isdigit(*b)) return b;
503 * Complete unrolling marks bottom-line (i.e. entire conversion)
504 * improvements of 20%.
507 const char* findFirstNonDigit(const char* b, const char* e) {
509 auto const c = static_cast<unsigned>(*b) - '0';
515 // Maximum value of number when represented as a string
516 template <class T> struct MaxString {
517 static const char*const value;
522 * Lookup tables that converts from a decimal character value to an integral
523 * binary value, shifted by a decimal "shift" multiplier.
524 * For all character values in the range '0'..'9', the table at those
525 * index locations returns the actual decimal value shifted by the multiplier.
526 * For all other values, the lookup table returns an invalid OOR value.
528 // Out-of-range flag value, larger than the largest value that can fit in
529 // four decimal bytes (9999), but four of these added up together should
530 // still not overflow uint16_t.
531 constexpr int32_t OOR = 10000;
533 __attribute__((aligned(16))) constexpr uint16_t shift1[] = {
534 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
535 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
536 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
537 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
538 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
539 1, 2, 3, 4, 5, 6, 7, 8, 9, OOR, OOR,
540 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
541 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
542 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
543 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
544 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
545 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
546 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
547 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
548 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
549 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
550 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
551 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
552 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
553 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
554 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
555 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
556 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
557 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
558 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
559 OOR, OOR, OOR, OOR, OOR, OOR // 250
562 __attribute__((aligned(16))) constexpr uint16_t shift10[] = {
563 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
564 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
565 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
566 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
567 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
568 10, 20, 30, 40, 50, 60, 70, 80, 90, OOR, OOR,
569 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
570 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
571 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
572 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
573 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
574 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
575 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
576 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
577 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
578 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
579 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
580 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
581 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
582 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
583 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
584 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
585 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
586 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
587 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
588 OOR, OOR, OOR, OOR, OOR, OOR // 250
591 __attribute__((aligned(16))) constexpr uint16_t shift100[] = {
592 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
593 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
594 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
595 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
596 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
597 100, 200, 300, 400, 500, 600, 700, 800, 900, OOR, OOR,
598 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
599 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
600 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
601 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
602 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
603 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
604 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
605 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
606 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
607 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
608 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
609 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
610 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
611 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
612 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
613 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
614 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
615 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
616 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
617 OOR, OOR, OOR, OOR, OOR, OOR // 250
620 __attribute__((aligned(16))) constexpr uint16_t shift1000[] = {
621 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
622 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
623 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
624 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
625 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
626 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, OOR, OOR,
627 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
628 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
629 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
630 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
631 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
632 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
633 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
634 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
635 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
636 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
637 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
638 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
639 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
640 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
641 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
642 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
643 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
644 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
645 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
646 OOR, OOR, OOR, OOR, OOR, OOR // 250
650 * String represented as a pair of pointers to char to unsigned
651 * integrals. Assumes NO whitespace before or after, and also that the
652 * string is composed entirely of digits. Tgt must be unsigned, and no
653 * sign is allowed in the string (even it's '+'). String may be empty,
654 * in which case digits_to throws.
657 Tgt digits_to(const char * b, const char * e) {
659 static_assert(!std::is_signed<Tgt>::value, "Unsigned type expected");
662 const size_t size = e - b;
664 /* Although the string is entirely made of digits, we still need to
665 * check for overflow.
667 if (size >= std::numeric_limits<Tgt>::digits10 + 1) {
668 // Leading zeros? If so, recurse to keep things simple
669 if (b < e && *b == '0') {
671 if (b == e) return 0; // just zeros, e.g. "0000"
672 if (*b != '0') return digits_to<Tgt>(b, e);
675 FOLLY_RANGE_CHECK(size == std::numeric_limits<Tgt>::digits10 + 1 &&
676 strncmp(b, detail::MaxString<Tgt>::value, size) <= 0,
677 "Numeric overflow upon conversion");
680 // Here we know that the number won't overflow when
681 // converted. Proceed without checks.
685 for (; e - b >= 4; b += 4) {
687 const int32_t r0 = shift1000[static_cast<size_t>(b[0])];
688 const int32_t r1 = shift100[static_cast<size_t>(b[1])];
689 const int32_t r2 = shift10[static_cast<size_t>(b[2])];
690 const int32_t r3 = shift1[static_cast<size_t>(b[3])];
691 const auto sum = r0 + r1 + r2 + r3;
692 assert(sum < OOR && "Assumption: string only has digits");
698 const int32_t r0 = shift100[static_cast<size_t>(b[0])];
699 const int32_t r1 = shift10[static_cast<size_t>(b[1])];
700 const int32_t r2 = shift1[static_cast<size_t>(b[2])];
701 const auto sum = r0 + r1 + r2;
702 assert(sum < OOR && "Assumption: string only has digits");
703 return result * 1000 + sum;
706 const int32_t r0 = shift10[static_cast<size_t>(b[0])];
707 const int32_t r1 = shift1[static_cast<size_t>(b[1])];
708 const auto sum = r0 + r1;
709 assert(sum < OOR && "Assumption: string only has digits");
710 return result * 100 + sum;
713 const int32_t sum = shift1[static_cast<size_t>(b[0])];
714 assert(sum < OOR && "Assumption: string only has digits");
715 return result * 10 + sum;
720 FOLLY_RANGE_CHECK(size > 0, "Found no digits to convert in input");
725 bool str_to_bool(StringPiece * src);
727 } // namespace detail
730 * String represented as a pair of pointers to char to unsigned
731 * integrals. Assumes NO whitespace before or after.
734 typename std::enable_if<
735 std::is_integral<Tgt>::value && !std::is_signed<Tgt>::value
736 && !std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
738 to(const char * b, const char * e) {
739 return detail::digits_to<Tgt>(b, e);
743 * String represented as a pair of pointers to char to signed
744 * integrals. Assumes NO whitespace before or after. Allows an
745 * optional leading sign.
748 typename std::enable_if<
749 std::is_integral<Tgt>::value && std::is_signed<Tgt>::value,
751 to(const char * b, const char * e) {
752 FOLLY_RANGE_CHECK(b < e, "Empty input string in conversion to integral");
755 Tgt result = -to<typename std::make_unsigned<Tgt>::type>(b + 1, e);
756 FOLLY_RANGE_CHECK(result <= 0, "Negative overflow.");
759 FOLLY_RANGE_CHECK(*b == '+', "Invalid lead character");
762 Tgt result = to<typename std::make_unsigned<Tgt>::type>(b, e);
763 FOLLY_RANGE_CHECK(result >= 0, "Overflow.");
768 * Parsing strings to integrals. These routines differ from
769 * to<integral>(string) in that they take a POINTER TO a StringPiece
770 * and alter that StringPiece to reflect progress information.
774 * StringPiece to integrals, with progress information. Alters the
775 * StringPiece parameter to munch the already-parsed characters.
778 typename std::enable_if<
779 std::is_integral<Tgt>::value
780 && !std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
782 to(StringPiece * src) {
784 auto b = src->data(), past = src->data() + src->size();
786 FOLLY_RANGE_CHECK(b < past, "No digits found in input string");
787 if (!isspace(*b)) break;
792 // First digit is customized because we test for sign
793 bool negative = false;
794 /* static */ if (std::is_signed<Tgt>::value) {
799 FOLLY_RANGE_CHECK(*m == '+', "Invalid leading character in conversion"
806 FOLLY_RANGE_CHECK(m < past, "No digits found in input string");
807 FOLLY_RANGE_CHECK(isdigit(*m), "Non-digit character found");
808 m = detail::findFirstNonDigit<Tgt>(m + 1, past);
811 /* static */ if (!std::is_signed<Tgt>::value) {
812 result = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
814 auto t = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
817 FOLLY_RANGE_CHECK(result <= 0, "Negative overflow");
820 FOLLY_RANGE_CHECK(result >= 0, "Overflow");
823 src->advance(m - src->data());
828 * StringPiece to bool, with progress information. Alters the
829 * StringPiece parameter to munch the already-parsed characters.
832 typename std::enable_if<
833 std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
835 to(StringPiece * src) {
836 return detail::str_to_bool(src);
842 * Enforce that the suffix following a number is made up only of whitespace.
844 inline void enforceWhitespace(const char* b, const char* e) {
845 for (; b != e; ++b) {
846 FOLLY_RANGE_CHECK(isspace(*b), to<std::string>("Non-whitespace: ", *b));
850 } // namespace detail
853 * String or StringPiece to integrals. Accepts leading and trailing
854 * whitespace, but no non-space trailing characters.
857 typename std::enable_if<
858 std::is_integral<Tgt>::value,
860 to(StringPiece src) {
861 Tgt result = to<Tgt>(&src);
862 detail::enforceWhitespace(src.data(), src.data() + src.size());
866 /*******************************************************************************
867 * Conversions from string types to floating-point types.
868 ******************************************************************************/
871 * StringPiece to double, with progress information. Alters the
872 * StringPiece parameter to munch the already-parsed characters.
875 inline typename std::enable_if<
876 std::is_floating_point<Tgt>::value,
878 to(StringPiece *const src) {
879 using namespace double_conversion;
880 static StringToDoubleConverter
881 conv(StringToDoubleConverter::ALLOW_TRAILING_JUNK
882 | StringToDoubleConverter::ALLOW_LEADING_SPACES,
884 // return this for junk input string
885 std::numeric_limits<double>::quiet_NaN(),
888 FOLLY_RANGE_CHECK(!src->empty(), "No digits found in input string");
891 auto result = conv.StringToDouble(src->data(), src->size(),
892 &length); // processed char count
894 if (!std::isnan(result)) {
895 src->advance(length);
899 for (;; src->advance(1)) {
901 throw std::range_error("Unable to convert an empty string"
902 " to a floating point value.");
904 if (!isspace(src->front())) {
909 // Was that "inf[inity]"?
910 if (src->size() >= 3 && toupper((*src)[0]) == 'I'
911 && toupper((*src)[1]) == 'N' && toupper((*src)[2]) == 'F') {
912 if (src->size() >= 8 &&
913 toupper((*src)[3]) == 'I' &&
914 toupper((*src)[4]) == 'N' &&
915 toupper((*src)[5]) == 'I' &&
916 toupper((*src)[6]) == 'T' &&
917 toupper((*src)[7]) == 'Y') {
922 return std::numeric_limits<Tgt>::infinity();
925 // Was that "-inf[inity]"?
926 if (src->size() >= 4 && toupper((*src)[0]) == '-'
927 && toupper((*src)[1]) == 'I' && toupper((*src)[2]) == 'N'
928 && toupper((*src)[3]) == 'F') {
929 if (src->size() >= 9 &&
930 toupper((*src)[4]) == 'I' &&
931 toupper((*src)[5]) == 'N' &&
932 toupper((*src)[6]) == 'I' &&
933 toupper((*src)[7]) == 'T' &&
934 toupper((*src)[8]) == 'Y') {
939 return -std::numeric_limits<Tgt>::infinity();
943 if (src->size() >= 3 && toupper((*src)[0]) == 'N'
944 && toupper((*src)[1]) == 'A' && toupper((*src)[2]) == 'N') {
946 return std::numeric_limits<Tgt>::quiet_NaN();
950 if (src->size() >= 4 &&
951 toupper((*src)[0]) == '-' &&
952 toupper((*src)[1]) == 'N' &&
953 toupper((*src)[2]) == 'A' &&
954 toupper((*src)[3]) == 'N') {
956 return -std::numeric_limits<Tgt>::quiet_NaN();
960 throw std::range_error("Unable to convert \"" + src->toString()
961 + "\" to a floating point value.");
965 * Any string, const char*, or StringPiece to double.
968 typename std::enable_if<
969 std::is_floating_point<Tgt>::value,
971 to(StringPiece src) {
972 Tgt result = to<double>(&src);
973 detail::enforceWhitespace(src.data(), src.data() + src.size());
977 /*******************************************************************************
978 * Integral to floating point and back
979 ******************************************************************************/
982 * Checked conversion from integral to flating point and back. The
983 * result must be convertible back to the source type without loss of
984 * precision. This seems Draconian but sometimes is what's needed, and
985 * complements existing routines nicely. For various rounding
986 * routines, see <math>.
988 template <class Tgt, class Src>
989 typename std::enable_if<
990 (std::is_integral<Src>::value && std::is_floating_point<Tgt>::value)
992 (std::is_floating_point<Src>::value && std::is_integral<Tgt>::value),
994 to(const Src & value) {
996 auto witness = static_cast<Src>(result);
997 if (value != witness) {
998 throw std::range_error(
999 to<std::string>("to<>: loss of precision when converting ", value,
1000 " to type ", typeid(Tgt).name()).c_str());
1005 /*******************************************************************************
1006 * Enum to anything and back
1007 ******************************************************************************/
1009 #if defined(__GNUC__) && __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
1010 // std::underlying_type became available by gcc 4.7.0
1012 template <class Tgt, class Src>
1013 typename std::enable_if<std::is_enum<Src>::value, Tgt>::type
1014 to(const Src & value) {
1015 return to<Tgt>(static_cast<typename std::underlying_type<Src>::type>(value));
1018 template <class Tgt, class Src>
1019 typename std::enable_if<std::is_enum<Tgt>::value, Tgt>::type
1020 to(const Src & value) {
1021 return static_cast<Tgt>(to<typename std::underlying_type<Tgt>::type>(value));
1026 template <class Tgt, class Src>
1027 typename std::enable_if<std::is_enum<Src>::value, Tgt>::type
1028 to(const Src & value) {
1029 /* static */ if (Src(-1) < 0) {
1030 /* static */ if (sizeof(Src) <= sizeof(int)) {
1031 return to<Tgt>(static_cast<int>(value));
1033 return to<Tgt>(static_cast<long>(value));
1036 /* static */ if (sizeof(Src) <= sizeof(int)) {
1037 return to<Tgt>(static_cast<unsigned int>(value));
1039 return to<Tgt>(static_cast<unsigned long>(value));
1044 template <class Tgt, class Src>
1045 typename std::enable_if<std::is_enum<Tgt>::value, Tgt>::type
1046 to(const Src & value) {
1047 /* static */ if (Tgt(-1) < 0) {
1048 /* static */ if (sizeof(Tgt) <= sizeof(int)) {
1049 return static_cast<Tgt>(to<int>(value));
1051 return static_cast<Tgt>(to<long>(value));
1054 /* static */ if (sizeof(Tgt) <= sizeof(int)) {
1055 return static_cast<Tgt>(to<unsigned int>(value));
1057 return static_cast<Tgt>(to<unsigned long>(value));
1062 #endif // gcc 4.7 onwards
1064 } // namespace folly
1066 // FOLLY_CONV_INTERNAL is defined by Conv.cpp. Keep the FOLLY_RANGE_CHECK
1067 // macro for use in Conv.cpp, but #undefine it everywhere else we are included,
1068 // to avoid defining this global macro name in other files that include Conv.h.
1069 #ifndef FOLLY_CONV_INTERNAL
1070 #undef FOLLY_RANGE_CHECK
1073 #endif /* FOLLY_BASE_CONV_H_ */