| 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 { |
| 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. |
| 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 |
| 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 template <typename T> |
| 38 bool ParseNonNegativeIntegerHelper(const base::StringPiece& input, T* output) { |
| 39 // The input MUST start with a decimal digit, since it is not allowed to start |
| 40 // with '-' (by virtue of being non-negative), and nor is it allowed to start |
| 41 // with '+'. |
| 12 if (input.empty() || input[0] > '9' || input[0] < '0') | 42 if (input.empty() || input[0] > '9' || input[0] < '0') |
| 13 return false; | 43 return false; |
| 14 | 44 |
| 15 int result; | 45 // Dispatch to the appropriate flavor of base::StringToXXX() by calling one of |
| 16 if (!base::StringToInt(input, &result)) | 46 // the overloads defined above. |
| 47 T result; |
| 48 if (!StringToNumber(input, &result)) |
| 49 return false; |
| 50 |
| 51 // The result cannot be negative since inputs starting with a '-' were already |
| 52 // rejected. |
| 53 DCHECK_GE(result, T()); |
| 54 |
| 55 *output = result; |
| 56 return true; |
| 57 } |
| 58 |
| 59 template <typename T> |
| 60 bool ParseSignedIntegerHelper(const base::StringPiece& input, T* output) { |
| 61 // The input MUST start with a decimal digit or a '-'. Numbers starting with |
| 62 // '+' are rejected here (since StringToNumber() might otherwise accept it). |
| 63 if (input.empty() || |
| 64 !(input[0] == '-' || (input[0] >= '0' && input[0] <= '9'))) { |
| 65 return false; |
| 66 } |
| 67 |
| 68 // Dispatch to the appropriate flavor of base::StringToXXX() by calling one of |
| 69 // the overloads defined above. |
| 70 T result; |
| 71 if (!StringToNumber(input, &result)) |
| 17 return false; | 72 return false; |
| 18 | 73 |
| 19 *output = result; | 74 *output = result; |
| 20 return true; | 75 return true; |
| 21 } | 76 } |
| 22 | 77 |
| 78 } // namespace |
| 79 |
| 80 bool ParseNonNegativeInteger(const base::StringPiece& input, int32_t* output) { |
| 81 return ParseNonNegativeIntegerHelper(input, output); |
| 82 } |
| 83 |
| 84 bool ParseNonNegativeInteger(const base::StringPiece& input, uint32_t* output) { |
| 85 return ParseNonNegativeIntegerHelper(input, output); |
| 86 } |
| 87 |
| 88 bool ParseNonNegativeInteger(const base::StringPiece& input, int64_t* output) { |
| 89 return ParseNonNegativeIntegerHelper(input, output); |
| 90 } |
| 91 |
| 92 bool ParseNonNegativeInteger(const base::StringPiece& input, uint64_t* output) { |
| 93 return ParseNonNegativeIntegerHelper(input, output); |
| 94 } |
| 95 |
| 96 bool ParseSignedInteger(const base::StringPiece& input, int64_t* output) { |
| 97 return ParseSignedIntegerHelper(input, output); |
| 98 } |
| 99 |
| 100 bool ParseSignedInteger(const base::StringPiece& input, int32_t* output) { |
| 101 return ParseSignedIntegerHelper(input, output); |
| 102 } |
| 103 |
| 23 } // namespace net | 104 } // namespace net |
| OLD | NEW |