OLD | NEW |
(Empty) | |
| 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 |
| 11 #include "webrtc/base/string_to_number.h" |
| 12 |
| 13 namespace rtc { |
| 14 namespace string_to_number_internal { |
| 15 |
| 16 rtc::Optional<long long int> ParseSigned(const char* str, int base) { |
| 17 RTC_DCHECK(str); |
| 18 if (isdigit(str[0]) || str[0] == '-') { |
| 19 char* end = nullptr; |
| 20 errno = 0; |
| 21 const long long int value = std::strtoll(str, &end, base); |
| 22 if (end && *end == '\0' && end != str && errno == 0) { |
| 23 return rtc::Optional<long long int>(value); |
| 24 } |
| 25 } |
| 26 return rtc::Optional<long long int>(); |
| 27 } |
| 28 |
| 29 rtc::Optional<unsigned long long int> ParseUnsigned(const char* str, int base) { |
| 30 RTC_DCHECK(str); |
| 31 if (isdigit(str[0]) || str[0] == '-') { |
| 32 // Explicitly discard negative values. std::strtoull parsing causes unsigned |
| 33 // wraparound. We cannot just reject values that start with -, though, since |
| 34 // -0 is perfectly fine, as is -0000000000000000000000000000000. |
| 35 const bool is_negative = str[0] == '-'; |
| 36 char* end = nullptr; |
| 37 errno = 0; |
| 38 const unsigned long long int value = std::strtoull(str, &end, base); |
| 39 if (end && *end == '\0' && end != str && errno == 0 && |
| 40 (value == 0 || !is_negative)) { |
| 41 return rtc::Optional<unsigned long long int>(value); |
| 42 } |
| 43 } |
| 44 return rtc::Optional<unsigned long long int>(); |
| 45 } |
| 46 |
| 47 } // namespace string_to_number_internal |
| 48 } // namespace rtc |
OLD | NEW |