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.
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 // V8 JavaScript implementation
43 #include <double-conversion/double-conversion.h>
45 #define FOLLY_RANGE_CHECK(condition, message) \
46 ((condition) ? (void)0 : throw std::range_error( \
47 (__FILE__ "(" + std::to_string((long long int) __LINE__) + "): " \
48 + (message)).c_str()))
52 /*******************************************************************************
53 * Integral to integral
54 ******************************************************************************/
57 * Checked conversion from integral to integral. The checks are only
58 * performed when meaningful, e.g. conversion from int to long goes
61 template <class Tgt, class Src>
62 typename std::enable_if<
63 std::is_integral<Src>::value && std::is_integral<Tgt>::value,
65 to(const Src & value) {
66 /* static */ if (std::numeric_limits<Tgt>::max()
67 < std::numeric_limits<Src>::max()) {
69 (!greater_than<Tgt, std::numeric_limits<Tgt>::max()>(value)),
73 /* static */ if (std::is_signed<Src>::value &&
74 (!std::is_signed<Tgt>::value || sizeof(Src) > sizeof(Tgt))) {
76 (!less_than<Tgt, std::numeric_limits<Tgt>::min()>(value)),
80 return static_cast<Tgt>(value);
83 /*******************************************************************************
84 * Floating point to floating point
85 ******************************************************************************/
87 template <class Tgt, class Src>
88 typename std::enable_if<
89 std::is_floating_point<Tgt>::value && std::is_floating_point<Src>::value,
91 to(const Src & value) {
92 /* static */ if (std::numeric_limits<Tgt>::max() <
93 std::numeric_limits<Src>::max()) {
94 FOLLY_RANGE_CHECK(value <= std::numeric_limits<Tgt>::max(),
96 FOLLY_RANGE_CHECK(value >= -std::numeric_limits<Tgt>::max(),
99 return boost::implicit_cast<Tgt>(value);
102 /*******************************************************************************
104 ******************************************************************************/
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 // This class exists to specialize away std::tuple_element in the case where we
122 // have 0 template arguments. Without this, Clang/libc++ will blow a
123 // static_assert even if tuple_element is protected by an enable_if.
124 template <class... Ts>
125 struct last_element {
126 typedef typename std::enable_if<
128 typename std::tuple_element<
129 sizeof...(Ts) - 1, std::tuple<Ts...>
134 struct last_element<> {
138 } // namespace detail
140 /*******************************************************************************
141 * Conversions from integral types to string types.
142 ******************************************************************************/
144 #if FOLLY_HAVE_INT128_T
147 template <typename IntegerType>
148 constexpr unsigned int
150 return ceil((double(sizeof(IntegerType) * CHAR_BIT) * M_LN2) / M_LN10);
154 unsafeTelescope128(char * buffer, unsigned int room, unsigned __int128 x) {
155 typedef unsigned __int128 Usrc;
156 unsigned int p = room - 1;
158 while (x >= (Usrc(1) << 64)) { // Using 128-bit division while needed
159 const auto y = x / 10;
160 const auto digit = x % 10;
162 buffer[p--] = '0' + digit;
166 uint64_t xx = x; // Moving to faster 64-bit division thereafter
169 const auto y = xx / 10ULL;
170 const auto digit = xx % 10ULL;
172 buffer[p--] = '0' + digit;
176 buffer[p] = '0' + xx;
185 * Returns the number of digits in the base 10 representation of an
186 * uint64_t. Useful for preallocating buffers and such. It's also used
187 * internally, see below. Measurements suggest that defining a
188 * separate overload for 32-bit integers is not worthwhile.
191 inline uint32_t digits10(uint64_t v) {
194 if (LIKELY(v < 10)) return result;
195 if (LIKELY(v < 100)) return result + 1;
196 if (LIKELY(v < 1000)) return result + 2;
197 if (LIKELY(v < 10000)) return result + 3;
198 // Skip ahead by 4 orders of magnitude
205 * Copies the ASCII base 10 representation of v into buffer and
206 * returns the number of bytes written. Does NOT append a \0. Assumes
207 * the buffer points to digits10(v) bytes of valid memory. Note that
208 * uint64 needs at most 20 bytes, uint32_t needs at most 10 bytes,
209 * uint16_t needs at most 5 bytes, and so on. Measurements suggest
210 * that defining a separate overload for 32-bit integers is not
213 * This primitive is unsafe because it makes the size assumption and
214 * because it does not add a terminating \0.
217 inline uint32_t uint64ToBufferUnsafe(uint64_t v, char *const buffer) {
218 auto const result = digits10(v);
219 // WARNING: using size_t or pointer arithmetic for pos slows down
220 // the loop below 20x. This is because several 32-bit ops can be
221 // done in parallel, but only fewer 64-bit ones.
222 uint32_t pos = result - 1;
224 // Keep these together so a peephole optimization "sees" them and
225 // computes them in one shot.
226 auto const q = v / 10;
227 auto const r = static_cast<uint32_t>(v % 10);
228 buffer[pos--] = '0' + r;
231 // Last digit is trivial to handle
232 buffer[pos] = static_cast<uint32_t>(v) + '0';
237 * A single char gets appended.
240 void toAppend(char value, Tgt * result) {
245 constexpr typename std::enable_if<
246 std::is_same<T, char>::value,
248 estimateSpaceNeeded(T) {
253 * Ubiquitous helper template for writing string appenders
255 template <class T> struct IsSomeString {
256 enum { value = std::is_same<T, std::string>::value
257 || std::is_same<T, fbstring>::value };
261 * Everything implicitly convertible to const char* gets appended.
263 template <class Tgt, class Src>
264 typename std::enable_if<
265 std::is_convertible<Src, const char*>::value
266 && IsSomeString<Tgt>::value>::type
267 toAppend(Src value, Tgt * result) {
268 // Treat null pointers like an empty string, as in:
269 // operator<<(std::ostream&, const char*).
270 const char* c = value;
272 result->append(value);
277 typename std::enable_if<
278 std::is_convertible<Src, const char*>::value,
280 estimateSpaceNeeded(Src value) {
281 const char *c = value;
283 return folly::StringPiece(value).size();
289 typename std::enable_if<
290 (std::is_convertible<Src, folly::StringPiece>::value ||
291 IsSomeString<Src>::value) &&
292 !std::is_convertible<Src, const char*>::value,
294 estimateSpaceNeeded(Src value) {
295 return folly::StringPiece(value).size();
299 typename std::enable_if<
300 std::is_pointer<Src>::value &&
301 IsSomeString<std::remove_pointer<Src>>::value,
303 estimateSpaceNeeded(Src value) {
304 return value->size();
308 * Strings get appended, too.
310 template <class Tgt, class Src>
311 typename std::enable_if<
312 IsSomeString<Src>::value && IsSomeString<Tgt>::value>::type
313 toAppend(const Src& value, Tgt * result) {
314 result->append(value);
318 * and StringPiece objects too
321 typename std::enable_if<
322 IsSomeString<Tgt>::value>::type
323 toAppend(StringPiece value, Tgt * result) {
324 result->append(value.data(), value.size());
328 * There's no implicit conversion from fbstring to other string types,
329 * so make a specialization.
332 typename std::enable_if<
333 IsSomeString<Tgt>::value>::type
334 toAppend(const fbstring& value, Tgt * result) {
335 result->append(value.data(), value.size());
338 #if FOLLY_HAVE_INT128_T
340 * Special handling for 128 bit integers.
345 toAppend(__int128 value, Tgt * result) {
346 typedef unsigned __int128 Usrc;
347 char buffer[detail::digitsEnough<unsigned __int128>() + 1];
351 p = detail::unsafeTelescope128(buffer, sizeof(buffer), Usrc(-value));
354 p = detail::unsafeTelescope128(buffer, sizeof(buffer), value);
357 result->append(buffer + p, buffer + sizeof(buffer));
362 toAppend(unsigned __int128 value, Tgt * result) {
363 char buffer[detail::digitsEnough<unsigned __int128>()];
366 p = detail::unsafeTelescope128(buffer, sizeof(buffer), value);
368 result->append(buffer + p, buffer + sizeof(buffer));
372 constexpr typename std::enable_if<
373 std::is_same<T, __int128>::value,
375 estimateSpaceNeeded(T) {
376 return detail::digitsEnough<__int128>();
380 constexpr typename std::enable_if<
381 std::is_same<T, unsigned __int128>::value,
383 estimateSpaceNeeded(T) {
384 return detail::digitsEnough<unsigned __int128>();
390 * int32_t and int64_t to string (by appending) go through here. The
391 * result is APPENDED to a preexisting string passed as the second
392 * parameter. This should be efficient with fbstring because fbstring
393 * incurs no dynamic allocation below 23 bytes and no number has more
394 * than 22 bytes in its textual representation (20 for digits, one for
395 * sign, one for the terminating 0).
397 template <class Tgt, class Src>
398 typename std::enable_if<
399 std::is_integral<Src>::value && std::is_signed<Src>::value &&
400 IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
401 toAppend(Src value, Tgt * result) {
404 result->push_back('-');
405 result->append(buffer, uint64ToBufferUnsafe(-uint64_t(value), buffer));
407 result->append(buffer, uint64ToBufferUnsafe(value, buffer));
412 typename std::enable_if<
413 std::is_integral<Src>::value && std::is_signed<Src>::value
414 && sizeof(Src) >= 4 && sizeof(Src) < 16,
416 estimateSpaceNeeded(Src value) {
418 return 1 + digits10(static_cast<uint64_t>(-value));
421 return digits10(static_cast<uint64_t>(value));
425 * As above, but for uint32_t and uint64_t.
427 template <class Tgt, class Src>
428 typename std::enable_if<
429 std::is_integral<Src>::value && !std::is_signed<Src>::value
430 && IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
431 toAppend(Src value, Tgt * result) {
433 result->append(buffer, buffer + uint64ToBufferUnsafe(value, buffer));
437 typename std::enable_if<
438 std::is_integral<Src>::value && !std::is_signed<Src>::value
439 && sizeof(Src) >= 4 && sizeof(Src) < 16,
441 estimateSpaceNeeded(Src value) {
442 return digits10(value);
446 * All small signed and unsigned integers to string go through 32-bit
447 * types int32_t and uint32_t, respectively.
449 template <class Tgt, class Src>
450 typename std::enable_if<
451 std::is_integral<Src>::value
452 && IsSomeString<Tgt>::value && sizeof(Src) < 4>::type
453 toAppend(Src value, Tgt * result) {
455 std::conditional<std::is_signed<Src>::value, int64_t, uint64_t>::type
457 toAppend<Tgt>(static_cast<Intermediate>(value), result);
461 typename std::enable_if<
462 std::is_integral<Src>::value
464 && !std::is_same<Src, char>::value,
466 estimateSpaceNeeded(Src value) {
468 std::conditional<std::is_signed<Src>::value, int64_t, uint64_t>::type
470 return estimateSpaceNeeded(static_cast<Intermediate>(value));
473 #if defined(__clang__) || __GNUC_PREREQ(4, 7)
474 // std::underlying_type became available by gcc 4.7.0
477 * Enumerated values get appended as integers.
479 template <class Tgt, class Src>
480 typename std::enable_if<
481 std::is_enum<Src>::value && IsSomeString<Tgt>::value>::type
482 toAppend(Src value, Tgt * result) {
484 static_cast<typename std::underlying_type<Src>::type>(value), result);
488 typename std::enable_if<
489 std::is_enum<Src>::value, size_t>::type
490 estimateSpaceNeeded(Src value) {
491 return estimateSpaceNeeded(
492 static_cast<typename std::underlying_type<Src>::type>(value));
498 * Enumerated values get appended as integers.
500 template <class Tgt, class Src>
501 typename std::enable_if<
502 std::is_enum<Src>::value && IsSomeString<Tgt>::value>::type
503 toAppend(Src value, Tgt * result) {
504 /* static */ if (Src(-1) < 0) {
505 /* static */ if (sizeof(Src) <= sizeof(int)) {
506 toAppend(static_cast<int>(value), result);
508 toAppend(static_cast<long>(value), result);
511 /* static */ if (sizeof(Src) <= sizeof(int)) {
512 toAppend(static_cast<unsigned int>(value), result);
514 toAppend(static_cast<unsigned long>(value), result);
520 typename std::enable_if<
521 std::is_enum<Src>::value, size_t>::type
522 estimateSpaceNeeded(Src value) {
523 /* static */ if (Src(-1) < 0) {
524 /* static */ if (sizeof(Src) <= sizeof(int)) {
525 return estimateSpaceNeeded(static_cast<int>(value));
527 return estimateSpaceNeeded(static_cast<long>(value));
530 /* static */ if (sizeof(Src) <= sizeof(int)) {
531 return estimateSpaceNeeded(static_cast<unsigned int>(value));
533 return estimateSpaceNeeded(static_cast<unsigned long>(value));
538 #endif // gcc 4.7 onwards
540 /*******************************************************************************
541 * Conversions from floating-point types to string types.
542 ******************************************************************************/
544 /** Wrapper around DoubleToStringConverter **/
545 template <class Tgt, class Src>
546 typename std::enable_if<
547 std::is_floating_point<Src>::value
548 && IsSomeString<Tgt>::value>::type
552 double_conversion::DoubleToStringConverter::DtoaMode mode,
553 unsigned int numDigits) {
554 using namespace double_conversion;
555 DoubleToStringConverter
556 conv(DoubleToStringConverter::NO_FLAGS,
557 "infinity", "NaN", 'E',
558 -6, // decimal in shortest low
559 21, // decimal in shortest high
560 6, // max leading padding zeros
561 1); // max trailing padding zeros
563 StringBuilder builder(buffer, sizeof(buffer));
565 case DoubleToStringConverter::SHORTEST:
566 conv.ToShortest(value, &builder);
568 case DoubleToStringConverter::FIXED:
569 conv.ToFixed(value, numDigits, &builder);
572 CHECK(mode == DoubleToStringConverter::PRECISION);
573 conv.ToPrecision(value, numDigits, &builder);
576 const size_t length = builder.position();
578 result->append(buffer, length);
582 * As above, but for floating point
584 template <class Tgt, class Src>
585 typename std::enable_if<
586 std::is_floating_point<Src>::value
587 && IsSomeString<Tgt>::value>::type
588 toAppend(Src value, Tgt * result) {
590 value, result, double_conversion::DoubleToStringConverter::SHORTEST, 0);
594 * Very primitive, lets say its our best effort
597 typename std::enable_if<
598 std::is_floating_point<Src>::value, size_t>::type
599 estimateSpaceNeeded(Src value) {
607 return sofar + 10; // lets assume 0 + '.' + 8 precision digits
610 if (value < static_cast<double>(std::numeric_limits<uint64_t>::max())) {
611 sofar += digits10(static_cast<uint64_t>(value));
613 return 64; // give up, it will be more than 23 anyway
616 return sofar + 10; // integral part + '.' + 8 precision digits
620 * This can be specialized, together with adding specialization
621 * for estimateSpaceNeed for your type, so that we allocate
622 * as much as you need instead of the default
625 struct HasLengthEstimator : std::false_type {};
628 constexpr typename std::enable_if<
629 !std::is_fundamental<Src>::value
630 && !IsSomeString<Src>::value
631 && !std::is_convertible<Src, const char*>::value
632 && !std::is_convertible<Src, StringPiece>::value
633 && !std::is_enum<Src>::value
634 && !HasLengthEstimator<Src>::value,
636 estimateSpaceNeeded(const Src&) {
637 return sizeof(Src) + 1; // dumbest best effort ever?
642 inline size_t estimateSpaceToReserve(size_t sofar) {
646 template <class T, class... Ts>
647 size_t estimateSpaceToReserve(size_t sofar, const T& v, const Ts&... vs) {
648 return estimateSpaceToReserve(sofar + estimateSpaceNeeded(v), vs...);
652 size_t estimateSpaceToReserve(size_t sofar, const T& v) {
653 return sofar + estimateSpaceNeeded(v);
657 void reserveInTarget(const Ts&...vs) {
658 getLastElement(vs...)->reserve(estimateSpaceToReserve(0, vs...));
661 template<class Delimiter, class...Ts>
662 void reserveInTargetDelim(const Delimiter& d, const Ts&...vs) {
663 static_assert(sizeof...(vs) >= 2, "Needs at least 2 args");
664 size_t fordelim = (sizeof...(vs) - 2) * estimateSpaceToReserve(0, d);
665 getLastElement(vs...)->reserve(estimateSpaceToReserve(fordelim, vs...));
669 * Variadic base case: append one element
671 template <class T, class Tgt>
672 typename std::enable_if<
673 IsSomeString<typename std::remove_pointer<Tgt>::type>
675 toAppendStrImpl(const T& v, Tgt result) {
679 template <class T, class... Ts>
680 typename std::enable_if<sizeof...(Ts) >= 2
682 typename std::remove_pointer<
683 typename detail::last_element<Ts...>::type
684 >::type>::value>::type
685 toAppendStrImpl(const T& v, const Ts&... vs) {
686 toAppend(v, getLastElement(vs...));
687 toAppendStrImpl(vs...);
690 template <class Delimiter, class T, class Tgt>
691 typename std::enable_if<
692 IsSomeString<typename std::remove_pointer<Tgt>::type>
694 toAppendDelimStrImpl(const Delimiter& delim, const T& v, Tgt result) {
698 template <class Delimiter, class T, class... Ts>
699 typename std::enable_if<sizeof...(Ts) >= 2
701 typename std::remove_pointer<
702 typename detail::last_element<Ts...>::type
703 >::type>::value>::type
704 toAppendDelimStrImpl(const Delimiter& delim, const T& v, const Ts&... vs) {
705 // we are really careful here, calling toAppend with just one element does
706 // not try to estimate space needed (as we already did that). If we call
707 // toAppend(v, delim, ....) we would do unnecesary size calculation
708 toAppend(v, detail::getLastElement(vs...));
709 toAppend(delim, detail::getLastElement(vs...));
710 toAppendDelimStrImpl(delim, vs...);
716 * Variadic conversion to string. Appends each element in turn.
717 * If we have two or more things to append, we will reserve
718 * the space for them (at least we will try).
720 template <class... Ts>
721 typename std::enable_if<sizeof...(Ts) >= 3
723 typename std::remove_pointer<
724 typename detail::last_element<Ts...>::type
725 >::type>::value>::type
726 toAppend(const Ts&... vs) {
727 detail::reserveInTarget(vs...);
728 detail::toAppendStrImpl(vs...);
732 * Variadic base case: do nothing.
735 typename std::enable_if<IsSomeString<Tgt>::value>::type
736 toAppend(Tgt* result) {
740 * Variadic base case: do nothing.
742 template <class Delimiter, class Tgt>
743 typename std::enable_if<IsSomeString<Tgt>::value>::type
744 toAppendDelim(const Delimiter& delim, Tgt* result) {
748 * 1 element: same as toAppend.
750 template <class Delimiter, class T, class Tgt>
751 typename std::enable_if<IsSomeString<Tgt>::value>::type
752 toAppendDelim(const Delimiter& delim, const T& v, Tgt* tgt) {
757 * Append to string with a delimiter in between elements.
759 template <class Delimiter, class... Ts>
760 typename std::enable_if<sizeof...(Ts) >= 3
762 typename std::remove_pointer<
763 typename detail::last_element<Ts...>::type
764 >::type>::value>::type
765 toAppendDelim(const Delimiter& delim, const Ts&... vs) {
766 detail::reserveInTargetDelim(delim, vs...);
767 detail::toAppendDelimStrImpl(delim, vs...);
771 * to<SomeString>(SomeString str) returns itself. As both std::string and
772 * folly::fbstring use Copy-on-Write, it's much more efficient by
773 * avoiding copying the underlying char array.
775 template <class Tgt, class Src>
776 typename std::enable_if<
777 IsSomeString<Tgt>::value && std::is_same<Tgt, Src>::value,
779 to(const Src & value) {
784 * to<SomeString>(v1, v2, ...) uses toAppend() (see below) as back-end
787 template <class Tgt, class... Ts>
788 typename std::enable_if<
789 IsSomeString<Tgt>::value && (
790 sizeof...(Ts) != 1 ||
791 !std::is_same<Tgt, typename detail::last_element<Ts...>::type>::value),
793 to(const Ts&... vs) {
795 toAppend(vs..., &result);
800 * toDelim<SomeString>(SomeString str) returns itself.
802 template <class Tgt, class Delim, class Src>
803 typename std::enable_if<
804 IsSomeString<Tgt>::value && std::is_same<Tgt, Src>::value,
806 toDelim(const Delim& delim, const Src & value) {
811 * toDelim<SomeString>(delim, v1, v2, ...) uses toAppendDelim() as
812 * back-end for all types.
814 template <class Tgt, class Delim, class... Ts>
815 typename std::enable_if<
816 IsSomeString<Tgt>::value && (
817 sizeof...(Ts) != 1 ||
818 !std::is_same<Tgt, typename detail::last_element<Ts...>::type>::value),
820 toDelim(const Delim& delim, const Ts&... vs) {
822 toAppendDelim(delim, vs..., &result);
826 /*******************************************************************************
827 * Conversions from string types to integral types.
828 ******************************************************************************/
833 * Finds the first non-digit in a string. The number of digits
834 * searched depends on the precision of the Tgt integral. Assumes the
835 * string starts with NO whitespace and NO sign.
837 * The semantics of the routine is:
839 * if (b >= e || !isdigit(*b)) return b;
842 * Complete unrolling marks bottom-line (i.e. entire conversion)
843 * improvements of 20%.
846 const char* findFirstNonDigit(const char* b, const char* e) {
848 auto const c = static_cast<unsigned>(*b) - '0';
854 // Maximum value of number when represented as a string
855 template <class T> struct MaxString {
856 static const char*const value;
861 * Lookup tables that converts from a decimal character value to an integral
862 * binary value, shifted by a decimal "shift" multiplier.
863 * For all character values in the range '0'..'9', the table at those
864 * index locations returns the actual decimal value shifted by the multiplier.
865 * For all other values, the lookup table returns an invalid OOR value.
867 // Out-of-range flag value, larger than the largest value that can fit in
868 // four decimal bytes (9999), but four of these added up together should
869 // still not overflow uint16_t.
870 constexpr int32_t OOR = 10000;
872 __attribute__((__aligned__(16))) constexpr uint16_t shift1[] = {
873 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
874 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
875 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
876 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
877 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
878 1, 2, 3, 4, 5, 6, 7, 8, 9, OOR, OOR,
879 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
880 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
881 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
882 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
883 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
884 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
885 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
886 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
887 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
888 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
889 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
890 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
891 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
892 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
893 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
894 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
895 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
896 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
897 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
898 OOR, OOR, OOR, OOR, OOR, OOR // 250
901 __attribute__((__aligned__(16))) constexpr uint16_t shift10[] = {
902 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
903 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
904 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
905 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
906 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
907 10, 20, 30, 40, 50, 60, 70, 80, 90, OOR, OOR,
908 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
909 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
910 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
911 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
912 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
913 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
914 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
915 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
916 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
917 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
918 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
919 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
920 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
921 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
922 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
923 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
924 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
925 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
926 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
927 OOR, OOR, OOR, OOR, OOR, OOR // 250
930 __attribute__((__aligned__(16))) constexpr uint16_t shift100[] = {
931 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
932 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
933 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
934 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
935 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
936 100, 200, 300, 400, 500, 600, 700, 800, 900, OOR, OOR,
937 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
938 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
939 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
940 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
941 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
942 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
943 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
944 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
945 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
946 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
947 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
948 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
949 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
950 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
951 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
952 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
953 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
954 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
955 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
956 OOR, OOR, OOR, OOR, OOR, OOR // 250
959 __attribute__((__aligned__(16))) constexpr uint16_t shift1000[] = {
960 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
961 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
962 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
963 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
964 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
965 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, OOR, OOR,
966 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
967 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
968 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
969 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
970 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
971 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
972 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
973 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
974 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
975 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
976 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
977 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
978 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
979 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
980 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
981 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
982 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
983 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
984 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
985 OOR, OOR, OOR, OOR, OOR, OOR // 250
989 * String represented as a pair of pointers to char to unsigned
990 * integrals. Assumes NO whitespace before or after, and also that the
991 * string is composed entirely of digits. Tgt must be unsigned, and no
992 * sign is allowed in the string (even it's '+'). String may be empty,
993 * in which case digits_to throws.
996 Tgt digits_to(const char * b, const char * e) {
998 static_assert(!std::is_signed<Tgt>::value, "Unsigned type expected");
1001 const size_t size = e - b;
1003 /* Although the string is entirely made of digits, we still need to
1004 * check for overflow.
1006 if (size >= std::numeric_limits<Tgt>::digits10 + 1) {
1007 // Leading zeros? If so, recurse to keep things simple
1008 if (b < e && *b == '0') {
1010 if (b == e) return 0; // just zeros, e.g. "0000"
1011 if (*b != '0') return digits_to<Tgt>(b, e);
1014 FOLLY_RANGE_CHECK(size == std::numeric_limits<Tgt>::digits10 + 1 &&
1015 strncmp(b, detail::MaxString<Tgt>::value, size) <= 0,
1016 "Numeric overflow upon conversion");
1019 // Here we know that the number won't overflow when
1020 // converted. Proceed without checks.
1024 for (; e - b >= 4; b += 4) {
1026 const int32_t r0 = shift1000[static_cast<size_t>(b[0])];
1027 const int32_t r1 = shift100[static_cast<size_t>(b[1])];
1028 const int32_t r2 = shift10[static_cast<size_t>(b[2])];
1029 const int32_t r3 = shift1[static_cast<size_t>(b[3])];
1030 const auto sum = r0 + r1 + r2 + r3;
1031 assert(sum < OOR && "Assumption: string only has digits");
1037 const int32_t r0 = shift100[static_cast<size_t>(b[0])];
1038 const int32_t r1 = shift10[static_cast<size_t>(b[1])];
1039 const int32_t r2 = shift1[static_cast<size_t>(b[2])];
1040 const auto sum = r0 + r1 + r2;
1041 assert(sum < OOR && "Assumption: string only has digits");
1042 return result * 1000 + sum;
1045 const int32_t r0 = shift10[static_cast<size_t>(b[0])];
1046 const int32_t r1 = shift1[static_cast<size_t>(b[1])];
1047 const auto sum = r0 + r1;
1048 assert(sum < OOR && "Assumption: string only has digits");
1049 return result * 100 + sum;
1052 const int32_t sum = shift1[static_cast<size_t>(b[0])];
1053 assert(sum < OOR && "Assumption: string only has digits");
1054 return result * 10 + sum;
1059 FOLLY_RANGE_CHECK(size > 0, "Found no digits to convert in input");
1064 bool str_to_bool(StringPiece * src);
1066 } // namespace detail
1069 * String represented as a pair of pointers to char to unsigned
1070 * integrals. Assumes NO whitespace before or after.
1072 template <class Tgt>
1073 typename std::enable_if<
1074 std::is_integral<Tgt>::value && !std::is_signed<Tgt>::value
1075 && !std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
1077 to(const char * b, const char * e) {
1078 return detail::digits_to<Tgt>(b, e);
1082 * String represented as a pair of pointers to char to signed
1083 * integrals. Assumes NO whitespace before or after. Allows an
1084 * optional leading sign.
1086 template <class Tgt>
1087 typename std::enable_if<
1088 std::is_integral<Tgt>::value && std::is_signed<Tgt>::value,
1090 to(const char * b, const char * e) {
1091 FOLLY_RANGE_CHECK(b < e, "Empty input string in conversion to integral");
1094 Tgt result = -to<typename std::make_unsigned<Tgt>::type>(b + 1, e);
1095 FOLLY_RANGE_CHECK(result <= 0, "Negative overflow.");
1098 FOLLY_RANGE_CHECK(*b == '+', "Invalid lead character");
1101 Tgt result = to<typename std::make_unsigned<Tgt>::type>(b, e);
1102 FOLLY_RANGE_CHECK(result >= 0, "Overflow.");
1107 * Parsing strings to integrals. These routines differ from
1108 * to<integral>(string) in that they take a POINTER TO a StringPiece
1109 * and alter that StringPiece to reflect progress information.
1113 * StringPiece to integrals, with progress information. Alters the
1114 * StringPiece parameter to munch the already-parsed characters.
1116 template <class Tgt>
1117 typename std::enable_if<
1118 std::is_integral<Tgt>::value
1119 && !std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
1121 to(StringPiece * src) {
1123 auto b = src->data(), past = src->data() + src->size();
1125 FOLLY_RANGE_CHECK(b < past, "No digits found in input string");
1126 if (!isspace(*b)) break;
1131 // First digit is customized because we test for sign
1132 bool negative = false;
1133 /* static */ if (std::is_signed<Tgt>::value) {
1138 FOLLY_RANGE_CHECK(*m == '+', "Invalid leading character in conversion"
1145 FOLLY_RANGE_CHECK(m < past, "No digits found in input string");
1146 FOLLY_RANGE_CHECK(isdigit(*m), "Non-digit character found");
1147 m = detail::findFirstNonDigit<Tgt>(m + 1, past);
1150 /* static */ if (!std::is_signed<Tgt>::value) {
1151 result = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
1153 auto t = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
1156 FOLLY_RANGE_CHECK(is_non_positive(result), "Negative overflow");
1159 FOLLY_RANGE_CHECK(is_non_negative(result), "Overflow");
1162 src->advance(m - src->data());
1167 * StringPiece to bool, with progress information. Alters the
1168 * StringPiece parameter to munch the already-parsed characters.
1170 template <class Tgt>
1171 typename std::enable_if<
1172 std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
1174 to(StringPiece * src) {
1175 return detail::str_to_bool(src);
1181 * Enforce that the suffix following a number is made up only of whitespace.
1183 inline void enforceWhitespace(const char* b, const char* e) {
1184 for (; b != e; ++b) {
1185 FOLLY_RANGE_CHECK(isspace(*b), to<std::string>("Non-whitespace: ", *b));
1189 } // namespace detail
1192 * String or StringPiece to integrals. Accepts leading and trailing
1193 * whitespace, but no non-space trailing characters.
1195 template <class Tgt>
1196 typename std::enable_if<
1197 std::is_integral<Tgt>::value,
1199 to(StringPiece src) {
1200 Tgt result = to<Tgt>(&src);
1201 detail::enforceWhitespace(src.data(), src.data() + src.size());
1205 /*******************************************************************************
1206 * Conversions from string types to floating-point types.
1207 ******************************************************************************/
1210 * StringPiece to double, with progress information. Alters the
1211 * StringPiece parameter to munch the already-parsed characters.
1213 template <class Tgt>
1214 inline typename std::enable_if<
1215 std::is_floating_point<Tgt>::value,
1217 to(StringPiece *const src) {
1218 using namespace double_conversion;
1219 static StringToDoubleConverter
1220 conv(StringToDoubleConverter::ALLOW_TRAILING_JUNK
1221 | StringToDoubleConverter::ALLOW_LEADING_SPACES,
1223 // return this for junk input string
1224 std::numeric_limits<double>::quiet_NaN(),
1227 FOLLY_RANGE_CHECK(!src->empty(), "No digits found in input string");
1230 auto result = conv.StringToDouble(src->data(), src->size(),
1231 &length); // processed char count
1233 if (!std::isnan(result)) {
1234 src->advance(length);
1238 for (;; src->advance(1)) {
1240 throw std::range_error("Unable to convert an empty string"
1241 " to a floating point value.");
1243 if (!isspace(src->front())) {
1248 // Was that "inf[inity]"?
1249 if (src->size() >= 3 && toupper((*src)[0]) == 'I'
1250 && toupper((*src)[1]) == 'N' && toupper((*src)[2]) == 'F') {
1251 if (src->size() >= 8 &&
1252 toupper((*src)[3]) == 'I' &&
1253 toupper((*src)[4]) == 'N' &&
1254 toupper((*src)[5]) == 'I' &&
1255 toupper((*src)[6]) == 'T' &&
1256 toupper((*src)[7]) == 'Y') {
1261 return std::numeric_limits<Tgt>::infinity();
1264 // Was that "-inf[inity]"?
1265 if (src->size() >= 4 && toupper((*src)[0]) == '-'
1266 && toupper((*src)[1]) == 'I' && toupper((*src)[2]) == 'N'
1267 && toupper((*src)[3]) == 'F') {
1268 if (src->size() >= 9 &&
1269 toupper((*src)[4]) == 'I' &&
1270 toupper((*src)[5]) == 'N' &&
1271 toupper((*src)[6]) == 'I' &&
1272 toupper((*src)[7]) == 'T' &&
1273 toupper((*src)[8]) == 'Y') {
1278 return -std::numeric_limits<Tgt>::infinity();
1282 if (src->size() >= 3 && toupper((*src)[0]) == 'N'
1283 && toupper((*src)[1]) == 'A' && toupper((*src)[2]) == 'N') {
1285 return std::numeric_limits<Tgt>::quiet_NaN();
1289 if (src->size() >= 4 &&
1290 toupper((*src)[0]) == '-' &&
1291 toupper((*src)[1]) == 'N' &&
1292 toupper((*src)[2]) == 'A' &&
1293 toupper((*src)[3]) == 'N') {
1295 return -std::numeric_limits<Tgt>::quiet_NaN();
1299 throw std::range_error("Unable to convert \"" + src->toString()
1300 + "\" to a floating point value.");
1304 * Any string, const char*, or StringPiece to double.
1306 template <class Tgt>
1307 typename std::enable_if<
1308 std::is_floating_point<Tgt>::value,
1310 to(StringPiece src) {
1311 Tgt result = to<double>(&src);
1312 detail::enforceWhitespace(src.data(), src.data() + src.size());
1316 /*******************************************************************************
1317 * Integral to floating point and back
1318 ******************************************************************************/
1321 * Checked conversion from integral to flating point and back. The
1322 * result must be convertible back to the source type without loss of
1323 * precision. This seems Draconian but sometimes is what's needed, and
1324 * complements existing routines nicely. For various rounding
1325 * routines, see <math>.
1327 template <class Tgt, class Src>
1328 typename std::enable_if<
1329 (std::is_integral<Src>::value && std::is_floating_point<Tgt>::value)
1331 (std::is_floating_point<Src>::value && std::is_integral<Tgt>::value),
1333 to(const Src & value) {
1335 auto witness = static_cast<Src>(result);
1336 if (value != witness) {
1337 throw std::range_error(
1338 to<std::string>("to<>: loss of precision when converting ", value,
1339 " to type ", typeid(Tgt).name()).c_str());
1344 /*******************************************************************************
1345 * Enum to anything and back
1346 ******************************************************************************/
1348 #if defined(__clang__) || __GNUC_PREREQ(4, 7)
1349 // std::underlying_type became available by gcc 4.7.0
1351 template <class Tgt, class Src>
1352 typename std::enable_if<std::is_enum<Src>::value, Tgt>::type
1353 to(const Src & value) {
1354 return to<Tgt>(static_cast<typename std::underlying_type<Src>::type>(value));
1357 template <class Tgt, class Src>
1358 typename std::enable_if<std::is_enum<Tgt>::value, Tgt>::type
1359 to(const Src & value) {
1360 return static_cast<Tgt>(to<typename std::underlying_type<Tgt>::type>(value));
1365 template <class Tgt, class Src>
1366 typename std::enable_if<std::is_enum<Src>::value, Tgt>::type
1367 to(const Src & value) {
1368 /* static */ if (Src(-1) < 0) {
1369 /* static */ if (sizeof(Src) <= sizeof(int)) {
1370 return to<Tgt>(static_cast<int>(value));
1372 return to<Tgt>(static_cast<long>(value));
1375 /* static */ if (sizeof(Src) <= sizeof(int)) {
1376 return to<Tgt>(static_cast<unsigned int>(value));
1378 return to<Tgt>(static_cast<unsigned long>(value));
1383 template <class Tgt, class Src>
1384 typename std::enable_if<std::is_enum<Tgt>::value, Tgt>::type
1385 to(const Src & value) {
1386 /* static */ if (Tgt(-1) < 0) {
1387 /* static */ if (sizeof(Tgt) <= sizeof(int)) {
1388 return static_cast<Tgt>(to<int>(value));
1390 return static_cast<Tgt>(to<long>(value));
1393 /* static */ if (sizeof(Tgt) <= sizeof(int)) {
1394 return static_cast<Tgt>(to<unsigned int>(value));
1396 return static_cast<Tgt>(to<unsigned long>(value));
1401 #endif // gcc 4.7 onwards
1403 } // namespace folly
1405 // FOLLY_CONV_INTERNAL is defined by Conv.cpp. Keep the FOLLY_RANGE_CHECK
1406 // macro for use in Conv.cpp, but #undefine it everywhere else we are included,
1407 // to avoid defining this global macro name in other files that include Conv.h.
1408 #ifndef FOLLY_CONV_INTERNAL
1409 #undef FOLLY_RANGE_CHECK
1412 #endif /* FOLLY_BASE_CONV_H_ */