fbcode: __x__-protect all __attribute__ keywords, mechanically
[folly.git] / folly / Conv.h
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * Converts anything to anything, with an emphasis on performance and
19  * safety.
20  *
21  * @author Andrei Alexandrescu (andrei.alexandrescu@fb.com)
22  */
23
24 #ifndef FOLLY_BASE_CONV_H_
25 #define FOLLY_BASE_CONV_H_
26
27 #include <folly/FBString.h>
28 #include <folly/Likely.h>
29 #include <folly/Preprocessor.h>
30 #include <folly/Range.h>
31
32 #include <boost/implicit_cast.hpp>
33 #include <type_traits>
34 #include <limits>
35 #include <string>
36 #include <tuple>
37 #include <stdexcept>
38 #include <typeinfo>
39
40 #include <limits.h>
41
42 // V8 JavaScript implementation
43 #include <double-conversion/double-conversion.h>
44
45 #define FOLLY_RANGE_CHECK(condition, message)                           \
46   ((condition) ? (void)0 : throw std::range_error(                      \
47     (__FILE__ "(" + std::to_string((long long int) __LINE__) + "): "    \
48      + (message)).c_str()))
49
50 namespace folly {
51
52 /*******************************************************************************
53  * Integral to integral
54  ******************************************************************************/
55
56 /**
57  * Checked conversion from integral to integral. The checks are only
58  * performed when meaningful, e.g. conversion from int to long goes
59  * unchecked.
60  */
61 template <class Tgt, class Src>
62 typename std::enable_if<
63   std::is_integral<Src>::value && std::is_integral<Tgt>::value,
64   Tgt>::type
65 to(const Src & value) {
66   /* static */ if (std::numeric_limits<Tgt>::max()
67                    < std::numeric_limits<Src>::max()) {
68     FOLLY_RANGE_CHECK(
69       (!greater_than<Tgt, std::numeric_limits<Tgt>::max()>(value)),
70       "Overflow"
71     );
72   }
73   /* static */ if (std::is_signed<Src>::value &&
74                    (!std::is_signed<Tgt>::value || sizeof(Src) > sizeof(Tgt))) {
75     FOLLY_RANGE_CHECK(
76       (!less_than<Tgt, std::numeric_limits<Tgt>::min()>(value)),
77       "Negative overflow"
78     );
79   }
80   return static_cast<Tgt>(value);
81 }
82
83 /*******************************************************************************
84  * Floating point to floating point
85  ******************************************************************************/
86
87 template <class Tgt, class Src>
88 typename std::enable_if<
89   std::is_floating_point<Tgt>::value && std::is_floating_point<Src>::value,
90   Tgt>::type
91 to(const Src & value) {
92   /* static */ if (std::numeric_limits<Tgt>::max() <
93                    std::numeric_limits<Src>::max()) {
94     FOLLY_RANGE_CHECK(value <= std::numeric_limits<Tgt>::max(),
95                       "Overflow");
96     FOLLY_RANGE_CHECK(value >= -std::numeric_limits<Tgt>::max(),
97                       "Negative overflow");
98   }
99   return boost::implicit_cast<Tgt>(value);
100 }
101
102 /*******************************************************************************
103  * Anything to string
104  ******************************************************************************/
105
106 namespace detail {
107
108 template <class T>
109 const T& getLastElement(const T & v) {
110   return v;
111 }
112
113 template <class T, class... Ts>
114 typename std::tuple_element<
115   sizeof...(Ts),
116   std::tuple<T, Ts...> >::type const&
117   getLastElement(const T& v, const Ts&... vs) {
118   return getLastElement(vs...);
119 }
120
121 // This class exists to specialize away std::tuple_element in the case where we
122 // have 0 template arguments. Without this, Clang/libc++ will blow a
123 // static_assert even if tuple_element is protected by an enable_if.
124 template <class... Ts>
125 struct last_element {
126   typedef typename std::enable_if<
127     sizeof...(Ts) >= 1,
128     typename std::tuple_element<
129       sizeof...(Ts) - 1, std::tuple<Ts...>
130     >::type>::type type;
131 };
132
133 template <>
134 struct last_element<> {
135   typedef void type;
136 };
137
138 } // namespace detail
139
140 /*******************************************************************************
141  * Conversions from integral types to string types.
142  ******************************************************************************/
143
144 #if FOLLY_HAVE_INT128_T
145 namespace detail {
146
147 template <typename IntegerType>
148 constexpr unsigned int
149 digitsEnough() {
150   return ceil((double(sizeof(IntegerType) * CHAR_BIT) * M_LN2) / M_LN10);
151 }
152
153 inline unsigned int
154 unsafeTelescope128(char * buffer, unsigned int room, unsigned __int128 x) {
155   typedef unsigned __int128 Usrc;
156   unsigned int p = room - 1;
157
158   while (x >= (Usrc(1) << 64)) { // Using 128-bit division while needed
159     const auto y = x / 10;
160     const auto digit = x % 10;
161
162     buffer[p--] = '0' + digit;
163     x = y;
164   }
165
166   uint64_t xx = x; // Moving to faster 64-bit division thereafter
167
168   while (xx >= 10) {
169     const auto y = xx / 10ULL;
170     const auto digit = xx % 10ULL;
171
172     buffer[p--] = '0' + digit;
173     xx = y;
174   }
175
176   buffer[p] = '0' + xx;
177
178   return p;
179 }
180
181 }
182 #endif
183
184 /**
185  * Returns the number of digits in the base 10 representation of an
186  * uint64_t. Useful for preallocating buffers and such. It's also used
187  * internally, see below. Measurements suggest that defining a
188  * separate overload for 32-bit integers is not worthwhile.
189  */
190
191 inline uint32_t digits10(uint64_t v) {
192   uint32_t result = 1;
193   for (;;) {
194     if (LIKELY(v < 10)) return result;
195     if (LIKELY(v < 100)) return result + 1;
196     if (LIKELY(v < 1000)) return result + 2;
197     if (LIKELY(v < 10000)) return result + 3;
198     // Skip ahead by 4 orders of magnitude
199     v /= 10000U;
200     result += 4;
201   }
202 }
203
204 /**
205  * Copies the ASCII base 10 representation of v into buffer and
206  * returns the number of bytes written. Does NOT append a \0. Assumes
207  * the buffer points to digits10(v) bytes of valid memory. Note that
208  * uint64 needs at most 20 bytes, uint32_t needs at most 10 bytes,
209  * uint16_t needs at most 5 bytes, and so on. Measurements suggest
210  * that defining a separate overload for 32-bit integers is not
211  * worthwhile.
212  *
213  * This primitive is unsafe because it makes the size assumption and
214  * because it does not add a terminating \0.
215  */
216
217 inline uint32_t uint64ToBufferUnsafe(uint64_t v, char *const buffer) {
218   auto const result = digits10(v);
219   // WARNING: using size_t or pointer arithmetic for pos slows down
220   // the loop below 20x. This is because several 32-bit ops can be
221   // done in parallel, but only fewer 64-bit ones.
222   uint32_t pos = result - 1;
223   while (v >= 10) {
224     // Keep these together so a peephole optimization "sees" them and
225     // computes them in one shot.
226     auto const q = v / 10;
227     auto const r = static_cast<uint32_t>(v % 10);
228     buffer[pos--] = '0' + r;
229     v = q;
230   }
231   // Last digit is trivial to handle
232   buffer[pos] = static_cast<uint32_t>(v) + '0';
233   return result;
234 }
235
236 /**
237  * A single char gets appended.
238  */
239 template <class Tgt>
240 void toAppend(char value, Tgt * result) {
241   *result += value;
242 }
243
244 template<class T>
245 constexpr typename std::enable_if<
246   std::is_same<T, char>::value,
247   size_t>::type
248 estimateSpaceNeeded(T) {
249   return 1;
250 }
251
252 /**
253  * Ubiquitous helper template for writing string appenders
254  */
255 template <class T> struct IsSomeString {
256   enum { value = std::is_same<T, std::string>::value
257          || std::is_same<T, fbstring>::value };
258 };
259
260 /**
261  * Everything implicitly convertible to const char* gets appended.
262  */
263 template <class Tgt, class Src>
264 typename std::enable_if<
265   std::is_convertible<Src, const char*>::value
266   && IsSomeString<Tgt>::value>::type
267 toAppend(Src value, Tgt * result) {
268   // Treat null pointers like an empty string, as in:
269   // operator<<(std::ostream&, const char*).
270   const char* c = value;
271   if (c) {
272     result->append(value);
273   }
274 }
275
276 template<class Src>
277 typename std::enable_if<
278   std::is_convertible<Src, const char*>::value,
279   size_t>::type
280 estimateSpaceNeeded(Src value) {
281   const char *c = value;
282   if (c) {
283     return folly::StringPiece(value).size();
284   };
285   return 0;
286 }
287
288 template<class Src>
289 typename std::enable_if<
290   (std::is_convertible<Src, folly::StringPiece>::value ||
291   IsSomeString<Src>::value) &&
292   !std::is_convertible<Src, const char*>::value,
293   size_t>::type
294 estimateSpaceNeeded(Src value) {
295   return folly::StringPiece(value).size();
296 }
297
298 template<class Src>
299 typename std::enable_if<
300   std::is_pointer<Src>::value &&
301   IsSomeString<std::remove_pointer<Src>>::value,
302   size_t>::type
303 estimateSpaceNeeded(Src value) {
304   return value->size();
305 }
306
307 /**
308  * Strings get appended, too.
309  */
310 template <class Tgt, class Src>
311 typename std::enable_if<
312   IsSomeString<Src>::value && IsSomeString<Tgt>::value>::type
313 toAppend(const Src& value, Tgt * result) {
314   result->append(value);
315 }
316
317 /**
318  * and StringPiece objects too
319  */
320 template <class Tgt>
321 typename std::enable_if<
322    IsSomeString<Tgt>::value>::type
323 toAppend(StringPiece value, Tgt * result) {
324   result->append(value.data(), value.size());
325 }
326
327 /**
328  * There's no implicit conversion from fbstring to other string types,
329  * so make a specialization.
330  */
331 template <class Tgt>
332 typename std::enable_if<
333    IsSomeString<Tgt>::value>::type
334 toAppend(const fbstring& value, Tgt * result) {
335   result->append(value.data(), value.size());
336 }
337
338 #if FOLLY_HAVE_INT128_T
339 /**
340  * Special handling for 128 bit integers.
341  */
342
343 template <class Tgt>
344 void
345 toAppend(__int128 value, Tgt * result) {
346   typedef unsigned __int128 Usrc;
347   char buffer[detail::digitsEnough<unsigned __int128>() + 1];
348   unsigned int p;
349
350   if (value < 0) {
351     p = detail::unsafeTelescope128(buffer, sizeof(buffer), Usrc(-value));
352     buffer[--p] = '-';
353   } else {
354     p = detail::unsafeTelescope128(buffer, sizeof(buffer), value);
355   }
356
357   result->append(buffer + p, buffer + sizeof(buffer));
358 }
359
360 template <class Tgt>
361 void
362 toAppend(unsigned __int128 value, Tgt * result) {
363   char buffer[detail::digitsEnough<unsigned __int128>()];
364   unsigned int p;
365
366   p = detail::unsafeTelescope128(buffer, sizeof(buffer), value);
367
368   result->append(buffer + p, buffer + sizeof(buffer));
369 }
370
371 template<class T>
372 constexpr typename std::enable_if<
373   std::is_same<T, __int128>::value,
374   size_t>::type
375 estimateSpaceNeeded(T) {
376   return detail::digitsEnough<__int128>();
377 }
378
379 template<class T>
380 constexpr typename std::enable_if<
381   std::is_same<T, unsigned __int128>::value,
382   size_t>::type
383 estimateSpaceNeeded(T) {
384   return detail::digitsEnough<unsigned __int128>();
385 }
386
387 #endif
388
389 /**
390  * int32_t and int64_t to string (by appending) go through here. The
391  * result is APPENDED to a preexisting string passed as the second
392  * parameter. This should be efficient with fbstring because fbstring
393  * incurs no dynamic allocation below 23 bytes and no number has more
394  * than 22 bytes in its textual representation (20 for digits, one for
395  * sign, one for the terminating 0).
396  */
397 template <class Tgt, class Src>
398 typename std::enable_if<
399   std::is_integral<Src>::value && std::is_signed<Src>::value &&
400   IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
401 toAppend(Src value, Tgt * result) {
402   char buffer[20];
403   if (value < 0) {
404     result->push_back('-');
405     result->append(buffer, uint64ToBufferUnsafe(-uint64_t(value), buffer));
406   } else {
407     result->append(buffer, uint64ToBufferUnsafe(value, buffer));
408   }
409 }
410
411 template <class Src>
412 typename std::enable_if<
413   std::is_integral<Src>::value && std::is_signed<Src>::value
414   && sizeof(Src) >= 4 && sizeof(Src) < 16,
415   size_t>::type
416 estimateSpaceNeeded(Src value) {
417   if (value < 0) {
418     return 1 + digits10(static_cast<uint64_t>(-value));
419   }
420
421   return digits10(static_cast<uint64_t>(value));
422 }
423
424 /**
425  * As above, but for uint32_t and uint64_t.
426  */
427 template <class Tgt, class Src>
428 typename std::enable_if<
429   std::is_integral<Src>::value && !std::is_signed<Src>::value
430   && IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
431 toAppend(Src value, Tgt * result) {
432   char buffer[20];
433   result->append(buffer, buffer + uint64ToBufferUnsafe(value, buffer));
434 }
435
436 template <class Src>
437 typename std::enable_if<
438   std::is_integral<Src>::value && !std::is_signed<Src>::value
439   && sizeof(Src) >= 4 && sizeof(Src) < 16,
440   size_t>::type
441 estimateSpaceNeeded(Src value) {
442   return digits10(value);
443 }
444
445 /**
446  * All small signed and unsigned integers to string go through 32-bit
447  * types int32_t and uint32_t, respectively.
448  */
449 template <class Tgt, class Src>
450 typename std::enable_if<
451   std::is_integral<Src>::value
452   && IsSomeString<Tgt>::value && sizeof(Src) < 4>::type
453 toAppend(Src value, Tgt * result) {
454   typedef typename
455     std::conditional<std::is_signed<Src>::value, int64_t, uint64_t>::type
456     Intermediate;
457   toAppend<Tgt>(static_cast<Intermediate>(value), result);
458 }
459
460 template <class Src>
461 typename std::enable_if<
462   std::is_integral<Src>::value
463   && sizeof(Src) < 4
464   && !std::is_same<Src, char>::value,
465   size_t>::type
466 estimateSpaceNeeded(Src value) {
467   typedef typename
468     std::conditional<std::is_signed<Src>::value, int64_t, uint64_t>::type
469     Intermediate;
470   return estimateSpaceNeeded(static_cast<Intermediate>(value));
471 }
472
473 #if defined(__clang__) || __GNUC_PREREQ(4, 7)
474 // std::underlying_type became available by gcc 4.7.0
475
476 /**
477  * Enumerated values get appended as integers.
478  */
479 template <class Tgt, class Src>
480 typename std::enable_if<
481   std::is_enum<Src>::value && IsSomeString<Tgt>::value>::type
482 toAppend(Src value, Tgt * result) {
483   toAppend(
484       static_cast<typename std::underlying_type<Src>::type>(value), result);
485 }
486
487 template <class Src>
488 typename std::enable_if<
489   std::is_enum<Src>::value, size_t>::type
490 estimateSpaceNeeded(Src value) {
491   return estimateSpaceNeeded(
492       static_cast<typename std::underlying_type<Src>::type>(value));
493 }
494
495 #else
496
497 /**
498  * Enumerated values get appended as integers.
499  */
500 template <class Tgt, class Src>
501 typename std::enable_if<
502   std::is_enum<Src>::value && IsSomeString<Tgt>::value>::type
503 toAppend(Src value, Tgt * result) {
504   /* static */ if (Src(-1) < 0) {
505     /* static */ if (sizeof(Src) <= sizeof(int)) {
506       toAppend(static_cast<int>(value), result);
507     } else {
508       toAppend(static_cast<long>(value), result);
509     }
510   } else {
511     /* static */ if (sizeof(Src) <= sizeof(int)) {
512       toAppend(static_cast<unsigned int>(value), result);
513     } else {
514       toAppend(static_cast<unsigned long>(value), result);
515     }
516   }
517 }
518
519 template <class Src>
520 typename std::enable_if<
521   std::is_enum<Src>::value, size_t>::type
522 estimateSpaceNeeded(Src value) {
523   /* static */ if (Src(-1) < 0) {
524     /* static */ if (sizeof(Src) <= sizeof(int)) {
525       return estimateSpaceNeeded(static_cast<int>(value));
526     } else {
527       return estimateSpaceNeeded(static_cast<long>(value));
528     }
529   } else {
530     /* static */ if (sizeof(Src) <= sizeof(int)) {
531       return estimateSpaceNeeded(static_cast<unsigned int>(value));
532     } else {
533       return estimateSpaceNeeded(static_cast<unsigned long>(value));
534     }
535   }
536 }
537
538 #endif // gcc 4.7 onwards
539
540 /*******************************************************************************
541  * Conversions from floating-point types to string types.
542  ******************************************************************************/
543
544 /** Wrapper around DoubleToStringConverter **/
545 template <class Tgt, class Src>
546 typename std::enable_if<
547   std::is_floating_point<Src>::value
548   && IsSomeString<Tgt>::value>::type
549 toAppend(
550   Src value,
551   Tgt * result,
552   double_conversion::DoubleToStringConverter::DtoaMode mode,
553   unsigned int numDigits) {
554   using namespace double_conversion;
555   DoubleToStringConverter
556     conv(DoubleToStringConverter::NO_FLAGS,
557          "infinity", "NaN", 'E',
558          -6,  // decimal in shortest low
559          21,  // decimal in shortest high
560          6,   // max leading padding zeros
561          1);  // max trailing padding zeros
562   char buffer[256];
563   StringBuilder builder(buffer, sizeof(buffer));
564   switch (mode) {
565     case DoubleToStringConverter::SHORTEST:
566       conv.ToShortest(value, &builder);
567       break;
568     case DoubleToStringConverter::FIXED:
569       conv.ToFixed(value, numDigits, &builder);
570       break;
571     default:
572       CHECK(mode == DoubleToStringConverter::PRECISION);
573       conv.ToPrecision(value, numDigits, &builder);
574       break;
575   }
576   const size_t length = builder.position();
577   builder.Finalize();
578   result->append(buffer, length);
579 }
580
581 /**
582  * As above, but for floating point
583  */
584 template <class Tgt, class Src>
585 typename std::enable_if<
586   std::is_floating_point<Src>::value
587   && IsSomeString<Tgt>::value>::type
588 toAppend(Src value, Tgt * result) {
589   toAppend(
590     value, result, double_conversion::DoubleToStringConverter::SHORTEST, 0);
591 }
592
593 /**
594  * Very primitive, lets say its our best effort
595  */
596 template <class Src>
597 typename std::enable_if<
598   std::is_floating_point<Src>::value, size_t>::type
599 estimateSpaceNeeded(Src value) {
600   size_t sofar = 0;
601   if (value < 0) {
602     ++sofar;
603     value = -value;
604   }
605
606   if (value < 1) {
607     return sofar + 10; // lets assume 0 + '.' + 8 precision digits
608   }
609
610   if (value < static_cast<double>(std::numeric_limits<uint64_t>::max())) {
611     sofar += digits10(static_cast<uint64_t>(value));
612   } else {
613     return 64; // give up, it will be more than 23 anyway
614   }
615
616   return sofar + 10; // integral part + '.' + 8 precision digits
617 }
618
619 /**
620  * This can be specialized, together with adding specialization
621  * for estimateSpaceNeed for your type, so that we allocate
622  * as much as you need instead of the default
623  */
624 template<class Src>
625 struct HasLengthEstimator : std::false_type {};
626
627 template <class Src>
628 constexpr typename std::enable_if<
629   !std::is_fundamental<Src>::value
630   && !IsSomeString<Src>::value
631   && !std::is_convertible<Src, const char*>::value
632   && !std::is_convertible<Src, StringPiece>::value
633   && !std::is_enum<Src>::value
634   && !HasLengthEstimator<Src>::value,
635   size_t>::type
636 estimateSpaceNeeded(const Src&) {
637   return sizeof(Src) + 1; // dumbest best effort ever?
638 }
639
640 namespace detail {
641
642 inline size_t estimateSpaceToReserve(size_t sofar) {
643   return sofar;
644 }
645
646 template <class T, class... Ts>
647 size_t estimateSpaceToReserve(size_t sofar, const T& v, const Ts&... vs) {
648   return estimateSpaceToReserve(sofar + estimateSpaceNeeded(v), vs...);
649 }
650
651 template<class T>
652 size_t estimateSpaceToReserve(size_t sofar, const T& v) {
653   return sofar + estimateSpaceNeeded(v);
654 }
655
656 template<class...Ts>
657 void reserveInTarget(const Ts&...vs) {
658   getLastElement(vs...)->reserve(estimateSpaceToReserve(0, vs...));
659 }
660
661 template<class Delimiter, class...Ts>
662 void reserveInTargetDelim(const Delimiter& d, const Ts&...vs) {
663   static_assert(sizeof...(vs) >= 2, "Needs at least 2 args");
664   size_t fordelim = (sizeof...(vs) - 2) * estimateSpaceToReserve(0, d);
665   getLastElement(vs...)->reserve(estimateSpaceToReserve(fordelim, vs...));
666 }
667
668 /**
669  * Variadic base case: append one element
670  */
671 template <class T, class Tgt>
672 typename std::enable_if<
673   IsSomeString<typename std::remove_pointer<Tgt>::type>
674   ::value>::type
675 toAppendStrImpl(const T& v, Tgt result) {
676   toAppend(v, result);
677 }
678
679 template <class T, class... Ts>
680 typename std::enable_if<sizeof...(Ts) >= 2
681   && IsSomeString<
682   typename std::remove_pointer<
683     typename detail::last_element<Ts...>::type
684   >::type>::value>::type
685 toAppendStrImpl(const T& v, const Ts&... vs) {
686   toAppend(v, getLastElement(vs...));
687   toAppendStrImpl(vs...);
688 }
689
690 template <class Delimiter, class T, class Tgt>
691 typename std::enable_if<
692   IsSomeString<typename std::remove_pointer<Tgt>::type>
693   ::value>::type
694 toAppendDelimStrImpl(const Delimiter& delim, const T& v, Tgt result) {
695   toAppend(v, result);
696 }
697
698 template <class Delimiter, class T, class... Ts>
699 typename std::enable_if<sizeof...(Ts) >= 2
700   && IsSomeString<
701   typename std::remove_pointer<
702     typename detail::last_element<Ts...>::type
703   >::type>::value>::type
704 toAppendDelimStrImpl(const Delimiter& delim, const T& v, const Ts&... vs) {
705   // we are really careful here, calling toAppend with just one element does
706   // not try to estimate space needed (as we already did that). If we call
707   // toAppend(v, delim, ....) we would do unnecesary size calculation
708   toAppend(v, detail::getLastElement(vs...));
709   toAppend(delim, detail::getLastElement(vs...));
710   toAppendDelimStrImpl(delim, vs...);
711 }
712 } // folly::detail
713
714
715 /**
716  * Variadic conversion to string. Appends each element in turn.
717  * If we have two or more things to append, we will reserve
718  * the space for them (at least we will try).
719  */
720 template <class... Ts>
721 typename std::enable_if<sizeof...(Ts) >= 3
722   && IsSomeString<
723   typename std::remove_pointer<
724     typename detail::last_element<Ts...>::type
725   >::type>::value>::type
726 toAppend(const Ts&... vs) {
727   detail::reserveInTarget(vs...);
728   detail::toAppendStrImpl(vs...);
729 }
730
731 /**
732  * Variadic base case: do nothing.
733  */
734 template <class Tgt>
735 typename std::enable_if<IsSomeString<Tgt>::value>::type
736 toAppend(Tgt* result) {
737 }
738
739 /**
740  * Variadic base case: do nothing.
741  */
742 template <class Delimiter, class Tgt>
743 typename std::enable_if<IsSomeString<Tgt>::value>::type
744 toAppendDelim(const Delimiter& delim, Tgt* result) {
745 }
746
747 /**
748  * 1 element: same as toAppend.
749  */
750 template <class Delimiter, class T, class Tgt>
751 typename std::enable_if<IsSomeString<Tgt>::value>::type
752 toAppendDelim(const Delimiter& delim, const T& v, Tgt* tgt) {
753   toAppend(v, tgt);
754 }
755
756 /**
757  * Append to string with a delimiter in between elements.
758  */
759 template <class Delimiter, class... Ts>
760 typename std::enable_if<sizeof...(Ts) >= 3
761   && IsSomeString<
762   typename std::remove_pointer<
763     typename detail::last_element<Ts...>::type
764   >::type>::value>::type
765 toAppendDelim(const Delimiter& delim, const Ts&... vs) {
766   detail::reserveInTargetDelim(delim, vs...);
767   detail::toAppendDelimStrImpl(delim, vs...);
768 }
769
770 /**
771  * to<SomeString>(SomeString str) returns itself. As both std::string and
772  * folly::fbstring use Copy-on-Write, it's much more efficient by
773  * avoiding copying the underlying char array.
774  */
775 template <class Tgt, class Src>
776 typename std::enable_if<
777   IsSomeString<Tgt>::value && std::is_same<Tgt, Src>::value,
778   Tgt>::type
779 to(const Src & value) {
780   return value;
781 }
782
783 /**
784  * to<SomeString>(v1, v2, ...) uses toAppend() (see below) as back-end
785  * for all types.
786  */
787 template <class Tgt, class... Ts>
788 typename std::enable_if<
789   IsSomeString<Tgt>::value && (
790     sizeof...(Ts) != 1 ||
791     !std::is_same<Tgt, typename detail::last_element<Ts...>::type>::value),
792   Tgt>::type
793 to(const Ts&... vs) {
794   Tgt result;
795   toAppend(vs..., &result);
796   return result;
797 }
798
799 /**
800  * toDelim<SomeString>(SomeString str) returns itself.
801  */
802 template <class Tgt, class Delim, class Src>
803 typename std::enable_if<
804   IsSomeString<Tgt>::value && std::is_same<Tgt, Src>::value,
805   Tgt>::type
806 toDelim(const Delim& delim, const Src & value) {
807   return value;
808 }
809
810 /**
811  * toDelim<SomeString>(delim, v1, v2, ...) uses toAppendDelim() as
812  * back-end for all types.
813  */
814 template <class Tgt, class Delim, class... Ts>
815 typename std::enable_if<
816   IsSomeString<Tgt>::value && (
817     sizeof...(Ts) != 1 ||
818     !std::is_same<Tgt, typename detail::last_element<Ts...>::type>::value),
819   Tgt>::type
820 toDelim(const Delim& delim, const Ts&... vs) {
821   Tgt result;
822   toAppendDelim(delim, vs..., &result);
823   return result;
824 }
825
826 /*******************************************************************************
827  * Conversions from string types to integral types.
828  ******************************************************************************/
829
830 namespace detail {
831
832 /**
833  * Finds the first non-digit in a string. The number of digits
834  * searched depends on the precision of the Tgt integral. Assumes the
835  * string starts with NO whitespace and NO sign.
836  *
837  * The semantics of the routine is:
838  *   for (;; ++b) {
839  *     if (b >= e || !isdigit(*b)) return b;
840  *   }
841  *
842  *  Complete unrolling marks bottom-line (i.e. entire conversion)
843  *  improvements of 20%.
844  */
845   template <class Tgt>
846   const char* findFirstNonDigit(const char* b, const char* e) {
847     for (; b < e; ++b) {
848       auto const c = static_cast<unsigned>(*b) - '0';
849       if (c >= 10) break;
850     }
851     return b;
852   }
853
854   // Maximum value of number when represented as a string
855   template <class T> struct MaxString {
856     static const char*const value;
857   };
858
859
860 /*
861  * Lookup tables that converts from a decimal character value to an integral
862  * binary value, shifted by a decimal "shift" multiplier.
863  * For all character values in the range '0'..'9', the table at those
864  * index locations returns the actual decimal value shifted by the multiplier.
865  * For all other values, the lookup table returns an invalid OOR value.
866  */
867 // Out-of-range flag value, larger than the largest value that can fit in
868 // four decimal bytes (9999), but four of these added up together should
869 // still not overflow uint16_t.
870 constexpr int32_t OOR = 10000;
871
872 __attribute__((__aligned__(16))) constexpr uint16_t shift1[] = {
873   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 0-9
874   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  10
875   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  20
876   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  30
877   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0,         //  40
878   1, 2, 3, 4, 5, 6, 7, 8, 9, OOR, OOR,
879   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  60
880   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  70
881   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  80
882   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  90
883   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 100
884   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 110
885   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 120
886   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 130
887   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 140
888   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 150
889   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 160
890   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 170
891   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 180
892   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 190
893   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 200
894   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 210
895   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 220
896   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 230
897   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 240
898   OOR, OOR, OOR, OOR, OOR, OOR                       // 250
899 };
900
901 __attribute__((__aligned__(16))) constexpr uint16_t shift10[] = {
902   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 0-9
903   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  10
904   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  20
905   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  30
906   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0,         //  40
907   10, 20, 30, 40, 50, 60, 70, 80, 90, OOR, OOR,
908   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  60
909   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  70
910   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  80
911   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  90
912   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 100
913   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 110
914   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 120
915   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 130
916   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 140
917   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 150
918   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 160
919   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 170
920   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 180
921   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 190
922   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 200
923   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 210
924   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 220
925   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 230
926   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 240
927   OOR, OOR, OOR, OOR, OOR, OOR                       // 250
928 };
929
930 __attribute__((__aligned__(16))) constexpr uint16_t shift100[] = {
931   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 0-9
932   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  10
933   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  20
934   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  30
935   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0,         //  40
936   100, 200, 300, 400, 500, 600, 700, 800, 900, OOR, OOR,
937   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  60
938   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  70
939   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  80
940   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  90
941   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 100
942   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 110
943   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 120
944   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 130
945   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 140
946   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 150
947   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 160
948   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 170
949   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 180
950   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 190
951   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 200
952   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 210
953   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 220
954   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 230
955   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 240
956   OOR, OOR, OOR, OOR, OOR, OOR                       // 250
957 };
958
959 __attribute__((__aligned__(16))) constexpr uint16_t shift1000[] = {
960   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 0-9
961   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  10
962   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  20
963   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  30
964   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, 0,         //  40
965   1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, OOR, OOR,
966   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  60
967   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  70
968   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  80
969   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  //  90
970   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 100
971   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 110
972   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 120
973   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 130
974   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 140
975   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 150
976   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 160
977   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 170
978   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 180
979   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 190
980   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 200
981   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 210
982   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 220
983   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 230
984   OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR, OOR,  // 240
985   OOR, OOR, OOR, OOR, OOR, OOR                       // 250
986 };
987
988 /**
989  * String represented as a pair of pointers to char to unsigned
990  * integrals. Assumes NO whitespace before or after, and also that the
991  * string is composed entirely of digits. Tgt must be unsigned, and no
992  * sign is allowed in the string (even it's '+'). String may be empty,
993  * in which case digits_to throws.
994  */
995   template <class Tgt>
996   Tgt digits_to(const char * b, const char * e) {
997
998     static_assert(!std::is_signed<Tgt>::value, "Unsigned type expected");
999     assert(b <= e);
1000
1001     const size_t size = e - b;
1002
1003     /* Although the string is entirely made of digits, we still need to
1004      * check for overflow.
1005      */
1006     if (size >= std::numeric_limits<Tgt>::digits10 + 1) {
1007       // Leading zeros? If so, recurse to keep things simple
1008       if (b < e && *b == '0') {
1009         for (++b;; ++b) {
1010           if (b == e) return 0; // just zeros, e.g. "0000"
1011           if (*b != '0') return digits_to<Tgt>(b, e);
1012         }
1013       }
1014       FOLLY_RANGE_CHECK(size == std::numeric_limits<Tgt>::digits10 + 1 &&
1015                         strncmp(b, detail::MaxString<Tgt>::value, size) <= 0,
1016                         "Numeric overflow upon conversion");
1017     }
1018
1019     // Here we know that the number won't overflow when
1020     // converted. Proceed without checks.
1021
1022     Tgt result = 0;
1023
1024     for (; e - b >= 4; b += 4) {
1025       result *= 10000;
1026       const int32_t r0 = shift1000[static_cast<size_t>(b[0])];
1027       const int32_t r1 = shift100[static_cast<size_t>(b[1])];
1028       const int32_t r2 = shift10[static_cast<size_t>(b[2])];
1029       const int32_t r3 = shift1[static_cast<size_t>(b[3])];
1030       const auto sum = r0 + r1 + r2 + r3;
1031       assert(sum < OOR && "Assumption: string only has digits");
1032       result += sum;
1033     }
1034
1035     switch (e - b) {
1036       case 3: {
1037         const int32_t r0 = shift100[static_cast<size_t>(b[0])];
1038         const int32_t r1 = shift10[static_cast<size_t>(b[1])];
1039         const int32_t r2 = shift1[static_cast<size_t>(b[2])];
1040         const auto sum = r0 + r1 + r2;
1041         assert(sum < OOR && "Assumption: string only has digits");
1042         return result * 1000 + sum;
1043       }
1044       case 2: {
1045         const int32_t r0 = shift10[static_cast<size_t>(b[0])];
1046         const int32_t r1 = shift1[static_cast<size_t>(b[1])];
1047         const auto sum = r0 + r1;
1048         assert(sum < OOR && "Assumption: string only has digits");
1049         return result * 100 + sum;
1050       }
1051       case 1: {
1052         const int32_t sum = shift1[static_cast<size_t>(b[0])];
1053         assert(sum < OOR && "Assumption: string only has digits");
1054         return result * 10 + sum;
1055       }
1056     }
1057
1058     assert(b == e);
1059     FOLLY_RANGE_CHECK(size > 0, "Found no digits to convert in input");
1060     return result;
1061   }
1062
1063
1064   bool str_to_bool(StringPiece * src);
1065
1066 }                                 // namespace detail
1067
1068 /**
1069  * String represented as a pair of pointers to char to unsigned
1070  * integrals. Assumes NO whitespace before or after.
1071  */
1072 template <class Tgt>
1073 typename std::enable_if<
1074   std::is_integral<Tgt>::value && !std::is_signed<Tgt>::value
1075   && !std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
1076   Tgt>::type
1077 to(const char * b, const char * e) {
1078   return detail::digits_to<Tgt>(b, e);
1079 }
1080
1081 /**
1082  * String represented as a pair of pointers to char to signed
1083  * integrals. Assumes NO whitespace before or after. Allows an
1084  * optional leading sign.
1085  */
1086 template <class Tgt>
1087 typename std::enable_if<
1088   std::is_integral<Tgt>::value && std::is_signed<Tgt>::value,
1089   Tgt>::type
1090 to(const char * b, const char * e) {
1091   FOLLY_RANGE_CHECK(b < e, "Empty input string in conversion to integral");
1092   if (!isdigit(*b)) {
1093     if (*b == '-') {
1094       Tgt result = -to<typename std::make_unsigned<Tgt>::type>(b + 1, e);
1095       FOLLY_RANGE_CHECK(result <= 0, "Negative overflow.");
1096       return result;
1097     }
1098     FOLLY_RANGE_CHECK(*b == '+', "Invalid lead character");
1099     ++b;
1100   }
1101   Tgt result = to<typename std::make_unsigned<Tgt>::type>(b, e);
1102   FOLLY_RANGE_CHECK(result >= 0, "Overflow.");
1103   return result;
1104 }
1105
1106 /**
1107  * Parsing strings to integrals. These routines differ from
1108  * to<integral>(string) in that they take a POINTER TO a StringPiece
1109  * and alter that StringPiece to reflect progress information.
1110  */
1111
1112 /**
1113  * StringPiece to integrals, with progress information. Alters the
1114  * StringPiece parameter to munch the already-parsed characters.
1115  */
1116 template <class Tgt>
1117 typename std::enable_if<
1118   std::is_integral<Tgt>::value
1119   && !std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
1120   Tgt>::type
1121 to(StringPiece * src) {
1122
1123   auto b = src->data(), past = src->data() + src->size();
1124   for (;; ++b) {
1125     FOLLY_RANGE_CHECK(b < past, "No digits found in input string");
1126     if (!isspace(*b)) break;
1127   }
1128
1129   auto m = b;
1130
1131   // First digit is customized because we test for sign
1132   bool negative = false;
1133   /* static */ if (std::is_signed<Tgt>::value) {
1134     if (!isdigit(*m)) {
1135       if (*m == '-') {
1136         negative = true;
1137       } else {
1138         FOLLY_RANGE_CHECK(*m == '+', "Invalid leading character in conversion"
1139                           " to integral");
1140       }
1141       ++b;
1142       ++m;
1143     }
1144   }
1145   FOLLY_RANGE_CHECK(m < past, "No digits found in input string");
1146   FOLLY_RANGE_CHECK(isdigit(*m), "Non-digit character found");
1147   m = detail::findFirstNonDigit<Tgt>(m + 1, past);
1148
1149   Tgt result;
1150   /* static */ if (!std::is_signed<Tgt>::value) {
1151     result = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
1152   } else {
1153     auto t = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
1154     if (negative) {
1155       result = -t;
1156       FOLLY_RANGE_CHECK(is_non_positive(result), "Negative overflow");
1157     } else {
1158       result = t;
1159       FOLLY_RANGE_CHECK(is_non_negative(result), "Overflow");
1160     }
1161   }
1162   src->advance(m - src->data());
1163   return result;
1164 }
1165
1166 /**
1167  * StringPiece to bool, with progress information. Alters the
1168  * StringPiece parameter to munch the already-parsed characters.
1169  */
1170 template <class Tgt>
1171 typename std::enable_if<
1172   std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
1173   Tgt>::type
1174 to(StringPiece * src) {
1175   return detail::str_to_bool(src);
1176 }
1177
1178 namespace detail {
1179
1180 /**
1181  * Enforce that the suffix following a number is made up only of whitespace.
1182  */
1183 inline void enforceWhitespace(const char* b, const char* e) {
1184   for (; b != e; ++b) {
1185     FOLLY_RANGE_CHECK(isspace(*b), to<std::string>("Non-whitespace: ", *b));
1186   }
1187 }
1188
1189 }  // namespace detail
1190
1191 /**
1192  * String or StringPiece to integrals. Accepts leading and trailing
1193  * whitespace, but no non-space trailing characters.
1194  */
1195 template <class Tgt>
1196 typename std::enable_if<
1197   std::is_integral<Tgt>::value,
1198   Tgt>::type
1199 to(StringPiece src) {
1200   Tgt result = to<Tgt>(&src);
1201   detail::enforceWhitespace(src.data(), src.data() + src.size());
1202   return result;
1203 }
1204
1205 /*******************************************************************************
1206  * Conversions from string types to floating-point types.
1207  ******************************************************************************/
1208
1209 /**
1210  * StringPiece to double, with progress information. Alters the
1211  * StringPiece parameter to munch the already-parsed characters.
1212  */
1213 template <class Tgt>
1214 inline typename std::enable_if<
1215   std::is_floating_point<Tgt>::value,
1216   Tgt>::type
1217 to(StringPiece *const src) {
1218   using namespace double_conversion;
1219   static StringToDoubleConverter
1220     conv(StringToDoubleConverter::ALLOW_TRAILING_JUNK
1221          | StringToDoubleConverter::ALLOW_LEADING_SPACES,
1222          0.0,
1223          // return this for junk input string
1224          std::numeric_limits<double>::quiet_NaN(),
1225          nullptr, nullptr);
1226
1227   FOLLY_RANGE_CHECK(!src->empty(), "No digits found in input string");
1228
1229   int length;
1230   auto result = conv.StringToDouble(src->data(), src->size(),
1231                                        &length); // processed char count
1232
1233   if (!std::isnan(result)) {
1234     src->advance(length);
1235     return result;
1236   }
1237
1238   for (;; src->advance(1)) {
1239     if (src->empty()) {
1240       throw std::range_error("Unable to convert an empty string"
1241                              " to a floating point value.");
1242     }
1243     if (!isspace(src->front())) {
1244       break;
1245     }
1246   }
1247
1248   // Was that "inf[inity]"?
1249   if (src->size() >= 3 && toupper((*src)[0]) == 'I'
1250         && toupper((*src)[1]) == 'N' && toupper((*src)[2]) == 'F') {
1251     if (src->size() >= 8 &&
1252         toupper((*src)[3]) == 'I' &&
1253         toupper((*src)[4]) == 'N' &&
1254         toupper((*src)[5]) == 'I' &&
1255         toupper((*src)[6]) == 'T' &&
1256         toupper((*src)[7]) == 'Y') {
1257       src->advance(8);
1258     } else {
1259       src->advance(3);
1260     }
1261     return std::numeric_limits<Tgt>::infinity();
1262   }
1263
1264   // Was that "-inf[inity]"?
1265   if (src->size() >= 4 && toupper((*src)[0]) == '-'
1266       && toupper((*src)[1]) == 'I' && toupper((*src)[2]) == 'N'
1267       && toupper((*src)[3]) == 'F') {
1268     if (src->size() >= 9 &&
1269         toupper((*src)[4]) == 'I' &&
1270         toupper((*src)[5]) == 'N' &&
1271         toupper((*src)[6]) == 'I' &&
1272         toupper((*src)[7]) == 'T' &&
1273         toupper((*src)[8]) == 'Y') {
1274       src->advance(9);
1275     } else {
1276       src->advance(4);
1277     }
1278     return -std::numeric_limits<Tgt>::infinity();
1279   }
1280
1281   // "nan"?
1282   if (src->size() >= 3 && toupper((*src)[0]) == 'N'
1283         && toupper((*src)[1]) == 'A' && toupper((*src)[2]) == 'N') {
1284     src->advance(3);
1285     return std::numeric_limits<Tgt>::quiet_NaN();
1286   }
1287
1288   // "-nan"?
1289   if (src->size() >= 4 &&
1290       toupper((*src)[0]) == '-' &&
1291       toupper((*src)[1]) == 'N' &&
1292       toupper((*src)[2]) == 'A' &&
1293       toupper((*src)[3]) == 'N') {
1294     src->advance(4);
1295     return -std::numeric_limits<Tgt>::quiet_NaN();
1296   }
1297
1298   // All bets are off
1299   throw std::range_error("Unable to convert \"" + src->toString()
1300                          + "\" to a floating point value.");
1301 }
1302
1303 /**
1304  * Any string, const char*, or StringPiece to double.
1305  */
1306 template <class Tgt>
1307 typename std::enable_if<
1308   std::is_floating_point<Tgt>::value,
1309   Tgt>::type
1310 to(StringPiece src) {
1311   Tgt result = to<double>(&src);
1312   detail::enforceWhitespace(src.data(), src.data() + src.size());
1313   return result;
1314 }
1315
1316 /*******************************************************************************
1317  * Integral to floating point and back
1318  ******************************************************************************/
1319
1320 /**
1321  * Checked conversion from integral to flating point and back. The
1322  * result must be convertible back to the source type without loss of
1323  * precision. This seems Draconian but sometimes is what's needed, and
1324  * complements existing routines nicely. For various rounding
1325  * routines, see <math>.
1326  */
1327 template <class Tgt, class Src>
1328 typename std::enable_if<
1329   (std::is_integral<Src>::value && std::is_floating_point<Tgt>::value)
1330   ||
1331   (std::is_floating_point<Src>::value && std::is_integral<Tgt>::value),
1332   Tgt>::type
1333 to(const Src & value) {
1334   Tgt result = value;
1335   auto witness = static_cast<Src>(result);
1336   if (value != witness) {
1337     throw std::range_error(
1338       to<std::string>("to<>: loss of precision when converting ", value,
1339                       " to type ", typeid(Tgt).name()).c_str());
1340   }
1341   return result;
1342 }
1343
1344 /*******************************************************************************
1345  * Enum to anything and back
1346  ******************************************************************************/
1347
1348 #if defined(__clang__) || __GNUC_PREREQ(4, 7)
1349 // std::underlying_type became available by gcc 4.7.0
1350
1351 template <class Tgt, class Src>
1352 typename std::enable_if<std::is_enum<Src>::value, Tgt>::type
1353 to(const Src & value) {
1354   return to<Tgt>(static_cast<typename std::underlying_type<Src>::type>(value));
1355 }
1356
1357 template <class Tgt, class Src>
1358 typename std::enable_if<std::is_enum<Tgt>::value, Tgt>::type
1359 to(const Src & value) {
1360   return static_cast<Tgt>(to<typename std::underlying_type<Tgt>::type>(value));
1361 }
1362
1363 #else
1364
1365 template <class Tgt, class Src>
1366 typename std::enable_if<std::is_enum<Src>::value, Tgt>::type
1367 to(const Src & value) {
1368   /* static */ if (Src(-1) < 0) {
1369     /* static */ if (sizeof(Src) <= sizeof(int)) {
1370       return to<Tgt>(static_cast<int>(value));
1371     } else {
1372       return to<Tgt>(static_cast<long>(value));
1373     }
1374   } else {
1375     /* static */ if (sizeof(Src) <= sizeof(int)) {
1376       return to<Tgt>(static_cast<unsigned int>(value));
1377     } else {
1378       return to<Tgt>(static_cast<unsigned long>(value));
1379     }
1380   }
1381 }
1382
1383 template <class Tgt, class Src>
1384 typename std::enable_if<std::is_enum<Tgt>::value, Tgt>::type
1385 to(const Src & value) {
1386   /* static */ if (Tgt(-1) < 0) {
1387     /* static */ if (sizeof(Tgt) <= sizeof(int)) {
1388       return static_cast<Tgt>(to<int>(value));
1389     } else {
1390       return static_cast<Tgt>(to<long>(value));
1391     }
1392   } else {
1393     /* static */ if (sizeof(Tgt) <= sizeof(int)) {
1394       return static_cast<Tgt>(to<unsigned int>(value));
1395     } else {
1396       return static_cast<Tgt>(to<unsigned long>(value));
1397     }
1398   }
1399 }
1400
1401 #endif // gcc 4.7 onwards
1402
1403 } // namespace folly
1404
1405 // FOLLY_CONV_INTERNAL is defined by Conv.cpp.  Keep the FOLLY_RANGE_CHECK
1406 // macro for use in Conv.cpp, but #undefine it everywhere else we are included,
1407 // to avoid defining this global macro name in other files that include Conv.h.
1408 #ifndef FOLLY_CONV_INTERNAL
1409 #undef FOLLY_RANGE_CHECK
1410 #endif
1411
1412 #endif /* FOLLY_BASE_CONV_H_ */