Chromium Code Reviews| 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 if (input.empty()) | |
| 49 return SetError(ParseIntError::FAILED_PARSE, optional_error); | |
| 50 | |
| 51 bool starts_with_negative = input[0] == '-'; | |
| 52 bool starts_with_digit = input[0] >= '0' && input[0] <= '9'; | |
| 53 | |
| 54 // Numbers must start with either a digit or a negative sign | |
| 55 // | |
| 56 // Reject everything else to prevent the permissive behavior of | |
| 57 // StringToIntXXX() when it comes to accepting a leading '+'. | |
| 58 if (!starts_with_digit) { | |
| 59 if (format == ParseIntFormat::NON_NEGATIVE || !starts_with_negative) | |
| 60 return SetError(ParseIntError::FAILED_PARSE, optional_error); | |
| 61 } | |
| 62 | |
| 63 // Dispatch to the appropriate flavor of base::StringToXXX() by calling one of | |
| 64 // the type-specific overloads. | |
| 65 T result; | |
| 66 if (StringToNumber(input, &result)) { | |
| 67 *output = result; | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 // Set an error that distinguishes between parsing/underflow/overflow errors. | |
| 72 // | |
| 73 // Note that base::StringToXXX() functions have a magical API that modify the | |
| 74 // output on failure to indicate whether underflow/overflow happened. | |
| 75 // | |
| 76 // You would think these can be used here, but unfortunately they return those | |
|
mmenke
2016/04/07 20:42:53
nit: Think it's best to avoid personal pronouns l
eroman
2016/04/08 18:57:32
Done. I simplified the various comments in this fu
| |
| 77 // magic values in multiple cases making it impossible to distinguish | |
| 78 // underflow/overflow from failed parsing due to leading/trailing whitespace. | |
| 79 // | |
| 80 // So instead look at the number to see if it was valid. | |
| 81 | |
| 82 // Optimization: If the error is not going to be inspected, don't bother | |
| 83 // refining it. | |
| 84 if (!optional_error) | |
| 13 return false; | 85 return false; |
| 14 | 86 |
| 15 int result; | 87 // Strip any leading negative sign off the number. |
| 16 if (!base::StringToInt(input, &result)) | 88 base::StringPiece numeric_portion = |
| 17 return false; | 89 starts_with_negative ? input.substr(1) : input; |
| 18 | 90 |
| 19 *output = result; | 91 // Test if |numeric_portion| is a valid non-negative integer. |
| 20 return true; | 92 if (!numeric_portion.empty() && |
| 93 numeric_portion.find_first_not_of("0123456789") == std::string::npos) { | |
| 94 // If it was, the failure must have been due to underflow/overflow. | |
| 95 return SetError(starts_with_negative ? ParseIntError::FAILED_UNDERFLOW | |
| 96 : ParseIntError::FAILED_OVERFLOW, | |
| 97 optional_error); | |
| 98 } | |
| 99 | |
| 100 // Otherwise it was a mundane parsing error. | |
| 101 return SetError(ParseIntError::FAILED_PARSE, optional_error); | |
| 102 } | |
| 103 | |
| 104 } // namespace | |
| 105 | |
| 106 bool ParseInt32(const base::StringPiece& input, | |
| 107 ParseIntFormat format, | |
| 108 int32_t* output, | |
| 109 ParseIntError* optional_error) { | |
| 110 return ParseIntHelper(input, format, output, optional_error); | |
| 111 } | |
| 112 | |
| 113 bool ParseInt64(const base::StringPiece& input, | |
| 114 ParseIntFormat format, | |
| 115 int64_t* output, | |
| 116 ParseIntError* optional_error) { | |
| 117 return ParseIntHelper(input, format, output, optional_error); | |
| 118 } | |
| 119 | |
| 120 bool ParseUint32(const base::StringPiece& input, | |
| 121 uint32_t* output, | |
| 122 ParseIntError* optional_error) { | |
| 123 return ParseIntHelper(input, ParseIntFormat::NON_NEGATIVE, output, | |
| 124 optional_error); | |
| 125 } | |
| 126 | |
| 127 bool ParseUint64(const base::StringPiece& input, | |
| 128 uint64_t* output, | |
| 129 ParseIntError* optional_error) { | |
| 130 return ParseIntHelper(input, ParseIntFormat::NON_NEGATIVE, output, | |
| 131 optional_error); | |
| 21 } | 132 } |
| 22 | 133 |
| 23 } // namespace net | 134 } // namespace net |
| OLD | NEW |