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 <string> |
| 15 | #include <limits> |
ossu | a280f7c | 2017-04-06 02:02:15 -0700 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "api/optional.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 18 | |
| 19 | namespace rtc { |
| 20 | |
| 21 | // This file declares a family of functions to parse integers from strings. |
| 22 | // The standard C library functions either fail to indicate errors (atoi, etc.) |
| 23 | // or are a hassle to work with (strtol, sscanf, etc.). The standard C++ library |
| 24 | // functions (std::stoi, etc.) indicate errors by throwing exceptions, which |
| 25 | // are disabled in WebRTC. |
| 26 | // |
| 27 | // Integers are parsed using one of the following functions: |
| 28 | // rtc::Optional<int-type> StringToNumber(const char* str, int base = 10); |
| 29 | // rtc::Optional<int-type> StringToNumber(const std::string& str, |
| 30 | // int base = 10); |
| 31 | // |
| 32 | // These functions parse a value from the beginning of a string into one of the |
| 33 | // fundamental integer types, or returns an empty Optional if parsing |
| 34 | // failed. Values outside of the range supported by the type will be |
| 35 | // rejected. The strings must begin with a digit or a minus sign. No leading |
| 36 | // space nor trailing contents are allowed. |
| 37 | // By setting base to 0, one of octal, decimal or hexadecimal will be |
| 38 | // detected from the string's prefix (0, nothing or 0x, respectively). |
| 39 | // If non-zero, base can be set to a value between 2 and 36 inclusively. |
| 40 | // |
| 41 | // If desired, this interface could be extended with support for floating-point |
| 42 | // types. |
| 43 | |
| 44 | namespace string_to_number_internal { |
| 45 | // These must be (unsigned) long long, to match the signature of strto(u)ll. |
| 46 | using unsigned_type = unsigned long long; // NOLINT(runtime/int) |
| 47 | using signed_type = long long; // NOLINT(runtime/int) |
| 48 | |
| 49 | rtc::Optional<signed_type> ParseSigned(const char* str, int base); |
| 50 | rtc::Optional<unsigned_type> ParseUnsigned(const char* str, int base); |
| 51 | } // namespace string_to_number_internal |
| 52 | |
| 53 | template <typename T> |
| 54 | typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value, |
| 55 | rtc::Optional<T>>::type |
| 56 | StringToNumber(const char* str, int base = 10) { |
| 57 | using string_to_number_internal::signed_type; |
| 58 | static_assert( |
| 59 | std::numeric_limits<T>::max() <= |
| 60 | std::numeric_limits<signed_type>::max() && |
| 61 | std::numeric_limits<T>::lowest() >= |
| 62 | std::numeric_limits<signed_type>::lowest(), |
| 63 | "StringToNumber only supports signed integers as large as long long int"); |
| 64 | rtc::Optional<signed_type> value = |
| 65 | string_to_number_internal::ParseSigned(str, base); |
| 66 | if (value && *value >= std::numeric_limits<T>::lowest() && |
| 67 | *value <= std::numeric_limits<T>::max()) { |
Oskar Sundbom | 9c78058 | 2017-11-27 10:28:22 +0100 | [diff] [blame] | 68 | return static_cast<T>(*value); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 69 | } |
Oskar Sundbom | 9c78058 | 2017-11-27 10:28:22 +0100 | [diff] [blame] | 70 | return rtc::nullopt; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | template <typename T> |
| 74 | typename std::enable_if<std::is_integral<T>::value && |
| 75 | std::is_unsigned<T>::value, |
| 76 | rtc::Optional<T>>::type |
| 77 | StringToNumber(const char* str, int base = 10) { |
| 78 | using string_to_number_internal::unsigned_type; |
| 79 | static_assert(std::numeric_limits<T>::max() <= |
| 80 | std::numeric_limits<unsigned_type>::max(), |
| 81 | "StringToNumber only supports unsigned integers as large as " |
| 82 | "unsigned long long int"); |
| 83 | rtc::Optional<unsigned_type> value = |
| 84 | string_to_number_internal::ParseUnsigned(str, base); |
| 85 | if (value && *value <= std::numeric_limits<T>::max()) { |
Oskar Sundbom | 9c78058 | 2017-11-27 10:28:22 +0100 | [diff] [blame] | 86 | return static_cast<T>(*value); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 87 | } |
Oskar Sundbom | 9c78058 | 2017-11-27 10:28:22 +0100 | [diff] [blame] | 88 | return rtc::nullopt; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // The std::string overloads only exists if there is a matching const char* |
| 92 | // version. |
| 93 | template <typename T> |
| 94 | auto StringToNumber(const std::string& str, int base = 10) |
| 95 | -> decltype(StringToNumber<T>(str.c_str(), base)) { |
| 96 | return StringToNumber<T>(str.c_str(), base); |
| 97 | } |
| 98 | |
| 99 | } // namespace rtc |
ossu | a280f7c | 2017-04-06 02:02:15 -0700 | [diff] [blame] | 100 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 101 | #endif // RTC_BASE_STRING_TO_NUMBER_H_ |