chromium/net/base/parse_number.h

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_BASE_PARSE_NUMBER_H_
#define NET_BASE_PARSE_NUMBER_H_

#include <cstdint>
#include <string_view>

#include "net/base/net_export.h"

// This file contains utility functions for parsing numbers, in the context of
// network protocols.
//
// Q: Doesn't //base already provide these in string_number_conversions.h, with
//    functions like base::StringToInt()?
//
// A: Yes, and those functions are used under the hood by these implementations.
//
//    However using the base::StringTo*() has historically led to subtle bugs
//    in the context of parsing network protocols:
//
//      * Permitting a leading '+'
//      * Incorrectly classifying overflow/underflow from a parsing failure
//      * Allowing negative numbers for non-negative fields
//
//   This API tries to avoid these problems by picking sensible defaults for
//   //net code. For more details see crbug.com/596523.

namespace net {

// Format to use when parsing integers.
enum class ParseIntFormat {};

// The specific reason why a ParseInt*() function failed.
enum class ParseIntError {};

// The ParseInt*() functions parse a string representing a number.
//
// The format of the strings that are accepted is controlled by the |format|
// parameter. This allows rejecting negative numbers.
//
// These functions return true on success, and fill |*output| with the result.
//
// On failure, it is guaranteed that |*output| was not modified. If
// |optional_error| was non-null, then it is filled with the reason for the
// failure.
[[nodiscard]] NET_EXPORT bool ParseInt32(
    std::string_view input,
    ParseIntFormat format,
    int32_t* output,
    ParseIntError* optional_error = nullptr);

[[nodiscard]] NET_EXPORT bool ParseInt64(
    std::string_view input,
    ParseIntFormat format,
    int64_t* output,
    ParseIntError* optional_error = nullptr);

// The ParseUint*() functions parse a string representing a number.
//
// These are equivalent to calling ParseInt*(), except with unsigned output
// types. ParseIntFormat may only be one of {NON_NEGATIVE, STRICT_NON_NEGATIVE}.
[[nodiscard]] NET_EXPORT bool ParseUint32(
    std::string_view input,
    ParseIntFormat format,
    uint32_t* output,
    ParseIntError* optional_error = nullptr);

[[nodiscard]] NET_EXPORT bool ParseUint64(
    std::string_view input,
    ParseIntFormat format,
    uint64_t* output,
    ParseIntError* optional_error = nullptr);

}  // namespace net

#endif  // NET_BASE_PARSE_NUMBER_H_