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

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

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 #include "webrtc/base/string_to_number.h"
12
13 namespace rtc {
14
15 template <typename T>
16 typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value,
17 rtc::Optional<T>>::type
18 StringToNumber(const char* str, int base) {
19 static_assert(
20 sizeof(long long int) >= sizeof(T),
kwiberg-webrtc 2017/02/18 12:28:53 Compare std::numeric_limits<>::min() and ::max() i
21 "StringToNumber only supports signed integers as large as long long int");
22 RTC_DCHECK(str);
23 char* end = nullptr;
24 errno = 0;
25 const long long int value = std::strtoll(str, &end, base);
26 if (end && *end == 0 && end != str && errno == 0 &&
kwiberg-webrtc 2017/02/18 12:28:53 Style nit: *end == '\0'
27 value >= std::numeric_limits<T>::min() &&
28 value <= std::numeric_limits<T>::max()) {
29 return rtc::Optional<T>(static_cast<T>(value));
30 }
31 return rtc::Optional<T>();
32 }
33
34 template <typename T>
35 typename std::enable_if<std::is_integral<T>::value &&
36 std::is_unsigned<T>::value,
37 rtc::Optional<T>>::type
38 StringToNumber(const char* str, int base) {
39 static_assert(
40 sizeof(long long int) >= sizeof(T),
41 "StringToNumber only supports unsigned integers as large as unsigned "
42 "long long int");
43 RTC_DCHECK(str);
44 // Explicitly discard negative values. std::strtoull parsing causes unsigned
45 // wraparound. We cannot just reject values that start with -, though, since
46 // -0 is perfectly fine, as is -0000000000000000000000000000000.
47 bool is_negative = false;
48 for (const char *ptr = str; isspace(*ptr) || *ptr == '-'; ++ptr) {
49 if (*ptr == '-') {
50 is_negative = true;
51 }
52 }
53 char* end = nullptr;
54 errno = 0;
55 const unsigned long long int value = std::strtoull(str, &end, base);
56 if (end && *end == 0 && end != str && errno == 0 &&
57 value >= std::numeric_limits<T>::min() &&
kwiberg-webrtc 2017/02/18 12:28:53 value and T are both unsigned, so this is triviall
58 value <= std::numeric_limits<T>::max() && (value == 0 || !is_negative)) {
59 return rtc::Optional<T>(static_cast<T>(value));
kwiberg-webrtc 2017/02/18 12:28:53 Hmm. Do you have a unit test for e.g. parsing "-25
60 }
61 return rtc::Optional<T>();
62 }
63
64 #define RTC_STN_INSTANCE(T) \
65 template rtc::Optional<T> StringToNumber<T>(const char*, int)
66
67 RTC_STN_INSTANCE(signed char);
68 RTC_STN_INSTANCE(unsigned char);
69 RTC_STN_INSTANCE(short);
70 RTC_STN_INSTANCE(unsigned short);
71 RTC_STN_INSTANCE(int);
72 RTC_STN_INSTANCE(unsigned int);
73 RTC_STN_INSTANCE(long);
74 RTC_STN_INSTANCE(unsigned long);
75 RTC_STN_INSTANCE(long long);
76 RTC_STN_INSTANCE(unsigned long long);
77
78 #undef RTC_STN_INSTANCE
79
80 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698