blob: 5e8aea5237fa79ef77a71f69cdee733ad17a7339 [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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#ifndef RTC_BASE_STRING_TO_NUMBER_H_
12#define RTC_BASE_STRING_TO_NUMBER_H_
ossua280f7c2017-04-06 09:02:1513
Henrik Kjellanderec78f1c2017-06-29 05:52:5014#include <limits>
Florent Castelli8037fc62024-08-29 13:00:4015#include <optional>
Yves Gerey665174f2018-06-19 13:03:0516#include <string>
Yves Gerey988cc082018-10-23 10:03:0117#include <type_traits>
ossua280f7c2017-04-06 09:02:1518
Ali Tofigh3d6c6552022-03-25 13:28:2719#include "absl/strings/string_view.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5020
21namespace rtc {
22
23// This file declares a family of functions to parse integers from strings.
24// The standard C library functions either fail to indicate errors (atoi, etc.)
25// or are a hassle to work with (strtol, sscanf, etc.). The standard C++ library
26// functions (std::stoi, etc.) indicate errors by throwing exceptions, which
27// are disabled in WebRTC.
28//
Ali Tofigh3d6c6552022-03-25 13:28:2729// Integers are parsed using:
Florent Castelli8037fc62024-08-29 13:00:4030// std::optional<int-type> StringToNumber(absl::string_view str,
Ali Tofigh3d6c6552022-03-25 13:28:2731// int base = 10);
Henrik Kjellanderec78f1c2017-06-29 05:52:5032//
33// These functions parse a value from the beginning of a string into one of the
34// fundamental integer types, or returns an empty Optional if parsing
35// failed. Values outside of the range supported by the type will be
36// rejected. The strings must begin with a digit or a minus sign. No leading
37// space nor trailing contents are allowed.
38// By setting base to 0, one of octal, decimal or hexadecimal will be
39// detected from the string's prefix (0, nothing or 0x, respectively).
40// If non-zero, base can be set to a value between 2 and 36 inclusively.
Henrik Kjellanderec78f1c2017-06-29 05:52:5041
42namespace string_to_number_internal {
43// These must be (unsigned) long long, to match the signature of strto(u)ll.
44using unsigned_type = unsigned long long; // NOLINT(runtime/int)
Yves Gerey665174f2018-06-19 13:03:0545using signed_type = long long; // NOLINT(runtime/int)
Henrik Kjellanderec78f1c2017-06-29 05:52:5046
Florent Castelli8037fc62024-08-29 13:00:4047std::optional<signed_type> ParseSigned(absl::string_view str, int base);
48std::optional<unsigned_type> ParseUnsigned(absl::string_view str, int base);
Jonas Olsson6b1985d2018-07-05 09:59:4849
50template <typename T>
Florent Castelli8037fc62024-08-29 13:00:4051std::optional<T> ParseFloatingPoint(absl::string_view str);
Henrik Kjellanderec78f1c2017-06-29 05:52:5052} // namespace string_to_number_internal
53
54template <typename T>
55typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value,
Florent Castelli8037fc62024-08-29 13:00:4056 std::optional<T>>::type
Ali Tofigh3d6c6552022-03-25 13:28:2757StringToNumber(absl::string_view str, int base = 10) {
Henrik Kjellanderec78f1c2017-06-29 05:52:5058 using string_to_number_internal::signed_type;
59 static_assert(
60 std::numeric_limits<T>::max() <=
61 std::numeric_limits<signed_type>::max() &&
62 std::numeric_limits<T>::lowest() >=
63 std::numeric_limits<signed_type>::lowest(),
64 "StringToNumber only supports signed integers as large as long long int");
Florent Castelli8037fc62024-08-29 13:00:4065 std::optional<signed_type> value =
Henrik Kjellanderec78f1c2017-06-29 05:52:5066 string_to_number_internal::ParseSigned(str, base);
67 if (value && *value >= std::numeric_limits<T>::lowest() &&
68 *value <= std::numeric_limits<T>::max()) {
Oskar Sundbom9c780582017-11-27 09:28:2269 return static_cast<T>(*value);
Henrik Kjellanderec78f1c2017-06-29 05:52:5070 }
Florent Castelli8037fc62024-08-29 13:00:4071 return std::nullopt;
Henrik Kjellanderec78f1c2017-06-29 05:52:5072}
73
74template <typename T>
75typename std::enable_if<std::is_integral<T>::value &&
76 std::is_unsigned<T>::value,
Florent Castelli8037fc62024-08-29 13:00:4077 std::optional<T>>::type
Ali Tofigh3d6c6552022-03-25 13:28:2778StringToNumber(absl::string_view str, int base = 10) {
Henrik Kjellanderec78f1c2017-06-29 05:52:5079 using string_to_number_internal::unsigned_type;
80 static_assert(std::numeric_limits<T>::max() <=
81 std::numeric_limits<unsigned_type>::max(),
82 "StringToNumber only supports unsigned integers as large as "
83 "unsigned long long int");
Florent Castelli8037fc62024-08-29 13:00:4084 std::optional<unsigned_type> value =
Henrik Kjellanderec78f1c2017-06-29 05:52:5085 string_to_number_internal::ParseUnsigned(str, base);
86 if (value && *value <= std::numeric_limits<T>::max()) {
Oskar Sundbom9c780582017-11-27 09:28:2287 return static_cast<T>(*value);
Henrik Kjellanderec78f1c2017-06-29 05:52:5088 }
Florent Castelli8037fc62024-08-29 13:00:4089 return std::nullopt;
Henrik Kjellanderec78f1c2017-06-29 05:52:5090}
91
Jonas Olsson6b1985d2018-07-05 09:59:4892template <typename T>
93typename std::enable_if<std::is_floating_point<T>::value,
Florent Castelli8037fc62024-08-29 13:00:4094 std::optional<T>>::type
Ali Tofigh3d6c6552022-03-25 13:28:2795StringToNumber(absl::string_view str, int base = 10) {
Jonas Olsson6b1985d2018-07-05 09:59:4896 static_assert(
97 std::numeric_limits<T>::max() <= std::numeric_limits<long double>::max(),
98 "StringToNumber only supports floating-point numbers as large "
99 "as long double");
100 return string_to_number_internal::ParseFloatingPoint<T>(str);
101}
102
Henrik Kjellanderec78f1c2017-06-29 05:52:50103} // namespace rtc
ossua280f7c2017-04-06 09:02:15104
Mirko Bonadei92ea95e2017-09-15 04:47:31105#endif // RTC_BASE_STRING_TO_NUMBER_H_