blob: 634652b83f6be2bea367fb9128c1da4dffd91304 [file] [log] [blame]
ossua280f7c2017-04-06 02:02:15 -07001/*
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 Chapovalov6e9d8952018-04-09 20:30:51 +020011#include "rtc_base/string_to_number.h"
12
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <ctype.h>
hugoh6baee782017-06-08 16:38:40 -070014#include <cerrno>
ossua280f7c2017-04-06 02:02:15 -070015#include <cstdlib>
16
Danil Chapovalov6e9d8952018-04-09 20:30:51 +020017#include "rtc_base/checks.h"
ossua280f7c2017-04-06 02:02:15 -070018
19namespace rtc {
20namespace string_to_number_internal {
21
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020022absl::optional<signed_type> ParseSigned(const char* str, int base) {
ossua280f7c2017-04-06 02:02:15 -070023 RTC_DCHECK(str);
24 if (isdigit(str[0]) || str[0] == '-') {
25 char* end = nullptr;
26 errno = 0;
27 const signed_type value = std::strtoll(str, &end, base);
28 if (end && *end == '\0' && errno == 0) {
Oskar Sundbom9c780582017-11-27 10:28:22 +010029 return value;
ossua280f7c2017-04-06 02:02:15 -070030 }
31 }
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020032 return absl::nullopt;
ossua280f7c2017-04-06 02:02:15 -070033}
34
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020035absl::optional<unsigned_type> ParseUnsigned(const char* str, int base) {
ossua280f7c2017-04-06 02:02:15 -070036 RTC_DCHECK(str);
37 if (isdigit(str[0]) || str[0] == '-') {
38 // Explicitly discard negative values. std::strtoull parsing causes unsigned
39 // wraparound. We cannot just reject values that start with -, though, since
40 // -0 is perfectly fine, as is -0000000000000000000000000000000.
41 const bool is_negative = str[0] == '-';
42 char* end = nullptr;
43 errno = 0;
44 const unsigned_type value = std::strtoull(str, &end, base);
45 if (end && *end == '\0' && errno == 0 && (value == 0 || !is_negative)) {
Oskar Sundbom9c780582017-11-27 10:28:22 +010046 return value;
ossua280f7c2017-04-06 02:02:15 -070047 }
48 }
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020049 return absl::nullopt;
ossua280f7c2017-04-06 02:02:15 -070050}
51
Jonas Olsson6b1985d2018-07-05 11:59:48 +020052template <typename T>
53T StrToT(const char* str, char** str_end);
54
55template <>
56inline float StrToT(const char* str, char** str_end) {
57 return std::strtof(str, str_end);
58}
59
60template <>
61inline double StrToT(const char* str, char** str_end) {
62 return std::strtod(str, str_end);
63}
64
65template <>
66inline long double StrToT(const char* str, char** str_end) {
67 return std::strtold(str, str_end);
68}
69
70template <typename T>
71absl::optional<T> ParseFloatingPoint(const char* str) {
72 RTC_DCHECK(str);
73 if (*str == '\0')
74 return absl::nullopt;
75 char* end = nullptr;
76 errno = 0;
77 const T value = StrToT<T>(str, &end);
78 if (end && *end == '\0' && errno == 0) {
79 return value;
80 }
81 return absl::nullopt;
82}
83
84template absl::optional<float> ParseFloatingPoint(const char* str);
85template absl::optional<double> ParseFloatingPoint(const char* str);
86template absl::optional<long double> ParseFloatingPoint(const char* str);
87
ossua280f7c2017-04-06 02:02:15 -070088} // namespace string_to_number_internal
89} // namespace rtc