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