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