| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#ifndef RTC_BASE_STRING_TO_NUMBER_H_ |
| 12 |
#define RTC_BASE_STRING_TO_NUMBER_H_ |
| 13 |
|
| 14 |
#include <limits> |
| 15 |
#include <string> |
| 16 |
#include <type_traits> |
| 17 |
|
| 18 |
#include "absl/types/optional.h" |
| 19 |
|
| 20 |
namespace rtc { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
namespace string_to_number_internal { |
| 46 |
|
| 47 |
using unsigned_type = unsigned long long; |
| 48 |
using signed_type = long long; |
| 49 |
|
| 50 |
absl::optional<signed_type> ParseSigned(const char* str, int base); |
| 51 |
absl::optional<unsigned_type> ParseUnsigned(const char* str, int base); |
| 52 |
|
| 53 |
template <typename T> |
| 54 |
absl::optional<T> ParseFloatingPoint(const char* str); |
| 55 |
} |
| 56 |
|
| 57 |
template <typename T> |
| 58 |
typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value, |
| 59 |
absl::optional<T>>::type |
| 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"); |
| 68 |
absl::optional<signed_type> value = |
| 69 |
string_to_number_internal::ParseSigned(str, base); |
| 70 |
if (value && *value >= std::numeric_limits<T>::lowest() && |
| 71 |
*value <= std::numeric_limits<T>::max()) { |
| 72 |
return static_cast<T>(*value); |
| 73 |
} |
| 74 |
return absl::nullopt; |
| 75 |
} |
| 76 |
|
| 77 |
template <typename T> |
| 78 |
typename std::enable_if<std::is_integral<T>::value && |
| 79 |
std::is_unsigned<T>::value, |
| 80 |
absl::optional<T>>::type |
| 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"); |
| 87 |
absl::optional<unsigned_type> value = |
| 88 |
string_to_number_internal::ParseUnsigned(str, base); |
| 89 |
if (value && *value <= std::numeric_limits<T>::max()) { |
| 90 |
return static_cast<T>(*value); |
| 91 |
} |
| 92 |
return absl::nullopt; |
| 93 |
} |
| 94 |
|
| 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 |
|
| 106 |
|
| 107 |
|
| 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 |
} |
| 115 |
|
| 116 |
#endif |