Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: net/base/parse_number.cc

Issue 1828103002: Extend net/base/parse_number.h for parsing of negative numbers, and determining if there was overflo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proxy_num
Patch Set: improve comments Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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(ParseIntegerError error, ParseIntegerError* optional_error) {
38 if (optional_error)
39 *optional_error = error;
40 return false;
41 }
42
43 template <typename T>
44 bool ParseIntegerBase10Helper(const base::StringPiece& input,
45 bool allow_negative,
46 T* output,
47 ParseIntegerError* optional_error) {
48 if (input.empty())
49 return SetError(ParseIntegerError::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 everythin else to prevent the permissive behavior of
57 // StringToIntXXX() when it comes to accepting a leading '+'.
58 if (allow_negative && !starts_with_digit && !starts_with_negative) {
59 return SetError(ParseIntegerError::FAILED_PARSE, optional_error);
60 } else if (!allow_negative && !starts_with_digit) {
61 return SetError(ParseIntegerError::FAILED_PARSE, optional_error);
62 }
mmenke 2016/03/25 15:31:49 optional: Think this may be clearer as: if (!sta
eroman 2016/03/25 19:04:29 That is definitely better. Done.
63
64 // Dispatch to the appropriate flavor of base::StringToXXX() by calling one of
65 // the type-specific overloads.
66 T result;
67 if (StringToNumber(input, &result)) {
68 *output = result;
69 return true;
70 }
71
72 // Set an error that distinguishes between parsing/underflow/overflow errors.
73 //
74 // Note that base::StringToXXX() functions have a magical API that modify the
75 // output on failure to indicate whether underflow/overflow happened.
76 //
77 // You would think these can be used here, but unfortunately they return those
78 // magic values in multiple cases making it impossible to distinguish
79 // underflow/overflow from failed parsing due to leading/trailing whitespace.
80 //
81 // So instead look at the number to see if it was valid.
82
83 // Optimization: If the error is not going to be inspected, don't bother
84 // refining it.
85 if (!optional_error)
13 return false; 86 return false;
14 87
15 int result; 88 // Strip any leading negative sign off the number.
16 if (!base::StringToInt(input, &result)) 89 base::StringPiece numeric_portion =
17 return false; 90 starts_with_negative ? input.substr(1) : input;
18 91
19 *output = result; 92 // Test if |numeric_portion| is a valid non-negative integer.
20 return true; 93 if (!numeric_portion.empty() &&
94 numeric_portion.find_first_not_of("0123456789") == std::string::npos) {
95 // If it was, the failure must have been due to underflow/overflow.
96 return SetError(starts_with_negative ? ParseIntegerError::FAILED_UNDERFLOW
97 : ParseIntegerError::FAILED_OVERFLOW,
98 optional_error);
99 }
100
101 // Otherwise it was a mundane parsing error.
102 return SetError(ParseIntegerError::FAILED_PARSE, optional_error);
103 }
104
105 } // namespace
106
107 bool ParseIntegerBase10(const base::StringPiece& input,
108 bool allow_negative,
109 int32_t* output,
110 ParseIntegerError* optional_error) {
111 return ParseIntegerBase10Helper(input, allow_negative, output,
112 optional_error);
113 }
114
115 bool ParseIntegerBase10(const base::StringPiece& input,
116 bool allow_negative,
117 uint32_t* output,
118 ParseIntegerError* optional_error) {
119 return ParseIntegerBase10Helper(input, allow_negative, output,
120 optional_error);
121 }
122
123 bool ParseIntegerBase10(const base::StringPiece& input,
124 bool allow_negative,
125 int64_t* output,
126 ParseIntegerError* optional_error) {
127 return ParseIntegerBase10Helper(input, allow_negative, output,
128 optional_error);
129 }
130
131 bool ParseIntegerBase10(const base::StringPiece& input,
132 bool allow_negative,
133 uint64_t* output,
134 ParseIntegerError* optional_error) {
135 return ParseIntegerBase10Helper(input, allow_negative, output,
136 optional_error);
21 } 137 }
22 138
23 } // namespace net 139 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698