OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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 | |
11 #ifndef WEBRTC_BASE_STRING_TO_NUMBER_H_ | |
12 #define WEBRTC_BASE_STRING_TO_NUMBER_H_ | |
13 | |
14 #include <string> | |
15 #include <limits> | |
16 | |
17 #include "webrtc/base/optional.h" | |
18 | |
19 namespace 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 | |
44 namespace string_to_number_internal { | |
kwiberg-webrtc
2017/02/20 13:27:20
Bonus points for the very specific internal namesp
ossu
2017/02/20 14:09:06
Thanks! :)
| |
45 rtc::Optional<long long int> ParseSigned(const char* str, int base); | |
kwiberg-webrtc
2017/02/20 13:27:20
Just "long long" means the same thing as "long lon
ossu
2017/02/20 14:09:06
Done.
| |
46 rtc::Optional<unsigned long long int> ParseUnsigned(const char* str, int base); | |
47 } // namespace string_to_number_internal | |
48 | |
49 template <typename T> | |
50 typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value, | |
51 rtc::Optional<T>>::type | |
52 StringToNumber(const char* str, int base = 10) { | |
53 static_assert(std::numeric_limits<T>::max() <= | |
54 std::numeric_limits<long long int>::max() && | |
55 std::numeric_limits<T>::lowest() >= | |
56 std::numeric_limits<long long int>::lowest(), | |
57 "StringToNumber only supports signed integers as large as " | |
58 "long long int"); | |
kwiberg-webrtc
2017/02/20 13:27:20
Multi-line string literals are a bit easier to rea
ossu
2017/02/20 14:09:06
I'm not sure I agree and clang-format seems to spl
kwiberg-webrtc
2017/02/20 16:51:26
Acknowledged. Not everyone can be right all the ti
| |
59 rtc::Optional<long long int> value = | |
60 string_to_number_internal::ParseSigned(str, base); | |
61 if (value && *value >= std::numeric_limits<T>::min() && | |
62 *value <= std::numeric_limits<T>::max()) { | |
kwiberg-webrtc
2017/02/20 13:27:20
If you pass the min and max value to the internal
ossu
2017/02/20 14:09:06
I think this is clearer - we'll need to do a cast
kwiberg-webrtc
2017/02/20 16:51:26
Acknowledged.
| |
63 return rtc::Optional<T>(static_cast<T>(*value)); | |
kwiberg-webrtc
2017/02/20 13:27:20
This ought to be dchecked_cast, but I won't reques
ossu
2017/02/20 14:09:06
I didn't find that one. Found a checked_cast, thou
kwiberg-webrtc
2017/02/20 16:51:26
Yes, we only have checked_cast. dchecked_cast does
| |
64 } | |
65 return rtc::Optional<T>(); | |
66 } | |
67 | |
68 template <typename T> | |
69 typename std::enable_if<std::is_integral<T>::value && | |
70 std::is_unsigned<T>::value, | |
71 rtc::Optional<T>>::type | |
72 StringToNumber(const char* str, int base = 10) { | |
73 static_assert( | |
74 std::numeric_limits<T>::max() <= | |
75 std::numeric_limits<unsigned long long int>::max(), | |
76 "StringToNumber only supports unsigned integers as large as unsigned " | |
77 "long long int"); | |
78 rtc::Optional<unsigned long long int> value = | |
79 string_to_number_internal::ParseUnsigned(str, base); | |
80 if (value && *value <= std::numeric_limits<T>::max()) { | |
81 return rtc::Optional<T>(static_cast<T>(*value)); | |
82 } | |
83 return rtc::Optional<T>(); | |
84 } | |
85 | |
86 // The std::string overloads only exists if there is a matching const char* | |
87 // version. | |
88 template <typename T> | |
89 auto StringToNumber(const std::string& str, int base = 10) | |
90 -> decltype(StringToNumber<T>(str.c_str(), base)) { | |
91 return StringToNumber<T>(str.c_str(), base); | |
92 } | |
93 | |
94 } // namespace rtc | |
95 | |
96 #endif // WEBRTC_BASE_STRING_TO_NUMBER_H_ | |
OLD | NEW |