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