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

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: remove http_response_headers changes (moving to other CL) 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 return false;
14 13
15 int result; 14 // The string to number conversion functions in //base include the type in the
16 if (!base::StringToInt(input, &result)) 15 // name (like StringToInt64()). The following wrapper methods create a
17 return false; 16 // consistent interface to StringToXXX() that calls the appropriate //base
17 // version. This simplifies writing generic code with a template.
18 18
19 *output = result; 19 bool StringToNumber(const base::StringPiece& input, int32_t* output) {
20 return true; 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 ParseIntegerResult ParseIntegerHelper(const base::StringPiece& input,
39 ParseIntegerSignPolicy sign_policy,
40 T* output) {
41 if (input.empty())
42 return ParseIntegerResult::FAILED_PARSE;
43
44 bool starts_with_negative = input[0] == '-';
45 bool starts_with_digit = input[0] >= '0' && input[0] <= '9';
46
47 // Numbers must start with either a digit or a negative sign
48 //
49 // Reject everythin else to prevent the permissive behavior of
50 // StringToIntXXX() when it comes to accepting a leading '+'.
51 switch (sign_policy) {
52 case ParseIntegerSignPolicy::NON_NEGATIVE:
53 if (!starts_with_digit)
54 return ParseIntegerResult::FAILED_PARSE;
55 break;
56
57 case ParseIntegerSignPolicy::ANY:
58 if (!starts_with_digit && !starts_with_negative)
59 return ParseIntegerResult::FAILED_PARSE;
60 break;
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 ParseIntegerResult::OK;
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
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 // Strip any leading negative sign off the number.
83 base::StringPiece numeric_portion =
84 starts_with_negative ? input.substr(1) : input;
85
86 // Test if |numeric_portion| is a valid non-negative integer.
87 if (!numeric_portion.empty() &&
88 numeric_portion.find_first_not_of("0123456789") == std::string::npos) {
89 // If it was, the failure must have been due to underflow/overflow.
90 return starts_with_negative ? ParseIntegerResult::FAILED_UNDERFLOW
91 : ParseIntegerResult::FAILED_OVERFLOW;
92 }
93
94 // Otherwise it was a mundane parsing error.
95 return ParseIntegerResult::FAILED_PARSE;
96 }
97
98 template <typename T>
99 bool ParseNonNegativeIntegerHelper(const base::StringPiece& input, T* output) {
100 auto result =
101 ParseIntegerHelper(input, ParseIntegerSignPolicy::NON_NEGATIVE, output);
102 return result == ParseIntegerResult::OK;
103 }
104
105 } // namespace
106
107 ParseIntegerResult ParseInteger(const base::StringPiece& input,
108 ParseIntegerSignPolicy sign_policy,
109 int32_t* output) {
110 return ParseIntegerHelper(input, sign_policy, output);
111 }
112
113 ParseIntegerResult ParseInteger(const base::StringPiece& input,
114 ParseIntegerSignPolicy sign_policy,
115 uint32_t* output) {
116 return ParseIntegerHelper(input, sign_policy, output);
117 }
118
119 ParseIntegerResult ParseInteger(const base::StringPiece& input,
120 ParseIntegerSignPolicy sign_policy,
121 int64_t* output) {
122 return ParseIntegerHelper(input, sign_policy, output);
123 }
124
125 ParseIntegerResult ParseInteger(const base::StringPiece& input,
126 ParseIntegerSignPolicy sign_policy,
127 uint64_t* output) {
128 return ParseIntegerHelper(input, sign_policy, output);
129 }
130
131 bool ParseNonNegativeInteger(const base::StringPiece& input, int32_t* output) {
132 return ParseNonNegativeIntegerHelper(input, output);
133 }
134
135 bool ParseNonNegativeInteger(const base::StringPiece& input, uint32_t* output) {
136 return ParseNonNegativeIntegerHelper(input, output);
137 }
138
139 bool ParseNonNegativeInteger(const base::StringPiece& input, int64_t* output) {
140 return ParseNonNegativeIntegerHelper(input, output);
141 }
142
143 bool ParseNonNegativeInteger(const base::StringPiece& input, uint64_t* output) {
144 return ParseNonNegativeIntegerHelper(input, output);
21 } 145 }
22 146
23 } // namespace net 147 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698