| Index: net/base/parse_number.cc
|
| diff --git a/net/base/parse_number.cc b/net/base/parse_number.cc
|
| index d315bec63a906dba00b5f27e56dd20161ace6266..2ad0fd5faed845a1a4e5d5de58c5d8c081de9b46 100644
|
| --- a/net/base/parse_number.cc
|
| +++ b/net/base/parse_number.cc
|
| @@ -4,20 +4,101 @@
|
|
|
| #include "net/base/parse_number.h"
|
|
|
| +#include "base/logging.h"
|
| #include "base/strings/string_number_conversions.h"
|
|
|
| namespace net {
|
|
|
| -bool ParseNonNegativeDecimalInt(const base::StringPiece& input, int* output) {
|
| +namespace {
|
| +
|
| +// The string to number conversion functions in //base include the type in the
|
| +// name (like StringToInt64()). The following wrapper methods create a
|
| +// consistent interface to StringToXXX() that calls the appropriate //base
|
| +// version. This simplifies writing generic code with a template.
|
| +
|
| +bool StringToNumber(const base::StringPiece& input, int32_t* output) {
|
| + // This assumes ints are 32-bits.
|
| + return base::StringToInt(input, output);
|
| +}
|
| +
|
| +bool StringToNumber(const base::StringPiece& input, uint32_t* output) {
|
| + // This assumes ints are 32-bits
|
| + return base::StringToUint(input, output);
|
| +}
|
| +
|
| +bool StringToNumber(const base::StringPiece& input, int64_t* output) {
|
| + return base::StringToInt64(input, output);
|
| +}
|
| +
|
| +bool StringToNumber(const base::StringPiece& input, uint64_t* output) {
|
| + return base::StringToUint64(input, output);
|
| +}
|
| +
|
| +template <typename T>
|
| +bool ParseNonNegativeIntegerHelper(const base::StringPiece& input, T* output) {
|
| + // The input MUST start with a decimal digit, since it is not allowed to start
|
| + // with '-' (by virtue of being non-negative), and nor is it allowed to start
|
| + // with '+'.
|
| if (input.empty() || input[0] > '9' || input[0] < '0')
|
| return false;
|
|
|
| - int result;
|
| - if (!base::StringToInt(input, &result))
|
| + // Dispatch to the appropriate flavor of base::StringToXXX() by calling one of
|
| + // the overloads defined above.
|
| + T result;
|
| + if (!StringToNumber(input, &result))
|
| + return false;
|
| +
|
| + // The result cannot be negative since inputs starting with a '-' were already
|
| + // rejected.
|
| + DCHECK_GE(result, T());
|
| +
|
| + *output = result;
|
| + return true;
|
| +}
|
| +
|
| +template <typename T>
|
| +bool ParseSignedIntegerHelper(const base::StringPiece& input, T* output) {
|
| + // The input MUST start with a decimal digit or a '-'. Numbers starting with
|
| + // '+' are rejected here (since StringToNumber() might otherwise accept it).
|
| + if (input.empty() ||
|
| + !(input[0] == '-' || (input[0] >= '0' && input[0] <= '9'))) {
|
| + return false;
|
| + }
|
| +
|
| + // Dispatch to the appropriate flavor of base::StringToXXX() by calling one of
|
| + // the overloads defined above.
|
| + T result;
|
| + if (!StringToNumber(input, &result))
|
| return false;
|
|
|
| *output = result;
|
| return true;
|
| }
|
|
|
| +} // namespace
|
| +
|
| +bool ParseNonNegativeInteger(const base::StringPiece& input, int32_t* output) {
|
| + return ParseNonNegativeIntegerHelper(input, output);
|
| +}
|
| +
|
| +bool ParseNonNegativeInteger(const base::StringPiece& input, uint32_t* output) {
|
| + return ParseNonNegativeIntegerHelper(input, output);
|
| +}
|
| +
|
| +bool ParseNonNegativeInteger(const base::StringPiece& input, int64_t* output) {
|
| + return ParseNonNegativeIntegerHelper(input, output);
|
| +}
|
| +
|
| +bool ParseNonNegativeInteger(const base::StringPiece& input, uint64_t* output) {
|
| + return ParseNonNegativeIntegerHelper(input, output);
|
| +}
|
| +
|
| +bool ParseSignedInteger(const base::StringPiece& input, int64_t* output) {
|
| + return ParseSignedIntegerHelper(input, output);
|
| +}
|
| +
|
| +bool ParseSignedInteger(const base::StringPiece& input, int32_t* output) {
|
| + return ParseSignedIntegerHelper(input, output);
|
| +}
|
| +
|
| } // namespace net
|
|
|