2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Converts anything to anything, with an emphasis on performance and
21 * @author Andrei Alexandrescu (andrei.alexandrescu@fb.com)
33 #include <type_traits>
37 #include <boost/implicit_cast.hpp>
38 #include <double-conversion/double-conversion.h> // V8 JavaScript implementation
40 #include <folly/FBString.h>
41 #include <folly/Likely.h>
42 #include <folly/Preprocessor.h>
43 #include <folly/Range.h>
44 #include <folly/portability/Math.h>
46 #define FOLLY_RANGE_CHECK_STRINGIZE(x) #x
47 #define FOLLY_RANGE_CHECK_STRINGIZE2(x) FOLLY_RANGE_CHECK_STRINGIZE(x)
49 // Android doesn't support std::to_string so just use a placeholder there.
51 #define FOLLY_RANGE_CHECK_TO_STRING(x) std::string("N/A")
53 #define FOLLY_RANGE_CHECK_TO_STRING(x) std::to_string(x)
56 #define FOLLY_RANGE_CHECK(condition, message, src) \
57 ((condition) ? (void)0 : throw std::range_error( \
58 (std::string(__FILE__ "(" FOLLY_RANGE_CHECK_STRINGIZE2(__LINE__) "): ") \
59 + (message) + ": '" + (src) + "'").c_str()))
61 #define FOLLY_RANGE_CHECK_BEGIN_END(condition, message, b, e) \
62 FOLLY_RANGE_CHECK(condition, message, std::string((b), (e) - (b)))
64 #define FOLLY_RANGE_CHECK_STRINGPIECE(condition, message, sp) \
65 FOLLY_RANGE_CHECK(condition, message, std::string((sp).data(), (sp).size()))
70 * The identity conversion function.
71 * to<T>(T) returns itself for all types T.
73 template <class Tgt, class Src>
74 typename std::enable_if<std::is_same<Tgt, Src>::value, Tgt>::type
75 to(const Src & value) {
79 template <class Tgt, class Src>
80 typename std::enable_if<std::is_same<Tgt, Src>::value, Tgt>::type
82 return std::forward<Src>(value);
85 /*******************************************************************************
86 * Integral to integral
87 ******************************************************************************/
90 * Unchecked conversion from integral to boolean. This is different from the
91 * other integral conversions because we use the C convention of treating any
92 * non-zero value as true, instead of range checking.
94 template <class Tgt, class Src>
95 typename std::enable_if<
96 std::is_integral<Src>::value
97 && !std::is_same<Tgt, Src>::value
98 && std::is_same<Tgt, bool>::value,
100 to(const Src & value) {
105 * Checked conversion from integral to integral. The checks are only
106 * performed when meaningful, e.g. conversion from int to long goes
109 template <class Tgt, class Src>
110 typename std::enable_if<
111 std::is_integral<Src>::value
112 && !std::is_same<Tgt, Src>::value
113 && !std::is_same<Tgt, bool>::value
114 && std::is_integral<Tgt>::value,
116 to(const Src & value) {
117 /* static */ if (std::numeric_limits<Tgt>::max()
118 < std::numeric_limits<Src>::max()) {
120 (!greater_than<Tgt, std::numeric_limits<Tgt>::max()>(value)),
122 FOLLY_RANGE_CHECK_TO_STRING(value));
124 /* static */ if (std::is_signed<Src>::value &&
125 (!std::is_signed<Tgt>::value || sizeof(Src) > sizeof(Tgt))) {
127 (!less_than<Tgt, std::numeric_limits<Tgt>::min()>(value)),
129 FOLLY_RANGE_CHECK_TO_STRING(value));
131 return static_cast<Tgt>(value);
134 /*******************************************************************************
135 * Floating point to floating point
136 ******************************************************************************/
138 template <class Tgt, class Src>
139 typename std::enable_if<
140 std::is_floating_point<Tgt>::value
141 && std::is_floating_point<Src>::value
142 && !std::is_same<Tgt, Src>::value,
144 to(const Src & value) {
145 /* static */ if (std::numeric_limits<Tgt>::max() <
146 std::numeric_limits<Src>::max()) {
147 FOLLY_RANGE_CHECK(value <= std::numeric_limits<Tgt>::max(),
149 FOLLY_RANGE_CHECK_TO_STRING(value));
150 FOLLY_RANGE_CHECK(value >= -std::numeric_limits<Tgt>::max(),
152 FOLLY_RANGE_CHECK_TO_STRING(value));
154 return boost::implicit_cast<Tgt>(value);
157 /*******************************************************************************
159 ******************************************************************************/
164 const T& getLastElement(const T & v) {
168 template <class T, class... Ts>
169 typename std::tuple_element<
171 std::tuple<T, Ts...> >::type const&
172 getLastElement(const T&, const Ts&... vs) {
173 return getLastElement(vs...);
176 // This class exists to specialize away std::tuple_element in the case where we
177 // have 0 template arguments. Without this, Clang/libc++ will blow a
178 // static_assert even if tuple_element is protected by an enable_if.
179 template <class... Ts>
180 struct last_element {
181 typedef typename std::enable_if<
183 typename std::tuple_element<
184 sizeof...(Ts) - 1, std::tuple<Ts...>
189 struct last_element<> {
193 } // namespace detail
195 /*******************************************************************************
196 * Conversions from integral types to string types.
197 ******************************************************************************/
199 #if FOLLY_HAVE_INT128_T
202 template <typename IntegerType>
203 constexpr unsigned int
205 return (unsigned int)(ceil(sizeof(IntegerType) * CHAR_BIT * M_LN2 / M_LN10));
209 unsafeTelescope128(char * buffer, size_t room, unsigned __int128 x) {
210 typedef unsigned __int128 Usrc;
213 while (x >= (Usrc(1) << 64)) { // Using 128-bit division while needed
214 const auto y = x / 10;
215 const auto digit = x % 10;
217 buffer[p--] = '0' + digit;
221 uint64_t xx = x; // Moving to faster 64-bit division thereafter
224 const auto y = xx / 10ULL;
225 const auto digit = xx % 10ULL;
227 buffer[p--] = '0' + digit;
231 buffer[p] = '0' + xx;
240 * Returns the number of digits in the base 10 representation of an
241 * uint64_t. Useful for preallocating buffers and such. It's also used
242 * internally, see below. Measurements suggest that defining a
243 * separate overload for 32-bit integers is not worthwhile.
246 inline uint32_t digits10(uint64_t v) {
249 // For this arch we can get a little help from specialized CPU instructions
250 // which can count leading zeroes; 64 minus that is appx. log (base 2).
251 // Use that to approximate base-10 digits (log_10) and then adjust if needed.
253 // 10^i, defined for i 0 through 19.
254 // This is 20 * 8 == 160 bytes, which fits neatly into 5 cache lines
255 // (assuming a cache line size of 64).
256 static const uint64_t powersOf10[20] FOLLY_ALIGNED(64) = {
276 10000000000000000000UL,
279 // "count leading zeroes" operation not valid; for 0; special case this.
284 // bits is in the ballpark of log_2(v).
285 const uint8_t leadingZeroes = __builtin_clzll(v);
286 const auto bits = 63 - leadingZeroes;
288 // approximate log_10(v) == log_10(2) * bits.
289 // Integer magic below: 77/256 is appx. 0.3010 (log_10(2)).
290 // The +1 is to make this the ceiling of the log_10 estimate.
291 const uint32_t minLength = 1 + ((bits * 77) >> 8);
293 // return that log_10 lower bound, plus adjust if input >= 10^(that bound)
294 // in case there's a small error and we misjudged length.
295 return minLength + (uint32_t) (UNLIKELY (v >= powersOf10[minLength]));
301 if (LIKELY(v < 10)) return result;
302 if (LIKELY(v < 100)) return result + 1;
303 if (LIKELY(v < 1000)) return result + 2;
304 if (LIKELY(v < 10000)) return result + 3;
305 // Skip ahead by 4 orders of magnitude
314 * Copies the ASCII base 10 representation of v into buffer and
315 * returns the number of bytes written. Does NOT append a \0. Assumes
316 * the buffer points to digits10(v) bytes of valid memory. Note that
317 * uint64 needs at most 20 bytes, uint32_t needs at most 10 bytes,
318 * uint16_t needs at most 5 bytes, and so on. Measurements suggest
319 * that defining a separate overload for 32-bit integers is not
322 * This primitive is unsafe because it makes the size assumption and
323 * because it does not add a terminating \0.
326 inline uint32_t uint64ToBufferUnsafe(uint64_t v, char *const buffer) {
327 auto const result = digits10(v);
328 // WARNING: using size_t or pointer arithmetic for pos slows down
329 // the loop below 20x. This is because several 32-bit ops can be
330 // done in parallel, but only fewer 64-bit ones.
331 uint32_t pos = result - 1;
333 // Keep these together so a peephole optimization "sees" them and
334 // computes them in one shot.
335 auto const q = v / 10;
336 auto const r = static_cast<uint32_t>(v % 10);
337 buffer[pos--] = '0' + r;
340 // Last digit is trivial to handle
341 buffer[pos] = static_cast<uint32_t>(v) + '0';
346 * A single char gets appended.
349 void toAppend(char value, Tgt * result) {
354 constexpr typename std::enable_if<
355 std::is_same<T, char>::value,
357 estimateSpaceNeeded(T) {
362 * Ubiquitous helper template for writing string appenders
364 template <class T> struct IsSomeString {
365 enum { value = std::is_same<T, std::string>::value
366 || std::is_same<T, fbstring>::value };
370 * Everything implicitly convertible to const char* gets appended.
372 template <class Tgt, class Src>
373 typename std::enable_if<
374 std::is_convertible<Src, const char*>::value
375 && IsSomeString<Tgt>::value>::type
376 toAppend(Src value, Tgt * result) {
377 // Treat null pointers like an empty string, as in:
378 // operator<<(std::ostream&, const char*).
379 const char* c = value;
381 result->append(value);
386 typename std::enable_if<
387 std::is_convertible<Src, const char*>::value,
389 estimateSpaceNeeded(Src value) {
390 const char *c = value;
392 return folly::StringPiece(value).size();
398 typename std::enable_if<
399 (std::is_convertible<Src, folly::StringPiece>::value ||
400 IsSomeString<Src>::value) &&
401 !std::is_convertible<Src, const char*>::value,
403 estimateSpaceNeeded(Src value) {
404 return folly::StringPiece(value).size();
408 inline size_t estimateSpaceNeeded(std::nullptr_t /* value */) {
413 typename std::enable_if<
414 std::is_pointer<Src>::value &&
415 IsSomeString<std::remove_pointer<Src>>::value,
417 estimateSpaceNeeded(Src value) {
418 return value->size();
422 * Strings get appended, too.
424 template <class Tgt, class Src>
425 typename std::enable_if<
426 IsSomeString<Src>::value && IsSomeString<Tgt>::value>::type
427 toAppend(const Src& value, Tgt * result) {
428 result->append(value);
432 * and StringPiece objects too
435 typename std::enable_if<
436 IsSomeString<Tgt>::value>::type
437 toAppend(StringPiece value, Tgt * result) {
438 result->append(value.data(), value.size());
442 * There's no implicit conversion from fbstring to other string types,
443 * so make a specialization.
446 typename std::enable_if<
447 IsSomeString<Tgt>::value>::type
448 toAppend(const fbstring& value, Tgt * result) {
449 result->append(value.data(), value.size());
452 #if FOLLY_HAVE_INT128_T
454 * Special handling for 128 bit integers.
459 toAppend(__int128 value, Tgt * result) {
460 typedef unsigned __int128 Usrc;
461 char buffer[detail::digitsEnough<unsigned __int128>() + 1];
465 p = detail::unsafeTelescope128(buffer, sizeof(buffer), -Usrc(value));
468 p = detail::unsafeTelescope128(buffer, sizeof(buffer), value);
471 result->append(buffer + p, buffer + sizeof(buffer));
476 toAppend(unsigned __int128 value, Tgt * result) {
477 char buffer[detail::digitsEnough<unsigned __int128>()];
480 p = detail::unsafeTelescope128(buffer, sizeof(buffer), value);
482 result->append(buffer + p, buffer + sizeof(buffer));
486 constexpr typename std::enable_if<
487 std::is_same<T, __int128>::value,
489 estimateSpaceNeeded(T) {
490 return detail::digitsEnough<__int128>();
494 constexpr typename std::enable_if<
495 std::is_same<T, unsigned __int128>::value,
497 estimateSpaceNeeded(T) {
498 return detail::digitsEnough<unsigned __int128>();
504 * int32_t and int64_t to string (by appending) go through here. The
505 * result is APPENDED to a preexisting string passed as the second
506 * parameter. This should be efficient with fbstring because fbstring
507 * incurs no dynamic allocation below 23 bytes and no number has more
508 * than 22 bytes in its textual representation (20 for digits, one for
509 * sign, one for the terminating 0).
511 template <class Tgt, class Src>
512 typename std::enable_if<
513 std::is_integral<Src>::value && std::is_signed<Src>::value &&
514 IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
515 toAppend(Src value, Tgt * result) {
518 result->push_back('-');
519 result->append(buffer, uint64ToBufferUnsafe(-uint64_t(value), buffer));
521 result->append(buffer, uint64ToBufferUnsafe(value, buffer));
526 typename std::enable_if<
527 std::is_integral<Src>::value && std::is_signed<Src>::value
528 && sizeof(Src) >= 4 && sizeof(Src) < 16,
530 estimateSpaceNeeded(Src value) {
532 // When "value" is the smallest negative, negating it would evoke
533 // undefined behavior, so, instead of writing "-value" below, we write
534 // "~static_cast<uint64_t>(value) + 1"
535 return 1 + digits10(~static_cast<uint64_t>(value) + 1);
538 return digits10(static_cast<uint64_t>(value));
542 * As above, but for uint32_t and uint64_t.
544 template <class Tgt, class Src>
545 typename std::enable_if<
546 std::is_integral<Src>::value && !std::is_signed<Src>::value
547 && IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
548 toAppend(Src value, Tgt * result) {
550 result->append(buffer, uint64ToBufferUnsafe(value, buffer));
554 typename std::enable_if<
555 std::is_integral<Src>::value && !std::is_signed<Src>::value
556 && sizeof(Src) >= 4 && sizeof(Src) < 16,
558 estimateSpaceNeeded(Src value) {
559 return digits10(value);
563 * All small signed and unsigned integers to string go through 32-bit
564 * types int32_t and uint32_t, respectively.
566 template <class Tgt, class Src>
567 typename std::enable_if<
568 std::is_integral<Src>::value
569 && IsSomeString<Tgt>::value && sizeof(Src) < 4>::type
570 toAppend(Src value, Tgt * result) {
572 std::conditional<std::is_signed<Src>::value, int64_t, uint64_t>::type
574 toAppend<Tgt>(static_cast<Intermediate>(value), result);
578 typename std::enable_if<
579 std::is_integral<Src>::value
581 && !std::is_same<Src, char>::value,
583 estimateSpaceNeeded(Src value) {
585 std::conditional<std::is_signed<Src>::value, int64_t, uint64_t>::type
587 return estimateSpaceNeeded(static_cast<Intermediate>(value));
591 * Enumerated values get appended as integers.
593 template <class Tgt, class Src>
594 typename std::enable_if<
595 std::is_enum<Src>::value && IsSomeString<Tgt>::value>::type
596 toAppend(Src value, Tgt * result) {
598 static_cast<typename std::underlying_type<Src>::type>(value), result);
602 typename std::enable_if<
603 std::is_enum<Src>::value, size_t>::type
604 estimateSpaceNeeded(Src value) {
605 return estimateSpaceNeeded(
606 static_cast<typename std::underlying_type<Src>::type>(value));
609 /*******************************************************************************
610 * Conversions from floating-point types to string types.
611 ******************************************************************************/
614 constexpr int kConvMaxDecimalInShortestLow = -6;
615 constexpr int kConvMaxDecimalInShortestHigh = 21;
618 /** Wrapper around DoubleToStringConverter **/
619 template <class Tgt, class Src>
620 typename std::enable_if<
621 std::is_floating_point<Src>::value
622 && IsSomeString<Tgt>::value>::type
626 double_conversion::DoubleToStringConverter::DtoaMode mode,
627 unsigned int numDigits) {
628 using namespace double_conversion;
629 DoubleToStringConverter
630 conv(DoubleToStringConverter::NO_FLAGS,
631 "Infinity", "NaN", 'E',
632 detail::kConvMaxDecimalInShortestLow,
633 detail::kConvMaxDecimalInShortestHigh,
634 6, // max leading padding zeros
635 1); // max trailing padding zeros
637 StringBuilder builder(buffer, sizeof(buffer));
639 case DoubleToStringConverter::SHORTEST:
640 conv.ToShortest(value, &builder);
642 case DoubleToStringConverter::FIXED:
643 conv.ToFixed(value, numDigits, &builder);
646 CHECK(mode == DoubleToStringConverter::PRECISION);
647 conv.ToPrecision(value, numDigits, &builder);
650 const size_t length = builder.position();
652 result->append(buffer, length);
656 * As above, but for floating point
658 template <class Tgt, class Src>
659 typename std::enable_if<
660 std::is_floating_point<Src>::value
661 && IsSomeString<Tgt>::value>::type
662 toAppend(Src value, Tgt * result) {
664 value, result, double_conversion::DoubleToStringConverter::SHORTEST, 0);
668 * Upper bound of the length of the output from
669 * DoubleToStringConverter::ToShortest(double, StringBuilder*),
670 * as used in toAppend(double, string*).
673 typename std::enable_if<
674 std::is_floating_point<Src>::value, size_t>::type
675 estimateSpaceNeeded(Src value) {
676 // kBase10MaximalLength is 17. We add 1 for decimal point,
677 // e.g. 10.0/9 is 17 digits and 18 characters, including the decimal point.
678 constexpr int kMaxMantissaSpace =
679 double_conversion::DoubleToStringConverter::kBase10MaximalLength + 1;
680 // strlen("E-") + digits10(numeric_limits<double>::max_exponent10)
681 constexpr int kMaxExponentSpace = 2 + 3;
682 static const int kMaxPositiveSpace = std::max({
683 // E.g. 1.1111111111111111E-100.
684 kMaxMantissaSpace + kMaxExponentSpace,
685 // E.g. 0.000001.1111111111111111, if kConvMaxDecimalInShortestLow is -6.
686 kMaxMantissaSpace - detail::kConvMaxDecimalInShortestLow,
687 // If kConvMaxDecimalInShortestHigh is 21, then 1e21 is the smallest
688 // number > 1 which ToShortest outputs in exponential notation,
689 // so 21 is the longest non-exponential number > 1.
690 detail::kConvMaxDecimalInShortestHigh
692 return kMaxPositiveSpace + (value < 0); // +1 for minus sign, if negative
696 * This can be specialized, together with adding specialization
697 * for estimateSpaceNeed for your type, so that we allocate
698 * as much as you need instead of the default
701 struct HasLengthEstimator : std::false_type {};
704 constexpr typename std::enable_if<
705 !std::is_fundamental<Src>::value
706 #ifdef FOLLY_HAVE_INT128_T
707 // On OSX 10.10, is_fundamental<__int128> is false :-O
708 && !std::is_same<__int128, Src>::value
709 && !std::is_same<unsigned __int128, Src>::value
711 && !IsSomeString<Src>::value
712 && !std::is_convertible<Src, const char*>::value
713 && !std::is_convertible<Src, StringPiece>::value
714 && !std::is_enum<Src>::value
715 && !HasLengthEstimator<Src>::value,
717 estimateSpaceNeeded(const Src&) {
718 return sizeof(Src) + 1; // dumbest best effort ever?
724 typename std::enable_if<IsSomeString<Tgt>::value, size_t>::type
725 estimateSpaceToReserve(size_t sofar, Tgt*) {
729 template <class T, class... Ts>
730 size_t estimateSpaceToReserve(size_t sofar, const T& v, const Ts&... vs) {
731 return estimateSpaceToReserve(sofar + estimateSpaceNeeded(v), vs...);
735 void reserveInTarget(const Ts&...vs) {
736 getLastElement(vs...)->reserve(estimateSpaceToReserve(0, vs...));
739 template<class Delimiter, class...Ts>
740 void reserveInTargetDelim(const Delimiter& d, const Ts&...vs) {
741 static_assert(sizeof...(vs) >= 2, "Needs at least 2 args");
742 size_t fordelim = (sizeof...(vs) - 2) *
743 estimateSpaceToReserve(0, d, static_cast<std::string*>(nullptr));
744 getLastElement(vs...)->reserve(estimateSpaceToReserve(fordelim, vs...));
748 * Variadic base case: append one element
750 template <class T, class Tgt>
751 typename std::enable_if<
752 IsSomeString<typename std::remove_pointer<Tgt>::type>
754 toAppendStrImpl(const T& v, Tgt result) {
758 template <class T, class... Ts>
759 typename std::enable_if<sizeof...(Ts) >= 2
761 typename std::remove_pointer<
762 typename detail::last_element<Ts...>::type
763 >::type>::value>::type
764 toAppendStrImpl(const T& v, const Ts&... vs) {
765 toAppend(v, getLastElement(vs...));
766 toAppendStrImpl(vs...);
769 template <class Delimiter, class T, class Tgt>
770 typename std::enable_if<
771 IsSomeString<typename std::remove_pointer<Tgt>::type>::value>::type
772 toAppendDelimStrImpl(const Delimiter& /* delim */, const T& v, Tgt result) {
776 template <class Delimiter, class T, class... Ts>
777 typename std::enable_if<sizeof...(Ts) >= 2
779 typename std::remove_pointer<
780 typename detail::last_element<Ts...>::type
781 >::type>::value>::type
782 toAppendDelimStrImpl(const Delimiter& delim, const T& v, const Ts&... vs) {
783 // we are really careful here, calling toAppend with just one element does
784 // not try to estimate space needed (as we already did that). If we call
785 // toAppend(v, delim, ....) we would do unnecesary size calculation
786 toAppend(v, detail::getLastElement(vs...));
787 toAppend(delim, detail::getLastElement(vs...));
788 toAppendDelimStrImpl(delim, vs...);
794 * Variadic conversion to string. Appends each element in turn.
795 * If we have two or more things to append, we it will not reserve
796 * the space for them and will depend on strings exponential growth.
797 * If you just append once consider using toAppendFit which reserves
798 * the space needed (but does not have exponential as a result).
800 * Custom implementations of toAppend() can be provided in the same namespace as
801 * the type to customize printing. estimateSpaceNeed() may also be provided to
802 * avoid reallocations in toAppendFit():
804 * namespace other_namespace {
806 * template <class String>
807 * void toAppend(const OtherType&, String* out);
810 * size_t estimateSpaceNeeded(const OtherType&);
814 template <class... Ts>
815 typename std::enable_if<sizeof...(Ts) >= 3
817 typename std::remove_pointer<
818 typename detail::last_element<Ts...>::type
819 >::type>::value>::type
820 toAppend(const Ts&... vs) {
821 ::folly::detail::toAppendStrImpl(vs...);
825 // Special case pid_t on MSVC, because it's a void* rather than an
826 // integral type. We can't do a global special case because this is already
827 // dangerous enough (as most pointers will implicitly convert to a void*)
828 // just doing it for MSVC.
830 void toAppend(const pid_t a, Tgt* res) {
831 toAppend(uint64_t(a), res);
836 * Special version of the call that preallocates exaclty as much memory
837 * as need for arguments to be stored in target. This means we are
838 * not doing exponential growth when we append. If you are using it
839 * in a loop you are aiming at your foot with a big perf-destroying
841 * On the other hand if you are appending to a string once, this
842 * will probably save a few calls to malloc.
844 template <class... Ts>
845 typename std::enable_if<
847 typename std::remove_pointer<
848 typename detail::last_element<Ts...>::type
849 >::type>::value>::type
850 toAppendFit(const Ts&... vs) {
851 ::folly::detail::reserveInTarget(vs...);
856 void toAppendFit(const Ts&) {}
859 * Variadic base case: do nothing.
862 typename std::enable_if<IsSomeString<Tgt>::value>::type toAppend(
863 Tgt* /* result */) {}
866 * Variadic base case: do nothing.
868 template <class Delimiter, class Tgt>
869 typename std::enable_if<IsSomeString<Tgt>::value>::type toAppendDelim(
870 const Delimiter& /* delim */, Tgt* /* result */) {}
873 * 1 element: same as toAppend.
875 template <class Delimiter, class T, class Tgt>
876 typename std::enable_if<IsSomeString<Tgt>::value>::type toAppendDelim(
877 const Delimiter& /* delim */, const T& v, Tgt* tgt) {
882 * Append to string with a delimiter in between elements. Check out
883 * comments for toAppend for details about memory allocation.
885 template <class Delimiter, class... Ts>
886 typename std::enable_if<sizeof...(Ts) >= 3
888 typename std::remove_pointer<
889 typename detail::last_element<Ts...>::type
890 >::type>::value>::type
891 toAppendDelim(const Delimiter& delim, const Ts&... vs) {
892 detail::toAppendDelimStrImpl(delim, vs...);
896 * Detail in comment for toAppendFit
898 template <class Delimiter, class... Ts>
899 typename std::enable_if<
901 typename std::remove_pointer<
902 typename detail::last_element<Ts...>::type
903 >::type>::value>::type
904 toAppendDelimFit(const Delimiter& delim, const Ts&... vs) {
905 detail::reserveInTargetDelim(delim, vs...);
906 toAppendDelim(delim, vs...);
909 template <class De, class Ts>
910 void toAppendDelimFit(const De&, const Ts&) {}
913 * to<SomeString>(v1, v2, ...) uses toAppend() (see below) as back-end
916 template <class Tgt, class... Ts>
917 typename std::enable_if<
918 IsSomeString<Tgt>::value && (
919 sizeof...(Ts) != 1 ||
920 !std::is_same<Tgt, typename detail::last_element<Ts...>::type>::value),
922 to(const Ts&... vs) {
924 toAppendFit(vs..., &result);
929 * toDelim<SomeString>(SomeString str) returns itself.
931 template <class Tgt, class Delim, class Src>
932 typename std::enable_if<IsSomeString<Tgt>::value &&
933 std::is_same<Tgt, Src>::value,
935 toDelim(const Delim& /* delim */, const Src& value) {
940 * toDelim<SomeString>(delim, v1, v2, ...) uses toAppendDelim() as
941 * back-end for all types.
943 template <class Tgt, class Delim, class... Ts>
944 typename std::enable_if<
945 IsSomeString<Tgt>::value && (
946 sizeof...(Ts) != 1 ||
947 !std::is_same<Tgt, typename detail::last_element<Ts...>::type>::value),
949 toDelim(const Delim& delim, const Ts&... vs) {
951 toAppendDelimFit(delim, vs..., &result);
955 /*******************************************************************************
956 * Conversions from string types to integral types.
957 ******************************************************************************/
962 * Finds the first non-digit in a string. The number of digits
963 * searched depends on the precision of the Tgt integral. Assumes the
964 * string starts with NO whitespace and NO sign.
966 * The semantics of the routine is:
968 * if (b >= e || !isdigit(*b)) return b;
971 * Complete unrolling marks bottom-line (i.e. entire conversion)
972 * improvements of 20%.
975 const char* findFirstNonDigit(const char* b, const char* e) {
977 auto const c = static_cast<unsigned>(*b) - '0';
984 bool str_to_bool(StringPiece* src);
985 float str_to_float(StringPiece* src);
986 double str_to_double(StringPiece* src);
989 Tgt digits_to(const char* b, const char* e);
991 extern template unsigned char digits_to<unsigned char>(const char* b,
993 extern template unsigned short digits_to<unsigned short>(const char* b,
995 extern template unsigned int digits_to<unsigned int>(const char* b,
997 extern template unsigned long digits_to<unsigned long>(const char* b,
999 extern template unsigned long long digits_to<unsigned long long>(
1000 const char* b, const char* e);
1001 #if FOLLY_HAVE_INT128_T
1002 extern template unsigned __int128 digits_to<unsigned __int128>(const char* b,
1006 } // namespace detail
1009 * String represented as a pair of pointers to char to unsigned
1010 * integrals. Assumes NO whitespace before or after.
1012 template <class Tgt>
1013 typename std::enable_if<
1014 std::is_integral<Tgt>::value && !std::is_signed<Tgt>::value
1015 && !std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
1017 to(const char * b, const char * e) {
1018 return detail::digits_to<Tgt>(b, e);
1022 * String represented as a pair of pointers to char to signed
1023 * integrals. Assumes NO whitespace before or after. Allows an
1024 * optional leading sign.
1026 template <class Tgt>
1027 typename std::enable_if<
1028 std::is_integral<Tgt>::value && std::is_signed<Tgt>::value,
1030 to(const char * b, const char * e) {
1031 FOLLY_RANGE_CHECK(b < e, "Empty input string in conversion to integral",
1032 to<std::string>("b: ", intptr_t(b), " e: ", intptr_t(e)));
1035 Tgt result = -to<typename std::make_unsigned<Tgt>::type>(b + 1, e);
1036 FOLLY_RANGE_CHECK_BEGIN_END(result <= 0, "Negative overflow.", b, e);
1039 FOLLY_RANGE_CHECK_BEGIN_END(*b == '+', "Invalid lead character", b, e);
1042 Tgt result = to<typename std::make_unsigned<Tgt>::type>(b, e);
1043 FOLLY_RANGE_CHECK_BEGIN_END(result >= 0, "Overflow", b, e);
1049 * StringPiece to integrals, with progress information. Alters the
1050 * StringPiece parameter to munch the already-parsed characters.
1052 template <class Tgt>
1053 Tgt str_to_integral(StringPiece* src) {
1054 auto b = src->data(), past = src->data() + src->size();
1056 FOLLY_RANGE_CHECK_STRINGPIECE(b < past,
1057 "No digits found in input string", *src);
1058 if (!isspace(*b)) break;
1063 // First digit is customized because we test for sign
1064 bool negative = false;
1065 /* static */ if (std::is_signed<Tgt>::value) {
1070 FOLLY_RANGE_CHECK_STRINGPIECE(*m == '+', "Invalid leading character in "
1071 "conversion to integral", *src);
1077 FOLLY_RANGE_CHECK_STRINGPIECE(m < past, "No digits found in input string",
1079 FOLLY_RANGE_CHECK_STRINGPIECE(isdigit(*m), "Non-digit character found", *src);
1080 m = detail::findFirstNonDigit<Tgt>(m + 1, past);
1083 /* static */ if (!std::is_signed<Tgt>::value) {
1084 result = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
1086 auto t = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
1089 FOLLY_RANGE_CHECK_STRINGPIECE(is_non_positive(result),
1090 "Negative overflow", *src);
1093 FOLLY_RANGE_CHECK_STRINGPIECE(is_non_negative(result), "Overflow", *src);
1096 src->advance(m - src->data());
1101 * Enforce that the suffix following a number is made up only of whitespace.
1103 inline void enforceWhitespace(StringPiece sp) {
1104 for (char ch : sp) {
1105 FOLLY_RANGE_CHECK_STRINGPIECE(
1106 isspace(ch), to<std::string>("Non-whitespace: ", ch), sp);
1110 /*******************************************************************************
1111 * Conversions from string types to floating-point types.
1112 ******************************************************************************/
1115 } // namespace detail
1118 * StringPiece to bool, with progress information. Alters the
1119 * StringPiece parameter to munch the already-parsed characters.
1121 inline void parseTo(StringPiece* src, bool& out) {
1122 out = detail::str_to_bool(src);
1126 * Parsing strings to numeric types. These routines differ from
1127 * parseTo(str, numeric) routines in that they take a POINTER TO a StringPiece
1128 * and alter that StringPiece to reflect progress information.
1130 template <class Tgt>
1131 typename std::enable_if<
1132 std::is_integral<typename std::remove_cv<Tgt>::type>::value>::type
1133 parseTo(StringPiece* src, Tgt& out) {
1134 out = detail::str_to_integral<Tgt>(src);
1137 inline void parseTo(StringPiece* src, float& out) {
1138 out = detail::str_to_float(src);
1141 inline void parseTo(StringPiece* src, double& out) {
1142 out = detail::str_to_double(src);
1145 template <class Tgt>
1146 typename std::enable_if<
1147 std::is_floating_point<Tgt>::value ||
1148 std::is_integral<typename std::remove_cv<Tgt>::type>::value>::type
1149 parseTo(StringPiece src, Tgt& out) {
1151 detail::enforceWhitespace(src);
1154 /*******************************************************************************
1155 * Integral to floating point and back
1156 ******************************************************************************/
1161 * Check if a floating point value can safely be converted to an
1162 * integer value without triggering undefined behaviour.
1164 template <typename Tgt, typename Src>
1165 inline typename std::enable_if<
1166 std::is_floating_point<Src>::value && std::is_integral<Tgt>::value &&
1167 !std::is_same<Tgt, bool>::value,
1169 checkConversion(const Src& value) {
1170 constexpr Src tgtMaxAsSrc = static_cast<Src>(std::numeric_limits<Tgt>::max());
1171 constexpr Src tgtMinAsSrc = static_cast<Src>(std::numeric_limits<Tgt>::min());
1172 if (value >= tgtMaxAsSrc) {
1173 if (value > tgtMaxAsSrc) {
1176 const Src mmax = folly::nextafter(tgtMaxAsSrc, Src());
1177 if (static_cast<Tgt>(value - mmax) >
1178 std::numeric_limits<Tgt>::max() - static_cast<Tgt>(mmax)) {
1181 } else if (std::is_signed<Tgt>::value && value <= tgtMinAsSrc) {
1182 if (value < tgtMinAsSrc) {
1185 const Src mmin = folly::nextafter(tgtMinAsSrc, Src());
1186 if (static_cast<Tgt>(value - mmin) <
1187 std::numeric_limits<Tgt>::min() - static_cast<Tgt>(mmin)) {
1194 // Integers can always safely be converted to floating point values
1195 template <typename Tgt, typename Src>
1196 constexpr typename std::enable_if<
1197 std::is_integral<Src>::value && std::is_floating_point<Tgt>::value,
1199 checkConversion(const Src&) {
1203 // Also, floating point values can always be safely converted to bool
1204 // Per the standard, any floating point value that is not zero will yield true
1205 template <typename Tgt, typename Src>
1206 constexpr typename std::enable_if<
1207 std::is_floating_point<Src>::value && std::is_same<Tgt, bool>::value,
1209 checkConversion(const Src&) {
1215 * Checked conversion from integral to floating point and back. The
1216 * result must be convertible back to the source type without loss of
1217 * precision. This seems Draconian but sometimes is what's needed, and
1218 * complements existing routines nicely. For various rounding
1219 * routines, see <math>.
1221 template <class Tgt, class Src>
1222 typename std::enable_if<
1223 (std::is_integral<Src>::value && std::is_floating_point<Tgt>::value)
1225 (std::is_floating_point<Src>::value && std::is_integral<Tgt>::value),
1227 to(const Src & value) {
1228 if (detail::checkConversion<Tgt>(value)) {
1229 Tgt result = Tgt(value);
1230 if (detail::checkConversion<Src>(result)) {
1231 auto witness = static_cast<Src>(result);
1232 if (value == witness) {
1237 throw std::range_error(
1238 to<std::string>("to<>: loss of precision when converting ", value,
1239 #ifdef FOLLY_HAS_RTTI
1240 " to type ", typeid(Tgt).name()
1247 /*******************************************************************************
1248 * Custom Conversions
1250 * Any type can be used with folly::to by implementing parseTo. The
1251 * implementation should be provided in the namespace of the type to facilitate
1252 * argument-dependent lookup:
1254 * namespace other_namespace {
1255 * void parseTo(::folly::StringPiece, OtherType&);
1257 ******************************************************************************/
1259 typename std::enable_if<std::is_enum<T>::value>::type
1260 parseTo(StringPiece in, T& out) {
1261 typename std::underlying_type<T>::type tmp;
1263 out = static_cast<T>(tmp);
1266 inline void parseTo(StringPiece in, StringPiece& out) {
1270 inline void parseTo(StringPiece in, std::string& out) {
1272 out.append(in.data(), in.size());
1275 inline void parseTo(StringPiece in, fbstring& out) {
1277 out.append(in.data(), in.size());
1281 * String or StringPiece to target conversion. Accepts leading and trailing
1282 * whitespace, but no non-space trailing characters.
1285 template <class Tgt>
1286 typename std::enable_if<!std::is_same<StringPiece, Tgt>::value, Tgt>::type
1287 to(StringPiece src) {
1289 parseTo(src, result);
1293 template <class Tgt>
1294 Tgt to(StringPiece* src) {
1296 parseTo(src, result);
1300 /*******************************************************************************
1301 * Enum to anything and back
1302 ******************************************************************************/
1304 template <class Tgt, class Src>
1305 typename std::enable_if<
1306 std::is_enum<Src>::value && !std::is_same<Src, Tgt>::value, Tgt>::type
1307 to(const Src & value) {
1308 return to<Tgt>(static_cast<typename std::underlying_type<Src>::type>(value));
1311 template <class Tgt, class Src>
1312 typename std::enable_if<
1313 std::is_enum<Tgt>::value && !std::is_same<Src, Tgt>::value, Tgt>::type
1314 to(const Src & value) {
1315 return static_cast<Tgt>(to<typename std::underlying_type<Tgt>::type>(value));
1318 } // namespace folly
1320 // FOLLY_CONV_INTERNAL is defined by Conv.cpp. Keep the FOLLY_RANGE_CHECK
1321 // macro for use in Conv.cpp, but #undefine it everywhere else we are included,
1322 // to avoid defining this global macro name in other files that include Conv.h.
1323 #ifndef FOLLY_CONV_INTERNAL
1324 #undef FOLLY_RANGE_CHECK
1325 #undef FOLLY_RANGE_CHECK_BEGIN_END
1326 #undef FOLLY_RANGE_CHECK_STRINGPIECE
1327 #undef FOLLY_RANGE_CHECK_STRINGIZE
1328 #undef FOLLY_RANGE_CHECK_STRINGIZE2