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

Unified Diff: net/base/parse_number.h

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 side-by-side diff with in-line comments
Download patch
Index: net/base/parse_number.h
diff --git a/net/base/parse_number.h b/net/base/parse_number.h
index 0b88cf0435b3af69d1330c33d07acdbc11c60e52..385a1eb30aabd6c8e798480b262f32d7806c342f 100644
--- a/net/base/parse_number.h
+++ b/net/base/parse_number.h
@@ -27,6 +27,9 @@
// you wouldn't want to recognize "foo:+99" as having a port of 99. The same
// issue applies when parsing a content-length header.
Ryan Sleevi 2016/03/25 22:35:17 s/content-length/Content-Length/
eroman 2016/04/07 01:17:01 No longer applicable -I re-wrote this entire comme
//
+// Another difficulty with using base::StringToInt() is distinguishing
+// overflow/underflow from parsing failures.
Ryan Sleevi 2016/03/25 22:35:17 Not sure why this matters?
eroman 2016/03/25 23:02:23 Some code wants to know when overflow has happened
+//
// To reduce the risk of such problems, use of these functions over the
// //base versions.
@@ -34,34 +37,79 @@ class GURL;
namespace net {
-// Parses a string representing a decimal number to an |int|. Returns true on
-// success, and fills |*output| with the result. Note that |*output| is not
-// modified on failure.
-//
-// Recognized inputs take the form:
-// 1*DIGIT
-//
-// Where DIGIT is an ASCII number in the range '0' - '9'
+// Policy for parsing of integers that determines which form of numbers will be
+// accepted.
+enum class ParseInteger {
+ // Accept inputs of the form:
+ //
+ // 1*DIGIT
+ //
+ // This is consistent with the description in RFC 2616 for representing
Ryan Sleevi 2016/03/25 22:35:17 s/2616/7230/
eroman 2016/04/07 01:17:01 Done.
+ // numbers, as well as many other HTTP related standards.
Ryan Sleevi 2016/03/25 22:35:17 It's not just HTTP standards (DIGIT comes from the
eroman 2016/04/07 01:17:01 Done.
+ DISALLOW_NEGATIVE,
+
+ // Accept inputs of the form:
+ //
+ // ["-"] 1*DIGIT
+ //
+ // In other words, the number can optional start with a negative sign. Note
+ // that -0 is also counted as a valid input.
+ ALLOW_NEGATIVE
mmenke 2016/03/25 22:10:56 Think it's worth noting that if this is true, we r
eroman 2016/04/07 01:17:01 You mean a comment in DISALLOW_NEGATIVE? Done.
+};
+
+// The specific reason why a ParseInteger*() function failed.
+enum class ParseIntegerError {
+ // The parsed number couldn't fit into the provided output type because it was
+ // too large.
mmenke 2016/03/25 22:10:56 nit: suggest high / low instead of large/small. L
eroman 2016/04/07 01:17:01 Done.
+ FAILED_OVERFLOW,
+
+ // The parsed number couldn't fit into the provided output type because it was
+ // too small (negative).
+ FAILED_UNDERFLOW,
+
+ // The number failed to be parsed because it wasn't a valid decimal number (as
+ // determined by the policy).
+ FAILED_PARSE,
+};
+
+// ParseIntegerBase10() parses a string representing a decimal number to a
+// SIGNED integer result.
//
-// Note that:
-// * Parsing is locale independent
-// * Leading zeros are allowed (numbers needn't be in minimal encoding)
-// * Inputs that would overflow the output are rejected.
-// * Only accepts integers
+// Returns true on success, and fills |*output| with the result. If
+// |optional_error| was non-null, then it is filled with the reason for the
+// failure.
//
-// Examples of recognized inputs are:
-// "13"
-// "0"
-// "00013"
+// On failure it is guaranteed that |*output| was not modified.
//
-// Examples of rejected inputs are:
-// " 13"
-// "-13"
-// "+13"
-// "0x15"
-// "13.3"
-NET_EXPORT bool ParseNonNegativeDecimalInt(const base::StringPiece& input,
- int* output) WARN_UNUSED_RESULT;
+// See the ParseInteger enum for a description of what types of input are
+// accepted. Note that although the output type is signed, the parsing of
+// negative values can be disallowed by selecting the
mmenke 2016/03/25 22:10:56 "selecting the" -> "passing" / "using"?
eroman 2016/04/07 01:17:01 Done.
+// ParseInteger::DISALLOW_NEGATIVE.
+NET_EXPORT bool ParseIntegerBase10(const base::StringPiece& input,
+ ParseInteger policy,
+ int32_t* output,
+ ParseIntegerError* optional_error = nullptr)
Ryan Sleevi 2016/03/25 22:35:17 With respect to the defaults here, it seems like t
eroman 2016/03/25 23:02:23 The optionality of the error is primarily to make
+ WARN_UNUSED_RESULT;
+
+NET_EXPORT bool ParseIntegerBase10(const base::StringPiece& input,
+ ParseInteger policy,
+ int64_t* output,
+ ParseIntegerError* optional_error = nullptr)
+ WARN_UNUSED_RESULT;
Ryan Sleevi 2016/03/25 22:35:17 I would argue that overloading like this violates
mmenke 2016/03/25 22:49:59 "Use overloaded functions (including constructors)
eroman 2016/03/25 23:02:23 For technical reasons I am going to have to avoid
+
+// ParseUnsignedIntegerBase10() is the same as calling ParseIntegerBase10()
+// except:
+// * The output type is unsigned
+// * The policy is implied to be ParseInteger::DISALLOW_NEGATIVE
+NET_EXPORT bool ParseUnsignedIntegerBase10(
+ const base::StringPiece& input,
+ uint32_t* output,
+ ParseIntegerError* optional_error = nullptr) WARN_UNUSED_RESULT;
+
+NET_EXPORT bool ParseUnsignedIntegerBase10(
+ const base::StringPiece& input,
+ uint64_t* output,
+ ParseIntegerError* optional_error = nullptr) WARN_UNUSED_RESULT;
} // namespace net
« no previous file with comments | « net/base/ip_address.cc ('k') | net/base/parse_number.cc » ('j') | net/base/parse_number.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698