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