| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/string_number_conversions.h" | 5 #include "base/string_number_conversions.h" |
| 6 | 6 |
| 7 #include <ctype.h> | 7 #include <ctype.h> |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 #include <wctype.h> | 10 #include <wctype.h> |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 } | 288 } |
| 289 static value_type max() { | 289 static value_type max() { |
| 290 return std::numeric_limits<value_type>::max(); | 290 return std::numeric_limits<value_type>::max(); |
| 291 } | 291 } |
| 292 static const int kBase = BASE; | 292 static const int kBase = BASE; |
| 293 }; | 293 }; |
| 294 | 294 |
| 295 template<typename ITERATOR> | 295 template<typename ITERATOR> |
| 296 class BaseHexIteratorRangeToIntTraits | 296 class BaseHexIteratorRangeToIntTraits |
| 297 : public BaseIteratorRangeToNumberTraits<ITERATOR, int, 16> { | 297 : public BaseIteratorRangeToNumberTraits<ITERATOR, int, 16> { |
| 298 public: | 298 }; |
| 299 // Allow parsing of 0xFFFFFFFF, which is technically an overflow | 299 |
| 300 static unsigned int max() { | 300 template<typename ITERATOR> |
| 301 return std::numeric_limits<unsigned int>::max(); | 301 class BaseHexIteratorRangeToInt64Traits |
| 302 } | 302 : public BaseIteratorRangeToNumberTraits<ITERATOR, int64, 16> { |
| 303 }; | 303 }; |
| 304 | 304 |
| 305 typedef BaseHexIteratorRangeToIntTraits<StringPiece::const_iterator> | 305 typedef BaseHexIteratorRangeToIntTraits<StringPiece::const_iterator> |
| 306 HexIteratorRangeToIntTraits; | 306 HexIteratorRangeToIntTraits; |
| 307 | 307 |
| 308 typedef BaseHexIteratorRangeToInt64Traits<StringPiece::const_iterator> |
| 309 HexIteratorRangeToInt64Traits; |
| 310 |
| 308 template<typename STR> | 311 template<typename STR> |
| 309 bool HexStringToBytesT(const STR& input, std::vector<uint8>* output) { | 312 bool HexStringToBytesT(const STR& input, std::vector<uint8>* output) { |
| 310 DCHECK_EQ(output->size(), 0u); | 313 DCHECK_EQ(output->size(), 0u); |
| 311 size_t count = input.size(); | 314 size_t count = input.size(); |
| 312 if (count == 0 || (count % 2) != 0) | 315 if (count == 0 || (count % 2) != 0) |
| 313 return false; | 316 return false; |
| 314 for (uintptr_t i = 0; i < count / 2; ++i) { | 317 for (uintptr_t i = 0; i < count / 2; ++i) { |
| 315 uint8 msb = 0; // most significant 4 bits | 318 uint8 msb = 0; // most significant 4 bits |
| 316 uint8 lsb = 0; // least significant 4 bits | 319 uint8 lsb = 0; // least significant 4 bits |
| 317 if (!CharToDigit<16>(input[i * 2], &msb) || | 320 if (!CharToDigit<16>(input[i * 2], &msb) || |
| 318 !CharToDigit<16>(input[i * 2 + 1], &lsb)) | 321 !CharToDigit<16>(input[i * 2 + 1], &lsb)) |
| 319 return false; | 322 return false; |
| 320 output->push_back((msb << 4) | lsb); | 323 output->push_back((msb << 4) | lsb); |
| 321 } | 324 } |
| 322 return true; | 325 return true; |
| 323 } | 326 } |
| 324 | 327 |
| 325 template <typename VALUE, int BASE> | 328 template <typename VALUE, int BASE> |
| 326 class StringPieceToNumberTraits | 329 class StringPieceToNumberTraits |
| 327 : public BaseIteratorRangeToNumberTraits<StringPiece::const_iterator, | 330 : public BaseIteratorRangeToNumberTraits<StringPiece::const_iterator, |
| 328 VALUE, | 331 VALUE, |
| 329 BASE> {}; | 332 BASE> { |
| 333 }; |
| 330 | 334 |
| 331 template <typename VALUE> | 335 template <typename VALUE> |
| 332 bool StringToIntImpl(const StringPiece& input, VALUE* output) { | 336 bool StringToIntImpl(const StringPiece& input, VALUE* output) { |
| 333 return IteratorRangeToNumber<StringPieceToNumberTraits<VALUE, 10> >::Invoke( | 337 return IteratorRangeToNumber<StringPieceToNumberTraits<VALUE, 10> >::Invoke( |
| 334 input.begin(), input.end(), output); | 338 input.begin(), input.end(), output); |
| 335 } | 339 } |
| 336 | 340 |
| 337 template <typename VALUE, int BASE> | 341 template <typename VALUE, int BASE> |
| 338 class StringPiece16ToNumberTraits | 342 class StringPiece16ToNumberTraits |
| 339 : public BaseIteratorRangeToNumberTraits<StringPiece16::const_iterator, | 343 : public BaseIteratorRangeToNumberTraits<StringPiece16::const_iterator, |
| 340 VALUE, | 344 VALUE, |
| 341 BASE> {}; | 345 BASE> { |
| 346 }; |
| 342 | 347 |
| 343 template <typename VALUE> | 348 template <typename VALUE> |
| 344 bool String16ToIntImpl(const StringPiece16& input, VALUE* output) { | 349 bool String16ToIntImpl(const StringPiece16& input, VALUE* output) { |
| 345 return IteratorRangeToNumber<StringPiece16ToNumberTraits<VALUE, 10> >::Invoke( | 350 return IteratorRangeToNumber<StringPiece16ToNumberTraits<VALUE, 10> >::Invoke( |
| 346 input.begin(), input.end(), output); | 351 input.begin(), input.end(), output); |
| 347 } | 352 } |
| 348 | 353 |
| 349 } // namespace | 354 } // namespace |
| 350 | 355 |
| 351 std::string IntToString(int value) { | 356 std::string IntToString(int value) { |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 ret[(i * 2) + 1] = kHexChars[b & 0xf]; | 479 ret[(i * 2) + 1] = kHexChars[b & 0xf]; |
| 475 } | 480 } |
| 476 return ret; | 481 return ret; |
| 477 } | 482 } |
| 478 | 483 |
| 479 bool HexStringToInt(const StringPiece& input, int* output) { | 484 bool HexStringToInt(const StringPiece& input, int* output) { |
| 480 return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke( | 485 return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke( |
| 481 input.begin(), input.end(), output); | 486 input.begin(), input.end(), output); |
| 482 } | 487 } |
| 483 | 488 |
| 489 bool HexStringToInt64(const StringPiece& input, int64* output) { |
| 490 return IteratorRangeToNumber<HexIteratorRangeToInt64Traits>::Invoke( |
| 491 input.begin(), input.end(), output); |
| 492 } |
| 493 |
| 484 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { | 494 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { |
| 485 return HexStringToBytesT(input, output); | 495 return HexStringToBytesT(input, output); |
| 486 } | 496 } |
| 487 | 497 |
| 488 } // namespace base | 498 } // namespace base |
| OLD | NEW |