blob: 4cb521595defadd2f10bfd6dab4a38d1553617b3 [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
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020018#include "absl/types/optional.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019
20namespace rtc {
21
22// This file declares a family of functions to parse integers from strings.
23// The standard C library functions either fail to indicate errors (atoi, etc.)
24// or are a hassle to work with (strtol, sscanf, etc.). The standard C++ library
25// functions (std::stoi, etc.) indicate errors by throwing exceptions, which
26// are disabled in WebRTC.
27//
28// Integers are parsed using one of the following functions:
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020029// absl::optional<int-type> StringToNumber(const char* str, int base = 10);
30// absl::optional<int-type> StringToNumber(const std::string& str,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031// int base = 10);
32//
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.
41//
42// If desired, this interface could be extended with support for floating-point
43// types.
44
45namespace string_to_number_internal {
46// These must be (unsigned) long long, to match the signature of strto(u)ll.
47using unsigned_type = unsigned long long; // NOLINT(runtime/int)
Yves Gerey665174f2018-06-19 15:03:05 +020048using signed_type = long long; // NOLINT(runtime/int)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020050absl::optional<signed_type> ParseSigned(const char* str, int base);
51absl::optional<unsigned_type> ParseUnsigned(const char* str, int base);
Jonas Olsson6b1985d2018-07-05 11:59:48 +020052
53template <typename T>
54absl::optional<T> ParseFloatingPoint(const char* str);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055} // namespace string_to_number_internal
56
57template <typename T>
58typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value,
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020059 absl::optional<T>>::type
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060StringToNumber(const char* str, int base = 10) {
61 using string_to_number_internal::signed_type;
62 static_assert(
63 std::numeric_limits<T>::max() <=
64 std::numeric_limits<signed_type>::max() &&
65 std::numeric_limits<T>::lowest() >=
66 std::numeric_limits<signed_type>::lowest(),
67 "StringToNumber only supports signed integers as large as long long int");
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020068 absl::optional<signed_type> value =
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020069 string_to_number_internal::ParseSigned(str, base);
70 if (value && *value >= std::numeric_limits<T>::lowest() &&
71 *value <= std::numeric_limits<T>::max()) {
Oskar Sundbom9c780582017-11-27 10:28:22 +010072 return static_cast<T>(*value);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020073 }
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020074 return absl::nullopt;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020075}
76
77template <typename T>
78typename std::enable_if<std::is_integral<T>::value &&
79 std::is_unsigned<T>::value,
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020080 absl::optional<T>>::type
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081StringToNumber(const char* str, int base = 10) {
82 using string_to_number_internal::unsigned_type;
83 static_assert(std::numeric_limits<T>::max() <=
84 std::numeric_limits<unsigned_type>::max(),
85 "StringToNumber only supports unsigned integers as large as "
86 "unsigned long long int");
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020087 absl::optional<unsigned_type> value =
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020088 string_to_number_internal::ParseUnsigned(str, base);
89 if (value && *value <= std::numeric_limits<T>::max()) {
Oskar Sundbom9c780582017-11-27 10:28:22 +010090 return static_cast<T>(*value);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020091 }
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020092 return absl::nullopt;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020093}
94
Jonas Olsson6b1985d2018-07-05 11:59:48 +020095template <typename T>
96typename std::enable_if<std::is_floating_point<T>::value,
97 absl::optional<T>>::type
98StringToNumber(const char* str, int base = 10) {
99 static_assert(
100 std::numeric_limits<T>::max() <= std::numeric_limits<long double>::max(),
101 "StringToNumber only supports floating-point numbers as large "
102 "as long double");
103 return string_to_number_internal::ParseFloatingPoint<T>(str);
104}
105
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200106// The std::string overloads only exists if there is a matching const char*
107// version.
108template <typename T>
109auto StringToNumber(const std::string& str, int base = 10)
110 -> decltype(StringToNumber<T>(str.c_str(), base)) {
111 return StringToNumber<T>(str.c_str(), base);
112}
113
114} // namespace rtc
ossua280f7c2017-04-06 02:02:15 -0700115
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200116#endif // RTC_BASE_STRING_TO_NUMBER_H_