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

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: update interface only (didn't address other comments yet) Created 4 years, 8 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
55 //
56 // Reject everything else to prevent the permissive behavior of
57 // StringToIntXXX() when it comes to accepting a leading '+'.
58 if (policy == ParseInteger::ALLOW_NEGATIVE && !starts_with_digit &&
59 !starts_with_negative) {
60 return SetError(ParseIntegerError::FAILED_PARSE, optional_error);
61 } else if (policy == ParseInteger::DISALLOW_NEGATIVE && !starts_with_digit) {
62 return SetError(ParseIntegerError::FAILED_PARSE, optional_error);
63 }
64
65 // Dispatch to the appropriate flavor of base::StringToXXX() by calling one of
66 // the type-specific overloads.
67 T result;
68 if (StringToNumber(input, &result)) {
69 *output = result;
70 return true;
71 }
72
73 // Set an error that distinguishes between parsing/underflow/overflow errors.
74 //
75 // Note that base::StringToXXX() functions have a magical API that modify the
76 // output on failure to indicate whether underflow/overflow happened.
77 //
78 // You would think these can be used here, but unfortunately they return those
79 // magic values in multiple cases making it impossible to distinguish
80 // underflow/overflow from failed parsing due to leading/trailing whitespace.
81 //
82 // So instead look at the number to see if it was valid.
83
84 // Optimization: If the error is not going to be inspected, don't bother
85 // refining it.
86 if (!optional_error)
13 return false; 87 return false;
14 88
15 int result; 89 // Strip any leading negative sign off the number.
16 if (!base::StringToInt(input, &result)) 90 base::StringPiece numeric_portion =
17 return false; 91 starts_with_negative ? input.substr(1) : input;
18 92
19 *output = result; 93 // Test if |numeric_portion| is a valid non-negative integer.
20 return true; 94 if (!numeric_portion.empty() &&
95 numeric_portion.find_first_not_of("0123456789") == std::string::npos) {
96 // If it was, the failure must have been due to underflow/overflow.
97 return SetError(starts_with_negative ? ParseIntegerError::FAILED_UNDERFLOW
98 : ParseIntegerError::FAILED_OVERFLOW,
99 optional_error);
100 }
101
102 // Otherwise it was a mundane parsing error.
103 return SetError(ParseIntegerError::FAILED_PARSE, optional_error);
104 }
105
106 } // namespace
107
108 bool ParseIntegerBase10(const base::StringPiece& input,
109 ParseInteger policy,
110 int32_t* output,
111 ParseIntegerError* optional_error) {
112 return ParseIntegerBase10Helper(input, policy, output, optional_error);
113 }
114
115 bool ParseIntegerBase10(const base::StringPiece& input,
116 ParseInteger policy,
117 int64_t* output,
118 ParseIntegerError* optional_error) {
119 return ParseIntegerBase10Helper(input, policy, output, optional_error);
120 }
121
122 bool ParseUnsignedIntegerBase10(const base::StringPiece& input,
123 uint32_t* output,
124 ParseIntegerError* optional_error) {
125 return ParseIntegerBase10Helper(input, ParseInteger::DISALLOW_NEGATIVE,
126 output, optional_error);
127 }
128
129 bool ParseUnsignedIntegerBase10(const base::StringPiece& input,
130 uint64_t* output,
131 ParseIntegerError* optional_error) {
132 return ParseIntegerBase10Helper(input, ParseInteger::DISALLOW_NEGATIVE,
133 output, optional_error);
21 } 134 }
22 135
23 } // namespace net 136 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698