• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "strings/numbers.h"
2 
3 #include <float.h>  // for FLT_DIG
4 #include <cassert>
5 #include <memory>
6 
7 #include "strings/ascii_ctype.h"
8 
9 namespace dynamic_depth {
10 namespace strings {
11 namespace {
12 
13 // Represents integer values of digits.
14 // Uses 36 to indicate an invalid character since we support
15 // bases up to 36.
16 static const int8 kAsciiToInt[256] = {
17     36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,  // 16 36s.
18     36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
19     36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 0,  1,  2,  3,  4,  5,
20     6,  7,  8,  9,  36, 36, 36, 36, 36, 36, 36, 10, 11, 12, 13, 14, 15, 16, 17,
21     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
22     36, 36, 36, 36, 36, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
23     24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 36, 36, 36, 36, 36, 36,
24     36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
25     36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
26     36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
27     36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
28     36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
29     36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
30     36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36};
31 
32 // Parse the sign and optional hex or oct prefix in text.
safe_parse_sign_and_base(string * text,int * base_ptr,bool * negative_ptr)33 inline bool safe_parse_sign_and_base(string* text /*inout*/,
34                                      int* base_ptr /*inout*/,
35                                      bool* negative_ptr /*output*/) {
36   if (text->data() == NULL) {
37     return false;
38   }
39 
40   const char* start = text->data();
41   const char* end = start + text->size();
42   int base = *base_ptr;
43 
44   // Consume whitespace.
45   while (start < end && ascii_isspace(start[0])) {
46     ++start;
47   }
48   while (start < end && ascii_isspace(end[-1])) {
49     --end;
50   }
51   if (start >= end) {
52     return false;
53   }
54 
55   // Consume sign.
56   *negative_ptr = (start[0] == '-');
57   if (*negative_ptr || start[0] == '+') {
58     ++start;
59     if (start >= end) {
60       return false;
61     }
62   }
63 
64   // Consume base-dependent prefix.
65   //  base 0: "0x" -> base 16, "0" -> base 8, default -> base 10
66   //  base 16: "0x" -> base 16
67   // Also validate the base.
68   if (base == 0) {
69     if (end - start >= 2 && start[0] == '0' &&
70         (start[1] == 'x' || start[1] == 'X')) {
71       base = 16;
72       start += 2;
73       if (start >= end) {
74         // "0x" with no digits after is invalid.
75         return false;
76       }
77     } else if (end - start >= 1 && start[0] == '0') {
78       base = 8;
79       start += 1;
80     } else {
81       base = 10;
82     }
83   } else if (base == 16) {
84     if (end - start >= 2 && start[0] == '0' &&
85         (start[1] == 'x' || start[1] == 'X')) {
86       start += 2;
87       if (start >= end) {
88         // "0x" with no digits after is invalid.
89         return false;
90       }
91     }
92   } else if (base >= 2 && base <= 36) {
93     // okay
94   } else {
95     return false;
96   }
97   text->assign(start, end - start);
98   *base_ptr = base;
99   return true;
100 }
101 
102 // Consume digits.
103 //
104 // The classic loop:
105 //
106 //   for each digit
107 //     value = value * base + digit
108 //   value *= sign
109 //
110 // The classic loop needs overflow checking.  It also fails on the most
111 // negative integer, -2147483648 in 32-bit two's complement representation.
112 //
113 // My improved loop:
114 //
115 //  if (!negative)
116 //    for each digit
117 //      value = value * base
118 //      value = value + digit
119 //  else
120 //    for each digit
121 //      value = value * base
122 //      value = value - digit
123 //
124 // Overflow checking becomes simple.
125 
126 // Lookup tables per IntType:
127 // vmax/base and vmin/base are precomputed because division costs at least 8ns.
128 // TODO(junyer): Doing this per base instead (i.e. an array of structs, not a
129 // struct of arrays) would probably be better in terms of d-cache for the most
130 // commonly used bases.
131 template <typename IntType>
132 struct LookupTables {
133   static const IntType kVmaxOverBase[];
134   static const IntType kVminOverBase[];
135 };
136 
137 // An array initializer macro for X/base where base in [0, 36].
138 // However, note that lookups for base in [0, 1] should never happen because
139 // base has been validated to be in [2, 36] by safe_parse_sign_and_base().
140 #define X_OVER_BASE_INITIALIZER(X)                                    \
141   {                                                                   \
142       0,      0,      X / 2,  X / 3,  X / 4,  X / 5,  X / 6,  X / 7,  \
143       X / 8,  X / 9,  X / 10, X / 11, X / 12, X / 13, X / 14, X / 15, \
144       X / 16, X / 17, X / 18, X / 19, X / 20, X / 21, X / 22, X / 23, \
145       X / 24, X / 25, X / 26, X / 27, X / 28, X / 29, X / 30, X / 31, \
146       X / 32, X / 33, X / 34, X / 35, X / 36,                         \
147   };
148 
149 template <typename IntType>
150 const IntType LookupTables<IntType>::kVmaxOverBase[] =
151     X_OVER_BASE_INITIALIZER(std::numeric_limits<IntType>::max());
152 
153 template <typename IntType>
154 const IntType LookupTables<IntType>::kVminOverBase[] =
155     X_OVER_BASE_INITIALIZER(std::numeric_limits<IntType>::min());
156 
157 #undef X_OVER_BASE_INITIALIZER
158 
159 template <typename IntType>
safe_parse_positive_int(const string & text,int base,IntType * value_p)160 inline bool safe_parse_positive_int(const string& text, int base,
161                                     IntType* value_p) {
162   IntType value = 0;
163   const IntType vmax = std::numeric_limits<IntType>::max();
164   assert(vmax > 0);
165   assert(vmax >= base);
166   const IntType vmax_over_base = LookupTables<IntType>::kVmaxOverBase[base];
167   const char* start = text.data();
168   const char* end = start + text.size();
169   // loop over digits
170   for (; start < end; ++start) {
171     unsigned char c = static_cast<unsigned char>(start[0]);
172     int digit = kAsciiToInt[c];
173     if (digit >= base) {
174       *value_p = value;
175       return false;
176     }
177     if (value > vmax_over_base) {
178       *value_p = vmax;
179       return false;
180     }
181     value *= base;
182     if (value > vmax - digit) {
183       *value_p = vmax;
184       return false;
185     }
186     value += digit;
187   }
188   *value_p = value;
189   return true;
190 }
191 
192 template <typename IntType>
safe_parse_negative_int(const string & text,int base,IntType * value_p)193 inline bool safe_parse_negative_int(const string& text, int base,
194                                     IntType* value_p) {
195   IntType value = 0;
196   const IntType vmin = std::numeric_limits<IntType>::min();
197   assert(vmin < 0);
198   assert(vmin <= 0 - base);
199   IntType vmin_over_base = LookupTables<IntType>::kVminOverBase[base];
200   // 2003 c++ standard [expr.mul]
201   // "... the sign of the remainder is implementation-defined."
202   // Although (vmin/base)*base + vmin%base is always vmin.
203   // 2011 c++ standard tightens the spec but we cannot rely on it.
204   // TODO(junyer): Handle this in the lookup table generation.
205   if (vmin % base > 0) {
206     vmin_over_base += 1;
207   }
208   const char* start = text.data();
209   const char* end = start + text.size();
210   // loop over digits
211   for (; start < end; ++start) {
212     unsigned char c = static_cast<unsigned char>(start[0]);
213     int digit = kAsciiToInt[c];
214     if (digit >= base) {
215       *value_p = value;
216       return false;
217     }
218     if (value < vmin_over_base) {
219       *value_p = vmin;
220       return false;
221     }
222     value *= base;
223     if (value < vmin + digit) {
224       *value_p = vmin;
225       return false;
226     }
227     value -= digit;
228   }
229   *value_p = value;
230   return true;
231 }
232 
233 // Input format based on POSIX.1-2008 strtol
234 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html
235 template <typename IntType>
safe_int_internal(const string & text,IntType * value_p,int base)236 inline bool safe_int_internal(const string& text, IntType* value_p, int base) {
237   *value_p = 0;
238   bool negative;
239   string text_copy(text);
240   if (!safe_parse_sign_and_base(&text_copy, &base, &negative)) {
241     return false;
242   }
243   if (!negative) {
244     return safe_parse_positive_int(text_copy, base, value_p);
245   } else {
246     return safe_parse_negative_int(text_copy, base, value_p);
247   }
248 }
249 
250 template <typename IntType>
safe_uint_internal(const string & text,IntType * value_p,int base)251 inline bool safe_uint_internal(const string& text, IntType* value_p, int base) {
252   *value_p = 0;
253   bool negative;
254   string text_copy(text);
255   if (!safe_parse_sign_and_base(&text_copy, &base, &negative) || negative) {
256     return false;
257   }
258   return safe_parse_positive_int(text_copy, base, value_p);
259 }
260 
261 // Writes a two-character representation of 'i' to 'buf'. 'i' must be in the
262 // range 0 <= i < 100, and buf must have space for two characters. Example:
263 //   char buf[2];
264 //   PutTwoDigits(42, buf);
265 //   // buf[0] == '4'
266 //   // buf[1] == '2'
PutTwoDigits(size_t i,char * buf)267 inline void PutTwoDigits(size_t i, char* buf) {
268   static const char two_ASCII_digits[100][2] = {
269       {'0', '0'}, {'0', '1'}, {'0', '2'}, {'0', '3'}, {'0', '4'}, {'0', '5'},
270       {'0', '6'}, {'0', '7'}, {'0', '8'}, {'0', '9'}, {'1', '0'}, {'1', '1'},
271       {'1', '2'}, {'1', '3'}, {'1', '4'}, {'1', '5'}, {'1', '6'}, {'1', '7'},
272       {'1', '8'}, {'1', '9'}, {'2', '0'}, {'2', '1'}, {'2', '2'}, {'2', '3'},
273       {'2', '4'}, {'2', '5'}, {'2', '6'}, {'2', '7'}, {'2', '8'}, {'2', '9'},
274       {'3', '0'}, {'3', '1'}, {'3', '2'}, {'3', '3'}, {'3', '4'}, {'3', '5'},
275       {'3', '6'}, {'3', '7'}, {'3', '8'}, {'3', '9'}, {'4', '0'}, {'4', '1'},
276       {'4', '2'}, {'4', '3'}, {'4', '4'}, {'4', '5'}, {'4', '6'}, {'4', '7'},
277       {'4', '8'}, {'4', '9'}, {'5', '0'}, {'5', '1'}, {'5', '2'}, {'5', '3'},
278       {'5', '4'}, {'5', '5'}, {'5', '6'}, {'5', '7'}, {'5', '8'}, {'5', '9'},
279       {'6', '0'}, {'6', '1'}, {'6', '2'}, {'6', '3'}, {'6', '4'}, {'6', '5'},
280       {'6', '6'}, {'6', '7'}, {'6', '8'}, {'6', '9'}, {'7', '0'}, {'7', '1'},
281       {'7', '2'}, {'7', '3'}, {'7', '4'}, {'7', '5'}, {'7', '6'}, {'7', '7'},
282       {'7', '8'}, {'7', '9'}, {'8', '0'}, {'8', '1'}, {'8', '2'}, {'8', '3'},
283       {'8', '4'}, {'8', '5'}, {'8', '6'}, {'8', '7'}, {'8', '8'}, {'8', '9'},
284       {'9', '0'}, {'9', '1'}, {'9', '2'}, {'9', '3'}, {'9', '4'}, {'9', '5'},
285       {'9', '6'}, {'9', '7'}, {'9', '8'}, {'9', '9'}};
286   assert(i < 100);
287   memcpy(buf, two_ASCII_digits[i], 2);
288 }
289 
290 }  // anonymous namespace
291 
292 // ----------------------------------------------------------------------
293 // FastInt32ToBufferLeft()
294 // FastUInt32ToBufferLeft()
295 // FastInt64ToBufferLeft()
296 // FastUInt64ToBufferLeft()
297 //
298 // Like the Fast*ToBuffer() functions above, these are intended for speed.
299 // Unlike the Fast*ToBuffer() functions, however, these functions write
300 // their output to the beginning of the buffer (hence the name, as the
301 // output is left-aligned).  The caller is responsible for ensuring that
302 // the buffer has enough space to hold the output.
303 //
304 // Returns a pointer to the end of the string (i.e. the null character
305 // terminating the string).
306 // ----------------------------------------------------------------------
307 
308 // Used to optimize printing a decimal number's final digit.
309 const char one_ASCII_final_digits[10][2]{
310     {'0', 0}, {'1', 0}, {'2', 0}, {'3', 0}, {'4', 0},
311     {'5', 0}, {'6', 0}, {'7', 0}, {'8', 0}, {'9', 0},
312 };
313 
FastUInt32ToBufferLeft(uint32 u,char * buffer)314 char* FastUInt32ToBufferLeft(uint32 u, char* buffer) {
315   uint32 digits;
316   // The idea of this implementation is to trim the number of divides to as few
317   // as possible, and also reducing memory stores and branches, by going in
318   // steps of two digits at a time rather than one whenever possible.
319   // The huge-number case is first, in the hopes that the compiler will output
320   // that case in one branch-free block of code, and only output conditional
321   // branches into it from below.
322   if (u >= 1000000000) {     // >= 1,000,000,000
323     digits = u / 100000000;  // 100,000,000
324     u -= digits * 100000000;
325     PutTwoDigits(digits, buffer);
326     buffer += 2;
327   lt100_000_000:
328     digits = u / 1000000;  // 1,000,000
329     u -= digits * 1000000;
330     PutTwoDigits(digits, buffer);
331     buffer += 2;
332   lt1_000_000:
333     digits = u / 10000;  // 10,000
334     u -= digits * 10000;
335     PutTwoDigits(digits, buffer);
336     buffer += 2;
337   lt10_000:
338     digits = u / 100;
339     u -= digits * 100;
340     PutTwoDigits(digits, buffer);
341     buffer += 2;
342   lt100:
343     digits = u;
344     PutTwoDigits(digits, buffer);
345     buffer += 2;
346     *buffer = 0;
347     return buffer;
348   }
349 
350   if (u < 100) {
351     digits = u;
352     if (u >= 10) goto lt100;
353     memcpy(buffer, one_ASCII_final_digits[u], 2);
354     return buffer + 1;
355   }
356   if (u < 10000) {  // 10,000
357     if (u >= 1000) goto lt10_000;
358     digits = u / 100;
359     u -= digits * 100;
360     *buffer++ = '0' + digits;
361     goto lt100;
362   }
363   if (u < 1000000) {  // 1,000,000
364     if (u >= 100000) goto lt1_000_000;
365     digits = u / 10000;  //    10,000
366     u -= digits * 10000;
367     *buffer++ = '0' + digits;
368     goto lt10_000;
369   }
370   if (u < 100000000) {  // 100,000,000
371     if (u >= 10000000) goto lt100_000_000;
372     digits = u / 1000000;  //   1,000,000
373     u -= digits * 1000000;
374     *buffer++ = '0' + digits;
375     goto lt1_000_000;
376   }
377   // we already know that u < 1,000,000,000
378   digits = u / 100000000;  // 100,000,000
379   u -= digits * 100000000;
380   *buffer++ = '0' + digits;
381   goto lt100_000_000;
382 }
383 
FastInt32ToBufferLeft(int32 i,char * buffer)384 char* FastInt32ToBufferLeft(int32 i, char* buffer) {
385   uint32 u = i;
386   if (i < 0) {
387     *buffer++ = '-';
388     // We need to do the negation in modular (i.e., "unsigned")
389     // arithmetic; MSVC++ apprently warns for plain "-u", so
390     // we write the equivalent expression "0 - u" instead.
391     u = 0 - u;
392   }
393   return FastUInt32ToBufferLeft(u, buffer);
394 }
395 
FastUInt64ToBufferLeft(uint64 u64,char * buffer)396 char* FastUInt64ToBufferLeft(uint64 u64, char* buffer) {
397   uint32 u32 = static_cast<uint32>(u64);
398   if (u32 == u64) return FastUInt32ToBufferLeft(u32, buffer);
399 
400   // Here we know u64 has at least 10 decimal digits.
401   uint64 top_1to11 = u64 / 1000000000;
402   u32 = static_cast<uint32>(u64 - top_1to11 * 1000000000);
403   uint32 top_1to11_32 = static_cast<uint32>(top_1to11);
404 
405   if (top_1to11_32 == top_1to11) {
406     buffer = FastUInt32ToBufferLeft(top_1to11_32, buffer);
407   } else {
408     // top_1to11 has more than 32 bits too; print it in two steps.
409     uint32 top_8to9 = static_cast<uint32>(top_1to11 / 100);
410     uint32 mid_2 = static_cast<uint32>(top_1to11 - top_8to9 * 100);
411     buffer = FastUInt32ToBufferLeft(top_8to9, buffer);
412     PutTwoDigits(mid_2, buffer);
413     buffer += 2;
414   }
415 
416   // We have only 9 digits now, again the maximum uint32 can handle fully.
417   uint32 digits = u32 / 10000000;  // 10,000,000
418   u32 -= digits * 10000000;
419   PutTwoDigits(digits, buffer);
420   buffer += 2;
421   digits = u32 / 100000;  // 100,000
422   u32 -= digits * 100000;
423   PutTwoDigits(digits, buffer);
424   buffer += 2;
425   digits = u32 / 1000;  // 1,000
426   u32 -= digits * 1000;
427   PutTwoDigits(digits, buffer);
428   buffer += 2;
429   digits = u32 / 10;
430   u32 -= digits * 10;
431   PutTwoDigits(digits, buffer);
432   buffer += 2;
433   memcpy(buffer, one_ASCII_final_digits[u32], 2);
434   return buffer + 1;
435 }
436 
FastInt64ToBufferLeft(int64 i,char * buffer)437 char* FastInt64ToBufferLeft(int64 i, char* buffer) {
438   uint64 u = i;
439   if (i < 0) {
440     *buffer++ = '-';
441     u = 0 - u;
442   }
443   return FastUInt64ToBufferLeft(u, buffer);
444 }
445 
safe_strto32_base(const string & text,int32 * value,int base)446 bool safe_strto32_base(const string& text, int32* value, int base) {
447   return safe_int_internal<int32>(text, value, base);
448 }
449 
safe_strto64_base(const string & text,int64 * value,int base)450 bool safe_strto64_base(const string& text, int64* value, int base) {
451   return safe_int_internal<int64>(text, value, base);
452 }
453 
safe_strtou32_base(const string & text,uint32 * value,int base)454 bool safe_strtou32_base(const string& text, uint32* value, int base) {
455   return safe_uint_internal<uint32>(text, value, base);
456 }
457 
safe_strtou64_base(const string & text,uint64 * value,int base)458 bool safe_strtou64_base(const string& text, uint64* value, int base) {
459   return safe_uint_internal<uint64>(text, value, base);
460 }
461 
safe_strtof(const string & piece,float * value)462 bool safe_strtof(const string& piece, float* value) {
463   *value = 0.0;
464   if (piece.empty()) return false;
465   char buf[32];
466   std::unique_ptr<char[]> bigbuf;
467   char* str = buf;
468   if (piece.size() > sizeof(buf) - 1) {
469     bigbuf.reset(new char[piece.size() + 1]);
470     str = bigbuf.get();
471   }
472   memcpy(str, piece.data(), piece.size());
473   str[piece.size()] = '\0';
474 
475   char* endptr;
476 #ifdef COMPILER_MSVC  // has no strtof()
477   *value = strtod(str, &endptr);
478 #else
479   *value = strtof(str, &endptr);
480 #endif
481   if (endptr != str) {
482     while (ascii_isspace(*endptr)) ++endptr;
483   }
484   // Ignore range errors from strtod/strtof.
485   // The values it returns on underflow and
486   // overflow are the right fallback in a
487   // robust setting.
488   return *str != '\0' && *endptr == '\0';
489 }
490 
safe_strtod(const string & piece,double * value)491 bool safe_strtod(const string& piece, double* value) {
492   *value = 0.0;
493   if (piece.empty()) return false;
494   char buf[32];
495   std::unique_ptr<char[]> bigbuf;
496   char* str = buf;
497   if (piece.size() > sizeof(buf) - 1) {
498     bigbuf.reset(new char[piece.size() + 1]);
499     str = bigbuf.get();
500   }
501   memcpy(str, piece.data(), piece.size());
502   str[piece.size()] = '\0';
503 
504   char* endptr;
505   *value = strtod(str, &endptr);
506   if (endptr != str) {
507     while (ascii_isspace(*endptr)) ++endptr;
508   }
509   // Ignore range errors from strtod.  The values it
510   // returns on underflow and overflow are the right
511   // fallback in a robust setting.
512   return *str != '\0' && *endptr == '\0';
513 }
514 
SimpleFtoa(float value)515 string SimpleFtoa(float value) {
516   char buffer[kFastToBufferSize];
517   return FloatToBuffer(value, buffer);
518 }
519 
FloatToBuffer(float value,char * buffer)520 char* FloatToBuffer(float value, char* buffer) {
521   // FLT_DIG is 6 for IEEE-754 floats, which are used on almost all
522   // platforms these days.  Just in case some system exists where FLT_DIG
523   // is significantly larger -- and risks overflowing our buffer -- we have
524   // this assert.
525   assert(FLT_DIG < 10);
526 
527   int snprintf_result =
528       snprintf(buffer, kFastToBufferSize, "%.*g", FLT_DIG, value);
529 
530   // The snprintf should never overflow because the buffer is significantly
531   // larger than the precision we asked for.
532   assert(snprintf_result > 0 && snprintf_result < kFastToBufferSize);
533 
534   float parsed_value;
535   if (!safe_strtof(buffer, &parsed_value) || parsed_value != value) {
536     snprintf_result =
537         snprintf(buffer, kFastToBufferSize, "%.*g", FLT_DIG + 2, value);
538 
539     // Should never overflow; see above.
540     assert(snprintf_result > 0 && snprintf_result < kFastToBufferSize);
541   }
542   return buffer;
543 }
544 
545 }  // namespace strings
546 }  // namespace dynamic_depth
547