blob: 2cdc4b8440a03046e62d935dd8379be27bb4572d [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 <string>
15#include <limits>
ossua280f7c2017-04-06 02:02:15 -070016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/optional.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018
19namespace rtc {
20
21// This file declares a family of functions to parse integers from strings.
22// The standard C library functions either fail to indicate errors (atoi, etc.)
23// or are a hassle to work with (strtol, sscanf, etc.). The standard C++ library
24// functions (std::stoi, etc.) indicate errors by throwing exceptions, which
25// are disabled in WebRTC.
26//
27// Integers are parsed using one of the following functions:
28// rtc::Optional<int-type> StringToNumber(const char* str, int base = 10);
29// rtc::Optional<int-type> StringToNumber(const std::string& str,
30// int base = 10);
31//
32// These functions parse a value from the beginning of a string into one of the
33// fundamental integer types, or returns an empty Optional if parsing
34// failed. Values outside of the range supported by the type will be
35// rejected. The strings must begin with a digit or a minus sign. No leading
36// space nor trailing contents are allowed.
37// By setting base to 0, one of octal, decimal or hexadecimal will be
38// detected from the string's prefix (0, nothing or 0x, respectively).
39// If non-zero, base can be set to a value between 2 and 36 inclusively.
40//
41// If desired, this interface could be extended with support for floating-point
42// types.
43
44namespace string_to_number_internal {
45// These must be (unsigned) long long, to match the signature of strto(u)ll.
46using unsigned_type = unsigned long long; // NOLINT(runtime/int)
47using signed_type = long long; // NOLINT(runtime/int)
48
49rtc::Optional<signed_type> ParseSigned(const char* str, int base);
50rtc::Optional<unsigned_type> ParseUnsigned(const char* str, int base);
51} // namespace string_to_number_internal
52
53template <typename T>
54typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value,
55 rtc::Optional<T>>::type
56StringToNumber(const char* str, int base = 10) {
57 using string_to_number_internal::signed_type;
58 static_assert(
59 std::numeric_limits<T>::max() <=
60 std::numeric_limits<signed_type>::max() &&
61 std::numeric_limits<T>::lowest() >=
62 std::numeric_limits<signed_type>::lowest(),
63 "StringToNumber only supports signed integers as large as long long int");
64 rtc::Optional<signed_type> value =
65 string_to_number_internal::ParseSigned(str, base);
66 if (value && *value >= std::numeric_limits<T>::lowest() &&
67 *value <= std::numeric_limits<T>::max()) {
68 return rtc::Optional<T>(static_cast<T>(*value));
69 }
70 return rtc::Optional<T>();
71}
72
73template <typename T>
74typename std::enable_if<std::is_integral<T>::value &&
75 std::is_unsigned<T>::value,
76 rtc::Optional<T>>::type
77StringToNumber(const char* str, int base = 10) {
78 using string_to_number_internal::unsigned_type;
79 static_assert(std::numeric_limits<T>::max() <=
80 std::numeric_limits<unsigned_type>::max(),
81 "StringToNumber only supports unsigned integers as large as "
82 "unsigned long long int");
83 rtc::Optional<unsigned_type> value =
84 string_to_number_internal::ParseUnsigned(str, base);
85 if (value && *value <= std::numeric_limits<T>::max()) {
86 return rtc::Optional<T>(static_cast<T>(*value));
87 }
88 return rtc::Optional<T>();
89}
90
91// The std::string overloads only exists if there is a matching const char*
92// version.
93template <typename T>
94auto StringToNumber(const std::string& str, int base = 10)
95 -> decltype(StringToNumber<T>(str.c_str(), base)) {
96 return StringToNumber<T>(str.c_str(), base);
97}
98
99} // namespace rtc
ossua280f7c2017-04-06 02:02:15 -0700100
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200101#endif // RTC_BASE_STRING_TO_NUMBER_H_