Update the folly/README
[folly.git] / folly / Conv.h
1 /*
2  * Copyright 2012 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 "double-conversion.h"   // V8 JavaScript implementation
41
42 #define FOLLY_RANGE_CHECK(condition, message)                           \
43   ((condition) ? (void)0 : throw std::range_error(                      \
44     (__FILE__ "(" + std::to_string((long long int) __LINE__) + "): "    \
45      + (message)).c_str()))
46
47 namespace folly {
48
49 /*******************************************************************************
50  * Integral to integral
51  ******************************************************************************/
52
53 /**
54  * Checked conversion from integral to integral. The checks are only
55  * performed when meaningful, e.g. conversion from int to long goes
56  * unchecked.
57  */
58 template <class Tgt, class Src>
59 typename std::enable_if<
60   std::is_integral<Src>::value && std::is_integral<Tgt>::value,
61   Tgt>::type
62 to(const Src & value) {
63   /* static */ if (std::numeric_limits<Tgt>::max()
64                    < std::numeric_limits<Src>::max()) {
65     FOLLY_RANGE_CHECK(value <= std::numeric_limits<Tgt>::max(),
66                       "Overflow");
67   }
68   /* static */ if (std::is_signed<Src>::value &&
69                    (!std::is_signed<Tgt>::value || sizeof(Src) > sizeof(Tgt))) {
70     FOLLY_RANGE_CHECK(value >= std::numeric_limits<Tgt>::min(),
71                       "Negative overflow");
72   }
73   return static_cast<Tgt>(value);
74 }
75
76 /*******************************************************************************
77  * Floating point to floating point
78  ******************************************************************************/
79
80 template <class Tgt, class Src>
81 typename std::enable_if<
82   std::is_floating_point<Tgt>::value && std::is_floating_point<Src>::value,
83   Tgt>::type
84 to(const Src & value) {
85   /* static */ if (std::numeric_limits<Tgt>::max() <
86                    std::numeric_limits<Src>::max()) {
87     FOLLY_RANGE_CHECK(value <= std::numeric_limits<Tgt>::max(),
88                       "Overflow");
89     FOLLY_RANGE_CHECK(value >= -std::numeric_limits<Tgt>::max(),
90                       "Negative overflow");
91   }
92   return boost::implicit_cast<Tgt>(value);
93 }
94
95 /*******************************************************************************
96  * Anything to string
97  ******************************************************************************/
98
99 namespace detail {
100
101 template <class T> struct IsSomeString {
102   enum { value = std::is_same<T, std::string>::value
103          || std::is_same<T, fbstring>::value };
104 };
105
106 template <class T>
107 const T& getLastElement(const T & v) {
108   return v;
109 }
110
111 template <class T, class... Ts>
112 typename std::tuple_element<
113   sizeof...(Ts),
114   std::tuple<T, Ts...> >::type const&
115   getLastElement(const T& v, const Ts&... vs) {
116   return getLastElement(vs...);
117 }
118
119 /*******************************************************************************
120  * Conversions from integral types to string types.
121  ******************************************************************************/
122
123 // Returns the offset of the formatted string from the start of
124 // the supplied buffer. The new string will be at range
125 // [buf+begin,buf+bufLen). Uint will be either uint32_t or uint64_t.
126 template <class Uint>
127 size_t uintToBuffer(char*const buffer, size_t bufLen, Uint v) {
128   extern const char digit1[101], digit2[101];
129   for (;;) {
130     if (v < 100) {
131       if (v < 10) {
132         buffer[--bufLen] = static_cast<char>(v + '0');
133       } else {
134         size_t r = static_cast<size_t>(v);
135         bufLen -= 2;
136         buffer[bufLen] = digit1[r];
137         buffer[bufLen + 1] = digit2[r];
138       }
139       break;
140     }
141     Uint t = v;
142     v /= 100;
143     size_t r = static_cast<size_t> (t - v * 100);
144     bufLen -= 2;
145     buffer[bufLen] = digit1[r];
146     buffer[bufLen + 1] = digit2[r];
147   }
148   return bufLen;
149 }
150
151 const size_t kMaxInt64BufLen = 21;// 19 + 1 for possible '-' sign + 1 for \0
152
153 }                                 // namespace detail
154
155 /**
156  * A single char gets appended.
157  */
158 template <class Tgt>
159 void toAppend(char value, Tgt * result) {
160   *result += value;
161 }
162
163 /**
164  * Everything implicitly convertible to const char* gets appended.
165  */
166 template <class Tgt, class Src>
167 typename std::enable_if<
168   std::is_convertible<Src, const char*>::value
169   && detail::IsSomeString<Tgt>::value>::type
170 toAppend(Src value, Tgt * result) {
171   // Treat null pointers like an empty string, as in:
172   // operator<<(std::ostream&, const char*).
173   const char* c = value;
174   if (c) {
175     result->append(value);
176   }
177 }
178
179 /**
180  * Strings get appended, too.
181  */
182 template <class Tgt, class Src>
183 typename std::enable_if<
184   detail::IsSomeString<Src>::value && detail::IsSomeString<Tgt>::value>::type
185 toAppend(const Src& value, Tgt * result) {
186   result->append(value);
187 }
188
189 /**
190  * and StringPiece objects too
191  */
192 template <class Tgt>
193 typename std::enable_if<
194    detail::IsSomeString<Tgt>::value>::type
195 toAppend(StringPiece value, Tgt * result) {
196   result->append(value.data(), value.size());
197 }
198
199 /**
200  * There's no implicit conversion from fbstring to other string types,
201  * so make a specialization.
202  */
203 template <class Tgt>
204 typename std::enable_if<
205    detail::IsSomeString<Tgt>::value>::type
206 toAppend(const fbstring& value, Tgt * result) {
207   result->append(value.data(), value.size());
208 }
209
210 /**
211  * int32_t and int64_t to string (by appending) go through here. The
212  * result is APPENDED to a preexisting string passed as the second
213  * parameter. For convenience, the function also returns a reference
214  * to *result. This should be efficient with fbstring because fbstring
215  * incurs no dynamic allocation below 23 bytes and no number has more
216  * than 22 bytes in its textual representation (20 for digits, one for
217  * sign, one for the terminating 0).
218  */
219 template <class Tgt, class Src>
220 typename std::enable_if<
221   std::is_integral<Src>::value && std::is_signed<Src>::value
222   && detail::IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
223 toAppend(Src value, Tgt * result) {
224   typedef typename std::make_unsigned<Src>::type Usrc;
225   char buffer[detail::kMaxInt64BufLen];
226   size_t begin;
227   if (value < 0) {
228     begin = detail::uintToBuffer(buffer, sizeof(buffer),
229                                  static_cast<Usrc>(-value));
230     DCHECK_GE(begin, 1);
231     buffer[--begin] = '-';
232   } else {
233     begin = detail::uintToBuffer(buffer, sizeof(buffer),
234                                  static_cast<Usrc>(value));
235   }
236   result->append(buffer + begin, buffer + sizeof(buffer));
237 }
238
239 /**
240  * As above, but for uint32_t and uint64_t.
241  */
242 template <class Tgt, class Src>
243 typename std::enable_if<
244   std::is_integral<Src>::value && !std::is_signed<Src>::value
245   && detail::IsSomeString<Tgt>::value && sizeof(Src) >= 4>::type
246 toAppend(Src value, Tgt * result) {
247   char buffer[detail::kMaxInt64BufLen];
248   const size_t begin = detail::uintToBuffer(buffer, sizeof(buffer), value);
249   result->append(buffer + begin, buffer + sizeof(buffer));
250 }
251
252 /**
253  * All small signed and unsigned integers to string go through 32-bit
254  * types int32_t and uint32_t, respectively.
255  */
256 template <class Tgt, class Src>
257 typename std::enable_if<
258   std::is_integral<Src>::value
259   && detail::IsSomeString<Tgt>::value && sizeof(Src) < 4>::type
260 toAppend(Src value, Tgt * result) {
261   typedef typename
262     std::conditional<std::is_signed<Src>::value, int64_t, uint64_t>::type
263     Intermediate;
264   toAppend<Tgt>(static_cast<Intermediate>(value), result);
265 }
266
267 /**
268  * Enumerated values get appended as integers.
269  */
270 template <class Tgt, class Src>
271 typename std::enable_if<
272   std::is_enum<Src>::value && detail::IsSomeString<Tgt>::value>::type
273 toAppend(Src value, Tgt * result) {
274   /* static */ if (Src(-1) < 0) {
275     /* static */ if (sizeof(Src) <= sizeof(int)) {
276       toAppend(static_cast<int>(value), result);
277     } else {
278       toAppend(static_cast<long>(value), result);
279     }
280   } else {
281     /* static */ if (sizeof(Src) <= sizeof(int)) {
282       toAppend(static_cast<unsigned int>(value), result);
283     } else {
284       toAppend(static_cast<unsigned long>(value), result);
285     }
286   }
287 }
288
289 /*******************************************************************************
290  * Conversions from floating-point types to string types.
291  ******************************************************************************/
292
293 /** Wrapper around DoubleToStringConverter **/
294 template <class Tgt, class Src>
295 typename std::enable_if<
296   std::is_floating_point<Src>::value
297   && detail::IsSomeString<Tgt>::value>::type
298 toAppend(
299   Src value,
300   Tgt * result,
301   double_conversion::DoubleToStringConverter::DtoaMode mode,
302   unsigned int numDigits) {
303   using namespace double_conversion;
304   DoubleToStringConverter
305     conv(DoubleToStringConverter::NO_FLAGS,
306          "infinity", "NaN", 'E',
307          -6,  // decimal in shortest low
308          21,  // decimal in shortest high
309          6,   // max leading padding zeros
310          1);  // max trailing padding zeros
311   char buffer[256];
312   StringBuilder builder(buffer, sizeof(buffer));
313   switch (mode) {
314     case DoubleToStringConverter::SHORTEST:
315       conv.ToShortest(value, &builder);
316       break;
317     case DoubleToStringConverter::FIXED:
318       conv.ToFixed(value, numDigits, &builder);
319       break;
320     default:
321       CHECK(mode == DoubleToStringConverter::PRECISION);
322       conv.ToPrecision(value, numDigits, &builder);
323       break;
324   }
325   const size_t length = builder.position();
326   builder.Finalize();
327   result->append(buffer, length);
328 }
329
330 /**
331  * As above, but for floating point
332  */
333 template <class Tgt, class Src>
334 typename std::enable_if<
335   std::is_floating_point<Src>::value
336   && detail::IsSomeString<Tgt>::value>::type
337 toAppend(Src value, Tgt * result) {
338   toAppend(
339     value, result, double_conversion::DoubleToStringConverter::SHORTEST, 0);
340 }
341
342 /**
343  * Variadic conversion to string. Appends each element in turn.
344  */
345 template <class T, class... Ts>
346 typename std::enable_if<sizeof...(Ts) >= 2
347   && detail::IsSomeString<
348   typename std::remove_pointer<
349     typename std::tuple_element<
350       sizeof...(Ts) - 1, std::tuple<Ts...>
351       >::type>::type>::value>::type
352 toAppend(const T& v, const Ts&... vs) {
353   toAppend(v, detail::getLastElement(vs...));
354   toAppend(vs...);
355 }
356
357 /**
358  * Variadic base case: do nothing.
359  */
360 template <class Tgt>
361 typename std::enable_if<detail::IsSomeString<Tgt>::value>::type
362 toAppend(Tgt* result) {
363 }
364
365 /**
366  * to<SomeString>(v1, v2, ...) uses toAppend() (see below) as back-end
367  * for all types.
368  */
369 template <class Tgt, class... Ts>
370 typename std::enable_if<detail::IsSomeString<Tgt>::value, Tgt>::type
371 to(const Ts&... vs) {
372   Tgt result;
373   toAppend(vs..., &result);
374   return result;
375 }
376
377 /*******************************************************************************
378  * Conversions from string types to integral types.
379  ******************************************************************************/
380
381 namespace detail {
382
383 /**
384  * Finds the first non-digit in a string. The number of digits
385  * searched depends on the precision of the Tgt integral. Assumes the
386  * string starts with NO whitespace and NO sign.
387  *
388  * The semantics of the routine is:
389  *   for (;; ++b) {
390  *     if (b >= e || !isdigit(*b)) return b;
391  *   }
392  *
393  *  Complete unrolling marks bottom-line (i.e. entire conversion)
394  *  improvements of 20%.
395  */
396   template <class Tgt>
397   const char* findFirstNonDigit(const char* b, const char* e) {
398     for (; b < e; ++b) {
399       auto const c = static_cast<unsigned>(*b) - '0';
400       if (c >= 10) break;
401     }
402     return b;
403   }
404
405   // Maximum value of number when represented as a string
406   template <class T> struct MaxString {
407     static const char*const value;
408   };
409
410 /**
411  * String represented as a pair of pointers to char to unsigned
412  * integrals. Assumes NO whitespace before or after, and also that the
413  * string is composed entirely of digits. Tgt must be unsigned, and no
414  * sign is allowed in the string (even it's '+'). String may be empty,
415  * in which case digits_to throws.
416  */
417   template <class Tgt>
418   Tgt digits_to(const char * b, const char * e) {
419
420     static_assert(!std::is_signed<Tgt>::value, "Unsigned type expected");
421     assert(b <= e);
422
423     const size_t size = e - b;
424
425     /* Although the string is entirely made of digits, we still need to
426      * check for overflow.
427      */
428     if (size >= std::numeric_limits<Tgt>::digits10 + 1) {
429       // Leading zeros? If so, recurse to keep things simple
430       if (b < e && *b == '0') {
431         for (++b;; ++b) {
432           if (b == e) return 0; // just zeros, e.g. "0000"
433           if (*b != '0') return digits_to<Tgt>(b, e);
434         }
435       }
436       FOLLY_RANGE_CHECK(size == std::numeric_limits<Tgt>::digits10 + 1 &&
437                         strncmp(b, detail::MaxString<Tgt>::value, size) <= 0,
438                         "Numeric overflow upon conversion");
439     }
440
441     // Here we know that the number won't overflow when
442     // converted. Proceed without checks.
443
444     static const Tgt power10[20] = {
445       static_cast<Tgt>(10000000000000000000UL),
446       static_cast<Tgt>(1000000000000000000UL),
447       static_cast<Tgt>(100000000000000000UL),
448       static_cast<Tgt>(10000000000000000UL),
449       static_cast<Tgt>(1000000000000000UL),
450       static_cast<Tgt>(100000000000000UL),
451       static_cast<Tgt>(10000000000000UL),
452       static_cast<Tgt>(1000000000000UL),
453       static_cast<Tgt>(100000000000UL),
454       static_cast<Tgt>(10000000000UL),
455       static_cast<Tgt>(1000000000UL),
456       static_cast<Tgt>(100000000UL),
457       static_cast<Tgt>(10000000UL),
458       static_cast<Tgt>(1000000UL),
459       static_cast<Tgt>(100000UL),
460       static_cast<Tgt>(10000UL),
461       static_cast<Tgt>(1000UL),
462       static_cast<Tgt>(100UL),
463       static_cast<Tgt>(10UL),
464       static_cast<Tgt>(1UL),
465     };
466
467     size_t powIdx = sizeof(power10) / sizeof(*power10) - size;
468     Tgt result = 0;
469
470     for (; e - b >= 4; b += 4, powIdx += 4) {
471       const auto c0 = static_cast<unsigned>(*b) - '0';
472       if (c0 >= 10) goto failure;
473       const auto r0 = power10[powIdx] * c0;
474       const auto c1 = static_cast<unsigned>(b[1]) - '0';
475       if (c1 >= 10) goto failure;
476       const auto r1 = power10[powIdx + 1] * c1;
477       const auto c2 = static_cast<unsigned>(b[2]) - '0';
478       if (c2 >= 10) goto failure;
479       const auto r2 = power10[powIdx + 2] * c2;
480       const auto c3 = static_cast<unsigned>(b[3]) - '0';
481       if (c3 >= 10) goto failure;
482       const auto r3 = power10[powIdx + 3] * c3;
483       result += r0 + r1 + r2 + r3;
484     }
485
486     switch (e - b) {
487       case 3: {
488         const auto c0 = static_cast<unsigned>(*b) - '0';
489         if (c0 >= 10) goto failure;
490         const auto c1 = static_cast<unsigned>(b[1]) - '0';
491         if (c1 >= 10) goto failure;
492         const auto c2 = static_cast<unsigned>(b[2]) - '0';
493         if (c2 >= 10) goto failure;
494         return result + 100 * c0 + 10 * c1 + c2;
495       }
496       case 2: {
497         const auto c0 = static_cast<unsigned>(*b) - '0';
498         if (c0 >= 10) goto failure;
499         const auto c1 = static_cast<unsigned>(b[1]) - '0';
500         if (c1 >= 10) goto failure;
501         return result + 10 * c0 + c1;
502       }
503       case 1: {
504         const auto c0 = static_cast<unsigned>(*b) - '0';
505         if (c0 >= 10) goto failure;
506         return result + c0;
507       }
508     }
509
510     assert(b == e);
511     FOLLY_RANGE_CHECK(size > 0, "Found no digits to convert in input");
512     return result;
513
514     failure:
515     throw std::range_error("Cannot convert string " +
516                            std::string(e - size, e) + " to integral.");
517   }
518
519   bool str_to_bool(StringPiece * src);
520
521 }                                 // namespace detail
522
523 /**
524  * String represented as a pair of pointers to char to unsigned
525  * integrals. Assumes NO whitespace before or after.
526  */
527 template <class Tgt>
528 typename std::enable_if<
529   std::is_integral<Tgt>::value && !std::is_signed<Tgt>::value
530   && !std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
531   Tgt>::type
532 to(const char * b, const char * e) {
533   return detail::digits_to<Tgt>(b, e);
534 }
535
536 /**
537  * String represented as a pair of pointers to char to signed
538  * integrals. Assumes NO whitespace before or after. Allows an
539  * optional leading sign.
540  */
541 template <class Tgt>
542 typename std::enable_if<
543   std::is_integral<Tgt>::value && std::is_signed<Tgt>::value,
544   Tgt>::type
545 to(const char * b, const char * e) {
546   FOLLY_RANGE_CHECK(b < e, "Empty input string in conversion to integral");
547   if (!isdigit(*b)) {
548     if (*b == '-') {
549       Tgt result = -to<typename std::make_unsigned<Tgt>::type>(b + 1, e);
550       FOLLY_RANGE_CHECK(result <= 0, "Negative overflow.");
551       return result;
552     }
553     FOLLY_RANGE_CHECK(*b == '+', "Invalid lead character");
554     ++b;
555   }
556   Tgt result = to<typename std::make_unsigned<Tgt>::type>(b, e);
557   FOLLY_RANGE_CHECK(result >= 0, "Overflow.");
558   return result;
559 }
560
561 /**
562  * Parsing strings to integrals. These routines differ from
563  * to<integral>(string) in that they take a POINTER TO a StringPiece
564  * and alter that StringPiece to reflect progress information.
565  */
566
567 /**
568  * StringPiece to integrals, with progress information. Alters the
569  * StringPiece parameter to munch the already-parsed characters.
570  */
571 template <class Tgt>
572 typename std::enable_if<
573   std::is_integral<Tgt>::value
574   && !std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
575   Tgt>::type
576 to(StringPiece * src) {
577
578   auto b = src->data(), past = src->data() + src->size();
579   for (;; ++b) {
580     FOLLY_RANGE_CHECK(b < past, "No digits found in input string");
581     if (!isspace(*b)) break;
582   }
583
584   auto m = b;
585
586   // First digit is customized because we test for sign
587   bool negative = false;
588   /* static */ if (std::is_signed<Tgt>::value) {
589     if (!isdigit(*m)) {
590       if (*m == '-') {
591         negative = true;
592       } else {
593         FOLLY_RANGE_CHECK(*m == '+', "Invalid leading character in conversion"
594                           " to integral");
595       }
596       ++b;
597       ++m;
598     }
599   }
600   FOLLY_RANGE_CHECK(m < past, "No digits found in input string");
601   FOLLY_RANGE_CHECK(isdigit(*m), "Non-digit character found");
602   m = detail::findFirstNonDigit<Tgt>(m + 1, past);
603
604   Tgt result;
605   /* static */ if (!std::is_signed<Tgt>::value) {
606     result = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
607   } else {
608     auto t = detail::digits_to<typename std::make_unsigned<Tgt>::type>(b, m);
609     if (negative) {
610       result = -t;
611       FOLLY_RANGE_CHECK(result <= 0, "Negative overflow");
612     } else {
613       result = t;
614       FOLLY_RANGE_CHECK(result >= 0, "Overflow");
615     }
616   }
617   src->advance(m - src->data());
618   return result;
619 }
620
621 /**
622  * StringPiece to bool, with progress information. Alters the
623  * StringPiece parameter to munch the already-parsed characters.
624  */
625 template <class Tgt>
626 typename std::enable_if<
627   std::is_same<typename std::remove_cv<Tgt>::type, bool>::value,
628   Tgt>::type
629 to(StringPiece * src) {
630   return detail::str_to_bool(src);
631 }
632
633 namespace detail {
634
635 /**
636  * Enforce that the suffix following a number is made up only of whitespace.
637  */
638 inline void enforceWhitespace(const char* b, const char* e) {
639   for (; b != e; ++b) {
640     FOLLY_RANGE_CHECK(isspace(*b), to<std::string>("Non-whitespace: ", *b));
641   }
642 }
643
644 }  // namespace detail
645
646 /**
647  * String or StringPiece to integrals. Accepts leading and trailing
648  * whitespace, but no non-space trailing characters.
649  */
650 template <class Tgt>
651 typename std::enable_if<
652   std::is_integral<Tgt>::value,
653   Tgt>::type
654 to(StringPiece src) {
655   Tgt result = to<Tgt>(&src);
656   detail::enforceWhitespace(src.data(), src.data() + src.size());
657   return result;
658 }
659
660 /*******************************************************************************
661  * Conversions from string types to floating-point types.
662  ******************************************************************************/
663
664 /**
665  * StringPiece to double, with progress information. Alters the
666  * StringPiece parameter to munch the already-parsed characters.
667  */
668 template <class Tgt>
669 inline typename std::enable_if<
670   std::is_floating_point<Tgt>::value,
671   Tgt>::type
672 to(StringPiece *const src) {
673   using namespace double_conversion;
674   static StringToDoubleConverter
675     conv(StringToDoubleConverter::ALLOW_TRAILING_JUNK
676          | StringToDoubleConverter::ALLOW_LEADING_SPACES,
677          0.0,
678          // return this for junk input string
679          std::numeric_limits<double>::quiet_NaN(),
680          nullptr, nullptr);
681
682   FOLLY_RANGE_CHECK(!src->empty(), "No digits found in input string");
683
684   int length;
685   auto result = conv.StringToDouble(src->data(), src->size(),
686                                        &length); // processed char count
687
688   if (!std::isnan(result)) {
689     src->advance(length);
690     return result;
691   }
692
693   for (;; src->advance(1)) {
694     if (src->empty()) {
695       throw std::range_error("Unable to convert an empty string"
696                              " to a floating point value.");
697     }
698     if (!isspace(src->front())) {
699       break;
700     }
701   }
702
703   // Was that "inf[inity]"?
704   if (src->size() >= 3 && toupper((*src)[0]) == 'I'
705         && toupper((*src)[1]) == 'N' && toupper((*src)[2]) == 'F') {
706     if (src->size() >= 8 &&
707         toupper((*src)[3]) == 'I' &&
708         toupper((*src)[4]) == 'N' &&
709         toupper((*src)[5]) == 'I' &&
710         toupper((*src)[6]) == 'T' &&
711         toupper((*src)[7]) == 'Y') {
712       src->advance(8);
713     } else {
714       src->advance(3);
715     }
716     return std::numeric_limits<Tgt>::infinity();
717   }
718
719   // Was that "-inf[inity]"?
720   if (src->size() >= 4 && toupper((*src)[0]) == '-'
721       && toupper((*src)[1]) == 'I' && toupper((*src)[2]) == 'N'
722       && toupper((*src)[3]) == 'F') {
723     if (src->size() >= 9 &&
724         toupper((*src)[4]) == 'I' &&
725         toupper((*src)[5]) == 'N' &&
726         toupper((*src)[6]) == 'I' &&
727         toupper((*src)[7]) == 'T' &&
728         toupper((*src)[8]) == 'Y') {
729       src->advance(9);
730     } else {
731       src->advance(4);
732     }
733     return -std::numeric_limits<Tgt>::infinity();
734   }
735
736   // "nan"?
737   if (src->size() >= 3 && toupper((*src)[0]) == 'N'
738         && toupper((*src)[1]) == 'A' && toupper((*src)[2]) == 'N') {
739     src->advance(3);
740     return std::numeric_limits<Tgt>::quiet_NaN();
741   }
742
743   // All bets are off
744   throw std::range_error("Unable to convert \"" + src->toString()
745                          + "\" to a floating point value.");
746 }
747
748 /**
749  * Any string, const char*, or StringPiece to double.
750  */
751 template <class Tgt>
752 typename std::enable_if<
753   std::is_floating_point<Tgt>::value,
754   Tgt>::type
755 to(StringPiece src) {
756   Tgt result = to<double>(&src);
757   detail::enforceWhitespace(src.data(), src.data() + src.size());
758   return result;
759 }
760
761 /*******************************************************************************
762  * Integral to floating point and back
763  ******************************************************************************/
764
765 /**
766  * Checked conversion from integral to flating point and back. The
767  * result must be convertible back to the source type without loss of
768  * precision. This seems Draconian but sometimes is what's needed, and
769  * complements existing routines nicely. For various rounding
770  * routines, see <math>.
771  */
772 template <class Tgt, class Src>
773 typename std::enable_if<
774   (std::is_integral<Src>::value && std::is_floating_point<Tgt>::value)
775   ||
776   (std::is_floating_point<Src>::value && std::is_integral<Tgt>::value),
777   Tgt>::type
778 to(const Src & value) {
779   Tgt result = value;
780   auto witness = static_cast<Src>(result);
781   if (value != witness) {
782     throw std::range_error(
783       to<std::string>("to<>: loss of precision when converting ", value,
784                       " to type ", typeid(Tgt).name()).c_str());
785   }
786   return result;
787 }
788
789 /*******************************************************************************
790  * Enum to anything and back
791  ******************************************************************************/
792
793 template <class Tgt, class Src>
794 typename std::enable_if<std::is_enum<Src>::value, Tgt>::type
795 to(const Src & value) {
796   // TODO: uncomment this when underlying_type is available
797   // return to<Tgt>(static_cast<typename std::underlying_type<Src>::type>(
798   //    value));
799   /* static */ if (Src(-1) < 0) {
800     /* static */ if (sizeof(Src) <= sizeof(int)) {
801       return to<Tgt>(static_cast<int>(value));
802     } else {
803       return to<Tgt>(static_cast<long>(value));
804     }
805   } else {
806     /* static */ if (sizeof(Src) <= sizeof(int)) {
807       return to<Tgt>(static_cast<unsigned int>(value));
808     } else {
809       return to<Tgt>(static_cast<unsigned long>(value));
810     }
811   }
812 }
813
814 template <class Tgt, class Src>
815 typename std::enable_if<std::is_enum<Tgt>::value, Tgt>::type
816 to(const Src & value) {
817   // TODO: uncomment this when underlying_type is available
818   // return static_cast<Tgt>(
819   //    to<typename std::underlying_type<Tgt>::type>(value));
820   /* static */ if (Tgt(-1) < 0) {
821     /* static */ if (sizeof(Tgt) <= sizeof(int)) {
822       return static_cast<Tgt>(to<int>(value));
823     } else {
824       return static_cast<Tgt>(to<long>(value));
825     }
826   } else {
827     /* static */ if (sizeof(Tgt) <= sizeof(int)) {
828       return static_cast<Tgt>(to<unsigned int>(value));
829     } else {
830       return static_cast<Tgt>(to<unsigned long>(value));
831     }
832   }
833 }
834
835 } // namespace folly
836
837 // FOLLY_CONV_INTERNAL is defined by Conv.cpp.  Keep the FOLLY_RANGE_CHECK
838 // macro for use in Conv.cpp, but #undefine it everywhere else we are included,
839 // to avoid defining this global macro name in other files that include Conv.h.
840 #ifndef FOLLY_CONV_INTERNAL
841 #undef FOLLY_RANGE_CHECK
842 #endif
843
844 #endif /* FOLLY_BASE_CONV_H_ */