blob: 351610f31a66cb4c3c0e0faa2944de7a5d492e5e [file] [log] [blame]
ossua280f7c2017-04-06 09:02:151/*
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 18:30:5111#include "rtc_base/string_to_number.h"
12
Yves Gerey988cc082018-10-23 10:03:0113#include <ctype.h>
Jonas Olssona4d87372019-07-05 17:08:3314
hugoh6baee782017-06-08 23:38:4015#include <cerrno>
ossua280f7c2017-04-06 09:02:1516#include <cstdlib>
17
Danil Chapovalov6e9d8952018-04-09 18:30:5118#include "rtc_base/checks.h"
ossua280f7c2017-04-06 09:02:1519
20namespace rtc {
21namespace string_to_number_internal {
22
Danil Chapovalov0a1d1892018-06-21 09:48:2523absl::optional<signed_type> ParseSigned(const char* str, int base) {
ossua280f7c2017-04-06 09:02:1524 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 Sundbom9c780582017-11-27 09:28:2230 return value;
ossua280f7c2017-04-06 09:02:1531 }
32 }
Danil Chapovalov0a1d1892018-06-21 09:48:2533 return absl::nullopt;
ossua280f7c2017-04-06 09:02:1534}
35
Danil Chapovalov0a1d1892018-06-21 09:48:2536absl::optional<unsigned_type> ParseUnsigned(const char* str, int base) {
ossua280f7c2017-04-06 09:02:1537 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 Sundbom9c780582017-11-27 09:28:2247 return value;
ossua280f7c2017-04-06 09:02:1548 }
49 }
Danil Chapovalov0a1d1892018-06-21 09:48:2550 return absl::nullopt;
ossua280f7c2017-04-06 09:02:1551}
52
Jonas Olsson6b1985d2018-07-05 09:59:4853template <typename T>
54T StrToT(const char* str, char** str_end);
55
56template <>
57inline float StrToT(const char* str, char** str_end) {
58 return std::strtof(str, str_end);
59}
60
61template <>
62inline double StrToT(const char* str, char** str_end) {
63 return std::strtod(str, str_end);
64}
65
66template <>
67inline long double StrToT(const char* str, char** str_end) {
68 return std::strtold(str, str_end);
69}
70
71template <typename T>
72absl::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
85template absl::optional<float> ParseFloatingPoint(const char* str);
86template absl::optional<double> ParseFloatingPoint(const char* str);
87template absl::optional<long double> ParseFloatingPoint(const char* str);
88
ossua280f7c2017-04-06 09:02:1589} // namespace string_to_number_internal
90} // namespace rtc