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

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: Address remaining feedback 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 ParseInteger policy,
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
mmenke 2016/03/25 22:10:56 nit: +.
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 (policy == ParseInteger::DISALLOW_NEGATIVE || !starts_with_negative)
60 return SetError(ParseIntegerError::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
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 ? ParseIntegerError::FAILED_UNDERFLOW
96 : ParseIntegerError::FAILED_OVERFLOW,
97 optional_error);
98 }
99
100 // Otherwise it was a mundane parsing error.
101 return SetError(ParseIntegerError::FAILED_PARSE, optional_error);
102 }
103
104 } // namespace
105
106 bool ParseIntegerBase10(const base::StringPiece& input,
107 ParseInteger policy,
108 int32_t* output,
109 ParseIntegerError* optional_error) {
110 return ParseIntegerBase10Helper(input, policy, output, optional_error);
111 }
112
113 bool ParseIntegerBase10(const base::StringPiece& input,
114 ParseInteger policy,
115 int64_t* output,
116 ParseIntegerError* optional_error) {
117 return ParseIntegerBase10Helper(input, policy, output, optional_error);
118 }
119
120 bool ParseUnsignedIntegerBase10(const base::StringPiece& input,
121 uint32_t* output,
122 ParseIntegerError* optional_error) {
123 return ParseIntegerBase10Helper(input, ParseInteger::DISALLOW_NEGATIVE,
124 output, optional_error);
125 }
126
127 bool ParseUnsignedIntegerBase10(const base::StringPiece& input,
128 uint64_t* output,
129 ParseIntegerError* optional_error) {
130 return ParseIntegerBase10Helper(input, ParseInteger::DISALLOW_NEGATIVE,
131 output, optional_error);
21 } 132 }
22 133
23 } // namespace net 134 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698