ossu | a280f7c | 2017-04-06 02:02:15 -0700 | [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 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_STRING_TO_NUMBER_H_ |
| 12 | #define RTC_BASE_STRING_TO_NUMBER_H_ |
ossu | a280f7c | 2017-04-06 02:02:15 -0700 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #include <limits> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 15 | #include <string> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 16 | #include <type_traits> |
ossu | a280f7c | 2017-04-06 02:02:15 -0700 | [diff] [blame] | 17 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 18 | #include "absl/types/optional.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 19 | |
| 20 | namespace rtc { |
| 21 | |
| 22 | // This file declares a family of functions to parse integers from strings. |
| 23 | // The standard C library functions either fail to indicate errors (atoi, etc.) |
| 24 | // or are a hassle to work with (strtol, sscanf, etc.). The standard C++ library |
| 25 | // functions (std::stoi, etc.) indicate errors by throwing exceptions, which |
| 26 | // are disabled in WebRTC. |
| 27 | // |
| 28 | // Integers are parsed using one of the following functions: |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 29 | // absl::optional<int-type> StringToNumber(const char* str, int base = 10); |
| 30 | // absl::optional<int-type> StringToNumber(const std::string& str, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 31 | // int base = 10); |
| 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. |
| 41 | // |
| 42 | // If desired, this interface could be extended with support for floating-point |
| 43 | // types. |
| 44 | |
| 45 | namespace string_to_number_internal { |
| 46 | // These must be (unsigned) long long, to match the signature of strto(u)ll. |
| 47 | using unsigned_type = unsigned long long; // NOLINT(runtime/int) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 48 | using signed_type = long long; // NOLINT(runtime/int) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 49 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 50 | absl::optional<signed_type> ParseSigned(const char* str, int base); |
| 51 | absl::optional<unsigned_type> ParseUnsigned(const char* str, int base); |
Jonas Olsson | 6b1985d | 2018-07-05 11:59:48 +0200 | [diff] [blame] | 52 | |
| 53 | template <typename T> |
| 54 | absl::optional<T> ParseFloatingPoint(const char* str); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 55 | } // namespace string_to_number_internal |
| 56 | |
| 57 | template <typename T> |
| 58 | typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value, |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 59 | absl::optional<T>>::type |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 60 | StringToNumber(const char* str, int base = 10) { |
| 61 | using string_to_number_internal::signed_type; |
| 62 | static_assert( |
| 63 | std::numeric_limits<T>::max() <= |
| 64 | std::numeric_limits<signed_type>::max() && |
| 65 | std::numeric_limits<T>::lowest() >= |
| 66 | std::numeric_limits<signed_type>::lowest(), |
| 67 | "StringToNumber only supports signed integers as large as long long int"); |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 68 | absl::optional<signed_type> value = |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 69 | string_to_number_internal::ParseSigned(str, base); |
| 70 | if (value && *value >= std::numeric_limits<T>::lowest() && |
| 71 | *value <= std::numeric_limits<T>::max()) { |
Oskar Sundbom | 9c78058 | 2017-11-27 10:28:22 +0100 | [diff] [blame] | 72 | return static_cast<T>(*value); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 73 | } |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 74 | return absl::nullopt; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | template <typename T> |
| 78 | typename std::enable_if<std::is_integral<T>::value && |
| 79 | std::is_unsigned<T>::value, |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 80 | absl::optional<T>>::type |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 81 | StringToNumber(const char* str, int base = 10) { |
| 82 | using string_to_number_internal::unsigned_type; |
| 83 | static_assert(std::numeric_limits<T>::max() <= |
| 84 | std::numeric_limits<unsigned_type>::max(), |
| 85 | "StringToNumber only supports unsigned integers as large as " |
| 86 | "unsigned long long int"); |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 87 | absl::optional<unsigned_type> value = |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 88 | string_to_number_internal::ParseUnsigned(str, base); |
| 89 | if (value && *value <= std::numeric_limits<T>::max()) { |
Oskar Sundbom | 9c78058 | 2017-11-27 10:28:22 +0100 | [diff] [blame] | 90 | return static_cast<T>(*value); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 91 | } |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 92 | return absl::nullopt; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Jonas Olsson | 6b1985d | 2018-07-05 11:59:48 +0200 | [diff] [blame] | 95 | template <typename T> |
| 96 | typename std::enable_if<std::is_floating_point<T>::value, |
| 97 | absl::optional<T>>::type |
| 98 | StringToNumber(const char* str, int base = 10) { |
| 99 | static_assert( |
| 100 | std::numeric_limits<T>::max() <= std::numeric_limits<long double>::max(), |
| 101 | "StringToNumber only supports floating-point numbers as large " |
| 102 | "as long double"); |
| 103 | return string_to_number_internal::ParseFloatingPoint<T>(str); |
| 104 | } |
| 105 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 106 | // The std::string overloads only exists if there is a matching const char* |
| 107 | // version. |
| 108 | template <typename T> |
| 109 | auto StringToNumber(const std::string& str, int base = 10) |
| 110 | -> decltype(StringToNumber<T>(str.c_str(), base)) { |
| 111 | return StringToNumber<T>(str.c_str(), base); |
| 112 | } |
| 113 | |
| 114 | } // namespace rtc |
ossu | a280f7c | 2017-04-06 02:02:15 -0700 | [diff] [blame] | 115 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 116 | #endif // RTC_BASE_STRING_TO_NUMBER_H_ |