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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef RTC_BASE_STRING_TO_NUMBER_H_ |
| 12 | #define RTC_BASE_STRING_TO_NUMBER_H_ |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 14 | #include <limits> |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 15 | #include <optional> |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 16 | #include <string> |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 17 | #include <type_traits> |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 18 | |
Ali Tofigh | 3d6c655 | 2022-03-25 13:28:27 | [diff] [blame] | 19 | #include "absl/strings/string_view.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 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 | // |
Ali Tofigh | 3d6c655 | 2022-03-25 13:28:27 | [diff] [blame] | 29 | // Integers are parsed using: |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 30 | // std::optional<int-type> StringToNumber(absl::string_view str, |
Ali Tofigh | 3d6c655 | 2022-03-25 13:28:27 | [diff] [blame] | 31 | // int base = 10); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 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. |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 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) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 45 | using signed_type = long long; // NOLINT(runtime/int) |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 46 | |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 47 | std::optional<signed_type> ParseSigned(absl::string_view str, int base); |
| 48 | std::optional<unsigned_type> ParseUnsigned(absl::string_view str, int base); |
Jonas Olsson | 6b1985d | 2018-07-05 09:59:48 | [diff] [blame] | 49 | |
| 50 | template <typename T> |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 51 | std::optional<T> ParseFloatingPoint(absl::string_view str); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 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, |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 56 | std::optional<T>>::type |
Ali Tofigh | 3d6c655 | 2022-03-25 13:28:27 | [diff] [blame] | 57 | StringToNumber(absl::string_view str, int base = 10) { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 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"); |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 65 | std::optional<signed_type> value = |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 66 | string_to_number_internal::ParseSigned(str, base); |
| 67 | if (value && *value >= std::numeric_limits<T>::lowest() && |
| 68 | *value <= std::numeric_limits<T>::max()) { |
Oskar Sundbom | 9c78058 | 2017-11-27 09:28:22 | [diff] [blame] | 69 | return static_cast<T>(*value); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 70 | } |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 71 | return std::nullopt; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | template <typename T> |
| 75 | typename std::enable_if<std::is_integral<T>::value && |
| 76 | std::is_unsigned<T>::value, |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 77 | std::optional<T>>::type |
Ali Tofigh | 3d6c655 | 2022-03-25 13:28:27 | [diff] [blame] | 78 | StringToNumber(absl::string_view str, int base = 10) { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 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"); |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 84 | std::optional<unsigned_type> value = |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 85 | string_to_number_internal::ParseUnsigned(str, base); |
| 86 | if (value && *value <= std::numeric_limits<T>::max()) { |
Oskar Sundbom | 9c78058 | 2017-11-27 09:28:22 | [diff] [blame] | 87 | return static_cast<T>(*value); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 88 | } |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 89 | return std::nullopt; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 90 | } |
| 91 | |
Jonas Olsson | 6b1985d | 2018-07-05 09:59:48 | [diff] [blame] | 92 | template <typename T> |
| 93 | typename std::enable_if<std::is_floating_point<T>::value, |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 94 | std::optional<T>>::type |
Ali Tofigh | 3d6c655 | 2022-03-25 13:28:27 | [diff] [blame] | 95 | StringToNumber(absl::string_view str, int base = 10) { |
Jonas Olsson | 6b1985d | 2018-07-05 09:59:48 | [diff] [blame] | 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 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 103 | } // namespace rtc |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 104 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 105 | #endif // RTC_BASE_STRING_TO_NUMBER_H_ |