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_STRINGIZE(x) #x
46 #define FOLLY_RANGE_CHECK_STRINGIZE2(x) FOLLY_RANGE_CHECK_STRINGIZE(x)
48 #define FOLLY_RANGE_CHECK(condition, message) \
49 ((condition) ? (void)0 : throw std::range_error( \
50 (std::string(__FILE__ "(" FOLLY_RANGE_CHECK_STRINGIZE2(__LINE__) "): ") \
51 + (message)).c_str()))
55 /*******************************************************************************
56 * Integral to integral
57 ******************************************************************************/
60 * Checked conversion from integral to integral. The checks are only
61 * performed when meaningful, e.g. conversion from int to long goes
64 template <class Tgt, class Src>
65 typename std::enable_if<
66 std::is_integral<Src>::value && std::is_integral<Tgt>::value,
68 to(const Src & value) {
69 /* static */ if (std::numeric_limits<Tgt>::max()
70 < std::numeric_limits<Src>::max()) {
72 (!greater_than<Tgt, std::numeric_limits<Tgt>::max()>(value)),
76 /* static */ if (std::is_signed<Src>::value &&
77 (!std::is_signed<Tgt>::value || sizeof(Src) > sizeof(Tgt))) {
79 (!less_than<Tgt, std::numeric_limits<Tgt>::min()>(value)),
83 return static_cast<Tgt>(value);
86 /*******************************************************************************
87 * Floating point to floating point
88 ******************************************************************************/
90 template <class Tgt, class Src>
91 typename std::enable_if<
92 std::is_floating_point<Tgt>::value && std::is_floating_point<Src>::value,
94 to(const Src & value) {
95 /* static */ if (std::numeric_limits<Tgt>::max() <
96 std::numeric_limits<Src>::max()) {
97 FOLLY_RANGE_CHECK(value <= std::numeric_limits<Tgt>::max(),
99 FOLLY_RANGE_CHECK(value >= -std::numeric_limits<Tgt>::max(),
100 "Negative overflow");
102 return boost::implicit_cast<Tgt>(value);
105 /*******************************************************************************
107 ******************************************************************************/
112 const T& getLastElement(const T & v) {
116 template <class T, class... Ts>
117 typename std::tuple_element<
119 std::tuple<T, Ts...> >::type const&
120 getLastElement(const T& v, const Ts&... vs) {
121 return getLastElement(vs...);
124 // This class exists to specialize away std::tuple_element in the case where we
125 // have 0 template arguments. Without this, Clang/libc++ will blow a
126 // static_assert even if tuple_element is protected by an enable_if.
127 template <class... Ts>
128 struct last_element {
129 typedef typename std::enable_if<
131 typename std::tuple_element<
132 sizeof...(Ts) - 1, std::tuple<Ts...>
137 struct last_element<> {
141 } // namespace detail
143 /*******************************************************************************
144 * Conversions from integral types to string types.
145 ******************************************************************************/
147 #if FOLLY_HAVE_INT128_T
150 template <typename IntegerType>
151 constexpr unsigned int
153 return ceil((double(sizeof(IntegerType) * CHAR_BIT) * M_LN2) / M_LN10);
157 unsafeTelescope128(char * buffer, size_t room, unsigned __int128 x) {
158 typedef unsigned __int128 Usrc;
161 while (x >= (Usrc(1) << 64)) { // Using 128-bit division while needed
162 const auto y = x / 10;
163 const auto digit = x % 10;
165 buffer[p--] = '0' + digit;
169 uint64_t xx = x; // Moving to faster 64-bit division thereafter
172 const auto y = xx / 10ULL;
173 const auto digit = xx % 10ULL;
175 buffer[p--] = '0' + digit;
179 buffer[p] = '0' + xx;
188 * Returns the number of digits in the base 10 representation of an
189 * uint64_t. Useful for preallocating buffers and such. It's also used
190 * internally, see below. Measurements suggest that defining a
191 * separate overload for 32-bit integers is not worthwhile.
194 inline uint32_t digits10(uint64_t v) {
197 if (LIKELY(v < 10)) return result;
198 if (LIKELY(v < 100)) return result + 1;
199 if (LIKELY(v < 1000)) return result + 2;
200 if (LIKELY(v < 10000)) return result + 3;
201 // Skip ahead by 4 orders of magnitude
208 * Copies the ASCII base 10 representation of v into buffer and
209 * returns the number of bytes written. Does NOT append a \0. Assumes
210 * the buffer points to digits10(v) bytes of valid memory. Note that
211 * uint64 needs at most 20 bytes, uint32_t needs at most 10 bytes,
212 * uint16_t needs at most 5 bytes, and so on. Measurements suggest
213 * that defining a separate overload for 32-bit integers is not
216 * This primitive is unsafe because it makes the size assumption and
217 * because it does not add a terminating \0.
220 inline uint32_t uint64ToBufferUnsafe(uint64_t v, char *const buffer) {
221 auto const result = digits10(v);
222 // WARNING: using size_t or pointer arithmetic for pos slows down
223 // the loop below 20x. This is because several 32-bit ops can be
224 // done in parallel, but only fewer 64-bit ones.
225 uint32_t pos = result - 1;
227 // Keep these together so a peephole optimization "sees" them and
228 // computes them in one shot.
229 auto const q = v / 10;
230 auto const r = static_cast<uint32_t>(v % 10);
231 buffer[pos--] = '0' + r;
234 // Last digit is trivial to handle
235 buffer[pos] = static_cast<uint32_t>(v) + '0';
240 * A single char gets appended.
243 void toAppend(char value, Tgt * result) {
248 constexpr typename std::enable_if<
249 std::is_same<T, char>::value,
251 estimateSpaceNeeded(T) {
256 * Ubiquitous helper template for writing string appenders
258 template <class T> struct IsSomeString {
259 enum { value = std::is_same<T, std::string>::value
260 || std::is_same<T, fbstring>::value };
264 * Everything implicitly convertible to const char* gets appended.
266 template <class Tgt, class Src>
267 typename std::enable_if<
268 std::is_convertible<Src, const char*>::value
269 && IsSomeString<Tgt>::value>::type
270 toAppend(Src value, Tgt * result) {
271 // Treat null pointers like an empty string, as in:
272 // operator<<(std::ostream&, const char*).
273 const char* c = value;
275 result->append(value);
280 typename std::enable_if<
281 std::is_convertible<Src, const char*>::value,
283 estimateSpaceNeeded(Src value) {
284 const char *c = value;
286 return folly::StringPiece(value).size();
292 typename std::enable_if<
293 (std::is_convertible<Src, folly::StringPiece>::value ||
294 IsSomeString<Src>::value) &&
295 !std::is_convertible<Src, const char*>::value,
297 estimateSpaceNeeded(Src value) {
298 return folly::StringPiece(value).size();
302 typename std::enable_if<
303 std::is_pointer<Src>::value &&
304 IsSomeString<std::remove_pointer<Src>>::value,
306 estimateSpaceNeeded(Src value) {
307 return value->size();
311 * Strings get appended, too.
313 template <class Tgt, class Src>
314 typename std::enable_if<
315 IsSomeString<Src>::value && IsSomeString<Tgt>::value>::type
316 toAppend(const Src& value, Tgt * result) {
317 result->append(value);
321 * and StringPiece objects too
324 typename std::enable_if<
325 IsSomeString<Tgt>::value>::type
326 toAppend(StringPiece value, Tgt * result) {
327 result->append(value.data(), value.size());
331 * There's no implicit conversion from fbstring to other string types,
332 * so make a specialization.
335 typename std::enable_if<
336 IsSomeString<Tgt>::value>::type
337 toAppend(const fbstring& value, Tgt * result) {
338 result->append(value.data(), value.size());
341 #if FOLLY_HAVE_INT128_T
343 * Special handling for 128 bit integers.
348 toAppend(__int128 value, Tgt * result) {
349 typedef unsigned __int128 Usrc;
350 char buffer[detail::digitsEnough<unsigned __int128>() + 1];
354 p = detail::unsafeTelescope128(buffer, sizeof(buffer), Usrc(-value));
357 p = detail::unsafeTelescope128(buffer, sizeof(buffer), value);
360 result->append(buffer + p, buffer + sizeof(buffer));
365 toAppend(unsigned __int128 value, Tgt * result) {
366 char buffer[detail::digitsEnough<unsigned __int128>()];
369 p = detail::unsafeTelescope128(buffer, sizeof(buffer), value);
371 result->append(buffer + p, buffer + sizeof(buffer));
375 constexpr typename std::enable_if<
376 std::is_same<T, __int128>::value,
378 estimateSpaceNeeded(T) {
379 return detail::digitsEnough<__int128>();
383 constexpr typename std::enable_if<
384 std::is_same<T, unsigned __int128>::value,
386 estimateSpaceNeeded(T) {
387 return detail::digitsEnough<unsigned __int128>();
393 * int32_t and int64_t to string (by appending) go through here. The
394 * result is APPENDED to a preexisting string passed as the second
395 * parameter. This should be efficient with fbstring because fbstring
396 * incurs no dynamic allocation below 23 bytes and no number has more
397 * than 22 bytes in its textual representation (20 for digits, one for
398 * sign, one for the terminating 0).
400 template <class Tgt, class Src>
401 typename std::enable_if<
402 std::is_integral<Src>::value && std::is_signed<Src>::value &&
403 IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
404 toAppend(Src value, Tgt * result) {
407 result->push_back('-');
408 result->append(buffer, uint64ToBufferUnsafe(-uint64_t(value), buffer));
410 result->append(buffer, uint64ToBufferUnsafe(value, buffer));
415 typename std::enable_if<
416 std::is_integral<Src>::value && std::is_signed<Src>::value
417 && sizeof(Src) >= 4 && sizeof(Src) < 16,
419 estimateSpaceNeeded(Src value) {
421 return 1 + digits10(static_cast<uint64_t>(-value));
424 return digits10(static_cast<uint64_t>(value));
428 * As above, but for uint32_t and uint64_t.
430 template <class Tgt, class Src>
431 typename std::enable_if<
432 std::is_integral<Src>::value && !std::is_signed<Src>::value
433 && IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
434 toAppend(Src value, Tgt * result) {
436 result->append(buffer, buffer + uint64ToBufferUnsafe(value, buffer));
440 typename std::enable_if<
441 std::is_integral<Src>::value && !std::is_signed<Src>::value
442 && sizeof(Src) >= 4 && sizeof(Src) < 16,
444 estimateSpaceNeeded(Src value) {
445 return digits10(value);
449 * All small signed and unsigned integers to string go through 32-bit
450 * types int32_t and uint32_t, respectively.
452 template <class Tgt, class Src>
453 typename std::enable_if<
454 std::is_integral<Src>::value
455 && IsSomeString<Tgt>::value && sizeof(Src) < 4>::type
456 toAppend(Src value, Tgt * result) {
458 std::conditional<std::is_signed<Src>::value, int64_t, uint64_t>::type
460 toAppend<Tgt>(static_cast<Intermediate>(value), result);
464 typename std::enable_if<
465 std::is_integral<Src>::value
467 && !std::is_same<Src, char>::value,
469 estimateSpaceNeeded(Src value) {
471 std::conditional<std::is_signed<Src>::value, int64_t, uint64_t>::type
473 return estimateSpaceNeeded(static_cast<Intermediate>(value));
476 #if defined(__clang__) || __GNUC_PREREQ(4, 7)
477 // std::underlying_type became available by gcc 4.7.0
480 * Enumerated values get appended as integers.
482 template <class Tgt, class Src>
483 typename std::enable_if<
484 std::is_enum<Src>::value && IsSomeString<Tgt>::value>::type
485 toAppend(Src value, Tgt * result) {
487 static_cast<typename std::underlying_type<Src>::type>(value), result);
491 typename std::enable_if<
492 std::is_enum<Src>::value, size_t>::type
493 estimateSpaceNeeded(Src value) {
494 return estimateSpaceNeeded(
495 static_cast<typename std::underlying_type<Src>::type>(value));
501 * Enumerated values get appended as integers.
503 template <class Tgt, class Src>
504 typename std::enable_if<
505 std::is_enum<Src>::value && IsSomeString<Tgt>::value>::type
506 toAppend(Src value, Tgt * result) {
507 /* static */ if (Src(-1) < 0) {
508 /* static */ if (sizeof(Src) <= sizeof(int)) {
509 toAppend(static_cast<int>(value), result);
511 toAppend(static_cast<long>(value), result);
514 /* static */ if (sizeof(Src) <= sizeof(int)) {
515 toAppend(static_cast<unsigned int>(value), result);
517 toAppend(static_cast<unsigned long>(value), result);
523 typename std::enable_if<
524 std::is_enum<Src>::value, size_t>::type
525 estimateSpaceNeeded(Src value) {
526 /* static */ if (Src(-1) < 0) {
527 /* static */ if (sizeof(Src) <= sizeof(int)) {
528 return estimateSpaceNeeded(static_cast<int>(value));
530 return estimateSpaceNeeded(static_cast<long>(value));
533 /* static */ if (sizeof(Src) <= sizeof(int)) {
534 return estimateSpaceNeeded(static_cast<unsigned int>(value));
536 return estimateSpaceNeeded(static_cast<unsigned long>(value));
541 #endif // gcc 4.7 onwards
543 /*******************************************************************************
544 * Conversions from floating-point types to string types.
545 ******************************************************************************/
547 /** Wrapper around DoubleToStringConverter **/
548 template <class Tgt, class Src>
549 typename std::enable_if<
550 std::is_floating_point<Src>::value
551 && IsSomeString<Tgt>::value>::type
555 double_conversion::DoubleToStringConverter::DtoaMode mode,
556 unsigned int numDigits) {
557 using namespace double_conversion;
558 DoubleToStringConverter
559 conv(DoubleToStringConverter::NO_FLAGS,
560 "infinity", "NaN", 'E',
561 -6, // decimal in shortest low
562 21, // decimal in shortest high
563 6, // max leading padding zeros
564 1); // max trailing padding zeros
566 StringBuilder builder(buffer, sizeof(buffer));
568 case DoubleToStringConverter::SHORTEST:
569 conv.ToShortest(value, &builder);
571 case DoubleToStringConverter::FIXED:
572 conv.ToFixed(value, numDigits, &builder);
575 CHECK(mode == DoubleToStringConverter::PRECISION);
576 conv.ToPrecision(value, numDigits, &builder);
579 const size_t length = builder.position();
581 result->append(buffer, length);
585 * As above, but for floating point
587 template <class Tgt, class Src>
588 typename std::enable_if<
589 std::is_floating_point<Src>::value
590 && IsSomeString<Tgt>::value>::type
591 toAppend(Src value, Tgt * result) {
593 value, result, double_conversion::DoubleToStringConverter::SHORTEST, 0);
597 * Very primitive, lets say its our best effort
600 typename std::enable_if<
601 std::is_floating_point<Src>::value, size_t>::type
602 estimateSpaceNeeded(Src value) {
610 return sofar + 10; // lets assume 0 + '.' + 8 precision digits
613 if (value < static_cast<double>(std::numeric_limits<uint64_t>::max())) {
614 sofar += digits10(static_cast<uint64_t>(value));
616 return 64; // give up, it will be more than 23 anyway
619 return sofar + 10; // integral part + '.' + 8 precision digits
623 * This can be specialized, together with adding specialization
624 * for estimateSpaceNeed for your type, so that we allocate
625 * as much as you need instead of the default
628 struct HasLengthEstimator : std::false_type {};
631 constexpr typename std::enable_if<
632 !std::is_fundamental<Src>::value
633 && !IsSomeString<Src>::value
634 && !std::is_convertible<Src, const char*>::value
635 && !std::is_convertible<Src, StringPiece>::value
636 && !std::is_enum<Src>::value
637 && !HasLengthEstimator<Src>::value,
639 estimateSpaceNeeded(const Src&) {
640 return sizeof(Src) + 1; // dumbest best effort ever?
645 inline size_t estimateSpaceToReserve(size_t sofar) {
649 template <class T, class... Ts>
650 size_t estimateSpaceToReserve(size_t sofar, const T& v, const Ts&... vs) {
651 return estimateSpaceToReserve(sofar + estimateSpaceNeeded(v), vs...);
655 size_t estimateSpaceToReserve(size_t sofar, const T& v) {
656 return sofar + estimateSpaceNeeded(v);
660 void reserveInTarget(const Ts&...vs) {
661 getLastElement(vs...)->reserve(estimateSpaceToReserve(0, vs...));
664 template<class Delimiter, class...Ts>
665 void reserveInTargetDelim(const Delimiter& d, const Ts&...vs) {
666 static_assert(sizeof...(vs) >= 2, "Needs at least 2 args");
667 size_t fordelim = (sizeof...(vs) - 2) * estimateSpaceToReserve(0, d);
668 getLastElement(vs...)->reserve(estimateSpaceToReserve(fordelim, vs...));
672 * Variadic base case: append one element
674 template <class T, class Tgt>
675 typename std::enable_if<
676 IsSomeString<typename std::remove_pointer<Tgt>::type>
678 toAppendStrImpl(const T& v, Tgt result) {
682 template <class T, class... Ts>
683 typename std::enable_if<sizeof...(Ts) >= 2
685 typename std::remove_pointer<
686 typename detail::last_element<Ts...>::type
687 >::type>::value>::type
688 toAppendStrImpl(const T& v, const Ts&... vs) {
689 toAppend(v, getLastElement(vs...));
690 toAppendStrImpl(vs...);
693 template <class Delimiter, class T, class Tgt>
694 typename std::enable_if<
695 IsSomeString<typename std::remove_pointer<Tgt>::type>
697 toAppendDelimStrImpl(const Delimiter& delim, const T& v, Tgt result) {
701 template <class Delimiter, class T, class... Ts>
702 typename std::enable_if<sizeof...(Ts) >= 2
704 typename std::remove_pointer<
705 typename detail::last_element<Ts...>::type
706 >::type>::value>::type
707 toAppendDelimStrImpl(const Delimiter& delim, const T& v, const Ts&... vs) {
708 // we are really careful here, calling toAppend with just one element does
709 // not try to estimate space needed (as we already did that). If we call
710 // toAppend(v, delim, ....) we would do unnecesary size calculation
711 toAppend(v, detail::getLastElement(vs...));
712 toAppend(delim, detail::getLastElement(vs...));
713 toAppendDelimStrImpl(delim, vs...);
719 * Variadic conversion to string. Appends each element in turn.
720 * If we have two or more things to append, we will reserve
721 * the space for them (at least we will try).
723 template <class... Ts>
724 typename std::enable_if<sizeof...(Ts) >= 3
726 typename std::remove_pointer<
727 typename detail::last_element<Ts...>::type
728 >::type>::value>::type
729 toAppend(const Ts&... vs) {
730 detail::reserveInTarget(vs...);
731 detail::toAppendStrImpl(vs...);
735 * Variadic base case: do nothing.
738 typename std::enable_if<IsSomeString<Tgt>::value>::type
739 toAppend(Tgt* result) {
743 * Variadic base case: do nothing.
745 template <class Delimiter, class Tgt>
746 typename std::enable_if<IsSomeString<Tgt>::value>::type
747 toAppendDelim(const Delimiter& delim, Tgt* result) {
751 * 1 element: same as toAppend.
753 template <class Delimiter, class T, class Tgt>
754 typename std::enable_if<IsSomeString<Tgt>::value>::type
755 toAppendDelim(const Delimiter& delim, const T& v, Tgt* tgt) {
760 * Append to string with a delimiter in between elements.
762 template <class Delimiter, class... Ts>
763 typename std::enable_if<sizeof...(Ts) >= 3
765 typename std::remove_pointer<
766 typename detail::last_element<Ts...>::type
767 >::type>::value>::type
768 toAppendDelim(const Delimiter& delim, const Ts&... vs) {
769 detail::reserveInTargetDelim(delim, vs...);
770 detail::toAppendDelimStrImpl(delim, vs...);
774 * to<SomeString>(SomeString str) returns itself. As both std::string and
775 * folly::fbstring use Copy-on-Write, it's much more efficient by
776 * avoiding copying the underlying char array.
778 template <class Tgt, class Src>
779 typename std::enable_if<
780 IsSomeString<Tgt>::value && std::is_same<Tgt, Src>::value,
782 to(const Src & value) {
787 * to<SomeString>(v1, v2, ...) uses toAppend() (see below) as back-end
790 template <class Tgt, class... Ts>
791 typename std::enable_if<
792 IsSomeString<Tgt>::value && (
793 sizeof...(Ts) != 1 ||
794 !std::is_same<Tgt, typename detail::last_element<Ts...>::type>::value),
796 to(const Ts&... vs) {
798 toAppend(vs..., &result);
803 * toDelim<SomeString>(SomeString str) returns itself.
805 template <class Tgt, class Delim, class Src>
806 typename std::enable_if<
807 IsSomeString<Tgt>::value && std::is_same<Tgt, Src>::value,
809 toDelim(const Delim& delim, const Src & value) {
814 * toDelim<SomeString>(delim, v1, v2, ...) uses toAppendDelim() as
815 * back-end for all types.
817 template <class Tgt, class Delim, class... Ts>
818 typename std::enable_if<
819 IsSomeString<Tgt>::value && (
820 sizeof...(Ts) != 1 ||
821 !std::is_same<Tgt, typename detail::last_element<Ts...>::type>::value),
823 toDelim(const Delim& delim, const Ts&... vs) {
825 toAppendDelim(delim, vs..., &result);
829 /*******************************************************************************
830 * Conversions from string types to integral types.
831 ******************************************************************************/
836 * Finds the first non-digit in a string. The number of digits
837 * searched depends on the precision of the Tgt integral. Assumes the
838 * string starts with NO whitespace and NO sign.
840 * The semantics of the routine is:
842 * if (b >= e || !isdigit(*b)) return b;
845 * Complete unrolling marks bottom-line (i.e. entire conversion)
846 * improvements of 20%.
849 const char* findFirstNonDigit(const char* b, const char* e) {
851 auto const c = static_cast<unsigned>(*b) - '0';
857 // Maximum value of number when represented as a string
858 template <class T> struct MaxString {
859 static const char*const value;
864 * Lookup tables that converts from a decimal character value to an integral
865 * binary value, shifted by a decimal "shift" multiplier.
866 * For all character values in the range '0'..'9', the table at those
867 * index locations returns the actual decimal value shifted by the multiplier.
868 * For all other values, the lookup table returns an invalid OOR value.
870 // Out-of-range flag value, larger than the largest value that can fit in
871 // four decimal bytes (9999), but four of these added up together should
872 // still not overflow uint16_t.
873 constexpr int32_t OOR = 10000;
875 __attribute__((__aligned__(16))) constexpr uint16_t shift1[] = {
876 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
877 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
878 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
879 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
880 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
881 1, 2, 3, 4, 5, 6, 7, 8, 9, OOR, OOR,
882 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
883 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
884 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
885 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
886 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
887 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
888 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
889 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
890 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
891 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
892 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
893 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
894 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
895 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
896 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
897 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
898 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
899 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
900 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
901 OOR, OOR, OOR, OOR, OOR, OOR // 250
904 __attribute__((__aligned__(16))) constexpr uint16_t shift10[] = {
905 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
906 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
907 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
908 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
909 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
910 10, 20, 30, 40, 50, 60, 70, 80, 90, OOR, OOR,
911 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
912 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
913 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
914 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
915 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
916 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
917 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
918 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
919 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
920 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
921 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
922 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
923 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
924 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
925 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
926 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
927 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
928 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
929 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
930 OOR, OOR, OOR, OOR, OOR, OOR // 250
933 __attribute__((__aligned__(16))) constexpr uint16_t shift100[] = {
934 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
935 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
936 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
937 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
938 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
939 100, 200, 300, 400, 500, 600, 700, 800, 900, OOR, OOR,
940 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
941 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
942 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
943 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
944 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
945 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
946 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
947 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
948 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
949 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
950 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
951 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
952 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
953 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
954 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
955 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
956 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
957 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
958 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
959 OOR, OOR, OOR, OOR, OOR, OOR // 250
962 __attribute__((__aligned__(16))) constexpr uint16_t shift1000[] = {
963 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
964 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
965 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
966 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
967 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
968 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, OOR, OOR,
969 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
970 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
971 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
972 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
973 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
974 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
975 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
976 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
977 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
978 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
979 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
980 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
981 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
982 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
983 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
984 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
985 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
986 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
987 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
988 OOR, OOR, OOR, OOR, OOR, OOR // 250
992 * String represented as a pair of pointers to char to unsigned
993 * integrals. Assumes NO whitespace before or after, and also that the
994 * string is composed entirely of digits. Tgt must be unsigned, and no
995 * sign is allowed in the string (even it's '+'). String may be empty,
996 * in which case digits_to throws.
999 Tgt digits_to(const char * b, const char * e) {
1001 static_assert(!std::is_signed<Tgt>::value, "Unsigned type expected");
1004 const size_t size = e - b;
1006 /* Although the string is entirely made of digits, we still need to
1007 * check for overflow.
1009 if (size >= std::numeric_limits<Tgt>::digits10 + 1) {
1010 // Leading zeros? If so, recurse to keep things simple
1011 if (b < e && *b == '0') {
1013 if (b == e) return 0; // just zeros, e.g. "0000"
1014 if (*b != '0') return digits_to<Tgt>(b, e);
1017 FOLLY_RANGE_CHECK(size == std::numeric_limits<Tgt>::digits10 + 1 &&
1018 strncmp(b, detail::MaxString<Tgt>::value, size) <= 0,
1019 "Numeric overflow upon conversion");
1022 // Here we know that the number won't overflow when
1023 // converted. Proceed without checks.
1027 for (; e - b >= 4; b += 4) {
1029 const int32_t r0 = shift1000[static_cast<size_t>(b[0])];
1030 const int32_t r1 = shift100[static_cast<size_t>(b[1])];
1031 const int32_t r2 = shift10[static_cast<size_t>(b[2])];
1032 const int32_t r3 = shift1[static_cast<size_t>(b[3])];
1033 const auto sum = r0 + r1 + r2 + r3;
1034 assert(sum < OOR && "Assumption: string only has digits");
1040 const int32_t r0 = shift100[static_cast<size_t>(b[0])];
1041 const int32_t r1 = shift10[static_cast<size_t>(b[1])];
1042 const int32_t r2 = shift1[static_cast<size_t>(b[2])];
1043 const auto sum = r0 + r1 + r2;
1044 assert(sum < OOR && "Assumption: string only has digits");
1045 return result * 1000 + sum;
1048 const int32_t r0 = shift10[static_cast<size_t>(b[0])];
1049 const int32_t r1 = shift1[static_cast<size_t>(b[1])];
1050 const auto sum = r0 + r1;
1051 assert(sum < OOR && "Assumption: string only has digits");
1052 return result * 100 + sum;
1055 const int32_t sum = shift1[static_cast<size_t>(b[0])];
1056 assert(sum < OOR && "Assumption: string only has digits");
1057 return result * 10 + sum;
1062 FOLLY_RANGE_CHECK(size > 0, "Found no digits to convert in input");
1067 bool str_to_bool(StringPiece * src);
1069 } // namespace detail
1072 * String represented as a pair of pointers to char to unsigned
1073 * integrals. Assumes NO whitespace before or after.
1075 template <class Tgt>
1076 typename std::enable_if<
1077 std::is_integral<Tgt>::value && !std::is_signed<Tgt>::value
1078 && !std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
1080 to(const char * b, const char * e) {
1081 return detail::digits_to<Tgt>(b, e);
1085 * String represented as a pair of pointers to char to signed
1086 * integrals. Assumes NO whitespace before or after. Allows an
1087 * optional leading sign.
1089 template <class Tgt>
1090 typename std::enable_if<
1091 std::is_integral<Tgt>::value && std::is_signed<Tgt>::value,
1093 to(const char * b, const char * e) {
1094 FOLLY_RANGE_CHECK(b < e, "Empty input string in conversion to integral");
1097 Tgt result = -to<typename std::make_unsigned<Tgt>::type>(b + 1, e);
1098 FOLLY_RANGE_CHECK(result <= 0, "Negative overflow.");
1101 FOLLY_RANGE_CHECK(*b == '+', "Invalid lead character");
1104 Tgt result = to<typename std::make_unsigned<Tgt>::type>(b, e);
1105 FOLLY_RANGE_CHECK(result >= 0, "Overflow.");
1110 * Parsing strings to integrals. These routines differ from
1111 * to<integral>(string) in that they take a POINTER TO a StringPiece
1112 * and alter that StringPiece to reflect progress information.
1116 * StringPiece to integrals, with progress information. Alters the
1117 * StringPiece parameter to munch the already-parsed characters.
1119 template <class Tgt>
1120 typename std::enable_if<
1121 std::is_integral<Tgt>::value
1122 && !std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
1124 to(StringPiece * src) {
1126 auto b = src->data(), past = src->data() + src->size();
1128 FOLLY_RANGE_CHECK(b < past, "No digits found in input string");
1129 if (!isspace(*b)) break;
1134 // First digit is customized because we test for sign
1135 bool negative = false;
1136 /* static */ if (std::is_signed<Tgt>::value) {
1141 FOLLY_RANGE_CHECK(*m == '+', "Invalid leading character in conversion"
1148 FOLLY_RANGE_CHECK(m < past, "No digits found in input string");
1149 FOLLY_RANGE_CHECK(isdigit(*m), "Non-digit character found");
1150 m = detail::findFirstNonDigit<Tgt>(m + 1, past);
1153 /* static */ if (!std::is_signed<Tgt>::value) {
1154 result = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
1156 auto t = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
1159 FOLLY_RANGE_CHECK(is_non_positive(result), "Negative overflow");
1162 FOLLY_RANGE_CHECK(is_non_negative(result), "Overflow");
1165 src->advance(m - src->data());
1170 * StringPiece to bool, with progress information. Alters the
1171 * StringPiece parameter to munch the already-parsed characters.
1173 template <class Tgt>
1174 typename std::enable_if<
1175 std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
1177 to(StringPiece * src) {
1178 return detail::str_to_bool(src);
1184 * Enforce that the suffix following a number is made up only of whitespace.
1186 inline void enforceWhitespace(const char* b, const char* e) {
1187 for (; b != e; ++b) {
1188 FOLLY_RANGE_CHECK(isspace(*b), to<std::string>("Non-whitespace: ", *b));
1192 } // namespace detail
1195 * String or StringPiece to integrals. Accepts leading and trailing
1196 * whitespace, but no non-space trailing characters.
1198 template <class Tgt>
1199 typename std::enable_if<
1200 std::is_integral<Tgt>::value,
1202 to(StringPiece src) {
1203 Tgt result = to<Tgt>(&src);
1204 detail::enforceWhitespace(src.data(), src.data() + src.size());
1208 /*******************************************************************************
1209 * Conversions from string types to floating-point types.
1210 ******************************************************************************/
1213 * StringPiece to double, with progress information. Alters the
1214 * StringPiece parameter to munch the already-parsed characters.
1216 template <class Tgt>
1217 inline typename std::enable_if<
1218 std::is_floating_point<Tgt>::value,
1220 to(StringPiece *const src) {
1221 using namespace double_conversion;
1222 static StringToDoubleConverter
1223 conv(StringToDoubleConverter::ALLOW_TRAILING_JUNK
1224 | StringToDoubleConverter::ALLOW_LEADING_SPACES,
1226 // return this for junk input string
1227 std::numeric_limits<double>::quiet_NaN(),
1230 FOLLY_RANGE_CHECK(!src->empty(), "No digits found in input string");
1233 auto result = conv.StringToDouble(src->data(),
1234 static_cast<int>(src->size()),
1235 &length); // processed char count
1237 if (!std::isnan(result)) {
1238 src->advance(length);
1242 for (;; src->advance(1)) {
1244 throw std::range_error("Unable to convert an empty string"
1245 " to a floating point value.");
1247 if (!isspace(src->front())) {
1252 // Was that "inf[inity]"?
1253 if (src->size() >= 3 && toupper((*src)[0]) == 'I'
1254 && toupper((*src)[1]) == 'N' && toupper((*src)[2]) == 'F') {
1255 if (src->size() >= 8 &&
1256 toupper((*src)[3]) == 'I' &&
1257 toupper((*src)[4]) == 'N' &&
1258 toupper((*src)[5]) == 'I' &&
1259 toupper((*src)[6]) == 'T' &&
1260 toupper((*src)[7]) == 'Y') {
1265 return std::numeric_limits<Tgt>::infinity();
1268 // Was that "-inf[inity]"?
1269 if (src->size() >= 4 && toupper((*src)[0]) == '-'
1270 && toupper((*src)[1]) == 'I' && toupper((*src)[2]) == 'N'
1271 && toupper((*src)[3]) == 'F') {
1272 if (src->size() >= 9 &&
1273 toupper((*src)[4]) == 'I' &&
1274 toupper((*src)[5]) == 'N' &&
1275 toupper((*src)[6]) == 'I' &&
1276 toupper((*src)[7]) == 'T' &&
1277 toupper((*src)[8]) == 'Y') {
1282 return -std::numeric_limits<Tgt>::infinity();
1286 if (src->size() >= 3 && toupper((*src)[0]) == 'N'
1287 && toupper((*src)[1]) == 'A' && toupper((*src)[2]) == 'N') {
1289 return std::numeric_limits<Tgt>::quiet_NaN();
1293 if (src->size() >= 4 &&
1294 toupper((*src)[0]) == '-' &&
1295 toupper((*src)[1]) == 'N' &&
1296 toupper((*src)[2]) == 'A' &&
1297 toupper((*src)[3]) == 'N') {
1299 return -std::numeric_limits<Tgt>::quiet_NaN();
1303 throw std::range_error("Unable to convert \"" + src->toString()
1304 + "\" to a floating point value.");
1308 * Any string, const char*, or StringPiece to double.
1310 template <class Tgt>
1311 typename std::enable_if<
1312 std::is_floating_point<Tgt>::value,
1314 to(StringPiece src) {
1315 Tgt result = to<double>(&src);
1316 detail::enforceWhitespace(src.data(), src.data() + src.size());
1320 /*******************************************************************************
1321 * Integral to floating point and back
1322 ******************************************************************************/
1325 * Checked conversion from integral to flating point and back. The
1326 * result must be convertible back to the source type without loss of
1327 * precision. This seems Draconian but sometimes is what's needed, and
1328 * complements existing routines nicely. For various rounding
1329 * routines, see <math>.
1331 template <class Tgt, class Src>
1332 typename std::enable_if<
1333 (std::is_integral<Src>::value && std::is_floating_point<Tgt>::value)
1335 (std::is_floating_point<Src>::value && std::is_integral<Tgt>::value),
1337 to(const Src & value) {
1339 auto witness = static_cast<Src>(result);
1340 if (value != witness) {
1341 throw std::range_error(
1342 to<std::string>("to<>: loss of precision when converting ", value,
1343 " to type ", typeid(Tgt).name()).c_str());
1348 /*******************************************************************************
1349 * Enum to anything and back
1350 ******************************************************************************/
1352 #if defined(__clang__) || __GNUC_PREREQ(4, 7)
1353 // std::underlying_type became available by gcc 4.7.0
1355 template <class Tgt, class Src>
1356 typename std::enable_if<std::is_enum<Src>::value, Tgt>::type
1357 to(const Src & value) {
1358 return to<Tgt>(static_cast<typename std::underlying_type<Src>::type>(value));
1361 template <class Tgt, class Src>
1362 typename std::enable_if<std::is_enum<Tgt>::value, Tgt>::type
1363 to(const Src & value) {
1364 return static_cast<Tgt>(to<typename std::underlying_type<Tgt>::type>(value));
1369 template <class Tgt, class Src>
1370 typename std::enable_if<std::is_enum<Src>::value, Tgt>::type
1371 to(const Src & value) {
1372 /* static */ if (Src(-1) < 0) {
1373 /* static */ if (sizeof(Src) <= sizeof(int)) {
1374 return to<Tgt>(static_cast<int>(value));
1376 return to<Tgt>(static_cast<long>(value));
1379 /* static */ if (sizeof(Src) <= sizeof(int)) {
1380 return to<Tgt>(static_cast<unsigned int>(value));
1382 return to<Tgt>(static_cast<unsigned long>(value));
1387 template <class Tgt, class Src>
1388 typename std::enable_if<std::is_enum<Tgt>::value, Tgt>::type
1389 to(const Src & value) {
1390 /* static */ if (Tgt(-1) < 0) {
1391 /* static */ if (sizeof(Tgt) <= sizeof(int)) {
1392 return static_cast<Tgt>(to<int>(value));
1394 return static_cast<Tgt>(to<long>(value));
1397 /* static */ if (sizeof(Tgt) <= sizeof(int)) {
1398 return static_cast<Tgt>(to<unsigned int>(value));
1400 return static_cast<Tgt>(to<unsigned long>(value));
1405 #endif // gcc 4.7 onwards
1407 } // namespace folly
1409 // FOLLY_CONV_INTERNAL is defined by Conv.cpp. Keep the FOLLY_RANGE_CHECK
1410 // macro for use in Conv.cpp, but #undefine it everywhere else we are included,
1411 // to avoid defining this global macro name in other files that include Conv.h.
1412 #ifndef FOLLY_CONV_INTERNAL
1413 #undef FOLLY_RANGE_CHECK
1414 #undef FOLLY_RANGE_CHECK_STRINGIZE2
1415 #undef FOLLY_RANGE_CHECK_STRINGIZE
1418 #endif /* FOLLY_BASE_CONV_H_ */