ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 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 | |
Danil Chapovalov | 6e9d895 | 2018-04-09 18:30:51 | [diff] [blame] | 11 | #include "rtc_base/string_to_number.h" |
| 12 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 13 | #include <ctype.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 14 | |
hugoh | 6baee78 | 2017-06-08 23:38:40 | [diff] [blame] | 15 | #include <cerrno> |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 16 | #include <cstdlib> |
| 17 | |
Danil Chapovalov | 6e9d895 | 2018-04-09 18:30:51 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 19 | |
| 20 | namespace rtc { |
| 21 | namespace string_to_number_internal { |
| 22 | |
Ali Tofigh | 3d6c655 | 2022-03-25 13:28:27 | [diff] [blame] | 23 | absl::optional<signed_type> ParseSigned(absl::string_view str, int base) { |
| 24 | if (str.empty()) |
| 25 | return absl::nullopt; |
| 26 | |
Niels Möller | e66b83f | 2022-05-30 10:57:41 | [diff] [blame] | 27 | if (isdigit(static_cast<unsigned char>(str[0])) || str[0] == '-') { |
Ali Tofigh | 98bfd99 | 2022-07-22 20:10:49 | [diff] [blame] | 28 | std::string str_str(str); |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 29 | char* end = nullptr; |
| 30 | errno = 0; |
Ali Tofigh | 3d6c655 | 2022-03-25 13:28:27 | [diff] [blame] | 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) { |
Oskar Sundbom | 9c78058 | 2017-11-27 09:28:22 | [diff] [blame] | 35 | return value; |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 36 | } |
| 37 | } |
Danil Chapovalov | 0a1d189 | 2018-06-21 09:48:25 | [diff] [blame] | 38 | return absl::nullopt; |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 39 | } |
| 40 | |
Ali Tofigh | 3d6c655 | 2022-03-25 13:28:27 | [diff] [blame] | 41 | absl::optional<unsigned_type> ParseUnsigned(absl::string_view str, int base) { |
| 42 | if (str.empty()) |
| 43 | return absl::nullopt; |
| 44 | |
Niels Möller | e66b83f | 2022-05-30 10:57:41 | [diff] [blame] | 45 | if (isdigit(static_cast<unsigned char>(str[0])) || str[0] == '-') { |
Ali Tofigh | 98bfd99 | 2022-07-22 20:10:49 | [diff] [blame] | 46 | std::string str_str(str); |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 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; |
Ali Tofigh | 3d6c655 | 2022-03-25 13:28:27 | [diff] [blame] | 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)) { |
Oskar Sundbom | 9c78058 | 2017-11-27 09:28:22 | [diff] [blame] | 58 | return value; |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 59 | } |
| 60 | } |
Danil Chapovalov | 0a1d189 | 2018-06-21 09:48:25 | [diff] [blame] | 61 | return absl::nullopt; |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 62 | } |
| 63 | |
Jonas Olsson | 6b1985d | 2018-07-05 09:59:48 | [diff] [blame] | 64 | template <typename T> |
| 65 | T StrToT(const char* str, char** str_end); |
| 66 | |
| 67 | template <> |
| 68 | inline float StrToT(const char* str, char** str_end) { |
| 69 | return std::strtof(str, str_end); |
| 70 | } |
| 71 | |
| 72 | template <> |
| 73 | inline double StrToT(const char* str, char** str_end) { |
| 74 | return std::strtod(str, str_end); |
| 75 | } |
| 76 | |
| 77 | template <> |
| 78 | inline long double StrToT(const char* str, char** str_end) { |
| 79 | return std::strtold(str, str_end); |
| 80 | } |
| 81 | |
| 82 | template <typename T> |
Ali Tofigh | 3d6c655 | 2022-03-25 13:28:27 | [diff] [blame] | 83 | absl::optional<T> ParseFloatingPoint(absl::string_view str) { |
| 84 | if (str.empty()) |
Jonas Olsson | 6b1985d | 2018-07-05 09:59:48 | [diff] [blame] | 85 | return absl::nullopt; |
Ali Tofigh | 3d6c655 | 2022-03-25 13:28:27 | [diff] [blame] | 86 | |
| 87 | if (str[0] == '\0') |
| 88 | return absl::nullopt; |
Ali Tofigh | 98bfd99 | 2022-07-22 20:10:49 | [diff] [blame] | 89 | std::string str_str(str); |
Jonas Olsson | 6b1985d | 2018-07-05 09:59:48 | [diff] [blame] | 90 | char* end = nullptr; |
| 91 | errno = 0; |
Ali Tofigh | 3d6c655 | 2022-03-25 13:28:27 | [diff] [blame] | 92 | const T value = StrToT<T>(str_str.c_str(), &end); |
| 93 | if (end == str_str.c_str() + str_str.size() && errno == 0) { |
Jonas Olsson | 6b1985d | 2018-07-05 09:59:48 | [diff] [blame] | 94 | return value; |
| 95 | } |
| 96 | return absl::nullopt; |
| 97 | } |
| 98 | |
Ali Tofigh | 3d6c655 | 2022-03-25 13:28:27 | [diff] [blame] | 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); |
Jonas Olsson | 6b1985d | 2018-07-05 09:59:48 | [diff] [blame] | 102 | |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 103 | } // namespace string_to_number_internal |
| 104 | } // namespace rtc |