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)
34 #include <type_traits>
38 #include <boost/implicit_cast.hpp>
39 #include <double-conversion/double-conversion.h> // V8 JavaScript implementation
41 #include <folly/Demangle.h>
42 #include <folly/Expected.h>
43 #include <folly/FBString.h>
44 #include <folly/Likely.h>
45 #include <folly/Range.h>
46 #include <folly/Unit.h>
47 #include <folly/portability/Math.h>
51 // Keep this in sync with kErrorStrings in Conv.cpp
52 enum class ConversionCode : unsigned char {
62 STRING_TO_FLOAT_ERROR,
63 NON_WHITESPACE_AFTER_END,
64 ARITH_POSITIVE_OVERFLOW,
65 ARITH_NEGATIVE_OVERFLOW,
66 ARITH_LOSS_OF_PRECISION,
67 NUM_ERROR_CODES, // has to be the last entry
70 struct ConversionErrorBase : std::range_error {
71 using std::range_error::range_error;
74 class ConversionError : public ConversionErrorBase {
76 ConversionError(const std::string& str, ConversionCode code)
77 : ConversionErrorBase(str), code_(code) {}
79 ConversionError(const char* str, ConversionCode code)
80 : ConversionErrorBase(str), code_(code) {}
82 ConversionCode errorCode() const {
90 /*******************************************************************************
91 * Custom Error Translation
93 * Your overloaded parseTo() function can return a custom error code on failure.
94 * ::folly::to() will call makeConversionError to translate that error code into
95 * an object to throw. makeConversionError is found by argument-dependent
96 * lookup. It should have this signature:
98 * namespace other_namespace {
99 * enum YourErrorCode { BAD_ERROR, WORSE_ERROR };
101 * struct YourConversionError : ConversionErrorBase {
102 * YourConversionError(const char* what) : ConversionErrorBase(what) {}
105 * YourConversionError
106 * makeConversionError(YourErrorCode code, ::folly::StringPiece sp) {
108 * return YourConversionError(messageString);
110 ******************************************************************************/
111 ConversionError makeConversionError(ConversionCode code, StringPiece sp);
115 * Enforce that the suffix following a number is made up only of whitespace.
117 inline ConversionCode enforceWhitespaceErr(StringPiece sp) {
119 if (UNLIKELY(!std::isspace(c))) {
120 return ConversionCode::NON_WHITESPACE_AFTER_END;
123 return ConversionCode::SUCCESS;
127 * Keep this implementation around for prettyToDouble().
129 inline void enforceWhitespace(StringPiece sp) {
130 auto err = enforceWhitespaceErr(sp);
131 if (err != ConversionCode::SUCCESS) {
132 throw makeConversionError(err, sp);
138 * The identity conversion function.
139 * tryTo<T>(T) returns itself for all types T.
141 template <class Tgt, class Src>
142 typename std::enable_if<
143 std::is_same<Tgt, typename std::decay<Src>::type>::value,
144 Expected<Tgt, ConversionCode>>::type
146 return std::forward<Src>(value);
149 template <class Tgt, class Src>
150 typename std::enable_if<
151 std::is_same<Tgt, typename std::decay<Src>::type>::value,
154 return std::forward<Src>(value);
157 /*******************************************************************************
158 * Arithmetic to boolean
159 ******************************************************************************/
162 * Unchecked conversion from arithmetic to boolean. This is different from the
163 * other arithmetic conversions because we use the C convention of treating any
164 * non-zero value as true, instead of range checking.
166 template <class Tgt, class Src>
167 typename std::enable_if<
168 std::is_arithmetic<Src>::value && !std::is_same<Tgt, Src>::value &&
169 std::is_same<Tgt, bool>::value,
170 Expected<Tgt, ConversionCode>>::type
171 tryTo(const Src& value) {
172 return value != Src();
175 template <class Tgt, class Src>
176 typename std::enable_if<
177 std::is_arithmetic<Src>::value && !std::is_same<Tgt, Src>::value &&
178 std::is_same<Tgt, bool>::value,
180 to(const Src& value) {
181 return value != Src();
184 /*******************************************************************************
186 ******************************************************************************/
190 template <typename... Ts>
191 struct LastElementImpl {
192 static void call(Ignored<Ts>...) {}
195 template <typename Head, typename... Ts>
196 struct LastElementImpl<Head, Ts...> {
197 template <typename Last>
198 static Last call(Ignored<Ts>..., Last&& last) {
199 return std::forward<Last>(last);
203 template <typename... Ts>
204 auto getLastElement(const Ts&... ts)
205 -> decltype(LastElementImpl<Ts...>::call(ts...)) {
206 return LastElementImpl<Ts...>::call(ts...);
209 template <class... Ts>
210 struct LastElement : std::decay<decltype(
211 LastElementImpl<Ts...>::call(std::declval<Ts>()...))> {
214 } // namespace detail
216 /*******************************************************************************
217 * Conversions from integral types to string types.
218 ******************************************************************************/
220 #if FOLLY_HAVE_INT128_T
223 template <typename IntegerType>
224 constexpr unsigned int
226 return (unsigned int)(ceil(sizeof(IntegerType) * CHAR_BIT * M_LN2 / M_LN10));
230 unsafeTelescope128(char * buffer, size_t room, unsigned __int128 x) {
231 typedef unsigned __int128 Usrc;
234 while (x >= (Usrc(1) << 64)) { // Using 128-bit division while needed
235 const auto y = x / 10;
236 const auto digit = x % 10;
238 buffer[p--] = '0' + digit;
242 uint64_t xx = x; // Moving to faster 64-bit division thereafter
245 const auto y = xx / 10ULL;
246 const auto digit = xx % 10ULL;
248 buffer[p--] = '0' + digit;
252 buffer[p] = '0' + xx;
261 * Returns the number of digits in the base 10 representation of an
262 * uint64_t. Useful for preallocating buffers and such. It's also used
263 * internally, see below. Measurements suggest that defining a
264 * separate overload for 32-bit integers is not worthwhile.
267 inline uint32_t digits10(uint64_t v) {
270 // For this arch we can get a little help from specialized CPU instructions
271 // which can count leading zeroes; 64 minus that is appx. log (base 2).
272 // Use that to approximate base-10 digits (log_10) and then adjust if needed.
274 // 10^i, defined for i 0 through 19.
275 // This is 20 * 8 == 160 bytes, which fits neatly into 5 cache lines
276 // (assuming a cache line size of 64).
277 static const uint64_t powersOf10[20] FOLLY_ALIGNED(64) = {
297 10000000000000000000UL,
300 // "count leading zeroes" operation not valid; for 0; special case this.
305 // bits is in the ballpark of log_2(v).
306 const uint8_t leadingZeroes = __builtin_clzll(v);
307 const auto bits = 63 - leadingZeroes;
309 // approximate log_10(v) == log_10(2) * bits.
310 // Integer magic below: 77/256 is appx. 0.3010 (log_10(2)).
311 // The +1 is to make this the ceiling of the log_10 estimate.
312 const uint32_t minLength = 1 + ((bits * 77) >> 8);
314 // return that log_10 lower bound, plus adjust if input >= 10^(that bound)
315 // in case there's a small error and we misjudged length.
316 return minLength + (uint32_t) (UNLIKELY (v >= powersOf10[minLength]));
322 if (LIKELY(v < 10)) return result;
323 if (LIKELY(v < 100)) return result + 1;
324 if (LIKELY(v < 1000)) return result + 2;
325 if (LIKELY(v < 10000)) return result + 3;
326 // Skip ahead by 4 orders of magnitude
335 * Copies the ASCII base 10 representation of v into buffer and
336 * returns the number of bytes written. Does NOT append a \0. Assumes
337 * the buffer points to digits10(v) bytes of valid memory. Note that
338 * uint64 needs at most 20 bytes, uint32_t needs at most 10 bytes,
339 * uint16_t needs at most 5 bytes, and so on. Measurements suggest
340 * that defining a separate overload for 32-bit integers is not
343 * This primitive is unsafe because it makes the size assumption and
344 * because it does not add a terminating \0.
347 inline uint32_t uint64ToBufferUnsafe(uint64_t v, char *const buffer) {
348 auto const result = digits10(v);
349 // WARNING: using size_t or pointer arithmetic for pos slows down
350 // the loop below 20x. This is because several 32-bit ops can be
351 // done in parallel, but only fewer 64-bit ones.
352 uint32_t pos = result - 1;
354 // Keep these together so a peephole optimization "sees" them and
355 // computes them in one shot.
356 auto const q = v / 10;
357 auto const r = static_cast<uint32_t>(v % 10);
358 buffer[pos--] = '0' + r;
361 // Last digit is trivial to handle
362 buffer[pos] = static_cast<uint32_t>(v) + '0';
367 * A single char gets appended.
370 void toAppend(char value, Tgt * result) {
375 constexpr typename std::enable_if<
376 std::is_same<T, char>::value,
378 estimateSpaceNeeded(T) {
383 * Everything implicitly convertible to const char* gets appended.
385 template <class Tgt, class Src>
386 typename std::enable_if<
387 std::is_convertible<Src, const char*>::value
388 && IsSomeString<Tgt>::value>::type
389 toAppend(Src value, Tgt * result) {
390 // Treat null pointers like an empty string, as in:
391 // operator<<(std::ostream&, const char*).
392 const char* c = value;
394 result->append(value);
399 typename std::enable_if<std::is_convertible<Src, const char*>::value, size_t>::
401 estimateSpaceNeeded(Src value) {
402 const char *c = value;
404 return folly::StringPiece(value).size();
410 typename std::enable_if<
411 (std::is_convertible<Src, folly::StringPiece>::value ||
412 IsSomeString<Src>::value) &&
413 !std::is_convertible<Src, const char*>::value,
415 estimateSpaceNeeded(Src value) {
416 return folly::StringPiece(value).size();
420 inline size_t estimateSpaceNeeded(std::nullptr_t /* value */) {
425 typename std::enable_if<
426 std::is_pointer<Src>::value &&
427 IsSomeString<std::remove_pointer<Src>>::value,
429 estimateSpaceNeeded(Src value) {
430 return value->size();
434 * Strings get appended, too.
436 template <class Tgt, class Src>
437 typename std::enable_if<
438 IsSomeString<Src>::value && IsSomeString<Tgt>::value>::type
439 toAppend(const Src& value, Tgt * result) {
440 result->append(value);
444 * and StringPiece objects too
447 typename std::enable_if<
448 IsSomeString<Tgt>::value>::type
449 toAppend(StringPiece value, Tgt * result) {
450 result->append(value.data(), value.size());
454 * There's no implicit conversion from fbstring to other string types,
455 * so make a specialization.
458 typename std::enable_if<
459 IsSomeString<Tgt>::value>::type
460 toAppend(const fbstring& value, Tgt * result) {
461 result->append(value.data(), value.size());
464 #if FOLLY_HAVE_INT128_T
466 * Special handling for 128 bit integers.
471 toAppend(__int128 value, Tgt * result) {
472 typedef unsigned __int128 Usrc;
473 char buffer[detail::digitsEnough<unsigned __int128>() + 1];
477 p = detail::unsafeTelescope128(buffer, sizeof(buffer), -Usrc(value));
480 p = detail::unsafeTelescope128(buffer, sizeof(buffer), value);
483 result->append(buffer + p, buffer + sizeof(buffer));
488 toAppend(unsigned __int128 value, Tgt * result) {
489 char buffer[detail::digitsEnough<unsigned __int128>()];
492 p = detail::unsafeTelescope128(buffer, sizeof(buffer), value);
494 result->append(buffer + p, buffer + sizeof(buffer));
498 constexpr typename std::enable_if<
499 std::is_same<T, __int128>::value,
501 estimateSpaceNeeded(T) {
502 return detail::digitsEnough<__int128>();
506 constexpr typename std::enable_if<
507 std::is_same<T, unsigned __int128>::value,
509 estimateSpaceNeeded(T) {
510 return detail::digitsEnough<unsigned __int128>();
516 * int32_t and int64_t to string (by appending) go through here. The
517 * result is APPENDED to a preexisting string passed as the second
518 * parameter. This should be efficient with fbstring because fbstring
519 * incurs no dynamic allocation below 23 bytes and no number has more
520 * than 22 bytes in its textual representation (20 for digits, one for
521 * sign, one for the terminating 0).
523 template <class Tgt, class Src>
524 typename std::enable_if<
525 std::is_integral<Src>::value && std::is_signed<Src>::value &&
526 IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
527 toAppend(Src value, Tgt * result) {
530 result->push_back('-');
531 result->append(buffer, uint64ToBufferUnsafe(-uint64_t(value), buffer));
533 result->append(buffer, uint64ToBufferUnsafe(value, buffer));
538 typename std::enable_if<
539 std::is_integral<Src>::value && std::is_signed<Src>::value
540 && sizeof(Src) >= 4 && sizeof(Src) < 16,
542 estimateSpaceNeeded(Src value) {
544 // When "value" is the smallest negative, negating it would evoke
545 // undefined behavior, so, instead of writing "-value" below, we write
546 // "~static_cast<uint64_t>(value) + 1"
547 return 1 + digits10(~static_cast<uint64_t>(value) + 1);
550 return digits10(static_cast<uint64_t>(value));
554 * As above, but for uint32_t and uint64_t.
556 template <class Tgt, class Src>
557 typename std::enable_if<
558 std::is_integral<Src>::value && !std::is_signed<Src>::value
559 && IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
560 toAppend(Src value, Tgt * result) {
562 result->append(buffer, uint64ToBufferUnsafe(value, buffer));
566 typename std::enable_if<
567 std::is_integral<Src>::value && !std::is_signed<Src>::value
568 && sizeof(Src) >= 4 && sizeof(Src) < 16,
570 estimateSpaceNeeded(Src value) {
571 return digits10(value);
575 * All small signed and unsigned integers to string go through 32-bit
576 * types int32_t and uint32_t, respectively.
578 template <class Tgt, class Src>
579 typename std::enable_if<
580 std::is_integral<Src>::value
581 && IsSomeString<Tgt>::value && sizeof(Src) < 4>::type
582 toAppend(Src value, Tgt * result) {
584 std::conditional<std::is_signed<Src>::value, int64_t, uint64_t>::type
586 toAppend<Tgt>(static_cast<Intermediate>(value), result);
590 typename std::enable_if<
591 std::is_integral<Src>::value
593 && !std::is_same<Src, char>::value,
595 estimateSpaceNeeded(Src value) {
597 std::conditional<std::is_signed<Src>::value, int64_t, uint64_t>::type
599 return estimateSpaceNeeded(static_cast<Intermediate>(value));
603 * Enumerated values get appended as integers.
605 template <class Tgt, class Src>
606 typename std::enable_if<
607 std::is_enum<Src>::value && IsSomeString<Tgt>::value>::type
608 toAppend(Src value, Tgt * result) {
610 static_cast<typename std::underlying_type<Src>::type>(value), result);
614 typename std::enable_if<
615 std::is_enum<Src>::value, size_t>::type
616 estimateSpaceNeeded(Src value) {
617 return estimateSpaceNeeded(
618 static_cast<typename std::underlying_type<Src>::type>(value));
621 /*******************************************************************************
622 * Conversions from floating-point types to string types.
623 ******************************************************************************/
626 constexpr int kConvMaxDecimalInShortestLow = -6;
627 constexpr int kConvMaxDecimalInShortestHigh = 21;
630 /** Wrapper around DoubleToStringConverter **/
631 template <class Tgt, class Src>
632 typename std::enable_if<
633 std::is_floating_point<Src>::value
634 && IsSomeString<Tgt>::value>::type
638 double_conversion::DoubleToStringConverter::DtoaMode mode,
639 unsigned int numDigits) {
640 using namespace double_conversion;
641 DoubleToStringConverter
642 conv(DoubleToStringConverter::NO_FLAGS,
643 "Infinity", "NaN", 'E',
644 detail::kConvMaxDecimalInShortestLow,
645 detail::kConvMaxDecimalInShortestHigh,
646 6, // max leading padding zeros
647 1); // max trailing padding zeros
649 StringBuilder builder(buffer, sizeof(buffer));
651 case DoubleToStringConverter::SHORTEST:
652 conv.ToShortest(value, &builder);
654 case DoubleToStringConverter::FIXED:
655 conv.ToFixed(value, numDigits, &builder);
658 CHECK(mode == DoubleToStringConverter::PRECISION);
659 conv.ToPrecision(value, numDigits, &builder);
662 const size_t length = builder.position();
664 result->append(buffer, length);
668 * As above, but for floating point
670 template <class Tgt, class Src>
671 typename std::enable_if<
672 std::is_floating_point<Src>::value
673 && IsSomeString<Tgt>::value>::type
674 toAppend(Src value, Tgt * result) {
676 value, result, double_conversion::DoubleToStringConverter::SHORTEST, 0);
680 * Upper bound of the length of the output from
681 * DoubleToStringConverter::ToShortest(double, StringBuilder*),
682 * as used in toAppend(double, string*).
685 typename std::enable_if<
686 std::is_floating_point<Src>::value, size_t>::type
687 estimateSpaceNeeded(Src value) {
688 // kBase10MaximalLength is 17. We add 1 for decimal point,
689 // e.g. 10.0/9 is 17 digits and 18 characters, including the decimal point.
690 constexpr int kMaxMantissaSpace =
691 double_conversion::DoubleToStringConverter::kBase10MaximalLength + 1;
692 // strlen("E-") + digits10(numeric_limits<double>::max_exponent10)
693 constexpr int kMaxExponentSpace = 2 + 3;
694 static const int kMaxPositiveSpace = std::max({
695 // E.g. 1.1111111111111111E-100.
696 kMaxMantissaSpace + kMaxExponentSpace,
697 // E.g. 0.000001.1111111111111111, if kConvMaxDecimalInShortestLow is -6.
698 kMaxMantissaSpace - detail::kConvMaxDecimalInShortestLow,
699 // If kConvMaxDecimalInShortestHigh is 21, then 1e21 is the smallest
700 // number > 1 which ToShortest outputs in exponential notation,
701 // so 21 is the longest non-exponential number > 1.
702 detail::kConvMaxDecimalInShortestHigh
704 return kMaxPositiveSpace + (value < 0); // +1 for minus sign, if negative
708 * This can be specialized, together with adding specialization
709 * for estimateSpaceNeed for your type, so that we allocate
710 * as much as you need instead of the default
713 struct HasLengthEstimator : std::false_type {};
716 constexpr typename std::enable_if<
717 !std::is_fundamental<Src>::value
718 #if FOLLY_HAVE_INT128_T
719 // On OSX 10.10, is_fundamental<__int128> is false :-O
720 && !std::is_same<__int128, Src>::value
721 && !std::is_same<unsigned __int128, Src>::value
723 && !IsSomeString<Src>::value
724 && !std::is_convertible<Src, const char*>::value
725 && !std::is_convertible<Src, StringPiece>::value
726 && !std::is_enum<Src>::value
727 && !HasLengthEstimator<Src>::value,
729 estimateSpaceNeeded(const Src&) {
730 return sizeof(Src) + 1; // dumbest best effort ever?
736 typename std::enable_if<IsSomeString<Tgt>::value, size_t>::type
737 estimateSpaceToReserve(size_t sofar, Tgt*) {
741 template <class T, class... Ts>
742 size_t estimateSpaceToReserve(size_t sofar, const T& v, const Ts&... vs) {
743 return estimateSpaceToReserve(sofar + estimateSpaceNeeded(v), vs...);
747 void reserveInTarget(const Ts&...vs) {
748 getLastElement(vs...)->reserve(estimateSpaceToReserve(0, vs...));
751 template<class Delimiter, class...Ts>
752 void reserveInTargetDelim(const Delimiter& d, const Ts&...vs) {
753 static_assert(sizeof...(vs) >= 2, "Needs at least 2 args");
754 size_t fordelim = (sizeof...(vs) - 2) *
755 estimateSpaceToReserve(0, d, static_cast<std::string*>(nullptr));
756 getLastElement(vs...)->reserve(estimateSpaceToReserve(fordelim, vs...));
760 * Variadic base case: append one element
762 template <class T, class Tgt>
763 typename std::enable_if<
764 IsSomeString<typename std::remove_pointer<Tgt>::type>
766 toAppendStrImpl(const T& v, Tgt result) {
770 template <class T, class... Ts>
771 typename std::enable_if<
772 sizeof...(Ts) >= 2 &&
773 IsSomeString<typename std::remove_pointer<
774 typename detail::LastElement<const Ts&...>::type>::type>::value>::type
775 toAppendStrImpl(const T& v, const Ts&... vs) {
776 toAppend(v, getLastElement(vs...));
777 toAppendStrImpl(vs...);
780 template <class Delimiter, class T, class Tgt>
781 typename std::enable_if<
782 IsSomeString<typename std::remove_pointer<Tgt>::type>::value>::type
783 toAppendDelimStrImpl(const Delimiter& /* delim */, const T& v, Tgt result) {
787 template <class Delimiter, class T, class... Ts>
788 typename std::enable_if<
789 sizeof...(Ts) >= 2 &&
790 IsSomeString<typename std::remove_pointer<
791 typename detail::LastElement<const Ts&...>::type>::type>::value>::type
792 toAppendDelimStrImpl(const Delimiter& delim, const T& v, const Ts&... vs) {
793 // we are really careful here, calling toAppend with just one element does
794 // not try to estimate space needed (as we already did that). If we call
795 // toAppend(v, delim, ....) we would do unnecesary size calculation
796 toAppend(v, detail::getLastElement(vs...));
797 toAppend(delim, detail::getLastElement(vs...));
798 toAppendDelimStrImpl(delim, vs...);
804 * Variadic conversion to string. Appends each element in turn.
805 * If we have two or more things to append, we it will not reserve
806 * the space for them and will depend on strings exponential growth.
807 * If you just append once consider using toAppendFit which reserves
808 * the space needed (but does not have exponential as a result).
810 * Custom implementations of toAppend() can be provided in the same namespace as
811 * the type to customize printing. estimateSpaceNeed() may also be provided to
812 * avoid reallocations in toAppendFit():
814 * namespace other_namespace {
816 * template <class String>
817 * void toAppend(const OtherType&, String* out);
820 * size_t estimateSpaceNeeded(const OtherType&);
824 template <class... Ts>
825 typename std::enable_if<
826 sizeof...(Ts) >= 3 &&
827 IsSomeString<typename std::remove_pointer<
828 typename detail::LastElement<const Ts&...>::type>::type>::value>::type
829 toAppend(const Ts&... vs) {
830 ::folly::detail::toAppendStrImpl(vs...);
834 // Special case pid_t on MSVC, because it's a void* rather than an
835 // integral type. We can't do a global special case because this is already
836 // dangerous enough (as most pointers will implicitly convert to a void*)
837 // just doing it for MSVC.
839 void toAppend(const pid_t a, Tgt* res) {
840 toAppend(uint64_t(a), res);
845 * Special version of the call that preallocates exaclty as much memory
846 * as need for arguments to be stored in target. This means we are
847 * not doing exponential growth when we append. If you are using it
848 * in a loop you are aiming at your foot with a big perf-destroying
850 * On the other hand if you are appending to a string once, this
851 * will probably save a few calls to malloc.
853 template <class... Ts>
854 typename std::enable_if<IsSomeString<typename std::remove_pointer<
855 typename detail::LastElement<const Ts&...>::type>::type>::value>::type
856 toAppendFit(const Ts&... vs) {
857 ::folly::detail::reserveInTarget(vs...);
862 void toAppendFit(const Ts&) {}
865 * Variadic base case: do nothing.
868 typename std::enable_if<IsSomeString<Tgt>::value>::type toAppend(
869 Tgt* /* result */) {}
872 * Variadic base case: do nothing.
874 template <class Delimiter, class Tgt>
875 typename std::enable_if<IsSomeString<Tgt>::value>::type toAppendDelim(
876 const Delimiter& /* delim */, Tgt* /* result */) {}
879 * 1 element: same as toAppend.
881 template <class Delimiter, class T, class Tgt>
882 typename std::enable_if<IsSomeString<Tgt>::value>::type toAppendDelim(
883 const Delimiter& /* delim */, const T& v, Tgt* tgt) {
888 * Append to string with a delimiter in between elements. Check out
889 * comments for toAppend for details about memory allocation.
891 template <class Delimiter, class... Ts>
892 typename std::enable_if<
893 sizeof...(Ts) >= 3 &&
894 IsSomeString<typename std::remove_pointer<
895 typename detail::LastElement<const Ts&...>::type>::type>::value>::type
896 toAppendDelim(const Delimiter& delim, const Ts&... vs) {
897 detail::toAppendDelimStrImpl(delim, vs...);
901 * Detail in comment for toAppendFit
903 template <class Delimiter, class... Ts>
904 typename std::enable_if<IsSomeString<typename std::remove_pointer<
905 typename detail::LastElement<const Ts&...>::type>::type>::value>::type
906 toAppendDelimFit(const Delimiter& delim, const Ts&... vs) {
907 detail::reserveInTargetDelim(delim, vs...);
908 toAppendDelim(delim, vs...);
911 template <class De, class Ts>
912 void toAppendDelimFit(const De&, const Ts&) {}
915 * to<SomeString>(v1, v2, ...) uses toAppend() (see below) as back-end
918 template <class Tgt, class... Ts>
919 typename std::enable_if<
920 IsSomeString<Tgt>::value &&
921 (sizeof...(Ts) != 1 ||
922 !std::is_same<Tgt, typename detail::LastElement<const Ts&...>::type>::
925 to(const Ts&... vs) {
927 toAppendFit(vs..., &result);
932 * toDelim<SomeString>(SomeString str) returns itself.
934 template <class Tgt, class Delim, class Src>
935 typename std::enable_if<IsSomeString<Tgt>::value &&
936 std::is_same<Tgt, Src>::value,
938 toDelim(const Delim& /* delim */, const Src& value) {
943 * toDelim<SomeString>(delim, v1, v2, ...) uses toAppendDelim() as
944 * back-end for all types.
946 template <class Tgt, class Delim, class... Ts>
947 typename std::enable_if<
948 IsSomeString<Tgt>::value &&
949 (sizeof...(Ts) != 1 ||
950 !std::is_same<Tgt, typename detail::LastElement<const Ts&...>::type>::
953 toDelim(const Delim& delim, const Ts&... vs) {
955 toAppendDelimFit(delim, vs..., &result);
959 /*******************************************************************************
960 * Conversions from string types to integral types.
961 ******************************************************************************/
965 Expected<bool, ConversionCode> str_to_bool(StringPiece* src) noexcept;
967 template <typename T>
968 Expected<T, ConversionCode> str_to_floating(StringPiece* src) noexcept;
970 extern template Expected<float, ConversionCode> str_to_floating<float>(
971 StringPiece* src) noexcept;
972 extern template Expected<double, ConversionCode> str_to_floating<double>(
973 StringPiece* src) noexcept;
976 Expected<Tgt, ConversionCode> digits_to(const char* b, const char* e) noexcept;
978 extern template Expected<char, ConversionCode> digits_to<char>(
980 const char*) noexcept;
981 extern template Expected<signed char, ConversionCode> digits_to<signed char>(
983 const char*) noexcept;
984 extern template Expected<unsigned char, ConversionCode>
985 digits_to<unsigned char>(const char*, const char*) noexcept;
987 extern template Expected<short, ConversionCode> digits_to<short>(
989 const char*) noexcept;
990 extern template Expected<unsigned short, ConversionCode>
991 digits_to<unsigned short>(const char*, const char*) noexcept;
993 extern template Expected<int, ConversionCode> digits_to<int>(
995 const char*) noexcept;
996 extern template Expected<unsigned int, ConversionCode> digits_to<unsigned int>(
998 const char*) noexcept;
1000 extern template Expected<long, ConversionCode> digits_to<long>(
1002 const char*) noexcept;
1003 extern template Expected<unsigned long, ConversionCode>
1004 digits_to<unsigned long>(const char*, const char*) noexcept;
1006 extern template Expected<long long, ConversionCode> digits_to<long long>(
1008 const char*) noexcept;
1009 extern template Expected<unsigned long long, ConversionCode>
1010 digits_to<unsigned long long>(const char*, const char*) noexcept;
1012 #if FOLLY_HAVE_INT128_T
1013 extern template Expected<__int128, ConversionCode> digits_to<__int128>(
1015 const char*) noexcept;
1016 extern template Expected<unsigned __int128, ConversionCode>
1017 digits_to<unsigned __int128>(const char*, const char*) noexcept;
1021 Expected<T, ConversionCode> str_to_integral(StringPiece* src) noexcept;
1023 extern template Expected<char, ConversionCode> str_to_integral<char>(
1024 StringPiece* src) noexcept;
1025 extern template Expected<signed char, ConversionCode>
1026 str_to_integral<signed char>(StringPiece* src) noexcept;
1027 extern template Expected<unsigned char, ConversionCode>
1028 str_to_integral<unsigned char>(StringPiece* src) noexcept;
1030 extern template Expected<short, ConversionCode> str_to_integral<short>(
1031 StringPiece* src) noexcept;
1032 extern template Expected<unsigned short, ConversionCode>
1033 str_to_integral<unsigned short>(StringPiece* src) noexcept;
1035 extern template Expected<int, ConversionCode> str_to_integral<int>(
1036 StringPiece* src) noexcept;
1037 extern template Expected<unsigned int, ConversionCode>
1038 str_to_integral<unsigned int>(StringPiece* src) noexcept;
1040 extern template Expected<long, ConversionCode> str_to_integral<long>(
1041 StringPiece* src) noexcept;
1042 extern template Expected<unsigned long, ConversionCode>
1043 str_to_integral<unsigned long>(StringPiece* src) noexcept;
1045 extern template Expected<long long, ConversionCode> str_to_integral<long long>(
1046 StringPiece* src) noexcept;
1047 extern template Expected<unsigned long long, ConversionCode>
1048 str_to_integral<unsigned long long>(StringPiece* src) noexcept;
1050 #if FOLLY_HAVE_INT128_T
1051 extern template Expected<__int128, ConversionCode> str_to_integral<__int128>(
1052 StringPiece* src) noexcept;
1053 extern template Expected<unsigned __int128, ConversionCode>
1054 str_to_integral<unsigned __int128>(StringPiece* src) noexcept;
1057 template <typename T>
1059 enable_if<std::is_same<T, bool>::value, Expected<T, ConversionCode>>::type
1060 convertTo(StringPiece* src) noexcept {
1061 return str_to_bool(src);
1064 template <typename T>
1065 typename std::enable_if<
1066 std::is_floating_point<T>::value,
1067 Expected<T, ConversionCode>>::type
1068 convertTo(StringPiece* src) noexcept {
1069 return str_to_floating<T>(src);
1072 template <typename T>
1073 typename std::enable_if<
1074 std::is_integral<T>::value && !std::is_same<T, bool>::value,
1075 Expected<T, ConversionCode>>::type
1076 convertTo(StringPiece* src) noexcept {
1077 return str_to_integral<T>(src);
1080 } // namespace detail
1083 * String represented as a pair of pointers to char to unsigned
1084 * integrals. Assumes NO whitespace before or after.
1086 template <typename Tgt>
1087 typename std::enable_if<
1088 std::is_integral<Tgt>::value && !std::is_same<Tgt, bool>::value,
1089 Expected<Tgt, ConversionCode>>::type
1090 tryTo(const char* b, const char* e) {
1091 return detail::digits_to<Tgt>(b, e);
1094 template <typename Tgt>
1095 typename std::enable_if<
1096 std::is_integral<Tgt>::value && !std::is_same<Tgt, bool>::value,
1098 to(const char* b, const char* e) {
1099 return tryTo<Tgt>(b, e).thenOrThrow(
1100 [](Tgt res) { return res; },
1101 [=](ConversionCode code) {
1102 return makeConversionError(code, StringPiece(b, e));
1106 /*******************************************************************************
1107 * Conversions from string types to arithmetic types.
1108 ******************************************************************************/
1111 * Parsing strings to numeric types.
1113 template <typename Tgt>
1114 FOLLY_WARN_UNUSED_RESULT inline typename std::enable_if<
1115 std::is_arithmetic<Tgt>::value,
1116 Expected<StringPiece, ConversionCode>>::type
1117 parseTo(StringPiece src, Tgt& out) {
1118 return detail::convertTo<Tgt>(&src).then(
1119 [&](Tgt res) { return void(out = res), src; });
1122 /*******************************************************************************
1123 * Integral / Floating Point to integral / Floating Point
1124 ******************************************************************************/
1129 * Checked conversion from integral to integral. The checks are only
1130 * performed when meaningful, e.g. conversion from int to long goes
1133 template <class Tgt, class Src>
1134 typename std::enable_if<
1135 std::is_integral<Src>::value && !std::is_same<Tgt, Src>::value &&
1136 !std::is_same<Tgt, bool>::value &&
1137 std::is_integral<Tgt>::value,
1138 Expected<Tgt, ConversionCode>>::type
1139 convertTo(const Src& value) noexcept {
1141 std::numeric_limits<Tgt>::max() < std::numeric_limits<Src>::max()) {
1142 if (greater_than<Tgt, std::numeric_limits<Tgt>::max()>(value)) {
1143 return makeUnexpected(ConversionCode::ARITH_POSITIVE_OVERFLOW);
1147 std::is_signed<Src>::value &&
1148 (!std::is_signed<Tgt>::value || sizeof(Src) > sizeof(Tgt))) {
1149 if (less_than<Tgt, std::numeric_limits<Tgt>::min()>(value)) {
1150 return makeUnexpected(ConversionCode::ARITH_NEGATIVE_OVERFLOW);
1153 return static_cast<Tgt>(value);
1157 * Checked conversion from floating to floating. The checks are only
1158 * performed when meaningful, e.g. conversion from float to double goes
1161 template <class Tgt, class Src>
1162 typename std::enable_if<
1163 std::is_floating_point<Tgt>::value && std::is_floating_point<Src>::value &&
1164 !std::is_same<Tgt, Src>::value,
1165 Expected<Tgt, ConversionCode>>::type
1166 convertTo(const Src& value) noexcept {
1168 std::numeric_limits<Tgt>::max() < std::numeric_limits<Src>::max()) {
1169 if (value > std::numeric_limits<Tgt>::max()) {
1170 return makeUnexpected(ConversionCode::ARITH_POSITIVE_OVERFLOW);
1172 if (value < std::numeric_limits<Tgt>::lowest()) {
1173 return makeUnexpected(ConversionCode::ARITH_NEGATIVE_OVERFLOW);
1176 return boost::implicit_cast<Tgt>(value);
1180 * Check if a floating point value can safely be converted to an
1181 * integer value without triggering undefined behaviour.
1183 template <typename Tgt, typename Src>
1184 inline typename std::enable_if<
1185 std::is_floating_point<Src>::value && std::is_integral<Tgt>::value &&
1186 !std::is_same<Tgt, bool>::value,
1188 checkConversion(const Src& value) {
1189 constexpr Src tgtMaxAsSrc = static_cast<Src>(std::numeric_limits<Tgt>::max());
1190 constexpr Src tgtMinAsSrc = static_cast<Src>(std::numeric_limits<Tgt>::min());
1191 if (value >= tgtMaxAsSrc) {
1192 if (value > tgtMaxAsSrc) {
1195 const Src mmax = folly::nextafter(tgtMaxAsSrc, Src());
1196 if (static_cast<Tgt>(value - mmax) >
1197 std::numeric_limits<Tgt>::max() - static_cast<Tgt>(mmax)) {
1200 } else if (std::is_signed<Tgt>::value && value <= tgtMinAsSrc) {
1201 if (value < tgtMinAsSrc) {
1204 const Src mmin = folly::nextafter(tgtMinAsSrc, Src());
1205 if (static_cast<Tgt>(value - mmin) <
1206 std::numeric_limits<Tgt>::min() - static_cast<Tgt>(mmin)) {
1213 // Integers can always safely be converted to floating point values
1214 template <typename Tgt, typename Src>
1215 constexpr typename std::enable_if<
1216 std::is_integral<Src>::value && std::is_floating_point<Tgt>::value,
1218 checkConversion(const Src&) {
1222 // Also, floating point values can always be safely converted to bool
1223 // Per the standard, any floating point value that is not zero will yield true
1224 template <typename Tgt, typename Src>
1225 constexpr typename std::enable_if<
1226 std::is_floating_point<Src>::value && std::is_same<Tgt, bool>::value,
1228 checkConversion(const Src&) {
1233 * Checked conversion from integral to floating point and back. The
1234 * result must be convertible back to the source type without loss of
1235 * precision. This seems Draconian but sometimes is what's needed, and
1236 * complements existing routines nicely. For various rounding
1237 * routines, see <math>.
1239 template <typename Tgt, typename Src>
1240 typename std::enable_if<
1241 (std::is_integral<Src>::value && std::is_floating_point<Tgt>::value) ||
1242 (std::is_floating_point<Src>::value && std::is_integral<Tgt>::value),
1243 Expected<Tgt, ConversionCode>>::type
1244 convertTo(const Src& value) noexcept {
1245 if (LIKELY(checkConversion<Tgt>(value))) {
1246 Tgt result = static_cast<Tgt>(value);
1247 if (LIKELY(checkConversion<Src>(result))) {
1248 Src witness = static_cast<Src>(result);
1249 if (LIKELY(value == witness)) {
1254 return makeUnexpected(ConversionCode::ARITH_LOSS_OF_PRECISION);
1257 template <typename Tgt, typename Src>
1258 inline std::string errorValue(const Src& value) {
1259 #ifdef FOLLY_HAS_RTTI
1260 return to<std::string>("(", demangle(typeid(Tgt)), ") ", value);
1262 return to<std::string>(value);
1266 template <typename Tgt, typename Src>
1267 using IsArithToArith = std::integral_constant<
1269 !std::is_same<Tgt, Src>::value && !std::is_same<Tgt, bool>::value &&
1270 std::is_arithmetic<Src>::value &&
1271 std::is_arithmetic<Tgt>::value>;
1273 } // namespace detail
1275 template <typename Tgt, typename Src>
1276 typename std::enable_if<
1277 detail::IsArithToArith<Tgt, Src>::value,
1278 Expected<Tgt, ConversionCode>>::type
1279 tryTo(const Src& value) noexcept {
1280 return detail::convertTo<Tgt>(value);
1283 template <typename Tgt, typename Src>
1284 typename std::enable_if<detail::IsArithToArith<Tgt, Src>::value, Tgt>::type to(
1286 return tryTo<Tgt>(value).thenOrThrow(
1287 [](Tgt res) { return res; },
1288 [&](ConversionCode e) {
1289 return makeConversionError(e, detail::errorValue<Tgt>(value));
1293 /*******************************************************************************
1294 * Custom Conversions
1296 * Any type can be used with folly::to by implementing parseTo. The
1297 * implementation should be provided in the namespace of the type to facilitate
1298 * argument-dependent lookup:
1300 * namespace other_namespace {
1301 * ::folly::Expected<::folly::StringPiece, SomeErrorCode>
1302 * parseTo(::folly::StringPiece, OtherType&) noexcept;
1304 ******************************************************************************/
1306 FOLLY_WARN_UNUSED_RESULT typename std::enable_if<
1307 std::is_enum<T>::value,
1308 Expected<StringPiece, ConversionCode>>::type
1309 parseTo(StringPiece in, T& out) noexcept {
1310 typename std::underlying_type<T>::type tmp{};
1311 auto restOrError = parseTo(in, tmp);
1312 out = static_cast<T>(tmp); // Harmless if parseTo fails
1316 FOLLY_WARN_UNUSED_RESULT
1317 inline Expected<StringPiece, ConversionCode> parseTo(
1319 StringPiece& out) noexcept {
1321 return StringPiece{in.end(), in.end()};
1324 FOLLY_WARN_UNUSED_RESULT
1325 inline Expected<StringPiece, ConversionCode> parseTo(
1329 out.append(in.data(), in.size()); // TODO try/catch?
1330 return StringPiece{in.end(), in.end()};
1333 FOLLY_WARN_UNUSED_RESULT
1334 inline Expected<StringPiece, ConversionCode> parseTo(
1338 out.append(in.data(), in.size()); // TODO try/catch?
1339 return StringPiece{in.end(), in.end()};
1343 template <typename Tgt>
1344 using ParseToResult = decltype(parseTo(StringPiece{}, std::declval<Tgt&>()));
1346 struct CheckTrailingSpace {
1347 Expected<Unit, ConversionCode> operator()(StringPiece sp) const {
1348 auto e = enforceWhitespaceErr(sp);
1349 if (UNLIKELY(e != ConversionCode::SUCCESS))
1350 return makeUnexpected(e);
1355 template <class Error>
1358 constexpr Expected<Unit, Error> operator()(T&&) const {
1363 // Older versions of the parseTo customization point threw on error and
1364 // returned void. Handle that.
1365 template <class Tgt>
1366 inline typename std::enable_if<
1367 std::is_void<ParseToResult<Tgt>>::value,
1368 Expected<StringPiece, ConversionCode>>::type
1369 parseToWrap(StringPiece sp, Tgt& out) {
1371 return StringPiece(sp.end(), sp.end());
1374 template <class Tgt>
1375 inline typename std::enable_if<
1376 !std::is_void<ParseToResult<Tgt>>::value,
1377 ParseToResult<Tgt>>::type
1378 parseToWrap(StringPiece sp, Tgt& out) {
1379 return parseTo(sp, out);
1382 template <typename Tgt>
1383 using ParseToError = ExpectedErrorType<decltype(
1384 detail::parseToWrap(StringPiece{}, std::declval<Tgt&>()))>;
1386 } // namespace detail
1389 * String or StringPiece to target conversion. Accepts leading and trailing
1390 * whitespace, but no non-space trailing characters.
1393 template <class Tgt>
1394 inline typename std::enable_if<
1395 !std::is_same<StringPiece, Tgt>::value,
1396 Expected<Tgt, detail::ParseToError<Tgt>>>::type
1397 tryTo(StringPiece src) {
1399 using Error = detail::ParseToError<Tgt>;
1400 using Check = typename std::conditional<
1401 std::is_arithmetic<Tgt>::value,
1402 detail::CheckTrailingSpace,
1403 detail::ReturnUnit<Error>>::type;
1404 return parseTo(src, result).then(Check(), [&](Unit) {
1405 return std::move(result);
1409 template <class Tgt>
1411 typename std::enable_if<!std::is_same<StringPiece, Tgt>::value, Tgt>::type
1412 to(StringPiece src) {
1414 using Error = detail::ParseToError<Tgt>;
1415 using Check = typename std::conditional<
1416 std::is_arithmetic<Tgt>::value,
1417 detail::CheckTrailingSpace,
1418 detail::ReturnUnit<Error>>::type;
1419 auto tmp = detail::parseToWrap(src, result);
1421 .thenOrThrow(Check(), [&](Error e) { throw makeConversionError(e, src); })
1423 [&](Unit) { return std::move(result); },
1424 [&](Error e) { throw makeConversionError(e, tmp.value()); });
1428 * tryTo/to that take the strings by pointer so the caller gets information
1429 * about how much of the string was consumed by the conversion. These do not
1430 * check for trailing whitepsace.
1432 template <class Tgt>
1433 Expected<Tgt, detail::ParseToError<Tgt>> tryTo(StringPiece* src) {
1435 return parseTo(*src, result).then([&, src](StringPiece sp) -> Tgt {
1437 return std::move(result);
1441 template <class Tgt>
1442 Tgt to(StringPiece* src) {
1444 using Error = detail::ParseToError<Tgt>;
1445 return parseTo(*src, result)
1447 [&, src](StringPiece sp) -> Tgt {
1449 return std::move(result);
1451 [=](Error e) { return makeConversionError(e, *src); });
1454 /*******************************************************************************
1455 * Enum to anything and back
1456 ******************************************************************************/
1458 template <class Tgt, class Src>
1459 typename std::enable_if<
1460 std::is_enum<Src>::value && !std::is_same<Src, Tgt>::value,
1461 Expected<Tgt, ConversionCode>>::type
1462 tryTo(const Src& value) {
1463 using I = typename std::underlying_type<Src>::type;
1464 return tryTo<Tgt>(static_cast<I>(value));
1467 template <class Tgt, class Src>
1468 typename std::enable_if<
1469 std::is_enum<Tgt>::value && !std::is_same<Src, Tgt>::value,
1471 tryTo(const Src& value) {
1472 using I = typename std::underlying_type<Tgt>::type;
1473 return tryTo<I>(value).then([](I i) { return static_cast<Tgt>(i); });
1476 template <class Tgt, class Src>
1477 typename std::enable_if<
1478 std::is_enum<Src>::value && !std::is_same<Src, Tgt>::value,
1480 to(const Src& value) {
1481 return to<Tgt>(static_cast<typename std::underlying_type<Src>::type>(value));
1484 template <class Tgt, class Src>
1485 typename std::enable_if<
1486 std::is_enum<Tgt>::value && !std::is_same<Src, Tgt>::value, Tgt>::type
1487 to(const Src & value) {
1488 return static_cast<Tgt>(to<typename std::underlying_type<Tgt>::type>(value));
1491 } // namespace folly