xref: /aosp_15_r20/external/webrtc/rtc_base/string_to_number.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 #ifndef RTC_BASE_STRING_TO_NUMBER_H_
12 #define RTC_BASE_STRING_TO_NUMBER_H_
13 
14 #include <limits>
15 #include <string>
16 #include <type_traits>
17 
18 #include "absl/strings/string_view.h"
19 #include "absl/types/optional.h"
20 
21 namespace rtc {
22 
23 // This file declares a family of functions to parse integers from strings.
24 // The standard C library functions either fail to indicate errors (atoi, etc.)
25 // or are a hassle to work with (strtol, sscanf, etc.). The standard C++ library
26 // functions (std::stoi, etc.) indicate errors by throwing exceptions, which
27 // are disabled in WebRTC.
28 //
29 // Integers are parsed using:
30 //   absl::optional<int-type> StringToNumber(absl::string_view str,
31 //                                           int base = 10);
32 //
33 // These functions parse a value from the beginning of a string into one of the
34 // fundamental integer types, or returns an empty Optional if parsing
35 // failed. Values outside of the range supported by the type will be
36 // rejected. The strings must begin with a digit or a minus sign. No leading
37 // space nor trailing contents are allowed.
38 // By setting base to 0, one of octal, decimal or hexadecimal will be
39 // detected from the string's prefix (0, nothing or 0x, respectively).
40 // If non-zero, base can be set to a value between 2 and 36 inclusively.
41 
42 namespace string_to_number_internal {
43 // These must be (unsigned) long long, to match the signature of strto(u)ll.
44 using unsigned_type = unsigned long long;  // NOLINT(runtime/int)
45 using signed_type = long long;             // NOLINT(runtime/int)
46 
47 absl::optional<signed_type> ParseSigned(absl::string_view str, int base);
48 absl::optional<unsigned_type> ParseUnsigned(absl::string_view str, int base);
49 
50 template <typename T>
51 absl::optional<T> ParseFloatingPoint(absl::string_view str);
52 }  // namespace string_to_number_internal
53 
54 template <typename T>
55 typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value,
56                         absl::optional<T>>::type
57 StringToNumber(absl::string_view str, int base = 10) {
58   using string_to_number_internal::signed_type;
59   static_assert(
60       std::numeric_limits<T>::max() <=
61               std::numeric_limits<signed_type>::max() &&
62           std::numeric_limits<T>::lowest() >=
63               std::numeric_limits<signed_type>::lowest(),
64       "StringToNumber only supports signed integers as large as long long int");
65   absl::optional<signed_type> value =
66       string_to_number_internal::ParseSigned(str, base);
67   if (value && *value >= std::numeric_limits<T>::lowest() &&
68       *value <= std::numeric_limits<T>::max()) {
69     return static_cast<T>(*value);
70   }
71   return absl::nullopt;
72 }
73 
74 template <typename T>
75 typename std::enable_if<std::is_integral<T>::value &&
76                             std::is_unsigned<T>::value,
77                         absl::optional<T>>::type
78 StringToNumber(absl::string_view str, int base = 10) {
79   using string_to_number_internal::unsigned_type;
80   static_assert(std::numeric_limits<T>::max() <=
81                     std::numeric_limits<unsigned_type>::max(),
82                 "StringToNumber only supports unsigned integers as large as "
83                 "unsigned long long int");
84   absl::optional<unsigned_type> value =
85       string_to_number_internal::ParseUnsigned(str, base);
86   if (value && *value <= std::numeric_limits<T>::max()) {
87     return static_cast<T>(*value);
88   }
89   return absl::nullopt;
90 }
91 
92 template <typename T>
93 typename std::enable_if<std::is_floating_point<T>::value,
94                         absl::optional<T>>::type
95 StringToNumber(absl::string_view str, int base = 10) {
96   static_assert(
97       std::numeric_limits<T>::max() <= std::numeric_limits<long double>::max(),
98       "StringToNumber only supports floating-point numbers as large "
99       "as long double");
100   return string_to_number_internal::ParseFloatingPoint<T>(str);
101 }
102 
103 }  // namespace rtc
104 
105 #endif  // RTC_BASE_STRING_TO_NUMBER_H_
106