2 * Copyright 2012 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>
40 #include "double-conversion.h" // V8 JavaScript implementation
42 #define FOLLY_RANGE_CHECK(condition, message) \
43 ((condition) ? (void)0 : throw std::range_error( \
44 (__FILE__ "(" + std::to_string((long long int) __LINE__) + "): " \
45 + (message)).c_str()))
49 /*******************************************************************************
50 * Integral to integral
51 ******************************************************************************/
54 * Checked conversion from integral to integral. The checks are only
55 * performed when meaningful, e.g. conversion from int to long goes
58 template <class Tgt, class Src>
59 typename std::enable_if<
60 std::is_integral<Src>::value && std::is_integral<Tgt>::value,
62 to(const Src & value) {
63 /* static */ if (std::numeric_limits<Tgt>::max()
64 < std::numeric_limits<Src>::max()) {
65 FOLLY_RANGE_CHECK(value <= std::numeric_limits<Tgt>::max(),
68 /* static */ if (std::is_signed<Src>::value &&
69 (!std::is_signed<Tgt>::value || sizeof(Src) > sizeof(Tgt))) {
70 FOLLY_RANGE_CHECK(value >= std::numeric_limits<Tgt>::min(),
73 return static_cast<Tgt>(value);
76 /*******************************************************************************
77 * Floating point to floating point
78 ******************************************************************************/
80 template <class Tgt, class Src>
81 typename std::enable_if<
82 std::is_floating_point<Tgt>::value && std::is_floating_point<Src>::value,
84 to(const Src & value) {
85 /* static */ if (std::numeric_limits<Tgt>::max() <
86 std::numeric_limits<Src>::max()) {
87 FOLLY_RANGE_CHECK(value <= std::numeric_limits<Tgt>::max(),
89 FOLLY_RANGE_CHECK(value >= -std::numeric_limits<Tgt>::max(),
92 return boost::implicit_cast<Tgt>(value);
95 /*******************************************************************************
97 ******************************************************************************/
101 template <class T> struct IsSomeString {
102 enum { value = std::is_same<T, std::string>::value
103 || std::is_same<T, fbstring>::value };
107 const T& getLastElement(const T & v) {
111 template <class T, class... Ts>
112 typename std::tuple_element<
114 std::tuple<T, Ts...> >::type const&
115 getLastElement(const T& v, const Ts&... vs) {
116 return getLastElement(vs...);
119 /*******************************************************************************
120 * Conversions from integral types to string types.
121 ******************************************************************************/
123 // Returns the offset of the formatted string from the start of
124 // the supplied buffer. The new string will be at range
125 // [buf+begin,buf+bufLen). Uint will be either uint32_t or uint64_t.
126 template <class Uint>
127 size_t uintToBuffer(char*const buffer, size_t bufLen, Uint v) {
128 extern const char digit1[101], digit2[101];
132 buffer[--bufLen] = static_cast<char>(v + '0');
134 size_t r = static_cast<size_t>(v);
136 buffer[bufLen] = digit1[r];
137 buffer[bufLen + 1] = digit2[r];
143 size_t r = static_cast<size_t> (t - v * 100);
145 buffer[bufLen] = digit1[r];
146 buffer[bufLen + 1] = digit2[r];
151 const size_t kMaxInt64BufLen = 21;// 19 + 1 for possible '-' sign + 1 for \0
153 } // namespace detail
156 * A single char gets appended.
159 void toAppend(char value, Tgt * result) {
164 * Everything implicitly convertible to const char* gets appended.
166 template <class Tgt, class Src>
167 typename std::enable_if<
168 std::is_convertible<Src, const char*>::value
169 && detail::IsSomeString<Tgt>::value>::type
170 toAppend(Src value, Tgt * result) {
171 // Treat null pointers like an empty string, as in:
172 // operator<<(std::ostream&, const char*).
173 const char* c = value;
175 result->append(value);
180 * Strings get appended, too.
182 template <class Tgt, class Src>
183 typename std::enable_if<
184 detail::IsSomeString<Src>::value && detail::IsSomeString<Tgt>::value>::type
185 toAppend(const Src& value, Tgt * result) {
186 result->append(value);
190 * and StringPiece objects too
193 typename std::enable_if<
194 detail::IsSomeString<Tgt>::value>::type
195 toAppend(StringPiece value, Tgt * result) {
196 result->append(value.data(), value.size());
200 * There's no implicit conversion from fbstring to other string types,
201 * so make a specialization.
204 typename std::enable_if<
205 detail::IsSomeString<Tgt>::value>::type
206 toAppend(const fbstring& value, Tgt * result) {
207 result->append(value.data(), value.size());
211 * int32_t and int64_t to string (by appending) go through here. The
212 * result is APPENDED to a preexisting string passed as the second
213 * parameter. For convenience, the function also returns a reference
214 * to *result. This should be efficient with fbstring because fbstring
215 * incurs no dynamic allocation below 23 bytes and no number has more
216 * than 22 bytes in its textual representation (20 for digits, one for
217 * sign, one for the terminating 0).
219 template <class Tgt, class Src>
220 typename std::enable_if<
221 std::is_integral<Src>::value && std::is_signed<Src>::value
222 && detail::IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
223 toAppend(Src value, Tgt * result) {
224 typedef typename std::make_unsigned<Src>::type Usrc;
225 char buffer[detail::kMaxInt64BufLen];
228 begin = detail::uintToBuffer(buffer, sizeof(buffer),
229 static_cast<Usrc>(-value));
231 buffer[--begin] = '-';
233 begin = detail::uintToBuffer(buffer, sizeof(buffer),
234 static_cast<Usrc>(value));
236 result->append(buffer + begin, buffer + sizeof(buffer));
240 * As above, but for uint32_t and uint64_t.
242 template <class Tgt, class Src>
243 typename std::enable_if<
244 std::is_integral<Src>::value && !std::is_signed<Src>::value
245 && detail::IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
246 toAppend(Src value, Tgt * result) {
247 char buffer[detail::kMaxInt64BufLen];
248 const size_t begin = detail::uintToBuffer(buffer, sizeof(buffer), value);
249 result->append(buffer + begin, buffer + sizeof(buffer));
253 * All small signed and unsigned integers to string go through 32-bit
254 * types int32_t and uint32_t, respectively.
256 template <class Tgt, class Src>
257 typename std::enable_if<
258 std::is_integral<Src>::value
259 && detail::IsSomeString<Tgt>::value && sizeof(Src) < 4>::type
260 toAppend(Src value, Tgt * result) {
262 std::conditional<std::is_signed<Src>::value, int64_t, uint64_t>::type
264 toAppend<Tgt>(static_cast<Intermediate>(value), result);
268 * Enumerated values get appended as integers.
270 template <class Tgt, class Src>
271 typename std::enable_if<
272 std::is_enum<Src>::value && detail::IsSomeString<Tgt>::value>::type
273 toAppend(Src value, Tgt * result) {
274 /* static */ if (Src(-1) < 0) {
275 /* static */ if (sizeof(Src) <= sizeof(int)) {
276 toAppend(static_cast<int>(value), result);
278 toAppend(static_cast<long>(value), result);
281 /* static */ if (sizeof(Src) <= sizeof(int)) {
282 toAppend(static_cast<unsigned int>(value), result);
284 toAppend(static_cast<unsigned long>(value), result);
289 /*******************************************************************************
290 * Conversions from floating-point types to string types.
291 ******************************************************************************/
293 /** Wrapper around DoubleToStringConverter **/
294 template <class Tgt, class Src>
295 typename std::enable_if<
296 std::is_floating_point<Src>::value
297 && detail::IsSomeString<Tgt>::value>::type
301 double_conversion::DoubleToStringConverter::DtoaMode mode,
302 unsigned int numDigits) {
303 using namespace double_conversion;
304 DoubleToStringConverter
305 conv(DoubleToStringConverter::NO_FLAGS,
306 "infinity", "NaN", 'E',
307 -6, // decimal in shortest low
308 21, // decimal in shortest high
309 6, // max leading padding zeros
310 1); // max trailing padding zeros
312 StringBuilder builder(buffer, sizeof(buffer));
314 case DoubleToStringConverter::SHORTEST:
315 conv.ToShortest(value, &builder);
317 case DoubleToStringConverter::FIXED:
318 conv.ToFixed(value, numDigits, &builder);
321 CHECK(mode == DoubleToStringConverter::PRECISION);
322 conv.ToPrecision(value, numDigits, &builder);
325 const size_t length = builder.position();
327 result->append(buffer, length);
331 * As above, but for floating point
333 template <class Tgt, class Src>
334 typename std::enable_if<
335 std::is_floating_point<Src>::value
336 && detail::IsSomeString<Tgt>::value>::type
337 toAppend(Src value, Tgt * result) {
339 value, result, double_conversion::DoubleToStringConverter::SHORTEST, 0);
343 * Variadic conversion to string. Appends each element in turn.
345 template <class T, class... Ts>
346 typename std::enable_if<sizeof...(Ts) >= 2
347 && detail::IsSomeString<
348 typename std::remove_pointer<
349 typename std::tuple_element<
350 sizeof...(Ts) - 1, std::tuple<Ts...>
351 >::type>::type>::value>::type
352 toAppend(const T& v, const Ts&... vs) {
353 toAppend(v, detail::getLastElement(vs...));
358 * Variadic base case: do nothing.
361 typename std::enable_if<detail::IsSomeString<Tgt>::value>::type
362 toAppend(Tgt* result) {
366 * to<SomeString>(v1, v2, ...) uses toAppend() (see below) as back-end
369 template <class Tgt, class... Ts>
370 typename std::enable_if<detail::IsSomeString<Tgt>::value, Tgt>::type
371 to(const Ts&... vs) {
373 toAppend(vs..., &result);
377 /*******************************************************************************
378 * Conversions from string types to integral types.
379 ******************************************************************************/
384 * Finds the first non-digit in a string. The number of digits
385 * searched depends on the precision of the Tgt integral. Assumes the
386 * string starts with NO whitespace and NO sign.
388 * The semantics of the routine is:
390 * if (b >= e || !isdigit(*b)) return b;
393 * Complete unrolling marks bottom-line (i.e. entire conversion)
394 * improvements of 20%.
397 const char* findFirstNonDigit(const char* b, const char* e) {
399 auto const c = static_cast<unsigned>(*b) - '0';
405 // Maximum value of number when represented as a string
406 template <class T> struct MaxString {
407 static const char*const value;
411 * String represented as a pair of pointers to char to unsigned
412 * integrals. Assumes NO whitespace before or after, and also that the
413 * string is composed entirely of digits. Tgt must be unsigned, and no
414 * sign is allowed in the string (even it's '+'). String may be empty,
415 * in which case digits_to throws.
418 Tgt digits_to(const char * b, const char * e) {
420 static_assert(!std::is_signed<Tgt>::value, "Unsigned type expected");
423 const size_t size = e - b;
425 /* Although the string is entirely made of digits, we still need to
426 * check for overflow.
428 if (size >= std::numeric_limits<Tgt>::digits10 + 1) {
429 // Leading zeros? If so, recurse to keep things simple
430 if (b < e && *b == '0') {
432 if (b == e) return 0; // just zeros, e.g. "0000"
433 if (*b != '0') return digits_to<Tgt>(b, e);
436 FOLLY_RANGE_CHECK(size == std::numeric_limits<Tgt>::digits10 + 1 &&
437 strncmp(b, detail::MaxString<Tgt>::value, size) <= 0,
438 "Numeric overflow upon conversion");
441 // Here we know that the number won't overflow when
442 // converted. Proceed without checks.
444 static const Tgt power10[20] = {
445 static_cast<Tgt>(10000000000000000000UL),
446 static_cast<Tgt>(1000000000000000000UL),
447 static_cast<Tgt>(100000000000000000UL),
448 static_cast<Tgt>(10000000000000000UL),
449 static_cast<Tgt>(1000000000000000UL),
450 static_cast<Tgt>(100000000000000UL),
451 static_cast<Tgt>(10000000000000UL),
452 static_cast<Tgt>(1000000000000UL),
453 static_cast<Tgt>(100000000000UL),
454 static_cast<Tgt>(10000000000UL),
455 static_cast<Tgt>(1000000000UL),
456 static_cast<Tgt>(100000000UL),
457 static_cast<Tgt>(10000000UL),
458 static_cast<Tgt>(1000000UL),
459 static_cast<Tgt>(100000UL),
460 static_cast<Tgt>(10000UL),
461 static_cast<Tgt>(1000UL),
462 static_cast<Tgt>(100UL),
463 static_cast<Tgt>(10UL),
464 static_cast<Tgt>(1UL),
467 size_t powIdx = sizeof(power10) / sizeof(*power10) - size;
470 for (; e - b >= 4; b += 4, powIdx += 4) {
471 const auto c0 = static_cast<unsigned>(*b) - '0';
472 if (c0 >= 10) goto failure;
473 const auto r0 = power10[powIdx] * c0;
474 const auto c1 = static_cast<unsigned>(b[1]) - '0';
475 if (c1 >= 10) goto failure;
476 const auto r1 = power10[powIdx + 1] * c1;
477 const auto c2 = static_cast<unsigned>(b[2]) - '0';
478 if (c2 >= 10) goto failure;
479 const auto r2 = power10[powIdx + 2] * c2;
480 const auto c3 = static_cast<unsigned>(b[3]) - '0';
481 if (c3 >= 10) goto failure;
482 const auto r3 = power10[powIdx + 3] * c3;
483 result += r0 + r1 + r2 + r3;
488 const auto c0 = static_cast<unsigned>(*b) - '0';
489 if (c0 >= 10) goto failure;
490 const auto c1 = static_cast<unsigned>(b[1]) - '0';
491 if (c1 >= 10) goto failure;
492 const auto c2 = static_cast<unsigned>(b[2]) - '0';
493 if (c2 >= 10) goto failure;
494 return result + 100 * c0 + 10 * c1 + c2;
497 const auto c0 = static_cast<unsigned>(*b) - '0';
498 if (c0 >= 10) goto failure;
499 const auto c1 = static_cast<unsigned>(b[1]) - '0';
500 if (c1 >= 10) goto failure;
501 return result + 10 * c0 + c1;
504 const auto c0 = static_cast<unsigned>(*b) - '0';
505 if (c0 >= 10) goto failure;
511 FOLLY_RANGE_CHECK(size > 0, "Found no digits to convert in input");
515 throw std::range_error("Cannot convert string " +
516 std::string(e - size, e) + " to integral.");
519 bool str_to_bool(StringPiece * src);
521 } // namespace detail
524 * String represented as a pair of pointers to char to unsigned
525 * integrals. Assumes NO whitespace before or after.
528 typename std::enable_if<
529 std::is_integral<Tgt>::value && !std::is_signed<Tgt>::value
530 && !std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
532 to(const char * b, const char * e) {
533 return detail::digits_to<Tgt>(b, e);
537 * String represented as a pair of pointers to char to signed
538 * integrals. Assumes NO whitespace before or after. Allows an
539 * optional leading sign.
542 typename std::enable_if<
543 std::is_integral<Tgt>::value && std::is_signed<Tgt>::value,
545 to(const char * b, const char * e) {
546 FOLLY_RANGE_CHECK(b < e, "Empty input string in conversion to integral");
549 Tgt result = -to<typename std::make_unsigned<Tgt>::type>(b + 1, e);
550 FOLLY_RANGE_CHECK(result <= 0, "Negative overflow.");
553 FOLLY_RANGE_CHECK(*b == '+', "Invalid lead character");
556 Tgt result = to<typename std::make_unsigned<Tgt>::type>(b, e);
557 FOLLY_RANGE_CHECK(result >= 0, "Overflow.");
562 * Parsing strings to integrals. These routines differ from
563 * to<integral>(string) in that they take a POINTER TO a StringPiece
564 * and alter that StringPiece to reflect progress information.
568 * StringPiece to integrals, with progress information. Alters the
569 * StringPiece parameter to munch the already-parsed characters.
572 typename std::enable_if<
573 std::is_integral<Tgt>::value
574 && !std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
576 to(StringPiece * src) {
578 auto b = src->data(), past = src->data() + src->size();
580 FOLLY_RANGE_CHECK(b < past, "No digits found in input string");
581 if (!isspace(*b)) break;
586 // First digit is customized because we test for sign
587 bool negative = false;
588 /* static */ if (std::is_signed<Tgt>::value) {
593 FOLLY_RANGE_CHECK(*m == '+', "Invalid leading character in conversion"
600 FOLLY_RANGE_CHECK(m < past, "No digits found in input string");
601 FOLLY_RANGE_CHECK(isdigit(*m), "Non-digit character found");
602 m = detail::findFirstNonDigit<Tgt>(m + 1, past);
605 /* static */ if (!std::is_signed<Tgt>::value) {
606 result = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
608 auto t = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
611 FOLLY_RANGE_CHECK(result <= 0, "Negative overflow");
614 FOLLY_RANGE_CHECK(result >= 0, "Overflow");
617 src->advance(m - src->data());
622 * StringPiece to bool, with progress information. Alters the
623 * StringPiece parameter to munch the already-parsed characters.
626 typename std::enable_if<
627 std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
629 to(StringPiece * src) {
630 return detail::str_to_bool(src);
636 * Enforce that the suffix following a number is made up only of whitespace.
638 inline void enforceWhitespace(const char* b, const char* e) {
639 for (; b != e; ++b) {
640 FOLLY_RANGE_CHECK(isspace(*b), to<std::string>("Non-whitespace: ", *b));
644 } // namespace detail
647 * String or StringPiece to integrals. Accepts leading and trailing
648 * whitespace, but no non-space trailing characters.
651 typename std::enable_if<
652 std::is_integral<Tgt>::value,
654 to(StringPiece src) {
655 Tgt result = to<Tgt>(&src);
656 detail::enforceWhitespace(src.data(), src.data() + src.size());
660 /*******************************************************************************
661 * Conversions from string types to floating-point types.
662 ******************************************************************************/
665 * StringPiece to double, with progress information. Alters the
666 * StringPiece parameter to munch the already-parsed characters.
669 inline typename std::enable_if<
670 std::is_floating_point<Tgt>::value,
672 to(StringPiece *const src) {
673 using namespace double_conversion;
674 static StringToDoubleConverter
675 conv(StringToDoubleConverter::ALLOW_TRAILING_JUNK
676 | StringToDoubleConverter::ALLOW_LEADING_SPACES,
678 // return this for junk input string
679 std::numeric_limits<double>::quiet_NaN(),
682 FOLLY_RANGE_CHECK(!src->empty(), "No digits found in input string");
685 auto result = conv.StringToDouble(src->data(), src->size(),
686 &length); // processed char count
688 if (!std::isnan(result)) {
689 src->advance(length);
693 for (;; src->advance(1)) {
695 throw std::range_error("Unable to convert an empty string"
696 " to a floating point value.");
698 if (!isspace(src->front())) {
703 // Was that "inf[inity]"?
704 if (src->size() >= 3 && toupper((*src)[0]) == 'I'
705 && toupper((*src)[1]) == 'N' && toupper((*src)[2]) == 'F') {
706 if (src->size() >= 8 &&
707 toupper((*src)[3]) == 'I' &&
708 toupper((*src)[4]) == 'N' &&
709 toupper((*src)[5]) == 'I' &&
710 toupper((*src)[6]) == 'T' &&
711 toupper((*src)[7]) == 'Y') {
716 return std::numeric_limits<Tgt>::infinity();
719 // Was that "-inf[inity]"?
720 if (src->size() >= 4 && toupper((*src)[0]) == '-'
721 && toupper((*src)[1]) == 'I' && toupper((*src)[2]) == 'N'
722 && toupper((*src)[3]) == 'F') {
723 if (src->size() >= 9 &&
724 toupper((*src)[4]) == 'I' &&
725 toupper((*src)[5]) == 'N' &&
726 toupper((*src)[6]) == 'I' &&
727 toupper((*src)[7]) == 'T' &&
728 toupper((*src)[8]) == 'Y') {
733 return -std::numeric_limits<Tgt>::infinity();
737 if (src->size() >= 3 && toupper((*src)[0]) == 'N'
738 && toupper((*src)[1]) == 'A' && toupper((*src)[2]) == 'N') {
740 return std::numeric_limits<Tgt>::quiet_NaN();
744 throw std::range_error("Unable to convert \"" + src->toString()
745 + "\" to a floating point value.");
749 * Any string, const char*, or StringPiece to double.
752 typename std::enable_if<
753 std::is_floating_point<Tgt>::value,
755 to(StringPiece src) {
756 Tgt result = to<double>(&src);
757 detail::enforceWhitespace(src.data(), src.data() + src.size());
761 /*******************************************************************************
762 * Integral to floating point and back
763 ******************************************************************************/
766 * Checked conversion from integral to flating point and back. The
767 * result must be convertible back to the source type without loss of
768 * precision. This seems Draconian but sometimes is what's needed, and
769 * complements existing routines nicely. For various rounding
770 * routines, see <math>.
772 template <class Tgt, class Src>
773 typename std::enable_if<
774 (std::is_integral<Src>::value && std::is_floating_point<Tgt>::value)
776 (std::is_floating_point<Src>::value && std::is_integral<Tgt>::value),
778 to(const Src & value) {
780 auto witness = static_cast<Src>(result);
781 if (value != witness) {
782 throw std::range_error(
783 to<std::string>("to<>: loss of precision when converting ", value,
784 " to type ", typeid(Tgt).name()).c_str());
789 /*******************************************************************************
790 * Enum to anything and back
791 ******************************************************************************/
793 template <class Tgt, class Src>
794 typename std::enable_if<std::is_enum<Src>::value, Tgt>::type
795 to(const Src & value) {
796 // TODO: uncomment this when underlying_type is available
797 // return to<Tgt>(static_cast<typename std::underlying_type<Src>::type>(
799 /* static */ if (Src(-1) < 0) {
800 /* static */ if (sizeof(Src) <= sizeof(int)) {
801 return to<Tgt>(static_cast<int>(value));
803 return to<Tgt>(static_cast<long>(value));
806 /* static */ if (sizeof(Src) <= sizeof(int)) {
807 return to<Tgt>(static_cast<unsigned int>(value));
809 return to<Tgt>(static_cast<unsigned long>(value));
814 template <class Tgt, class Src>
815 typename std::enable_if<std::is_enum<Tgt>::value, Tgt>::type
816 to(const Src & value) {
817 // TODO: uncomment this when underlying_type is available
818 // return static_cast<Tgt>(
819 // to<typename std::underlying_type<Tgt>::type>(value));
820 /* static */ if (Tgt(-1) < 0) {
821 /* static */ if (sizeof(Tgt) <= sizeof(int)) {
822 return static_cast<Tgt>(to<int>(value));
824 return static_cast<Tgt>(to<long>(value));
827 /* static */ if (sizeof(Tgt) <= sizeof(int)) {
828 return static_cast<Tgt>(to<unsigned int>(value));
830 return static_cast<Tgt>(to<unsigned long>(value));
837 // FOLLY_CONV_INTERNAL is defined by Conv.cpp. Keep the FOLLY_RANGE_CHECK
838 // macro for use in Conv.cpp, but #undefine it everywhere else we are included,
839 // to avoid defining this global macro name in other files that include Conv.h.
840 #ifndef FOLLY_CONV_INTERNAL
841 #undef FOLLY_RANGE_CHECK
844 #endif /* FOLLY_BASE_CONV_H_ */