| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "net/base/parse_number.h" | 5 #include "net/base/parse_number.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_util.h" |
| 9 | 10 |
| 10 namespace net { | 11 namespace net { |
| 11 | 12 |
| 12 namespace { | 13 namespace { |
| 13 | 14 |
| 14 // The string to number conversion functions in //base include the type in the | 15 // The string to number conversion functions in //base include the type in the |
| 15 // name (like StringToInt64()). The following wrapper methods create a | 16 // name (like StringToInt64()). The following wrapper methods create a |
| 16 // consistent interface to StringToXXX() that calls the appropriate //base | 17 // consistent interface to StringToXXX() that calls the appropriate //base |
| 17 // version. This simplifies writing generic code with a template. | 18 // version. This simplifies writing generic code with a template. |
| 18 | 19 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 44 bool ParseIntHelper(const base::StringPiece& input, | 45 bool ParseIntHelper(const base::StringPiece& input, |
| 45 ParseIntFormat format, | 46 ParseIntFormat format, |
| 46 T* output, | 47 T* output, |
| 47 ParseIntError* optional_error) { | 48 ParseIntError* optional_error) { |
| 48 // Check that the input matches the format before calling StringToNumber(). | 49 // Check that the input matches the format before calling StringToNumber(). |
| 49 // Numbers must start with either a digit or a negative sign. | 50 // Numbers must start with either a digit or a negative sign. |
| 50 if (input.empty()) | 51 if (input.empty()) |
| 51 return SetError(ParseIntError::FAILED_PARSE, optional_error); | 52 return SetError(ParseIntError::FAILED_PARSE, optional_error); |
| 52 | 53 |
| 53 bool starts_with_negative = input[0] == '-'; | 54 bool starts_with_negative = input[0] == '-'; |
| 54 bool starts_with_digit = input[0] >= '0' && input[0] <= '9'; | 55 bool starts_with_digit = base::IsAsciiDigit(input[0]); |
| 55 | 56 |
| 56 if (!starts_with_digit) { | 57 if (!starts_with_digit) { |
| 57 if (format == ParseIntFormat::NON_NEGATIVE || !starts_with_negative) | 58 if (format == ParseIntFormat::NON_NEGATIVE || !starts_with_negative) |
| 58 return SetError(ParseIntError::FAILED_PARSE, optional_error); | 59 return SetError(ParseIntError::FAILED_PARSE, optional_error); |
| 59 } | 60 } |
| 60 | 61 |
| 61 // Dispatch to the appropriate flavor of base::StringToXXX() by calling one of | 62 // Dispatch to the appropriate flavor of base::StringToXXX() by calling one of |
| 62 // the type-specific overloads. | 63 // the type-specific overloads. |
| 63 T result; | 64 T result; |
| 64 if (StringToNumber(input, &result)) { | 65 if (StringToNumber(input, &result)) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 } | 118 } |
| 118 | 119 |
| 119 bool ParseUint64(const base::StringPiece& input, | 120 bool ParseUint64(const base::StringPiece& input, |
| 120 uint64_t* output, | 121 uint64_t* output, |
| 121 ParseIntError* optional_error) { | 122 ParseIntError* optional_error) { |
| 122 return ParseIntHelper(input, ParseIntFormat::NON_NEGATIVE, output, | 123 return ParseIntHelper(input, ParseIntFormat::NON_NEGATIVE, output, |
| 123 optional_error); | 124 optional_error); |
| 124 } | 125 } |
| 125 | 126 |
| 126 } // namespace net | 127 } // namespace net |
| OLD | NEW |