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