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 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 09:48:25 | [diff] [blame] | 23 | absl::optional<signed_type> ParseSigned(const char* str, int base) { |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 24 | RTC_DCHECK(str); |
| 25 | if (isdigit(str[0]) || str[0] == '-') { |
| 26 | char* end = nullptr; |
| 27 | errno = 0; |
| 28 | const signed_type value = std::strtoll(str, &end, base); |
| 29 | if (end && *end == '\0' && errno == 0) { |
Oskar Sundbom | 9c78058 | 2017-11-27 09:28:22 | [diff] [blame] | 30 | return value; |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 31 | } |
| 32 | } |
Danil Chapovalov | 0a1d189 | 2018-06-21 09:48:25 | [diff] [blame] | 33 | return absl::nullopt; |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 34 | } |
| 35 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 09:48:25 | [diff] [blame] | 36 | absl::optional<unsigned_type> ParseUnsigned(const char* str, int base) { |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 37 | RTC_DCHECK(str); |
| 38 | if (isdigit(str[0]) || str[0] == '-') { |
| 39 | // Explicitly discard negative values. std::strtoull parsing causes unsigned |
| 40 | // wraparound. We cannot just reject values that start with -, though, since |
| 41 | // -0 is perfectly fine, as is -0000000000000000000000000000000. |
| 42 | const bool is_negative = str[0] == '-'; |
| 43 | char* end = nullptr; |
| 44 | errno = 0; |
| 45 | const unsigned_type value = std::strtoull(str, &end, base); |
| 46 | if (end && *end == '\0' && errno == 0 && (value == 0 || !is_negative)) { |
Oskar Sundbom | 9c78058 | 2017-11-27 09:28:22 | [diff] [blame] | 47 | return value; |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 48 | } |
| 49 | } |
Danil Chapovalov | 0a1d189 | 2018-06-21 09:48:25 | [diff] [blame] | 50 | return absl::nullopt; |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 51 | } |
| 52 | |
Jonas Olsson | 6b1985d | 2018-07-05 09:59:48 | [diff] [blame] | 53 | template <typename T> |
| 54 | T StrToT(const char* str, char** str_end); |
| 55 | |
| 56 | template <> |
| 57 | inline float StrToT(const char* str, char** str_end) { |
| 58 | return std::strtof(str, str_end); |
| 59 | } |
| 60 | |
| 61 | template <> |
| 62 | inline double StrToT(const char* str, char** str_end) { |
| 63 | return std::strtod(str, str_end); |
| 64 | } |
| 65 | |
| 66 | template <> |
| 67 | inline long double StrToT(const char* str, char** str_end) { |
| 68 | return std::strtold(str, str_end); |
| 69 | } |
| 70 | |
| 71 | template <typename T> |
| 72 | absl::optional<T> ParseFloatingPoint(const char* str) { |
| 73 | RTC_DCHECK(str); |
| 74 | if (*str == '\0') |
| 75 | return absl::nullopt; |
| 76 | char* end = nullptr; |
| 77 | errno = 0; |
| 78 | const T value = StrToT<T>(str, &end); |
| 79 | if (end && *end == '\0' && errno == 0) { |
| 80 | return value; |
| 81 | } |
| 82 | return absl::nullopt; |
| 83 | } |
| 84 | |
| 85 | template absl::optional<float> ParseFloatingPoint(const char* str); |
| 86 | template absl::optional<double> ParseFloatingPoint(const char* str); |
| 87 | template absl::optional<long double> ParseFloatingPoint(const char* str); |
| 88 | |
ossu | a280f7c | 2017-04-06 09:02:15 | [diff] [blame] | 89 | } // namespace string_to_number_internal |
| 90 | } // namespace rtc |