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