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

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

Issue 2696003004: Added integer parsing functions in base/string_to_number.h (Closed)
Patch Set: Removed float support, moved template instances into .cc, made parsing stricter. Created 3 years, 10 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
OLDNEW
(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. By setting base to 0, one of octal, decimal or hexadecimal will be
36 // detected from the string's prefix (0, nothing or 0x, respectively).
37 // If non-zero, base can be set to a value between 2 and 36 inclusively.
38 //
39 // If desired, this interface could be extended with support for floating-point
40 // types.
41
42 template <typename T>
43 typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value,
44 rtc::Optional<T>>::type
45 StringToNumber(const char* str, int base = 10);
46
47 template <typename T>
48 typename std::enable_if<std::is_integral<T>::value &&
49 std::is_unsigned<T>::value,
50 rtc::Optional<T>>::type
51 StringToNumber(const char* str, int base = 10);
52
53 // The std::string overloads only exists if there is a matching const char*
54 // version.
55 template <typename T>
56 auto StringToNumber(const std::string& str, int base = 10)
57 -> decltype(StringToNumber<T>(str.c_str(), base)) {
58 return StringToNumber<T>(str.c_str(), base);
59 }
60
61 } // namespace rtc
62
63 #endif // WEBRTC_BASE_STRING_TO_NUMBER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698