blob: 1209eced44826daaeae31da38c139dd4334d0dab [file] [log] [blame]
ossua280f7c2017-04-06 09:02:151/*
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
Danil Chapovalov6e9d8952018-04-09 18:30:5111#include "rtc_base/string_to_number.h"
12
Yves Gerey988cc082018-10-23 10:03:0113#include <ctype.h>
Jonas Olssona4d87372019-07-05 17:08:3314
hugoh6baee782017-06-08 23:38:4015#include <cerrno>
ossua280f7c2017-04-06 09:02:1516#include <cstdlib>
17
Danil Chapovalov6e9d8952018-04-09 18:30:5118#include "rtc_base/checks.h"
ossua280f7c2017-04-06 09:02:1519
20namespace rtc {
21namespace string_to_number_internal {
22
Ali Tofigh3d6c6552022-03-25 13:28:2723absl::optional<signed_type> ParseSigned(absl::string_view str, int base) {
24 if (str.empty())
25 return absl::nullopt;
26
Niels Möllere66b83f2022-05-30 10:57:4127 if (isdigit(static_cast<unsigned char>(str[0])) || str[0] == '-') {
Ali Tofigh98bfd992022-07-22 20:10:4928 std::string str_str(str);
ossua280f7c2017-04-06 09:02:1529 char* end = nullptr;
30 errno = 0;
Ali Tofigh3d6c6552022-03-25 13:28:2731 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) {
Oskar Sundbom9c780582017-11-27 09:28:2235 return value;
ossua280f7c2017-04-06 09:02:1536 }
37 }
Danil Chapovalov0a1d1892018-06-21 09:48:2538 return absl::nullopt;
ossua280f7c2017-04-06 09:02:1539}
40
Ali Tofigh3d6c6552022-03-25 13:28:2741absl::optional<unsigned_type> ParseUnsigned(absl::string_view str, int base) {
42 if (str.empty())
43 return absl::nullopt;
44
Niels Möllere66b83f2022-05-30 10:57:4145 if (isdigit(static_cast<unsigned char>(str[0])) || str[0] == '-') {
Ali Tofigh98bfd992022-07-22 20:10:4946 std::string str_str(str);
ossua280f7c2017-04-06 09:02:1547 // 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;
Ali Tofigh3d6c6552022-03-25 13:28:2753 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)) {
Oskar Sundbom9c780582017-11-27 09:28:2258 return value;
ossua280f7c2017-04-06 09:02:1559 }
60 }
Danil Chapovalov0a1d1892018-06-21 09:48:2561 return absl::nullopt;
ossua280f7c2017-04-06 09:02:1562}
63
Jonas Olsson6b1985d2018-07-05 09:59:4864template <typename T>
65T StrToT(const char* str, char** str_end);
66
67template <>
68inline float StrToT(const char* str, char** str_end) {
69 return std::strtof(str, str_end);
70}
71
72template <>
73inline double StrToT(const char* str, char** str_end) {
74 return std::strtod(str, str_end);
75}
76
77template <>
78inline long double StrToT(const char* str, char** str_end) {
79 return std::strtold(str, str_end);
80}
81
82template <typename T>
Ali Tofigh3d6c6552022-03-25 13:28:2783absl::optional<T> ParseFloatingPoint(absl::string_view str) {
84 if (str.empty())
Jonas Olsson6b1985d2018-07-05 09:59:4885 return absl::nullopt;
Ali Tofigh3d6c6552022-03-25 13:28:2786
87 if (str[0] == '\0')
88 return absl::nullopt;
Ali Tofigh98bfd992022-07-22 20:10:4989 std::string str_str(str);
Jonas Olsson6b1985d2018-07-05 09:59:4890 char* end = nullptr;
91 errno = 0;
Ali Tofigh3d6c6552022-03-25 13:28:2792 const T value = StrToT<T>(str_str.c_str(), &end);
93 if (end == str_str.c_str() + str_str.size() && errno == 0) {
Jonas Olsson6b1985d2018-07-05 09:59:4894 return value;
95 }
96 return absl::nullopt;
97}
98
Ali Tofigh3d6c6552022-03-25 13:28:2799template absl::optional<float> ParseFloatingPoint(absl::string_view str);
100template absl::optional<double> ParseFloatingPoint(absl::string_view str);
101template absl::optional<long double> ParseFloatingPoint(absl::string_view str);
Jonas Olsson6b1985d2018-07-05 09:59:48102
ossua280f7c2017-04-06 09:02:15103} // namespace string_to_number_internal
104} // namespace rtc