| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <errno.h> | 7 #include <errno.h> |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 #include <wctype.h> | 9 #include <wctype.h> |
| 10 | 10 |
| (...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 } | 396 } |
| 397 | 397 |
| 398 bool StringToInt(std::string::const_iterator begin, | 398 bool StringToInt(std::string::const_iterator begin, |
| 399 std::string::const_iterator end, | 399 std::string::const_iterator end, |
| 400 int* output) { | 400 int* output) { |
| 401 return IteratorRangeToNumber<IteratorRangeToIntTraits>::Invoke(begin, | 401 return IteratorRangeToNumber<IteratorRangeToIntTraits>::Invoke(begin, |
| 402 end, | 402 end, |
| 403 output); | 403 output); |
| 404 } | 404 } |
| 405 | 405 |
| 406 #if !defined(STD_STRING_ITERATOR_IS_CHAR_POINTER) |
| 406 bool StringToInt(const char* begin, const char* end, int* output) { | 407 bool StringToInt(const char* begin, const char* end, int* output) { |
| 407 return IteratorRangeToNumber<CharBufferToIntTraits>::Invoke(begin, | 408 return IteratorRangeToNumber<CharBufferToIntTraits>::Invoke(begin, |
| 408 end, | 409 end, |
| 409 output); | 410 output); |
| 410 } | 411 } |
| 412 #endif |
| 411 | 413 |
| 412 bool StringToInt(const string16& input, int* output) { | 414 bool StringToInt(const string16& input, int* output) { |
| 413 return IteratorRangeToNumber<WideIteratorRangeToIntTraits>::Invoke( | 415 return IteratorRangeToNumber<WideIteratorRangeToIntTraits>::Invoke( |
| 414 input.begin(), input.end(), output); | 416 input.begin(), input.end(), output); |
| 415 } | 417 } |
| 416 | 418 |
| 417 bool StringToInt(string16::const_iterator begin, | 419 bool StringToInt(string16::const_iterator begin, |
| 418 string16::const_iterator end, | 420 string16::const_iterator end, |
| 419 int* output) { | 421 int* output) { |
| 420 return IteratorRangeToNumber<WideIteratorRangeToIntTraits>::Invoke(begin, | 422 return IteratorRangeToNumber<WideIteratorRangeToIntTraits>::Invoke(begin, |
| 421 end, | 423 end, |
| 422 output); | 424 output); |
| 423 } | 425 } |
| 424 | 426 |
| 427 #if !defined(BASE_STRING16_ITERATOR_IS_CHAR16_POINTER) |
| 425 bool StringToInt(const char16* begin, const char16* end, int* output) { | 428 bool StringToInt(const char16* begin, const char16* end, int* output) { |
| 426 return IteratorRangeToNumber<WideCharBufferToIntTraits>::Invoke(begin, | 429 return IteratorRangeToNumber<WideCharBufferToIntTraits>::Invoke(begin, |
| 427 end, | 430 end, |
| 428 output); | 431 output); |
| 429 } | 432 } |
| 433 #endif |
| 430 | 434 |
| 431 bool StringToInt64(const std::string& input, int64* output) { | 435 bool StringToInt64(const std::string& input, int64* output) { |
| 432 return IteratorRangeToNumber<IteratorRangeToInt64Traits>::Invoke( | 436 return IteratorRangeToNumber<IteratorRangeToInt64Traits>::Invoke( |
| 433 input.begin(), input.end(), output); | 437 input.begin(), input.end(), output); |
| 434 } | 438 } |
| 435 | 439 |
| 436 bool StringToInt64(std::string::const_iterator begin, | 440 bool StringToInt64(std::string::const_iterator begin, |
| 437 std::string::const_iterator end, | 441 std::string::const_iterator end, |
| 438 int64* output) { | 442 int64* output) { |
| 439 return IteratorRangeToNumber<IteratorRangeToInt64Traits>::Invoke(begin, | 443 return IteratorRangeToNumber<IteratorRangeToInt64Traits>::Invoke(begin, |
| 440 end, | 444 end, |
| 441 output); | 445 output); |
| 442 } | 446 } |
| 443 | 447 |
| 448 #if !defined(STD_STRING_ITERATOR_IS_CHAR_POINTER) |
| 444 bool StringToInt64(const char* begin, const char* end, int64* output) { | 449 bool StringToInt64(const char* begin, const char* end, int64* output) { |
| 445 return IteratorRangeToNumber<CharBufferToInt64Traits>::Invoke(begin, | 450 return IteratorRangeToNumber<CharBufferToInt64Traits>::Invoke(begin, |
| 446 end, | 451 end, |
| 447 output); | 452 output); |
| 448 } | 453 } |
| 454 #endif |
| 449 | 455 |
| 450 bool StringToInt64(const string16& input, int64* output) { | 456 bool StringToInt64(const string16& input, int64* output) { |
| 451 return IteratorRangeToNumber<WideIteratorRangeToInt64Traits>::Invoke( | 457 return IteratorRangeToNumber<WideIteratorRangeToInt64Traits>::Invoke( |
| 452 input.begin(), input.end(), output); | 458 input.begin(), input.end(), output); |
| 453 } | 459 } |
| 454 | 460 |
| 455 bool StringToInt64(string16::const_iterator begin, | 461 bool StringToInt64(string16::const_iterator begin, |
| 456 string16::const_iterator end, | 462 string16::const_iterator end, |
| 457 int64* output) { | 463 int64* output) { |
| 458 return IteratorRangeToNumber<WideIteratorRangeToInt64Traits>::Invoke(begin, | 464 return IteratorRangeToNumber<WideIteratorRangeToInt64Traits>::Invoke(begin, |
| 459 end, | 465 end, |
| 460 output); | 466 output); |
| 461 } | 467 } |
| 462 | 468 |
| 469 #if !defined(BASE_STRING16_ITERATOR_IS_CHAR16_POINTER) |
| 463 bool StringToInt64(const char16* begin, const char16* end, int64* output) { | 470 bool StringToInt64(const char16* begin, const char16* end, int64* output) { |
| 464 return IteratorRangeToNumber<WideCharBufferToInt64Traits>::Invoke(begin, | 471 return IteratorRangeToNumber<WideCharBufferToInt64Traits>::Invoke(begin, |
| 465 end, | 472 end, |
| 466 output); | 473 output); |
| 467 } | 474 } |
| 475 #endif |
| 468 | 476 |
| 469 bool StringToDouble(const std::string& input, double* output) { | 477 bool StringToDouble(const std::string& input, double* output) { |
| 470 errno = 0; // Thread-safe? It is on at least Mac, Linux, and Windows. | 478 errno = 0; // Thread-safe? It is on at least Mac, Linux, and Windows. |
| 471 char* endptr = NULL; | 479 char* endptr = NULL; |
| 472 *output = dmg_fp::strtod(input.c_str(), &endptr); | 480 *output = dmg_fp::strtod(input.c_str(), &endptr); |
| 473 | 481 |
| 474 // Cases to return false: | 482 // Cases to return false: |
| 475 // - If errno is ERANGE, there was an overflow or underflow. | 483 // - If errno is ERANGE, there was an overflow or underflow. |
| 476 // - If the input string is empty, there was nothing to parse. | 484 // - If the input string is empty, there was nothing to parse. |
| 477 // - If endptr does not point to the end of the string, there are either | 485 // - If endptr does not point to the end of the string, there are either |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 } | 522 } |
| 515 | 523 |
| 516 bool HexStringToInt(std::string::const_iterator begin, | 524 bool HexStringToInt(std::string::const_iterator begin, |
| 517 std::string::const_iterator end, | 525 std::string::const_iterator end, |
| 518 int* output) { | 526 int* output) { |
| 519 return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke(begin, | 527 return IteratorRangeToNumber<HexIteratorRangeToIntTraits>::Invoke(begin, |
| 520 end, | 528 end, |
| 521 output); | 529 output); |
| 522 } | 530 } |
| 523 | 531 |
| 532 #if !defined(STD_STRING_ITERATOR_IS_CHAR_POINTER) |
| 524 bool HexStringToInt(const char* begin, const char* end, int* output) { | 533 bool HexStringToInt(const char* begin, const char* end, int* output) { |
| 525 return IteratorRangeToNumber<HexCharBufferToIntTraits>::Invoke(begin, | 534 return IteratorRangeToNumber<HexCharBufferToIntTraits>::Invoke(begin, |
| 526 end, | 535 end, |
| 527 output); | 536 output); |
| 528 } | 537 } |
| 538 #endif |
| 529 | 539 |
| 530 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { | 540 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { |
| 531 return HexStringToBytesT(input, output); | 541 return HexStringToBytesT(input, output); |
| 532 } | 542 } |
| 533 | 543 |
| 534 } // namespace base | 544 } // namespace base |
| OLD | NEW |