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 | |
Danil Chapovalov | 6e9d895 | 2018-04-09 20:30:51 +0200 | [diff] [blame] | 11 | #include "rtc_base/string_to_number.h" |
| 12 | |
hugoh | 6baee78 | 2017-06-08 16:38:40 -0700 | [diff] [blame] | 13 | #include <cerrno> |
ossu | a280f7c | 2017-04-06 02:02:15 -0700 | [diff] [blame] | 14 | #include <cstdlib> |
| 15 | |
Danil Chapovalov | 6e9d895 | 2018-04-09 20:30:51 +0200 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
ossu | a280f7c | 2017-04-06 02:02:15 -0700 | [diff] [blame] | 17 | |
| 18 | namespace rtc { |
| 19 | namespace string_to_number_internal { |
| 20 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 21 | absl::optional<signed_type> ParseSigned(const char* str, int base) { |
ossu | a280f7c | 2017-04-06 02:02:15 -0700 | [diff] [blame] | 22 | RTC_DCHECK(str); |
| 23 | if (isdigit(str[0]) || str[0] == '-') { |
| 24 | char* end = nullptr; |
| 25 | errno = 0; |
| 26 | const signed_type value = std::strtoll(str, &end, base); |
| 27 | if (end && *end == '\0' && errno == 0) { |
Oskar Sundbom | 9c78058 | 2017-11-27 10:28:22 +0100 | [diff] [blame] | 28 | return value; |
ossu | a280f7c | 2017-04-06 02:02:15 -0700 | [diff] [blame] | 29 | } |
| 30 | } |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 31 | return absl::nullopt; |
ossu | a280f7c | 2017-04-06 02:02:15 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 34 | absl::optional<unsigned_type> ParseUnsigned(const char* str, int base) { |
ossu | a280f7c | 2017-04-06 02:02:15 -0700 | [diff] [blame] | 35 | RTC_DCHECK(str); |
| 36 | if (isdigit(str[0]) || str[0] == '-') { |
| 37 | // Explicitly discard negative values. std::strtoull parsing causes unsigned |
| 38 | // wraparound. We cannot just reject values that start with -, though, since |
| 39 | // -0 is perfectly fine, as is -0000000000000000000000000000000. |
| 40 | const bool is_negative = str[0] == '-'; |
| 41 | char* end = nullptr; |
| 42 | errno = 0; |
| 43 | const unsigned_type value = std::strtoull(str, &end, base); |
| 44 | if (end && *end == '\0' && errno == 0 && (value == 0 || !is_negative)) { |
Oskar Sundbom | 9c78058 | 2017-11-27 10:28:22 +0100 | [diff] [blame] | 45 | return value; |
ossu | a280f7c | 2017-04-06 02:02:15 -0700 | [diff] [blame] | 46 | } |
| 47 | } |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 48 | return absl::nullopt; |
ossu | a280f7c | 2017-04-06 02:02:15 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | } // namespace string_to_number_internal |
| 52 | } // namespace rtc |