blob: 7ea9f251ed468bbb6a8a0cf76c6888d491c0309e [file] [log] [blame]
ossua280f7c2017-04-06 02:02:15 -07001/*
2 * Copyright 2017 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_STRING_TO_NUMBER_H_
12#define RTC_BASE_STRING_TO_NUMBER_H_
ossua280f7c2017-04-06 02:02:15 -070013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <limits>
Yves Gerey665174f2018-06-19 15:03:05 +020015#include <string>
ossua280f7c2017-04-06 02:02:15 -070016
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020017#include "absl/types/optional.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018
19namespace rtc {
20
21// This file declares a family of functions to parse integers from strings.
22// The standard C library functions either fail to indicate errors (atoi, etc.)
23// or are a hassle to work with (strtol, sscanf, etc.). The standard C++ library
24// functions (std::stoi, etc.) indicate errors by throwing exceptions, which
25// are disabled in WebRTC.
26//
27// Integers are parsed using one of the following functions:
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020028// absl::optional<int-type> StringToNumber(const char* str, int base = 10);
29// absl::optional<int-type> StringToNumber(const std::string& str,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020030// int base = 10);
31//
32// These functions parse a value from the beginning of a string into one of the
33// fundamental integer types, or returns an empty Optional if parsing
34// failed. Values outside of the range supported by the type will be
35// rejected. The strings must begin with a digit or a minus sign. No leading
36// space nor trailing contents are allowed.
37// By setting base to 0, one of octal, decimal or hexadecimal will be
38// detected from the string's prefix (0, nothing or 0x, respectively).
39// If non-zero, base can be set to a value between 2 and 36 inclusively.
40//
41// If desired, this interface could be extended with support for floating-point
42// types.
43
44namespace string_to_number_internal {
45// These must be (unsigned) long long, to match the signature of strto(u)ll.
46using unsigned_type = unsigned long long; // NOLINT(runtime/int)
Yves Gerey665174f2018-06-19 15:03:05 +020047using signed_type = long long; // NOLINT(runtime/int)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020048
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020049absl::optional<signed_type> ParseSigned(const char* str, int base);
50absl::optional<unsigned_type> ParseUnsigned(const char* str, int base);
Jonas Olsson6b1985d2018-07-05 11:59:48 +020051
52template <typename T>
53absl::optional<T> ParseFloatingPoint(const char* str);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054} // namespace string_to_number_internal
55
56template <typename T>
57typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value,
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020058 absl::optional<T>>::type
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020059StringToNumber(const char* str, int base = 10) {
60 using string_to_number_internal::signed_type;
61 static_assert(
62 std::numeric_limits<T>::max() <=
63 std::numeric_limits<signed_type>::max() &&
64 std::numeric_limits<T>::lowest() >=
65 std::numeric_limits<signed_type>::lowest(),
66 "StringToNumber only supports signed integers as large as long long int");
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020067 absl::optional<signed_type> value =
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068 string_to_number_internal::ParseSigned(str, base);
69 if (value && *value >= std::numeric_limits<T>::lowest() &&
70 *value <= std::numeric_limits<T>::max()) {
Oskar Sundbom9c780582017-11-27 10:28:22 +010071 return static_cast<T>(*value);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020072 }
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020073 return absl::nullopt;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020074}
75
76template <typename T>
77typename std::enable_if<std::is_integral<T>::value &&
78 std::is_unsigned<T>::value,
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020079 absl::optional<T>>::type
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020080StringToNumber(const char* str, int base = 10) {
81 using string_to_number_internal::unsigned_type;
82 static_assert(std::numeric_limits<T>::max() <=
83 std::numeric_limits<unsigned_type>::max(),
84 "StringToNumber only supports unsigned integers as large as "
85 "unsigned long long int");
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020086 absl::optional<unsigned_type> value =
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020087 string_to_number_internal::ParseUnsigned(str, base);
88 if (value && *value <= std::numeric_limits<T>::max()) {
Oskar Sundbom9c780582017-11-27 10:28:22 +010089 return static_cast<T>(*value);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020090 }
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020091 return absl::nullopt;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020092}
93
Jonas Olsson6b1985d2018-07-05 11:59:48 +020094template <typename T>
95typename std::enable_if<std::is_floating_point<T>::value,
96 absl::optional<T>>::type
97StringToNumber(const char* str, int base = 10) {
98 static_assert(
99 std::numeric_limits<T>::max() <= std::numeric_limits<long double>::max(),
100 "StringToNumber only supports floating-point numbers as large "
101 "as long double");
102 return string_to_number_internal::ParseFloatingPoint<T>(str);
103}
104
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200105// The std::string overloads only exists if there is a matching const char*
106// version.
107template <typename T>
108auto StringToNumber(const std::string& str, int base = 10)
109 -> decltype(StringToNumber<T>(str.c_str(), base)) {
110 return StringToNumber<T>(str.c_str(), base);
111}
112
113} // namespace rtc
ossua280f7c2017-04-06 02:02:15 -0700114
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200115#endif // RTC_BASE_STRING_TO_NUMBER_H_