d13ebcb25e36de840cb26ea49b15fbc38079975a
[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 #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 <algorithm>
34 #include <type_traits>
35 #include <limits>
36 #include <string>
37 #include <tuple>
38 #include <stdexcept>
39 #include <typeinfo>
40
41 #include <limits.h>
42
43 // V8 JavaScript implementation
44 #include <double-conversion/double-conversion.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::move(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, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000,
258     10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000,
259     1000000000000000, 10000000000000000, 100000000000000000,
260     1000000000000000000, 10000000000000000000UL
261   };
262
263   // "count leading zeroes" operation not valid; for 0; special case this.
264   if UNLIKELY (! v) {
265     return 1;
266   }
267
268   // bits is in the ballpark of log_2(v).
269   const uint8_t leadingZeroes = __builtin_clzll(v);
270   const auto bits = 63 - leadingZeroes;
271
272   // approximate log_10(v) == log_10(2) * bits.
273   // Integer magic below: 77/256 is appx. 0.3010 (log_10(2)).
274   // The +1 is to make this the ceiling of the log_10 estimate.
275   const uint32_t minLength = 1 + ((bits * 77) >> 8);
276
277   // return that log_10 lower bound, plus adjust if input >= 10^(that bound)
278   // in case there's a small error and we misjudged length.
279   return minLength + (uint32_t) (UNLIKELY (v >= powersOf10[minLength]));
280
281 #else
282
283   uint32_t result = 1;
284   for (;;) {
285     if (LIKELY(v < 10)) return result;
286     if (LIKELY(v < 100)) return result + 1;
287     if (LIKELY(v < 1000)) return result + 2;
288     if (LIKELY(v < 10000)) return result + 3;
289     // Skip ahead by 4 orders of magnitude
290     v /= 10000U;
291     result += 4;
292   }
293
294 #endif
295 }
296
297 /**
298  * Copies the ASCII base 10 representation of v into buffer and
299  * returns the number of bytes written. Does NOT append a \0. Assumes
300  * the buffer points to digits10(v) bytes of valid memory. Note that
301  * uint64 needs at most 20 bytes, uint32_t needs at most 10 bytes,
302  * uint16_t needs at most 5 bytes, and so on. Measurements suggest
303  * that defining a separate overload for 32-bit integers is not
304  * worthwhile.
305  *
306  * This primitive is unsafe because it makes the size assumption and
307  * because it does not add a terminating \0.
308  */
309
310 inline uint32_t uint64ToBufferUnsafe(uint64_t v, char *const buffer) {
311   auto const result = digits10(v);
312   // WARNING: using size_t or pointer arithmetic for pos slows down
313   // the loop below 20x. This is because several 32-bit ops can be
314   // done in parallel, but only fewer 64-bit ones.
315   uint32_t pos = result - 1;
316   while (v >= 10) {
317     // Keep these together so a peephole optimization "sees" them and
318     // computes them in one shot.
319     auto const q = v / 10;
320     auto const r = static_cast<uint32_t>(v % 10);
321     buffer[pos--] = '0' + r;
322     v = q;
323   }
324   // Last digit is trivial to handle
325   buffer[pos] = static_cast<uint32_t>(v) + '0';
326   return result;
327 }
328
329 /**
330  * A single char gets appended.
331  */
332 template <class Tgt>
333 void toAppend(char value, Tgt * result) {
334   *result += value;
335 }
336
337 template<class T>
338 constexpr typename std::enable_if<
339   std::is_same<T, char>::value,
340   size_t>::type
341 estimateSpaceNeeded(T) {
342   return 1;
343 }
344
345 /**
346  * Ubiquitous helper template for writing string appenders
347  */
348 template <class T> struct IsSomeString {
349   enum { value = std::is_same<T, std::string>::value
350          || std::is_same<T, fbstring>::value };
351 };
352
353 /**
354  * Everything implicitly convertible to const char* gets appended.
355  */
356 template <class Tgt, class Src>
357 typename std::enable_if<
358   std::is_convertible<Src, const char*>::value
359   && IsSomeString<Tgt>::value>::type
360 toAppend(Src value, Tgt * result) {
361   // Treat null pointers like an empty string, as in:
362   // operator<<(std::ostream&, const char*).
363   const char* c = value;
364   if (c) {
365     result->append(value);
366   }
367 }
368
369 template<class Src>
370 typename std::enable_if<
371   std::is_convertible<Src, const char*>::value,
372   size_t>::type
373 estimateSpaceNeeded(Src value) {
374   const char *c = value;
375   if (c) {
376     return folly::StringPiece(value).size();
377   };
378   return 0;
379 }
380
381 template<class Src>
382 typename std::enable_if<
383   (std::is_convertible<Src, folly::StringPiece>::value ||
384   IsSomeString<Src>::value) &&
385   !std::is_convertible<Src, const char*>::value,
386   size_t>::type
387 estimateSpaceNeeded(Src value) {
388   return folly::StringPiece(value).size();
389 }
390
391 template <>
392 inline size_t estimateSpaceNeeded(std::nullptr_t /* value */) {
393   return 0;
394 }
395
396 template<class Src>
397 typename std::enable_if<
398   std::is_pointer<Src>::value &&
399   IsSomeString<std::remove_pointer<Src>>::value,
400   size_t>::type
401 estimateSpaceNeeded(Src value) {
402   return value->size();
403 }
404
405 /**
406  * Strings get appended, too.
407  */
408 template <class Tgt, class Src>
409 typename std::enable_if<
410   IsSomeString<Src>::value && IsSomeString<Tgt>::value>::type
411 toAppend(const Src& value, Tgt * result) {
412   result->append(value);
413 }
414
415 /**
416  * and StringPiece objects too
417  */
418 template <class Tgt>
419 typename std::enable_if<
420    IsSomeString<Tgt>::value>::type
421 toAppend(StringPiece value, Tgt * result) {
422   result->append(value.data(), value.size());
423 }
424
425 /**
426  * There's no implicit conversion from fbstring to other string types,
427  * so make a specialization.
428  */
429 template <class Tgt>
430 typename std::enable_if<
431    IsSomeString<Tgt>::value>::type
432 toAppend(const fbstring& value, Tgt * result) {
433   result->append(value.data(), value.size());
434 }
435
436 #if FOLLY_HAVE_INT128_T
437 /**
438  * Special handling for 128 bit integers.
439  */
440
441 template <class Tgt>
442 void
443 toAppend(__int128 value, Tgt * result) {
444   typedef unsigned __int128 Usrc;
445   char buffer[detail::digitsEnough<unsigned __int128>() + 1];
446   size_t p;
447
448   if (value < 0) {
449     p = detail::unsafeTelescope128(buffer, sizeof(buffer), Usrc(-value));
450     buffer[--p] = '-';
451   } else {
452     p = detail::unsafeTelescope128(buffer, sizeof(buffer), value);
453   }
454
455   result->append(buffer + p, buffer + sizeof(buffer));
456 }
457
458 template <class Tgt>
459 void
460 toAppend(unsigned __int128 value, Tgt * result) {
461   char buffer[detail::digitsEnough<unsigned __int128>()];
462   size_t p;
463
464   p = detail::unsafeTelescope128(buffer, sizeof(buffer), value);
465
466   result->append(buffer + p, buffer + sizeof(buffer));
467 }
468
469 template<class T>
470 constexpr typename std::enable_if<
471   std::is_same<T, __int128>::value,
472   size_t>::type
473 estimateSpaceNeeded(T) {
474   return detail::digitsEnough<__int128>();
475 }
476
477 template<class T>
478 constexpr typename std::enable_if<
479   std::is_same<T, unsigned __int128>::value,
480   size_t>::type
481 estimateSpaceNeeded(T) {
482   return detail::digitsEnough<unsigned __int128>();
483 }
484
485 #endif
486
487 /**
488  * int32_t and int64_t to string (by appending) go through here. The
489  * result is APPENDED to a preexisting string passed as the second
490  * parameter. This should be efficient with fbstring because fbstring
491  * incurs no dynamic allocation below 23 bytes and no number has more
492  * than 22 bytes in its textual representation (20 for digits, one for
493  * sign, one for the terminating 0).
494  */
495 template <class Tgt, class Src>
496 typename std::enable_if<
497   std::is_integral<Src>::value && std::is_signed<Src>::value &&
498   IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
499 toAppend(Src value, Tgt * result) {
500   char buffer[20];
501   if (value < 0) {
502     result->push_back('-');
503     result->append(buffer, uint64ToBufferUnsafe(-uint64_t(value), buffer));
504   } else {
505     result->append(buffer, uint64ToBufferUnsafe(value, buffer));
506   }
507 }
508
509 template <class Src>
510 typename std::enable_if<
511   std::is_integral<Src>::value && std::is_signed<Src>::value
512   && sizeof(Src) >= 4 && sizeof(Src) < 16,
513   size_t>::type
514 estimateSpaceNeeded(Src value) {
515   if (value < 0) {
516     // When "value" is the smallest negative, negating it would evoke
517     // undefined behavior, so, instead of writing "-value" below, we write
518     // "~static_cast<uint64_t>(value) + 1"
519     return 1 + digits10(~static_cast<uint64_t>(value) + 1);
520   }
521
522   return digits10(static_cast<uint64_t>(value));
523 }
524
525 /**
526  * As above, but for uint32_t and uint64_t.
527  */
528 template <class Tgt, class Src>
529 typename std::enable_if<
530   std::is_integral<Src>::value && !std::is_signed<Src>::value
531   && IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
532 toAppend(Src value, Tgt * result) {
533   char buffer[20];
534   result->append(buffer, buffer + uint64ToBufferUnsafe(value, buffer));
535 }
536
537 template <class Src>
538 typename std::enable_if<
539   std::is_integral<Src>::value && !std::is_signed<Src>::value
540   && sizeof(Src) >= 4 && sizeof(Src) < 16,
541   size_t>::type
542 estimateSpaceNeeded(Src value) {
543   return digits10(value);
544 }
545
546 /**
547  * All small signed and unsigned integers to string go through 32-bit
548  * types int32_t and uint32_t, respectively.
549  */
550 template <class Tgt, class Src>
551 typename std::enable_if<
552   std::is_integral<Src>::value
553   && IsSomeString<Tgt>::value && sizeof(Src) < 4>::type
554 toAppend(Src value, Tgt * result) {
555   typedef typename
556     std::conditional<std::is_signed<Src>::value, int64_t, uint64_t>::type
557     Intermediate;
558   toAppend<Tgt>(static_cast<Intermediate>(value), result);
559 }
560
561 template <class Src>
562 typename std::enable_if<
563   std::is_integral<Src>::value
564   && sizeof(Src) < 4
565   && !std::is_same<Src, char>::value,
566   size_t>::type
567 estimateSpaceNeeded(Src value) {
568   typedef typename
569     std::conditional<std::is_signed<Src>::value, int64_t, uint64_t>::type
570     Intermediate;
571   return estimateSpaceNeeded(static_cast<Intermediate>(value));
572 }
573
574 /**
575  * Enumerated values get appended as integers.
576  */
577 template <class Tgt, class Src>
578 typename std::enable_if<
579   std::is_enum<Src>::value && IsSomeString<Tgt>::value>::type
580 toAppend(Src value, Tgt * result) {
581   toAppend(
582       static_cast<typename std::underlying_type<Src>::type>(value), result);
583 }
584
585 template <class Src>
586 typename std::enable_if<
587   std::is_enum<Src>::value, size_t>::type
588 estimateSpaceNeeded(Src value) {
589   return estimateSpaceNeeded(
590       static_cast<typename std::underlying_type<Src>::type>(value));
591 }
592
593 /*******************************************************************************
594  * Conversions from floating-point types to string types.
595  ******************************************************************************/
596
597 namespace detail {
598 constexpr int kConvMaxDecimalInShortestLow = -6;
599 constexpr int kConvMaxDecimalInShortestHigh = 21;
600 } // folly::detail
601
602 /** Wrapper around DoubleToStringConverter **/
603 template <class Tgt, class Src>
604 typename std::enable_if<
605   std::is_floating_point<Src>::value
606   && IsSomeString<Tgt>::value>::type
607 toAppend(
608   Src value,
609   Tgt * result,
610   double_conversion::DoubleToStringConverter::DtoaMode mode,
611   unsigned int numDigits) {
612   using namespace double_conversion;
613   DoubleToStringConverter
614     conv(DoubleToStringConverter::NO_FLAGS,
615          "Infinity", "NaN", 'E',
616          detail::kConvMaxDecimalInShortestLow,
617          detail::kConvMaxDecimalInShortestHigh,
618          6,   // max leading padding zeros
619          1);  // max trailing padding zeros
620   char buffer[256];
621   StringBuilder builder(buffer, sizeof(buffer));
622   switch (mode) {
623     case DoubleToStringConverter::SHORTEST:
624       conv.ToShortest(value, &builder);
625       break;
626     case DoubleToStringConverter::FIXED:
627       conv.ToFixed(value, numDigits, &builder);
628       break;
629     default:
630       CHECK(mode == DoubleToStringConverter::PRECISION);
631       conv.ToPrecision(value, numDigits, &builder);
632       break;
633   }
634   const size_t length = builder.position();
635   builder.Finalize();
636   result->append(buffer, length);
637 }
638
639 /**
640  * As above, but for floating point
641  */
642 template <class Tgt, class Src>
643 typename std::enable_if<
644   std::is_floating_point<Src>::value
645   && IsSomeString<Tgt>::value>::type
646 toAppend(Src value, Tgt * result) {
647   toAppend(
648     value, result, double_conversion::DoubleToStringConverter::SHORTEST, 0);
649 }
650
651 /**
652  * Upper bound of the length of the output from
653  * DoubleToStringConverter::ToShortest(double, StringBuilder*),
654  * as used in toAppend(double, string*).
655  */
656 template <class Src>
657 typename std::enable_if<
658   std::is_floating_point<Src>::value, size_t>::type
659 estimateSpaceNeeded(Src value) {
660   // kBase10MaximalLength is 17. We add 1 for decimal point,
661   // e.g. 10.0/9 is 17 digits and 18 characters, including the decimal point.
662   constexpr int kMaxMantissaSpace =
663     double_conversion::DoubleToStringConverter::kBase10MaximalLength + 1;
664   // strlen("E-") + digits10(numeric_limits<double>::max_exponent10)
665   constexpr int kMaxExponentSpace = 2 + 3;
666   static const int kMaxPositiveSpace = std::max({
667       // E.g. 1.1111111111111111E-100.
668       kMaxMantissaSpace + kMaxExponentSpace,
669       // E.g. 0.000001.1111111111111111, if kConvMaxDecimalInShortestLow is -6.
670       kMaxMantissaSpace - detail::kConvMaxDecimalInShortestLow,
671       // If kConvMaxDecimalInShortestHigh is 21, then 1e21 is the smallest
672       // number > 1 which ToShortest outputs in exponential notation,
673       // so 21 is the longest non-exponential number > 1.
674       detail::kConvMaxDecimalInShortestHigh
675     });
676   return kMaxPositiveSpace + (value < 0);  // +1 for minus sign, if negative
677 }
678
679 /**
680  * This can be specialized, together with adding specialization
681  * for estimateSpaceNeed for your type, so that we allocate
682  * as much as you need instead of the default
683  */
684 template<class Src>
685 struct HasLengthEstimator : std::false_type {};
686
687 template <class Src>
688 constexpr typename std::enable_if<
689   !std::is_fundamental<Src>::value
690 #ifdef FOLLY_HAVE_INT128_T
691   // On OSX 10.10, is_fundamental<__int128> is false :-O
692   && !std::is_same<__int128, Src>::value
693   && !std::is_same<unsigned __int128, Src>::value
694 #endif
695   && !IsSomeString<Src>::value
696   && !std::is_convertible<Src, const char*>::value
697   && !std::is_convertible<Src, StringPiece>::value
698   && !std::is_enum<Src>::value
699   && !HasLengthEstimator<Src>::value,
700   size_t>::type
701 estimateSpaceNeeded(const Src&) {
702   return sizeof(Src) + 1; // dumbest best effort ever?
703 }
704
705 namespace detail {
706
707 inline size_t estimateSpaceToReserve(size_t sofar) {
708   return sofar;
709 }
710
711 template <class T, class... Ts>
712 size_t estimateSpaceToReserve(size_t sofar, const T& v, const Ts&... vs) {
713   return estimateSpaceToReserve(sofar + estimateSpaceNeeded(v), vs...);
714 }
715
716 template<class T>
717 size_t estimateSpaceToReserve(size_t sofar, const T& v) {
718   return sofar + estimateSpaceNeeded(v);
719 }
720
721 template<class...Ts>
722 void reserveInTarget(const Ts&...vs) {
723   getLastElement(vs...)->reserve(estimateSpaceToReserve(0, vs...));
724 }
725
726 template<class Delimiter, class...Ts>
727 void reserveInTargetDelim(const Delimiter& d, const Ts&...vs) {
728   static_assert(sizeof...(vs) >= 2, "Needs at least 2 args");
729   size_t fordelim = (sizeof...(vs) - 2) * estimateSpaceToReserve(0, d);
730   getLastElement(vs...)->reserve(estimateSpaceToReserve(fordelim, vs...));
731 }
732
733 /**
734  * Variadic base case: append one element
735  */
736 template <class T, class Tgt>
737 typename std::enable_if<
738   IsSomeString<typename std::remove_pointer<Tgt>::type>
739   ::value>::type
740 toAppendStrImpl(const T& v, Tgt result) {
741   toAppend(v, result);
742 }
743
744 template <class T, class... Ts>
745 typename std::enable_if<sizeof...(Ts) >= 2
746   && IsSomeString<
747   typename std::remove_pointer<
748     typename detail::last_element<Ts...>::type
749   >::type>::value>::type
750 toAppendStrImpl(const T& v, const Ts&... vs) {
751   toAppend(v, getLastElement(vs...));
752   toAppendStrImpl(vs...);
753 }
754
755 template <class Delimiter, class T, class Tgt>
756 typename std::enable_if<
757     IsSomeString<typename std::remove_pointer<Tgt>::type>::value>::type
758 toAppendDelimStrImpl(const Delimiter& /* delim */, const T& v, Tgt result) {
759   toAppend(v, result);
760 }
761
762 template <class Delimiter, class T, class... Ts>
763 typename std::enable_if<sizeof...(Ts) >= 2
764   && IsSomeString<
765   typename std::remove_pointer<
766     typename detail::last_element<Ts...>::type
767   >::type>::value>::type
768 toAppendDelimStrImpl(const Delimiter& delim, const T& v, const Ts&... vs) {
769   // we are really careful here, calling toAppend with just one element does
770   // not try to estimate space needed (as we already did that). If we call
771   // toAppend(v, delim, ....) we would do unnecesary size calculation
772   toAppend(v, detail::getLastElement(vs...));
773   toAppend(delim, detail::getLastElement(vs...));
774   toAppendDelimStrImpl(delim, vs...);
775 }
776 } // folly::detail
777
778
779 /**
780  * Variadic conversion to string. Appends each element in turn.
781  * If we have two or more things to append, we it will not reserve
782  * the space for them and will depend on strings exponential growth.
783  * If you just append once consider using toAppendFit which reserves
784  * the space needed (but does not have exponential as a result).
785  */
786 template <class... Ts>
787 typename std::enable_if<sizeof...(Ts) >= 3
788   && IsSomeString<
789   typename std::remove_pointer<
790     typename detail::last_element<Ts...>::type
791   >::type>::value>::type
792 toAppend(const Ts&... vs) {
793   ::folly::detail::toAppendStrImpl(vs...);
794 }
795
796 /**
797  * Special version of the call that preallocates exaclty as much memory
798  * as need for arguments to be stored in target. This means we are
799  * not doing exponential growth when we append. If you are using it
800  * in a loop you are aiming at your foot with a big perf-destroying
801  * bazooka.
802  * On the other hand if you are appending to a string once, this
803  * will probably save a few calls to malloc.
804  */
805 template <class... Ts>
806 typename std::enable_if<
807   IsSomeString<
808   typename std::remove_pointer<
809     typename detail::last_element<Ts...>::type
810   >::type>::value>::type
811 toAppendFit(const Ts&... vs) {
812   ::folly::detail::reserveInTarget(vs...);
813   toAppend(vs...);
814 }
815
816 template <class Ts>
817 void toAppendFit(const Ts&) {}
818
819 /**
820  * Variadic base case: do nothing.
821  */
822 template <class Tgt>
823 typename std::enable_if<IsSomeString<Tgt>::value>::type toAppend(
824     Tgt* /* result */) {}
825
826 /**
827  * Variadic base case: do nothing.
828  */
829 template <class Delimiter, class Tgt>
830 typename std::enable_if<IsSomeString<Tgt>::value>::type toAppendDelim(
831     const Delimiter& /* delim */, Tgt* /* result */) {}
832
833 /**
834  * 1 element: same as toAppend.
835  */
836 template <class Delimiter, class T, class Tgt>
837 typename std::enable_if<IsSomeString<Tgt>::value>::type toAppendDelim(
838     const Delimiter& /* delim */, const T& v, Tgt* tgt) {
839   toAppend(v, tgt);
840 }
841
842 /**
843  * Append to string with a delimiter in between elements. Check out
844  * comments for toAppend for details about memory allocation.
845  */
846 template <class Delimiter, class... Ts>
847 typename std::enable_if<sizeof...(Ts) >= 3
848   && IsSomeString<
849   typename std::remove_pointer<
850     typename detail::last_element<Ts...>::type
851   >::type>::value>::type
852 toAppendDelim(const Delimiter& delim, const Ts&... vs) {
853   detail::toAppendDelimStrImpl(delim, vs...);
854 }
855
856 /**
857  * Detail in comment for toAppendFit
858  */
859 template <class Delimiter, class... Ts>
860 typename std::enable_if<
861   IsSomeString<
862   typename std::remove_pointer<
863     typename detail::last_element<Ts...>::type
864   >::type>::value>::type
865 toAppendDelimFit(const Delimiter& delim, const Ts&... vs) {
866   detail::reserveInTargetDelim(delim, vs...);
867   toAppendDelim(delim, vs...);
868 }
869
870 template <class De, class Ts>
871 void toAppendDelimFit(const De&, const Ts&) {}
872
873 /**
874  * to<SomeString>(v1, v2, ...) uses toAppend() (see below) as back-end
875  * for all types.
876  */
877 template <class Tgt, class... Ts>
878 typename std::enable_if<
879   IsSomeString<Tgt>::value && (
880     sizeof...(Ts) != 1 ||
881     !std::is_same<Tgt, typename detail::last_element<Ts...>::type>::value),
882   Tgt>::type
883 to(const Ts&... vs) {
884   Tgt result;
885   toAppendFit(vs..., &result);
886   return result;
887 }
888
889 /**
890  * toDelim<SomeString>(SomeString str) returns itself.
891  */
892 template <class Tgt, class Delim, class Src>
893 typename std::enable_if<IsSomeString<Tgt>::value &&
894                             std::is_same<Tgt, Src>::value,
895                         Tgt>::type
896 toDelim(const Delim& /* delim */, const Src& value) {
897   return value;
898 }
899
900 /**
901  * toDelim<SomeString>(delim, v1, v2, ...) uses toAppendDelim() as
902  * back-end for all types.
903  */
904 template <class Tgt, class Delim, class... Ts>
905 typename std::enable_if<
906   IsSomeString<Tgt>::value && (
907     sizeof...(Ts) != 1 ||
908     !std::is_same<Tgt, typename detail::last_element<Ts...>::type>::value),
909   Tgt>::type
910 toDelim(const Delim& delim, const Ts&... vs) {
911   Tgt result;
912   toAppendDelimFit(delim, vs..., &result);
913   return result;
914 }
915
916 /*******************************************************************************
917  * Conversions from string types to integral types.
918  ******************************************************************************/
919
920 namespace detail {
921
922 /**
923  * Finds the first non-digit in a string. The number of digits
924  * searched depends on the precision of the Tgt integral. Assumes the
925  * string starts with NO whitespace and NO sign.
926  *
927  * The semantics of the routine is:
928  *   for (;; ++b) {
929  *     if (b >= e || !isdigit(*b)) return b;
930  *   }
931  *
932  *  Complete unrolling marks bottom-line (i.e. entire conversion)
933  *  improvements of 20%.
934  */
935   template <class Tgt>
936   const char* findFirstNonDigit(const char* b, const char* e) {
937     for (; b < e; ++b) {
938       auto const c = static_cast<unsigned>(*b) - '0';
939       if (c >= 10) break;
940     }
941     return b;
942   }
943
944   // Maximum value of number when represented as a string
945   template <class T> struct MaxString {
946     static const char*const value;
947   };
948
949   bool str_to_bool(StringPiece* src);
950
951   template <class Tgt>
952   Tgt digits_to(const char* b, const char* e);
953
954   extern template unsigned char digits_to<unsigned char>(const char* b,
955                                                          const char* e);
956   extern template unsigned short digits_to<unsigned short>(const char* b,
957                                                            const char* e);
958   extern template unsigned int digits_to<unsigned int>(const char* b,
959                                                        const char* e);
960   extern template unsigned long digits_to<unsigned long>(const char* b,
961                                                          const char* e);
962   extern template unsigned long long digits_to<unsigned long long>(
963       const char* b, const char* e);
964 #if FOLLY_HAVE_INT128_T
965   extern template unsigned __int128 digits_to<unsigned __int128>(const char* b,
966                                                                  const char* e);
967 #endif
968
969 }                                 // namespace detail
970
971 /**
972  * String represented as a pair of pointers to char to unsigned
973  * integrals. Assumes NO whitespace before or after.
974  */
975 template <class Tgt>
976 typename std::enable_if<
977   std::is_integral<Tgt>::value && !std::is_signed<Tgt>::value
978   && !std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
979   Tgt>::type
980 to(const char * b, const char * e) {
981   return detail::digits_to<Tgt>(b, e);
982 }
983
984 /**
985  * String represented as a pair of pointers to char to signed
986  * integrals. Assumes NO whitespace before or after. Allows an
987  * optional leading sign.
988  */
989 template <class Tgt>
990 typename std::enable_if<
991   std::is_integral<Tgt>::value && std::is_signed<Tgt>::value,
992   Tgt>::type
993 to(const char * b, const char * e) {
994   FOLLY_RANGE_CHECK(b < e, "Empty input string in conversion to integral",
995                     to<std::string>("b: ", intptr_t(b), " e: ", intptr_t(e)));
996   if (!isdigit(*b)) {
997     if (*b == '-') {
998       Tgt result = -to<typename std::make_unsigned<Tgt>::type>(b + 1, e);
999       FOLLY_RANGE_CHECK_BEGIN_END(result <= 0, "Negative overflow.", b, e);
1000       return result;
1001     }
1002     FOLLY_RANGE_CHECK_BEGIN_END(*b == '+', "Invalid lead character", b, e);
1003     ++b;
1004   }
1005   Tgt result = to<typename std::make_unsigned<Tgt>::type>(b, e);
1006   FOLLY_RANGE_CHECK_BEGIN_END(result >= 0, "Overflow", b, e);
1007   return result;
1008 }
1009
1010 /**
1011  * Parsing strings to integrals. These routines differ from
1012  * to<integral>(string) in that they take a POINTER TO a StringPiece
1013  * and alter that StringPiece to reflect progress information.
1014  */
1015
1016 /**
1017  * StringPiece to integrals, with progress information. Alters the
1018  * StringPiece parameter to munch the already-parsed characters.
1019  */
1020 template <class Tgt>
1021 typename std::enable_if<
1022   std::is_integral<Tgt>::value
1023   && !std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
1024   Tgt>::type
1025 to(StringPiece * src) {
1026
1027   auto b = src->data(), past = src->data() + src->size();
1028   for (;; ++b) {
1029     FOLLY_RANGE_CHECK_STRINGPIECE(b < past,
1030                                   "No digits found in input string", *src);
1031     if (!isspace(*b)) break;
1032   }
1033
1034   auto m = b;
1035
1036   // First digit is customized because we test for sign
1037   bool negative = false;
1038   /* static */ if (std::is_signed<Tgt>::value) {
1039     if (!isdigit(*m)) {
1040       if (*m == '-') {
1041         negative = true;
1042       } else {
1043         FOLLY_RANGE_CHECK_STRINGPIECE(*m == '+', "Invalid leading character in "
1044                                       "conversion to integral", *src);
1045       }
1046       ++b;
1047       ++m;
1048     }
1049   }
1050   FOLLY_RANGE_CHECK_STRINGPIECE(m < past, "No digits found in input string",
1051                                 *src);
1052   FOLLY_RANGE_CHECK_STRINGPIECE(isdigit(*m), "Non-digit character found", *src);
1053   m = detail::findFirstNonDigit<Tgt>(m + 1, past);
1054
1055   Tgt result;
1056   /* static */ if (!std::is_signed<Tgt>::value) {
1057     result = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
1058   } else {
1059     auto t = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
1060     if (negative) {
1061       result = -t;
1062       FOLLY_RANGE_CHECK_STRINGPIECE(is_non_positive(result),
1063                                     "Negative overflow", *src);
1064     } else {
1065       result = t;
1066       FOLLY_RANGE_CHECK_STRINGPIECE(is_non_negative(result), "Overflow", *src);
1067     }
1068   }
1069   src->advance(m - src->data());
1070   return result;
1071 }
1072
1073 /**
1074  * StringPiece to bool, with progress information. Alters the
1075  * StringPiece parameter to munch the already-parsed characters.
1076  */
1077 template <class Tgt>
1078 typename std::enable_if<
1079   std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
1080   Tgt>::type
1081 to(StringPiece * src) {
1082   return detail::str_to_bool(src);
1083 }
1084
1085 namespace detail {
1086
1087 /**
1088  * Enforce that the suffix following a number is made up only of whitespace.
1089  */
1090 inline void enforceWhitespace(const char* b, const char* e) {
1091   for (; b != e; ++b) {
1092     FOLLY_RANGE_CHECK_BEGIN_END(isspace(*b),
1093                                 to<std::string>("Non-whitespace: ", *b),
1094                                 b, e);
1095   }
1096 }
1097
1098 }  // namespace detail
1099
1100 /**
1101  * String or StringPiece to integrals. Accepts leading and trailing
1102  * whitespace, but no non-space trailing characters.
1103  */
1104 template <class Tgt>
1105 typename std::enable_if<
1106   std::is_integral<Tgt>::value,
1107   Tgt>::type
1108 to(StringPiece src) {
1109   Tgt result = to<Tgt>(&src);
1110   detail::enforceWhitespace(src.data(), src.data() + src.size());
1111   return result;
1112 }
1113
1114 /*******************************************************************************
1115  * Conversions from string types to floating-point types.
1116  ******************************************************************************/
1117
1118 /**
1119  * StringPiece to double, with progress information. Alters the
1120  * StringPiece parameter to munch the already-parsed characters.
1121  */
1122 template <class Tgt>
1123 inline typename std::enable_if<
1124   std::is_floating_point<Tgt>::value,
1125   Tgt>::type
1126 to(StringPiece *const src) {
1127   using namespace double_conversion;
1128   static StringToDoubleConverter
1129     conv(StringToDoubleConverter::ALLOW_TRAILING_JUNK
1130          | StringToDoubleConverter::ALLOW_LEADING_SPACES,
1131          0.0,
1132          // return this for junk input string
1133          std::numeric_limits<double>::quiet_NaN(),
1134          nullptr, nullptr);
1135
1136   FOLLY_RANGE_CHECK_STRINGPIECE(!src->empty(),
1137                                 "No digits found in input string", *src);
1138
1139   int length;
1140   auto result = conv.StringToDouble(src->data(),
1141                                     static_cast<int>(src->size()),
1142                                     &length); // processed char count
1143
1144   if (!std::isnan(result)) {
1145     src->advance(length);
1146     return result;
1147   }
1148
1149   for (;; src->advance(1)) {
1150     if (src->empty()) {
1151       throw std::range_error("Unable to convert an empty string"
1152                              " to a floating point value.");
1153     }
1154     if (!isspace(src->front())) {
1155       break;
1156     }
1157   }
1158
1159   // Was that "inf[inity]"?
1160   if (src->size() >= 3 && toupper((*src)[0]) == 'I'
1161         && toupper((*src)[1]) == 'N' && toupper((*src)[2]) == 'F') {
1162     if (src->size() >= 8 &&
1163         toupper((*src)[3]) == 'I' &&
1164         toupper((*src)[4]) == 'N' &&
1165         toupper((*src)[5]) == 'I' &&
1166         toupper((*src)[6]) == 'T' &&
1167         toupper((*src)[7]) == 'Y') {
1168       src->advance(8);
1169     } else {
1170       src->advance(3);
1171     }
1172     return std::numeric_limits<Tgt>::infinity();
1173   }
1174
1175   // Was that "-inf[inity]"?
1176   if (src->size() >= 4 && toupper((*src)[0]) == '-'
1177       && toupper((*src)[1]) == 'I' && toupper((*src)[2]) == 'N'
1178       && toupper((*src)[3]) == 'F') {
1179     if (src->size() >= 9 &&
1180         toupper((*src)[4]) == 'I' &&
1181         toupper((*src)[5]) == 'N' &&
1182         toupper((*src)[6]) == 'I' &&
1183         toupper((*src)[7]) == 'T' &&
1184         toupper((*src)[8]) == 'Y') {
1185       src->advance(9);
1186     } else {
1187       src->advance(4);
1188     }
1189     return -std::numeric_limits<Tgt>::infinity();
1190   }
1191
1192   // "nan"?
1193   if (src->size() >= 3 && toupper((*src)[0]) == 'N'
1194         && toupper((*src)[1]) == 'A' && toupper((*src)[2]) == 'N') {
1195     src->advance(3);
1196     return std::numeric_limits<Tgt>::quiet_NaN();
1197   }
1198
1199   // "-nan"?
1200   if (src->size() >= 4 &&
1201       toupper((*src)[0]) == '-' &&
1202       toupper((*src)[1]) == 'N' &&
1203       toupper((*src)[2]) == 'A' &&
1204       toupper((*src)[3]) == 'N') {
1205     src->advance(4);
1206     return -std::numeric_limits<Tgt>::quiet_NaN();
1207   }
1208
1209   // All bets are off
1210   throw std::range_error("Unable to convert \"" + src->toString()
1211                          + "\" to a floating point value.");
1212 }
1213
1214 /**
1215  * Any string, const char*, or StringPiece to double.
1216  */
1217 template <class Tgt>
1218 typename std::enable_if<
1219   std::is_floating_point<Tgt>::value,
1220   Tgt>::type
1221 to(StringPiece src) {
1222   Tgt result = Tgt(to<double>(&src));
1223   detail::enforceWhitespace(src.data(), src.data() + src.size());
1224   return result;
1225 }
1226
1227 /*******************************************************************************
1228  * Integral to floating point and back
1229  ******************************************************************************/
1230
1231 /**
1232  * Checked conversion from integral to floating point and back. The
1233  * result must be convertible back to the source type without loss of
1234  * precision. This seems Draconian but sometimes is what's needed, and
1235  * complements existing routines nicely. For various rounding
1236  * routines, see <math>.
1237  */
1238 template <class Tgt, class Src>
1239 typename std::enable_if<
1240   (std::is_integral<Src>::value && std::is_floating_point<Tgt>::value)
1241   ||
1242   (std::is_floating_point<Src>::value && std::is_integral<Tgt>::value),
1243   Tgt>::type
1244 to(const Src & value) {
1245   Tgt result = Tgt(value);
1246   auto witness = static_cast<Src>(result);
1247   if (value != witness) {
1248     throw std::range_error(
1249       to<std::string>("to<>: loss of precision when converting ", value,
1250 #ifdef FOLLY_HAS_RTTI
1251                       " to type ", typeid(Tgt).name()
1252 #else
1253                       " to other type"
1254 #endif
1255                       ).c_str());
1256   }
1257   return result;
1258 }
1259
1260 /*******************************************************************************
1261  * Enum to anything and back
1262  ******************************************************************************/
1263
1264 template <class Tgt, class Src>
1265 typename std::enable_if<
1266   std::is_enum<Src>::value && !std::is_same<Src, Tgt>::value, Tgt>::type
1267 to(const Src & value) {
1268   return to<Tgt>(static_cast<typename std::underlying_type<Src>::type>(value));
1269 }
1270
1271 template <class Tgt, class Src>
1272 typename std::enable_if<
1273   std::is_enum<Tgt>::value && !std::is_same<Src, Tgt>::value, Tgt>::type
1274 to(const Src & value) {
1275   return static_cast<Tgt>(to<typename std::underlying_type<Tgt>::type>(value));
1276 }
1277
1278 } // namespace folly
1279
1280 // FOLLY_CONV_INTERNAL is defined by Conv.cpp.  Keep the FOLLY_RANGE_CHECK
1281 // macro for use in Conv.cpp, but #undefine it everywhere else we are included,
1282 // to avoid defining this global macro name in other files that include Conv.h.
1283 #ifndef FOLLY_CONV_INTERNAL
1284 #undef FOLLY_RANGE_CHECK
1285 #undef FOLLY_RANGE_CHECK_BEGIN_END
1286 #undef FOLLY_RANGE_CHECK_STRINGPIECE
1287 #undef FOLLY_RANGE_CHECK_STRINGIZE
1288 #undef FOLLY_RANGE_CHECK_STRINGIZE2
1289 #endif
1290
1291 #endif /* FOLLY_BASE_CONV_H_ */