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