Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(420)

Side by Side Diff: webrtc/base/string_to_number.h

Issue 2877023002: Move webrtc/{base => rtc_base} (Closed)
Patch Set: update presubmit.py and DEPS include rules Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/stream_unittest.cc ('k') | webrtc/base/string_to_number.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2017 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2017 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #ifndef WEBRTC_BASE_STRING_TO_NUMBER_H_ 11 #ifndef WEBRTC_BASE_STRING_TO_NUMBER_H_
12 #define WEBRTC_BASE_STRING_TO_NUMBER_H_ 12 #define WEBRTC_BASE_STRING_TO_NUMBER_H_
13 13
14 #include <string>
15 #include <limits>
16 14
17 #include "webrtc/base/optional.h" 15 // This header is deprecated and is just left here temporarily during
18 16 // refactoring. See https://bugs.webrtc.org/7634 for more details.
19 namespace rtc { 17 #include "webrtc/rtc_base/string_to_number.h"
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 {
45 // These must be (unsigned) long long, to match the signature of strto(u)ll.
46 using unsigned_type = unsigned long long; // NOLINT(runtime/int)
47 using signed_type = long long; // NOLINT(runtime/int)
48
49 rtc::Optional<signed_type> ParseSigned(const char* str, int base);
50 rtc::Optional<unsigned_type> ParseUnsigned(const char* str, int base);
51 } // namespace string_to_number_internal
52
53 template <typename T>
54 typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value,
55 rtc::Optional<T>>::type
56 StringToNumber(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
73 template <typename T>
74 typename std::enable_if<std::is_integral<T>::value &&
75 std::is_unsigned<T>::value,
76 rtc::Optional<T>>::type
77 StringToNumber(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.
93 template <typename T>
94 auto 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
100 18
101 #endif // WEBRTC_BASE_STRING_TO_NUMBER_H_ 19 #endif // WEBRTC_BASE_STRING_TO_NUMBER_H_
OLDNEW
« no previous file with comments | « webrtc/base/stream_unittest.cc ('k') | webrtc/base/string_to_number.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698