blob: 1d704ee46499815c41901676210e723a81572e5b [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_STRING_TO_NUMBER_H_
12#define RTC_BASE_STRING_TO_NUMBER_H_
ossua280f7c2017-04-06 02:02:15 -070013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <limits>
Yves Gerey665174f2018-06-19 15:03:05 +020015#include <string>
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <type_traits>
ossua280f7c2017-04-06 02:02:15 -070017
Ali Tofigh3d6c6552022-03-25 14:28:27 +010018#include "absl/strings/string_view.h"
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020019#include "absl/types/optional.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020
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 14:28:27 +010029// Integers are parsed using:
30// absl::optional<int-type> StringToNumber(absl::string_view str,
31// int base = 10);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020032//
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 07:52:50 +020041
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 15:03:05 +020045using signed_type = long long; // NOLINT(runtime/int)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020046
Ali Tofigh3d6c6552022-03-25 14:28:27 +010047absl::optional<signed_type> ParseSigned(absl::string_view str, int base);
48absl::optional<unsigned_type> ParseUnsigned(absl::string_view str, int base);
Jonas Olsson6b1985d2018-07-05 11:59:48 +020049
50template <typename T>
Ali Tofigh3d6c6552022-03-25 14:28:27 +010051absl::optional<T> ParseFloatingPoint(absl::string_view str);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020052} // namespace string_to_number_internal
53
54template <typename T>
55typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value,
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020056 absl::optional<T>>::type
Ali Tofigh3d6c6552022-03-25 14:28:27 +010057StringToNumber(absl::string_view str, int base = 10) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020058 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");
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020065 absl::optional<signed_type> value =
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020066 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 10:28:22 +010069 return static_cast<T>(*value);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020070 }
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020071 return absl::nullopt;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020072}
73
74template <typename T>
75typename std::enable_if<std::is_integral<T>::value &&
76 std::is_unsigned<T>::value,
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020077 absl::optional<T>>::type
Ali Tofigh3d6c6552022-03-25 14:28:27 +010078StringToNumber(absl::string_view str, int base = 10) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020079 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");
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020084 absl::optional<unsigned_type> value =
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020085 string_to_number_internal::ParseUnsigned(str, base);
86 if (value && *value <= std::numeric_limits<T>::max()) {
Oskar Sundbom9c780582017-11-27 10:28:22 +010087 return static_cast<T>(*value);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020088 }
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020089 return absl::nullopt;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020090}
91
Jonas Olsson6b1985d2018-07-05 11:59:48 +020092template <typename T>
93typename std::enable_if<std::is_floating_point<T>::value,
94 absl::optional<T>>::type
Ali Tofigh3d6c6552022-03-25 14:28:27 +010095StringToNumber(absl::string_view str, int base = 10) {
Jonas Olsson6b1985d2018-07-05 11:59:48 +020096 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 07:52:50 +0200103} // namespace rtc
ossua280f7c2017-04-06 02:02:15 -0700104
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200105#endif // RTC_BASE_STRING_TO_NUMBER_H_