| 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.
|
| //
|
| +// Another difficulty with using base::StringToInt() is distinguishing
|
| +// overflow/underflow from parsing failures.
|
| +//
|
| // 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
|
| + // numbers, as well as many other HTTP related standards.
|
| + 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
|
| +};
|
| +
|
| +// 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.
|
| + 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
|
| +// ParseInteger::DISALLOW_NEGATIVE.
|
| +NET_EXPORT bool ParseIntegerBase10(const base::StringPiece& input,
|
| + ParseInteger policy,
|
| + int32_t* output,
|
| + ParseIntegerError* optional_error = nullptr)
|
| + WARN_UNUSED_RESULT;
|
| +
|
| +NET_EXPORT bool ParseIntegerBase10(const base::StringPiece& input,
|
| + ParseInteger policy,
|
| + int64_t* output,
|
| + ParseIntegerError* optional_error = nullptr)
|
| + WARN_UNUSED_RESULT;
|
| +
|
| +// 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
|
|
|
|
|