• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/strings/string_number_conversions.h"
6 
7 #include <ctype.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <wctype.h>
11 
12 #include <limits>
13 #include <type_traits>
14 
15 #include "base/logging.h"
16 #include "base/numerics/safe_math.h"
17 #include "base/scoped_clear_errno.h"
18 
19 namespace base {
20 
21 namespace {
22 
23 template <typename STR, typename INT>
24 struct IntToStringT {
IntToStringbase::__anon4a6443800111::IntToStringT25   static STR IntToString(INT value) {
26     // log10(2) ~= 0.3 bytes needed per bit or per byte log10(2**8) ~= 2.4.
27     // So round up to allocate 3 output characters per byte, plus 1 for '-'.
28     const size_t kOutputBufSize =
29         3 * sizeof(INT) + std::numeric_limits<INT>::is_signed;
30 
31     // Create the string in a temporary buffer, write it back to front, and
32     // then return the substr of what we ended up using.
33     using CHR = typename STR::value_type;
34     CHR outbuf[kOutputBufSize];
35 
36     // The ValueOrDie call below can never fail, because UnsignedAbs is valid
37     // for all valid inputs.
38     typename std::make_unsigned<INT>::type res =
39         CheckedNumeric<INT>(value).UnsignedAbs().ValueOrDie();
40 
41     CHR* end = outbuf + kOutputBufSize;
42     CHR* i = end;
43     do {
44       --i;
45       DCHECK(i != outbuf);
46       *i = static_cast<CHR>((res % 10) + '0');
47       res /= 10;
48     } while (res != 0);
49     if (IsValueNegative(value)) {
50       --i;
51       DCHECK(i != outbuf);
52       *i = static_cast<CHR>('-');
53     }
54     return STR(i, end);
55   }
56 };
57 
58 // Utility to convert a character to a digit in a given base
59 template<typename CHAR, int BASE, bool BASE_LTE_10> class BaseCharToDigit {
60 };
61 
62 // Faster specialization for bases <= 10
63 template<typename CHAR, int BASE> class BaseCharToDigit<CHAR, BASE, true> {
64  public:
Convert(CHAR c,uint8_t * digit)65   static bool Convert(CHAR c, uint8_t* digit) {
66     if (c >= '0' && c < '0' + BASE) {
67       *digit = static_cast<uint8_t>(c - '0');
68       return true;
69     }
70     return false;
71   }
72 };
73 
74 // Specialization for bases where 10 < base <= 36
75 template<typename CHAR, int BASE> class BaseCharToDigit<CHAR, BASE, false> {
76  public:
Convert(CHAR c,uint8_t * digit)77   static bool Convert(CHAR c, uint8_t* digit) {
78     if (c >= '0' && c <= '9') {
79       *digit = c - '0';
80     } else if (c >= 'a' && c < 'a' + BASE - 10) {
81       *digit = c - 'a' + 10;
82     } else if (c >= 'A' && c < 'A' + BASE - 10) {
83       *digit = c - 'A' + 10;
84     } else {
85       return false;
86     }
87     return true;
88   }
89 };
90 
91 template <int BASE, typename CHAR>
CharToDigit(CHAR c,uint8_t * digit)92 bool CharToDigit(CHAR c, uint8_t* digit) {
93   return BaseCharToDigit<CHAR, BASE, BASE <= 10>::Convert(c, digit);
94 }
95 
96 // There is an IsUnicodeWhitespace for wchars defined in string_util.h, but it
97 // is locale independent, whereas the functions we are replacing were
98 // locale-dependent. TBD what is desired, but for the moment let's not
99 // introduce a change in behaviour.
100 template<typename CHAR> class WhitespaceHelper {
101 };
102 
103 template<> class WhitespaceHelper<char> {
104  public:
Invoke(char c)105   static bool Invoke(char c) {
106     return 0 != isspace(static_cast<unsigned char>(c));
107   }
108 };
109 
110 template<> class WhitespaceHelper<char16> {
111  public:
Invoke(char16 c)112   static bool Invoke(char16 c) {
113     return 0 != iswspace(c);
114   }
115 };
116 
LocalIsWhitespace(CHAR c)117 template<typename CHAR> bool LocalIsWhitespace(CHAR c) {
118   return WhitespaceHelper<CHAR>::Invoke(c);
119 }
120 
121 // IteratorRangeToNumberTraits should provide:
122 //  - a typedef for iterator_type, the iterator type used as input.
123 //  - a typedef for value_type, the target numeric type.
124 //  - static functions min, max (returning the minimum and maximum permitted
125 //    values)
126 //  - constant kBase, the base in which to interpret the input
127 template<typename IteratorRangeToNumberTraits>
128 class IteratorRangeToNumber {
129  public:
130   typedef IteratorRangeToNumberTraits traits;
131   typedef typename traits::iterator_type const_iterator;
132   typedef typename traits::value_type value_type;
133 
134   // Generalized iterator-range-to-number conversion.
135   //
Invoke(const_iterator begin,const_iterator end,value_type * output)136   static bool Invoke(const_iterator begin,
137                      const_iterator end,
138                      value_type* output) {
139     bool valid = true;
140 
141     while (begin != end && LocalIsWhitespace(*begin)) {
142       valid = false;
143       ++begin;
144     }
145 
146     if (begin != end && *begin == '-') {
147       if (!std::numeric_limits<value_type>::is_signed) {
148         *output = 0;
149         valid = false;
150       } else if (!Negative::Invoke(begin + 1, end, output)) {
151         valid = false;
152       }
153     } else {
154       if (begin != end && *begin == '+') {
155         ++begin;
156       }
157       if (!Positive::Invoke(begin, end, output)) {
158         valid = false;
159       }
160     }
161 
162     return valid;
163   }
164 
165  private:
166   // Sign provides:
167   //  - a static function, CheckBounds, that determines whether the next digit
168   //    causes an overflow/underflow
169   //  - a static function, Increment, that appends the next digit appropriately
170   //    according to the sign of the number being parsed.
171   template<typename Sign>
172   class Base {
173    public:
Invoke(const_iterator begin,const_iterator end,typename traits::value_type * output)174     static bool Invoke(const_iterator begin, const_iterator end,
175                        typename traits::value_type* output) {
176       *output = 0;
177 
178       if (begin == end) {
179         return false;
180       }
181 
182       // Note: no performance difference was found when using template
183       // specialization to remove this check in bases other than 16
184       if (traits::kBase == 16 && end - begin > 2 && *begin == '0' &&
185           (*(begin + 1) == 'x' || *(begin + 1) == 'X')) {
186         begin += 2;
187       }
188 
189       for (const_iterator current = begin; current != end; ++current) {
190         uint8_t new_digit = 0;
191 
192         if (!CharToDigit<traits::kBase>(*current, &new_digit)) {
193           return false;
194         }
195 
196         if (current != begin) {
197           if (!Sign::CheckBounds(output, new_digit)) {
198             return false;
199           }
200           *output *= traits::kBase;
201         }
202 
203         Sign::Increment(new_digit, output);
204       }
205       return true;
206     }
207   };
208 
209   class Positive : public Base<Positive> {
210    public:
CheckBounds(value_type * output,uint8_t new_digit)211     static bool CheckBounds(value_type* output, uint8_t new_digit) {
212       if (*output > static_cast<value_type>(traits::max() / traits::kBase) ||
213           (*output == static_cast<value_type>(traits::max() / traits::kBase) &&
214            new_digit > traits::max() % traits::kBase)) {
215         *output = traits::max();
216         return false;
217       }
218       return true;
219     }
Increment(uint8_t increment,value_type * output)220     static void Increment(uint8_t increment, value_type* output) {
221       *output += increment;
222     }
223   };
224 
225   class Negative : public Base<Negative> {
226    public:
CheckBounds(value_type * output,uint8_t new_digit)227     static bool CheckBounds(value_type* output, uint8_t new_digit) {
228       if (*output < traits::min() / traits::kBase ||
229           (*output == traits::min() / traits::kBase &&
230            new_digit > 0 - traits::min() % traits::kBase)) {
231         *output = traits::min();
232         return false;
233       }
234       return true;
235     }
Increment(uint8_t increment,value_type * output)236     static void Increment(uint8_t increment, value_type* output) {
237       *output -= increment;
238     }
239   };
240 };
241 
242 template<typename ITERATOR, typename VALUE, int BASE>
243 class BaseIteratorRangeToNumberTraits {
244  public:
245   typedef ITERATOR iterator_type;
246   typedef VALUE value_type;
min()247   static value_type min() {
248     return std::numeric_limits<value_type>::min();
249   }
max()250   static value_type max() {
251     return std::numeric_limits<value_type>::max();
252   }
253   static const int kBase = BASE;
254 };
255 
256 template<typename ITERATOR>
257 class BaseHexIteratorRangeToIntTraits
258     : public BaseIteratorRangeToNumberTraits<ITERATOR, int, 16> {
259 };
260 
261 template <typename ITERATOR>
262 class BaseHexIteratorRangeToUIntTraits
263     : public BaseIteratorRangeToNumberTraits<ITERATOR, uint32_t, 16> {};
264 
265 template <typename ITERATOR>
266 class BaseHexIteratorRangeToInt64Traits
267     : public BaseIteratorRangeToNumberTraits<ITERATOR, int64_t, 16> {};
268 
269 template <typename ITERATOR>
270 class BaseHexIteratorRangeToUInt64Traits
271     : public BaseIteratorRangeToNumberTraits<ITERATOR, uint64_t, 16> {};
272 
273 typedef BaseHexIteratorRangeToIntTraits<StringPiece::const_iterator>
274     HexIteratorRangeToIntTraits;
275 
276 typedef BaseHexIteratorRangeToUIntTraits<StringPiece::const_iterator>
277     HexIteratorRangeToUIntTraits;
278 
279 typedef BaseHexIteratorRangeToInt64Traits<StringPiece::const_iterator>
280     HexIteratorRangeToInt64Traits;
281 
282 typedef BaseHexIteratorRangeToUInt64Traits<StringPiece::const_iterator>
283     HexIteratorRangeToUInt64Traits;
284 
285 template <typename STR>
HexStringToBytesT(const STR & input,std::vector<uint8_t> * output)286 bool HexStringToBytesT(const STR& input, std::vector<uint8_t>* output) {
287   DCHECK_EQ(output->size(), 0u);
288   size_t count = input.size();
289   if (count == 0 || (count % 2) != 0)
290     return false;
291   for (uintptr_t i = 0; i < count / 2; ++i) {
292     uint8_t msb = 0;  // most significant 4 bits
293     uint8_t lsb = 0;  // least significant 4 bits
294     if (!CharToDigit<16>(input[i * 2], &msb) ||
295         !CharToDigit<16>(input[i * 2 + 1], &lsb))
296       return false;
297     output->push_back((msb << 4) | lsb);
298   }
299   return true;
300 }
301 
302 template <typename VALUE, int BASE>
303 class StringPieceToNumberTraits
304     : public BaseIteratorRangeToNumberTraits<StringPiece::const_iterator,
305                                              VALUE,
306                                              BASE> {
307 };
308 
309 template <typename VALUE>
StringToIntImpl(const StringPiece & input,VALUE * output)310 bool StringToIntImpl(const StringPiece& input, VALUE* output) {
311   return IteratorRangeToNumber<StringPieceToNumberTraits<VALUE, 10> >::Invoke(
312       input.begin(), input.end(), output);
313 }
314 
315 template <typename VALUE, int BASE>
316 class StringPiece16ToNumberTraits
317     : public BaseIteratorRangeToNumberTraits<StringPiece16::const_iterator,
318                                              VALUE,
319                                              BASE> {
320 };
321 
322 template <typename VALUE>
String16ToIntImpl(const StringPiece16 & input,VALUE * output)323 bool String16ToIntImpl(const StringPiece16& input, VALUE* output) {
324   return IteratorRangeToNumber<StringPiece16ToNumberTraits<VALUE, 10> >::Invoke(
325       input.begin(), input.end(), output);
326 }
327 
328 }  // namespace
329 
IntToString(int value)330 std::string IntToString(int value) {
331   return IntToStringT<std::string, int>::IntToString(value);
332 }
333 
IntToString16(int value)334 string16 IntToString16(int value) {
335   return IntToStringT<string16, int>::IntToString(value);
336 }
337 
UintToString(unsigned int value)338 std::string UintToString(unsigned int value) {
339   return IntToStringT<std::string, unsigned int>::IntToString(value);
340 }
341 
UintToString16(unsigned int value)342 string16 UintToString16(unsigned int value) {
343   return IntToStringT<string16, unsigned int>::IntToString(value);
344 }
345 
Int64ToString(int64_t value)346 std::string Int64ToString(int64_t value) {
347   return IntToStringT<std::string, int64_t>::IntToString(value);
348 }
349 
Int64ToString16(int64_t value)350 string16 Int64ToString16(int64_t value) {
351   return IntToStringT<string16, int64_t>::IntToString(value);
352 }
353 
Uint64ToString(uint64_t value)354 std::string Uint64ToString(uint64_t value) {
355   return IntToStringT<std::string, uint64_t>::IntToString(value);
356 }
357 
Uint64ToString16(uint64_t value)358 string16 Uint64ToString16(uint64_t value) {
359   return IntToStringT<string16, uint64_t>::IntToString(value);
360 }
361 
SizeTToString(size_t value)362 std::string SizeTToString(size_t value) {
363   return IntToStringT<std::string, size_t>::IntToString(value);
364 }
365 
SizeTToString16(size_t value)366 string16 SizeTToString16(size_t value) {
367   return IntToStringT<string16, size_t>::IntToString(value);
368 }
369 
DoubleToString(double value)370 std::string DoubleToString(double value) {
371   auto ret = std::to_string(value);
372   // If this returned an integer, don't do anything.
373   if (ret.find('.') == std::string::npos) {
374     return ret;
375   }
376   // Otherwise, it has an annoying tendency to leave trailing zeros.
377   size_t len = ret.size();
378   while (len >= 2 && ret[len - 1] == '0' && ret[len - 2] != '.') {
379     --len;
380   }
381   ret.erase(len);
382   return ret;
383 }
384 
StringToInt(const StringPiece & input,int * output)385 bool StringToInt(const StringPiece& input, int* output) {
386   return StringToIntImpl(input, output);
387 }
388 
StringToInt(const StringPiece16 & input,int * output)389 bool StringToInt(const StringPiece16& input, int* output) {
390   return String16ToIntImpl(input, output);
391 }
392 
StringToUint(const StringPiece & input,unsigned * output)393 bool StringToUint(const StringPiece& input, unsigned* output) {
394   return StringToIntImpl(input, output);
395 }
396 
StringToUint(const StringPiece16 & input,unsigned * output)397 bool StringToUint(const StringPiece16& input, unsigned* output) {
398   return String16ToIntImpl(input, output);
399 }
400 
StringToInt64(const StringPiece & input,int64_t * output)401 bool StringToInt64(const StringPiece& input, int64_t* output) {
402   return StringToIntImpl(input, output);
403 }
404 
StringToInt64(const StringPiece16 & input,int64_t * output)405 bool StringToInt64(const StringPiece16& input, int64_t* output) {
406   return String16ToIntImpl(input, output);
407 }
408 
StringToUint64(const StringPiece & input,uint64_t * output)409 bool StringToUint64(const StringPiece& input, uint64_t* output) {
410   return StringToIntImpl(input, output);
411 }
412 
StringToUint64(const StringPiece16 & input,uint64_t * output)413 bool StringToUint64(const StringPiece16& input, uint64_t* output) {
414   return String16ToIntImpl(input, output);
415 }
416 
StringToSizeT(const StringPiece & input,size_t * output)417 bool StringToSizeT(const StringPiece& input, size_t* output) {
418   return StringToIntImpl(input, output);
419 }
420 
StringToSizeT(const StringPiece16 & input,size_t * output)421 bool StringToSizeT(const StringPiece16& input, size_t* output) {
422   return String16ToIntImpl(input, output);
423 }
424 
StringToDouble(const std::string & input,double * output)425 bool StringToDouble(const std::string& input, double* output) {
426   char* endptr = nullptr;
427   *output = strtod(input.c_str(), &endptr);
428 
429   // Cases to return false:
430   //  - If the input string is empty, there was nothing to parse.
431   //  - If endptr does not point to the end of the string, there are either
432   //    characters remaining in the string after a parsed number, or the string
433   //    does not begin with a parseable number.  endptr is compared to the
434   //    expected end given the string's stated length to correctly catch cases
435   //    where the string contains embedded NUL characters.
436   //  - If the first character is a space, there was leading whitespace
437   return !input.empty() &&
438          input.c_str() + input.length() == endptr &&
439          !isspace(input[0]) &&
440          *output != std::numeric_limits<double>::infinity() &&
441          *output != -std::numeric_limits<double>::infinity();
442 }
443 
444 // Note: if you need to add String16ToDouble, first ask yourself if it's
445 // really necessary. If it is, probably the best implementation here is to
446 // convert to 8-bit and then use the 8-bit version.
447 
448 // Note: if you need to add an iterator range version of StringToDouble, first
449 // ask yourself if it's really necessary. If it is, probably the best
450 // implementation here is to instantiate a string and use the string version.
451 
HexEncode(const void * bytes,size_t size)452 std::string HexEncode(const void* bytes, size_t size) {
453   static const char kHexChars[] = "0123456789ABCDEF";
454 
455   // Each input byte creates two output hex characters.
456   std::string ret(size * 2, '\0');
457 
458   for (size_t i = 0; i < size; ++i) {
459     char b = reinterpret_cast<const char*>(bytes)[i];
460     ret[(i * 2)] = kHexChars[(b >> 4) & 0xf];
461     ret[(i * 2) + 1] = kHexChars[b & 0xf];
462   }
463   return ret;
464 }
465 
HexStringToInt(const StringPiece & input,int * output)466 bool HexStringToInt(const StringPiece& input, int* output) {
467   return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke(
468     input.begin(), input.end(), output);
469 }
470 
HexStringToUInt(const StringPiece & input,uint32_t * output)471 bool HexStringToUInt(const StringPiece& input, uint32_t* output) {
472   return IteratorRangeToNumber<HexIteratorRangeToUIntTraits>::Invoke(
473       input.begin(), input.end(), output);
474 }
475 
HexStringToInt64(const StringPiece & input,int64_t * output)476 bool HexStringToInt64(const StringPiece& input, int64_t* output) {
477   return IteratorRangeToNumber<HexIteratorRangeToInt64Traits>::Invoke(
478     input.begin(), input.end(), output);
479 }
480 
HexStringToUInt64(const StringPiece & input,uint64_t * output)481 bool HexStringToUInt64(const StringPiece& input, uint64_t* output) {
482   return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke(
483       input.begin(), input.end(), output);
484 }
485 
HexStringToBytes(const std::string & input,std::vector<uint8_t> * output)486 bool HexStringToBytes(const std::string& input, std::vector<uint8_t>* output) {
487   return HexStringToBytesT(input, output);
488 }
489 
490 }  // namespace base
491