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(detail::estimateSpaceToReserve(0, vs...));
662 * Variadic base case: append one element
664 template <class T, class Tgt>
665 typename std::enable_if<
666 IsSomeString<typename std::remove_pointer<Tgt>::type>
668 toAppendStrImpl(const T& v, Tgt result) {
672 template <class T, class... Ts>
673 typename std::enable_if<sizeof...(Ts) >= 2
675 typename std::remove_pointer<
676 typename detail::last_element<Ts...>::type
677 >::type>::value>::type
678 toAppendStrImpl(const T& v, const Ts&... vs) {
679 toAppend(v, getLastElement(vs...));
680 toAppendStrImpl(vs...);
686 * Variadic conversion to string. Appends each element in turn.
687 * If we have two or more things to append, we will reserve
688 * the space for them (at least we will try).
690 template <class... Ts>
691 typename std::enable_if<sizeof...(Ts) >= 3
693 typename std::remove_pointer<
694 typename detail::last_element<Ts...>::type
695 >::type>::value>::type
696 toAppend(const Ts&... vs) {
697 detail::reserveInTarget(vs...);
698 detail::toAppendStrImpl(vs...);
702 * Variadic base case: do nothing.
705 typename std::enable_if<IsSomeString<Tgt>::value>::type
706 toAppend(Tgt* result) {
710 * Variadic base case: do nothing.
712 template <class Delimiter, class Tgt>
713 typename std::enable_if<IsSomeString<Tgt>::value>::type
714 toAppendDelim(const Delimiter& delim, Tgt* result) {
718 * 1 element: same as toAppend.
720 template <class Delimiter, class T, class Tgt>
721 typename std::enable_if<IsSomeString<Tgt>::value>::type
722 toAppendDelim(const Delimiter& delim, const T& v, Tgt* tgt) {
727 * Append to string with a delimiter in between elements.
729 template <class Delimiter, class T, class... Ts>
730 typename std::enable_if<sizeof...(Ts) >= 2
732 typename std::remove_pointer<
733 typename detail::last_element<Ts...>::type
734 >::type>::value>::type
735 toAppendDelim(const Delimiter& delim, const T& v, const Ts&... vs) {
736 toAppend(v, delim, detail::getLastElement(vs...));
737 toAppendDelim(delim, vs...);
741 * to<SomeString>(SomeString str) returns itself. As both std::string and
742 * folly::fbstring use Copy-on-Write, it's much more efficient by
743 * avoiding copying the underlying char array.
745 template <class Tgt, class Src>
746 typename std::enable_if<
747 IsSomeString<Tgt>::value && std::is_same<Tgt, Src>::value,
749 to(const Src & value) {
754 * to<SomeString>(v1, v2, ...) uses toAppend() (see below) as back-end
757 template <class Tgt, class... Ts>
758 typename std::enable_if<
759 IsSomeString<Tgt>::value && (
760 sizeof...(Ts) != 1 ||
761 !std::is_same<Tgt, typename detail::last_element<Ts...>::type>::value),
763 to(const Ts&... vs) {
765 toAppend(vs..., &result);
770 * toDelim<SomeString>(SomeString str) returns itself.
772 template <class Tgt, class Delim, class Src>
773 typename std::enable_if<
774 IsSomeString<Tgt>::value && std::is_same<Tgt, Src>::value,
776 toDelim(const Delim& delim, const Src & value) {
781 * toDelim<SomeString>(delim, v1, v2, ...) uses toAppendDelim() as
782 * back-end for all types.
784 template <class Tgt, class Delim, class... Ts>
785 typename std::enable_if<
786 IsSomeString<Tgt>::value && (
787 sizeof...(Ts) != 1 ||
788 !std::is_same<Tgt, typename detail::last_element<Ts...>::type>::value),
790 toDelim(const Delim& delim, const Ts&... vs) {
792 toAppendDelim(delim, vs..., &result);
796 /*******************************************************************************
797 * Conversions from string types to integral types.
798 ******************************************************************************/
803 * Finds the first non-digit in a string. The number of digits
804 * searched depends on the precision of the Tgt integral. Assumes the
805 * string starts with NO whitespace and NO sign.
807 * The semantics of the routine is:
809 * if (b >= e || !isdigit(*b)) return b;
812 * Complete unrolling marks bottom-line (i.e. entire conversion)
813 * improvements of 20%.
816 const char* findFirstNonDigit(const char* b, const char* e) {
818 auto const c = static_cast<unsigned>(*b) - '0';
824 // Maximum value of number when represented as a string
825 template <class T> struct MaxString {
826 static const char*const value;
831 * Lookup tables that converts from a decimal character value to an integral
832 * binary value, shifted by a decimal "shift" multiplier.
833 * For all character values in the range '0'..'9', the table at those
834 * index locations returns the actual decimal value shifted by the multiplier.
835 * For all other values, the lookup table returns an invalid OOR value.
837 // Out-of-range flag value, larger than the largest value that can fit in
838 // four decimal bytes (9999), but four of these added up together should
839 // still not overflow uint16_t.
840 constexpr int32_t OOR = 10000;
842 __attribute__((aligned(16))) constexpr uint16_t shift1[] = {
843 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
844 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
845 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
846 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
847 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
848 1, 2, 3, 4, 5, 6, 7, 8, 9, OOR, OOR,
849 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
850 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
851 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
852 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
853 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
854 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
855 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
856 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
857 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
858 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
859 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
860 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
861 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
862 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
863 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
864 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
865 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
866 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
867 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
868 OOR, OOR, OOR, OOR, OOR, OOR // 250
871 __attribute__((aligned(16))) constexpr uint16_t shift10[] = {
872 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
873 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
874 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
875 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
876 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
877 10, 20, 30, 40, 50, 60, 70, 80, 90, OOR, OOR,
878 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
879 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
880 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
881 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
882 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
883 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
884 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
885 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
886 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
887 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
888 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
889 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
890 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
891 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
892 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
893 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
894 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
895 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
896 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
897 OOR, OOR, OOR, OOR, OOR, OOR // 250
900 __attribute__((aligned(16))) constexpr uint16_t shift100[] = {
901 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
902 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
903 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
904 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
905 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
906 100, 200, 300, 400, 500, 600, 700, 800, 900, OOR, OOR,
907 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
908 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
909 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
910 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
911 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
912 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
913 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
914 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
915 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
916 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
917 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
918 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
919 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
920 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
921 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
922 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
923 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
924 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
925 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
926 OOR, OOR, OOR, OOR, OOR, OOR // 250
929 __attribute__((aligned(16))) constexpr uint16_t shift1000[] = {
930 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 0-9
931 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 10
932 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 20
933 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 30
934 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0, // 40
935 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, OOR, OOR,
936 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 60
937 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 70
938 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 80
939 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 90
940 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 100
941 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 110
942 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 120
943 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 130
944 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 140
945 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 150
946 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 160
947 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 170
948 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 180
949 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 190
950 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 200
951 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 210
952 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 220
953 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 230
954 OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, // 240
955 OOR, OOR, OOR, OOR, OOR, OOR // 250
959 * String represented as a pair of pointers to char to unsigned
960 * integrals. Assumes NO whitespace before or after, and also that the
961 * string is composed entirely of digits. Tgt must be unsigned, and no
962 * sign is allowed in the string (even it's '+'). String may be empty,
963 * in which case digits_to throws.
966 Tgt digits_to(const char * b, const char * e) {
968 static_assert(!std::is_signed<Tgt>::value, "Unsigned type expected");
971 const size_t size = e - b;
973 /* Although the string is entirely made of digits, we still need to
974 * check for overflow.
976 if (size >= std::numeric_limits<Tgt>::digits10 + 1) {
977 // Leading zeros? If so, recurse to keep things simple
978 if (b < e && *b == '0') {
980 if (b == e) return 0; // just zeros, e.g. "0000"
981 if (*b != '0') return digits_to<Tgt>(b, e);
984 FOLLY_RANGE_CHECK(size == std::numeric_limits<Tgt>::digits10 + 1 &&
985 strncmp(b, detail::MaxString<Tgt>::value, size) <= 0,
986 "Numeric overflow upon conversion");
989 // Here we know that the number won't overflow when
990 // converted. Proceed without checks.
994 for (; e - b >= 4; b += 4) {
996 const int32_t r0 = shift1000[static_cast<size_t>(b[0])];
997 const int32_t r1 = shift100[static_cast<size_t>(b[1])];
998 const int32_t r2 = shift10[static_cast<size_t>(b[2])];
999 const int32_t r3 = shift1[static_cast<size_t>(b[3])];
1000 const auto sum = r0 + r1 + r2 + r3;
1001 assert(sum < OOR && "Assumption: string only has digits");
1007 const int32_t r0 = shift100[static_cast<size_t>(b[0])];
1008 const int32_t r1 = shift10[static_cast<size_t>(b[1])];
1009 const int32_t r2 = shift1[static_cast<size_t>(b[2])];
1010 const auto sum = r0 + r1 + r2;
1011 assert(sum < OOR && "Assumption: string only has digits");
1012 return result * 1000 + sum;
1015 const int32_t r0 = shift10[static_cast<size_t>(b[0])];
1016 const int32_t r1 = shift1[static_cast<size_t>(b[1])];
1017 const auto sum = r0 + r1;
1018 assert(sum < OOR && "Assumption: string only has digits");
1019 return result * 100 + sum;
1022 const int32_t sum = shift1[static_cast<size_t>(b[0])];
1023 assert(sum < OOR && "Assumption: string only has digits");
1024 return result * 10 + sum;
1029 FOLLY_RANGE_CHECK(size > 0, "Found no digits to convert in input");
1034 bool str_to_bool(StringPiece * src);
1036 } // namespace detail
1039 * String represented as a pair of pointers to char to unsigned
1040 * integrals. Assumes NO whitespace before or after.
1042 template <class Tgt>
1043 typename std::enable_if<
1044 std::is_integral<Tgt>::value && !std::is_signed<Tgt>::value
1045 && !std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
1047 to(const char * b, const char * e) {
1048 return detail::digits_to<Tgt>(b, e);
1052 * String represented as a pair of pointers to char to signed
1053 * integrals. Assumes NO whitespace before or after. Allows an
1054 * optional leading sign.
1056 template <class Tgt>
1057 typename std::enable_if<
1058 std::is_integral<Tgt>::value && std::is_signed<Tgt>::value,
1060 to(const char * b, const char * e) {
1061 FOLLY_RANGE_CHECK(b < e, "Empty input string in conversion to integral");
1064 Tgt result = -to<typename std::make_unsigned<Tgt>::type>(b + 1, e);
1065 FOLLY_RANGE_CHECK(result <= 0, "Negative overflow.");
1068 FOLLY_RANGE_CHECK(*b == '+', "Invalid lead character");
1071 Tgt result = to<typename std::make_unsigned<Tgt>::type>(b, e);
1072 FOLLY_RANGE_CHECK(result >= 0, "Overflow.");
1077 * Parsing strings to integrals. These routines differ from
1078 * to<integral>(string) in that they take a POINTER TO a StringPiece
1079 * and alter that StringPiece to reflect progress information.
1083 * StringPiece to integrals, with progress information. Alters the
1084 * StringPiece parameter to munch the already-parsed characters.
1086 template <class Tgt>
1087 typename std::enable_if<
1088 std::is_integral<Tgt>::value
1089 && !std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
1091 to(StringPiece * src) {
1093 auto b = src->data(), past = src->data() + src->size();
1095 FOLLY_RANGE_CHECK(b < past, "No digits found in input string");
1096 if (!isspace(*b)) break;
1101 // First digit is customized because we test for sign
1102 bool negative = false;
1103 /* static */ if (std::is_signed<Tgt>::value) {
1108 FOLLY_RANGE_CHECK(*m == '+', "Invalid leading character in conversion"
1115 FOLLY_RANGE_CHECK(m < past, "No digits found in input string");
1116 FOLLY_RANGE_CHECK(isdigit(*m), "Non-digit character found");
1117 m = detail::findFirstNonDigit<Tgt>(m + 1, past);
1120 /* static */ if (!std::is_signed<Tgt>::value) {
1121 result = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
1123 auto t = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
1126 FOLLY_RANGE_CHECK(is_non_positive(result), "Negative overflow");
1129 FOLLY_RANGE_CHECK(is_non_negative(result), "Overflow");
1132 src->advance(m - src->data());
1137 * StringPiece to bool, with progress information. Alters the
1138 * StringPiece parameter to munch the already-parsed characters.
1140 template <class Tgt>
1141 typename std::enable_if<
1142 std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
1144 to(StringPiece * src) {
1145 return detail::str_to_bool(src);
1151 * Enforce that the suffix following a number is made up only of whitespace.
1153 inline void enforceWhitespace(const char* b, const char* e) {
1154 for (; b != e; ++b) {
1155 FOLLY_RANGE_CHECK(isspace(*b), to<std::string>("Non-whitespace: ", *b));
1159 } // namespace detail
1162 * String or StringPiece to integrals. Accepts leading and trailing
1163 * whitespace, but no non-space trailing characters.
1165 template <class Tgt>
1166 typename std::enable_if<
1167 std::is_integral<Tgt>::value,
1169 to(StringPiece src) {
1170 Tgt result = to<Tgt>(&src);
1171 detail::enforceWhitespace(src.data(), src.data() + src.size());
1175 /*******************************************************************************
1176 * Conversions from string types to floating-point types.
1177 ******************************************************************************/
1180 * StringPiece to double, with progress information. Alters the
1181 * StringPiece parameter to munch the already-parsed characters.
1183 template <class Tgt>
1184 inline typename std::enable_if<
1185 std::is_floating_point<Tgt>::value,
1187 to(StringPiece *const src) {
1188 using namespace double_conversion;
1189 static StringToDoubleConverter
1190 conv(StringToDoubleConverter::ALLOW_TRAILING_JUNK
1191 | StringToDoubleConverter::ALLOW_LEADING_SPACES,
1193 // return this for junk input string
1194 std::numeric_limits<double>::quiet_NaN(),
1197 FOLLY_RANGE_CHECK(!src->empty(), "No digits found in input string");
1200 auto result = conv.StringToDouble(src->data(), src->size(),
1201 &length); // processed char count
1203 if (!std::isnan(result)) {
1204 src->advance(length);
1208 for (;; src->advance(1)) {
1210 throw std::range_error("Unable to convert an empty string"
1211 " to a floating point value.");
1213 if (!isspace(src->front())) {
1218 // Was that "inf[inity]"?
1219 if (src->size() >= 3 && toupper((*src)[0]) == 'I'
1220 && toupper((*src)[1]) == 'N' && toupper((*src)[2]) == 'F') {
1221 if (src->size() >= 8 &&
1222 toupper((*src)[3]) == 'I' &&
1223 toupper((*src)[4]) == 'N' &&
1224 toupper((*src)[5]) == 'I' &&
1225 toupper((*src)[6]) == 'T' &&
1226 toupper((*src)[7]) == 'Y') {
1231 return std::numeric_limits<Tgt>::infinity();
1234 // Was that "-inf[inity]"?
1235 if (src->size() >= 4 && toupper((*src)[0]) == '-'
1236 && toupper((*src)[1]) == 'I' && toupper((*src)[2]) == 'N'
1237 && toupper((*src)[3]) == 'F') {
1238 if (src->size() >= 9 &&
1239 toupper((*src)[4]) == 'I' &&
1240 toupper((*src)[5]) == 'N' &&
1241 toupper((*src)[6]) == 'I' &&
1242 toupper((*src)[7]) == 'T' &&
1243 toupper((*src)[8]) == 'Y') {
1248 return -std::numeric_limits<Tgt>::infinity();
1252 if (src->size() >= 3 && toupper((*src)[0]) == 'N'
1253 && toupper((*src)[1]) == 'A' && toupper((*src)[2]) == 'N') {
1255 return std::numeric_limits<Tgt>::quiet_NaN();
1259 if (src->size() >= 4 &&
1260 toupper((*src)[0]) == '-' &&
1261 toupper((*src)[1]) == 'N' &&
1262 toupper((*src)[2]) == 'A' &&
1263 toupper((*src)[3]) == 'N') {
1265 return -std::numeric_limits<Tgt>::quiet_NaN();
1269 throw std::range_error("Unable to convert \"" + src->toString()
1270 + "\" to a floating point value.");
1274 * Any string, const char*, or StringPiece to double.
1276 template <class Tgt>
1277 typename std::enable_if<
1278 std::is_floating_point<Tgt>::value,
1280 to(StringPiece src) {
1281 Tgt result = to<double>(&src);
1282 detail::enforceWhitespace(src.data(), src.data() + src.size());
1286 /*******************************************************************************
1287 * Integral to floating point and back
1288 ******************************************************************************/
1291 * Checked conversion from integral to flating point and back. The
1292 * result must be convertible back to the source type without loss of
1293 * precision. This seems Draconian but sometimes is what's needed, and
1294 * complements existing routines nicely. For various rounding
1295 * routines, see <math>.
1297 template <class Tgt, class Src>
1298 typename std::enable_if<
1299 (std::is_integral<Src>::value && std::is_floating_point<Tgt>::value)
1301 (std::is_floating_point<Src>::value && std::is_integral<Tgt>::value),
1303 to(const Src & value) {
1305 auto witness = static_cast<Src>(result);
1306 if (value != witness) {
1307 throw std::range_error(
1308 to<std::string>("to<>: loss of precision when converting ", value,
1309 " to type ", typeid(Tgt).name()).c_str());
1314 /*******************************************************************************
1315 * Enum to anything and back
1316 ******************************************************************************/
1318 #if defined(__clang__) || __GNUC_PREREQ(4, 7)
1319 // std::underlying_type became available by gcc 4.7.0
1321 template <class Tgt, class Src>
1322 typename std::enable_if<std::is_enum<Src>::value, Tgt>::type
1323 to(const Src & value) {
1324 return to<Tgt>(static_cast<typename std::underlying_type<Src>::type>(value));
1327 template <class Tgt, class Src>
1328 typename std::enable_if<std::is_enum<Tgt>::value, Tgt>::type
1329 to(const Src & value) {
1330 return static_cast<Tgt>(to<typename std::underlying_type<Tgt>::type>(value));
1335 template <class Tgt, class Src>
1336 typename std::enable_if<std::is_enum<Src>::value, Tgt>::type
1337 to(const Src & value) {
1338 /* static */ if (Src(-1) < 0) {
1339 /* static */ if (sizeof(Src) <= sizeof(int)) {
1340 return to<Tgt>(static_cast<int>(value));
1342 return to<Tgt>(static_cast<long>(value));
1345 /* static */ if (sizeof(Src) <= sizeof(int)) {
1346 return to<Tgt>(static_cast<unsigned int>(value));
1348 return to<Tgt>(static_cast<unsigned long>(value));
1353 template <class Tgt, class Src>
1354 typename std::enable_if<std::is_enum<Tgt>::value, Tgt>::type
1355 to(const Src & value) {
1356 /* static */ if (Tgt(-1) < 0) {
1357 /* static */ if (sizeof(Tgt) <= sizeof(int)) {
1358 return static_cast<Tgt>(to<int>(value));
1360 return static_cast<Tgt>(to<long>(value));
1363 /* static */ if (sizeof(Tgt) <= sizeof(int)) {
1364 return static_cast<Tgt>(to<unsigned int>(value));
1366 return static_cast<Tgt>(to<unsigned long>(value));
1371 #endif // gcc 4.7 onwards
1373 } // namespace folly
1375 // FOLLY_CONV_INTERNAL is defined by Conv.cpp. Keep the FOLLY_RANGE_CHECK
1376 // macro for use in Conv.cpp, but #undefine it everywhere else we are included,
1377 // to avoid defining this global macro name in other files that include Conv.h.
1378 #ifndef FOLLY_CONV_INTERNAL
1379 #undef FOLLY_RANGE_CHECK
1382 #endif /* FOLLY_BASE_CONV_H_ */