| 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/strings/string_number_conversions.h" | 5 #include "base/strings/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> |
| 11 | 11 |
| 12 #include <limits> | 12 #include <limits> |
| 13 | 13 |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/scoped_clear_errno.h" |
| 15 #include "base/third_party/dmg_fp/dmg_fp.h" | 16 #include "base/third_party/dmg_fp/dmg_fp.h" |
| 16 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 template <typename STR, typename INT, typename UINT, bool NEG> | 23 template <typename STR, typename INT, typename UINT, bool NEG> |
| 23 struct IntToStringT { | 24 struct IntToStringT { |
| 24 // This is to avoid a compiler warning about unary minus on unsigned type. | 25 // This is to avoid a compiler warning about unary minus on unsigned type. |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 | 444 |
| 444 bool StringToSizeT(const StringPiece& input, size_t* output) { | 445 bool StringToSizeT(const StringPiece& input, size_t* output) { |
| 445 return StringToIntImpl(input, output); | 446 return StringToIntImpl(input, output); |
| 446 } | 447 } |
| 447 | 448 |
| 448 bool StringToSizeT(const StringPiece16& input, size_t* output) { | 449 bool StringToSizeT(const StringPiece16& input, size_t* output) { |
| 449 return String16ToIntImpl(input, output); | 450 return String16ToIntImpl(input, output); |
| 450 } | 451 } |
| 451 | 452 |
| 452 bool StringToDouble(const std::string& input, double* output) { | 453 bool StringToDouble(const std::string& input, double* output) { |
| 453 errno = 0; // Thread-safe? It is on at least Mac, Linux, and Windows. | 454 // Thread-safe? It is on at least Mac, Linux, and Windows. |
| 455 ScopedClearErrno clear_errno; |
| 456 |
| 454 char* endptr = NULL; | 457 char* endptr = NULL; |
| 455 *output = dmg_fp::strtod(input.c_str(), &endptr); | 458 *output = dmg_fp::strtod(input.c_str(), &endptr); |
| 456 | 459 |
| 457 // Cases to return false: | 460 // Cases to return false: |
| 458 // - If errno is ERANGE, there was an overflow or underflow. | 461 // - If errno is ERANGE, there was an overflow or underflow. |
| 459 // - If the input string is empty, there was nothing to parse. | 462 // - If the input string is empty, there was nothing to parse. |
| 460 // - If endptr does not point to the end of the string, there are either | 463 // - If endptr does not point to the end of the string, there are either |
| 461 // characters remaining in the string after a parsed number, or the string | 464 // characters remaining in the string after a parsed number, or the string |
| 462 // does not begin with a parseable number. endptr is compared to the | 465 // does not begin with a parseable number. endptr is compared to the |
| 463 // expected end given the string's stated length to correctly catch cases | 466 // expected end given the string's stated length to correctly catch cases |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 bool HexStringToUInt64(const StringPiece& input, uint64* output) { | 507 bool HexStringToUInt64(const StringPiece& input, uint64* output) { |
| 505 return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke( | 508 return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke( |
| 506 input.begin(), input.end(), output); | 509 input.begin(), input.end(), output); |
| 507 } | 510 } |
| 508 | 511 |
| 509 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { | 512 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { |
| 510 return HexStringToBytesT(input, output); | 513 return HexStringToBytesT(input, output); |
| 511 } | 514 } |
| 512 | 515 |
| 513 } // namespace base | 516 } // namespace base |
| OLD | NEW |