| 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/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 8 | 9 |
| 9 namespace net { | 10 namespace net { |
| 10 | 11 |
| 11 bool ParseNonNegativeDecimalInt(const base::StringPiece& input, int* output) { | 12 namespace { |
| 12 if (input.empty() || input[0] > '9' || input[0] < '0') | 13 |
| 14 // The string to number conversion functions in //base include the type in the |
| 15 // name (like StringToInt64()). The following wrapper methods create a |
| 16 // consistent interface to StringToXXX() that calls the appropriate //base |
| 17 // version. This simplifies writing generic code with a template. |
| 18 |
| 19 bool StringToNumber(const base::StringPiece& input, int32_t* output) { |
| 20 // This assumes ints are 32-bits (will fail compile if that ever changes). |
| 21 return base::StringToInt(input, output); |
| 22 } |
| 23 |
| 24 bool StringToNumber(const base::StringPiece& input, uint32_t* output) { |
| 25 // This assumes ints are 32-bits (will fail compile if that ever changes). |
| 26 return base::StringToUint(input, output); |
| 27 } |
| 28 |
| 29 bool StringToNumber(const base::StringPiece& input, int64_t* output) { |
| 30 return base::StringToInt64(input, output); |
| 31 } |
| 32 |
| 33 bool StringToNumber(const base::StringPiece& input, uint64_t* output) { |
| 34 return base::StringToUint64(input, output); |
| 35 } |
| 36 |
| 37 bool SetError(ParseIntError error, ParseIntError* optional_error) { |
| 38 if (optional_error) |
| 39 *optional_error = error; |
| 40 return false; |
| 41 } |
| 42 |
| 43 template <typename T> |
| 44 bool ParseIntHelper(const base::StringPiece& input, |
| 45 ParseIntFormat format, |
| 46 T* output, |
| 47 ParseIntError* optional_error) { |
| 48 // Check that the input matches the format before calling StringToNumber(). |
| 49 // Numbers must start with either a digit or a negative sign. |
| 50 if (input.empty()) |
| 51 return SetError(ParseIntError::FAILED_PARSE, optional_error); |
| 52 |
| 53 bool starts_with_negative = input[0] == '-'; |
| 54 bool starts_with_digit = input[0] >= '0' && input[0] <= '9'; |
| 55 |
| 56 if (!starts_with_digit) { |
| 57 if (format == ParseIntFormat::NON_NEGATIVE || !starts_with_negative) |
| 58 return SetError(ParseIntError::FAILED_PARSE, optional_error); |
| 59 } |
| 60 |
| 61 // Dispatch to the appropriate flavor of base::StringToXXX() by calling one of |
| 62 // the type-specific overloads. |
| 63 T result; |
| 64 if (StringToNumber(input, &result)) { |
| 65 *output = result; |
| 66 return true; |
| 67 } |
| 68 |
| 69 // Optimization: If the error is not going to be inspected, don't bother |
| 70 // calculating it. |
| 71 if (!optional_error) |
| 13 return false; | 72 return false; |
| 14 | 73 |
| 15 int result; | 74 // Set an error that distinguishes between parsing/underflow/overflow errors. |
| 16 if (!base::StringToInt(input, &result)) | 75 // |
| 17 return false; | 76 // Note that the output set by base::StringToXXX() on failure cannot be used |
| 77 // as it has ambiguity with parse errors. |
| 18 | 78 |
| 19 *output = result; | 79 // Strip any leading negative sign off the number. |
| 20 return true; | 80 base::StringPiece numeric_portion = |
| 81 starts_with_negative ? input.substr(1) : input; |
| 82 |
| 83 // Test if |numeric_portion| is a valid non-negative integer. |
| 84 if (!numeric_portion.empty() && |
| 85 numeric_portion.find_first_not_of("0123456789") == std::string::npos) { |
| 86 // If it was, the failure must have been due to underflow/overflow. |
| 87 return SetError(starts_with_negative ? ParseIntError::FAILED_UNDERFLOW |
| 88 : ParseIntError::FAILED_OVERFLOW, |
| 89 optional_error); |
| 90 } |
| 91 |
| 92 // Otherwise it was a mundane parsing error. |
| 93 return SetError(ParseIntError::FAILED_PARSE, optional_error); |
| 94 } |
| 95 |
| 96 } // namespace |
| 97 |
| 98 bool ParseInt32(const base::StringPiece& input, |
| 99 ParseIntFormat format, |
| 100 int32_t* output, |
| 101 ParseIntError* optional_error) { |
| 102 return ParseIntHelper(input, format, output, optional_error); |
| 103 } |
| 104 |
| 105 bool ParseInt64(const base::StringPiece& input, |
| 106 ParseIntFormat format, |
| 107 int64_t* output, |
| 108 ParseIntError* optional_error) { |
| 109 return ParseIntHelper(input, format, output, optional_error); |
| 110 } |
| 111 |
| 112 bool ParseUint32(const base::StringPiece& input, |
| 113 uint32_t* output, |
| 114 ParseIntError* optional_error) { |
| 115 return ParseIntHelper(input, ParseIntFormat::NON_NEGATIVE, output, |
| 116 optional_error); |
| 117 } |
| 118 |
| 119 bool ParseUint64(const base::StringPiece& input, |
| 120 uint64_t* output, |
| 121 ParseIntError* optional_error) { |
| 122 return ParseIntHelper(input, ParseIntFormat::NON_NEGATIVE, output, |
| 123 optional_error); |
| 21 } | 124 } |
| 22 | 125 |
| 23 } // namespace net | 126 } // namespace net |
| OLD | NEW |