xref: /aosp_15_r20/external/webrtc/rtc_base/string_to_number.cc (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 #include "rtc_base/string_to_number.h"
12 
13 #include <ctype.h>
14 
15 #include <cerrno>
16 #include <cstdlib>
17 
18 #include "rtc_base/checks.h"
19 
20 namespace rtc {
21 namespace string_to_number_internal {
22 
ParseSigned(absl::string_view str,int base)23 absl::optional<signed_type> ParseSigned(absl::string_view str, int base) {
24   if (str.empty())
25     return absl::nullopt;
26 
27   if (isdigit(static_cast<unsigned char>(str[0])) || str[0] == '-') {
28     std::string str_str(str);
29     char* end = nullptr;
30     errno = 0;
31     const signed_type value = std::strtoll(str_str.c_str(), &end, base);
32     // Check for errors and also make sure that there were no embedded nuls in
33     // the input string.
34     if (end == str_str.c_str() + str_str.size() && errno == 0) {
35       return value;
36     }
37   }
38   return absl::nullopt;
39 }
40 
ParseUnsigned(absl::string_view str,int base)41 absl::optional<unsigned_type> ParseUnsigned(absl::string_view str, int base) {
42   if (str.empty())
43     return absl::nullopt;
44 
45   if (isdigit(static_cast<unsigned char>(str[0])) || str[0] == '-') {
46     std::string str_str(str);
47     // Explicitly discard negative values. std::strtoull parsing causes unsigned
48     // wraparound. We cannot just reject values that start with -, though, since
49     // -0 is perfectly fine, as is -0000000000000000000000000000000.
50     const bool is_negative = str[0] == '-';
51     char* end = nullptr;
52     errno = 0;
53     const unsigned_type value = std::strtoull(str_str.c_str(), &end, base);
54     // Check for errors and also make sure that there were no embedded nuls in
55     // the input string.
56     if (end == str_str.c_str() + str_str.size() && errno == 0 &&
57         (value == 0 || !is_negative)) {
58       return value;
59     }
60   }
61   return absl::nullopt;
62 }
63 
64 template <typename T>
65 T StrToT(const char* str, char** str_end);
66 
67 template <>
StrToT(const char * str,char ** str_end)68 inline float StrToT(const char* str, char** str_end) {
69   return std::strtof(str, str_end);
70 }
71 
72 template <>
StrToT(const char * str,char ** str_end)73 inline double StrToT(const char* str, char** str_end) {
74   return std::strtod(str, str_end);
75 }
76 
77 template <>
StrToT(const char * str,char ** str_end)78 inline long double StrToT(const char* str, char** str_end) {
79   return std::strtold(str, str_end);
80 }
81 
82 template <typename T>
ParseFloatingPoint(absl::string_view str)83 absl::optional<T> ParseFloatingPoint(absl::string_view str) {
84   if (str.empty())
85     return absl::nullopt;
86 
87   if (str[0] == '\0')
88     return absl::nullopt;
89   std::string str_str(str);
90   char* end = nullptr;
91   errno = 0;
92   const T value = StrToT<T>(str_str.c_str(), &end);
93   if (end == str_str.c_str() + str_str.size() && errno == 0) {
94     return value;
95   }
96   return absl::nullopt;
97 }
98 
99 template absl::optional<float> ParseFloatingPoint(absl::string_view str);
100 template absl::optional<double> ParseFloatingPoint(absl::string_view str);
101 template absl::optional<long double> ParseFloatingPoint(absl::string_view str);
102 
103 }  // namespace string_to_number_internal
104 }  // namespace rtc
105