Index: webrtc/base/string_to_number.cc |
diff --git a/webrtc/base/string_to_number.cc b/webrtc/base/string_to_number.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..639ae593f89c2a66f80ce128faed4587821f9151 |
--- /dev/null |
+++ b/webrtc/base/string_to_number.cc |
@@ -0,0 +1,80 @@ |
+/* |
+ * Copyright 2017 The WebRTC Project Authors. All rights reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+ |
+#include "webrtc/base/string_to_number.h" |
+ |
+namespace rtc { |
+ |
+template <typename T> |
+typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value, |
+ rtc::Optional<T>>::type |
+StringToNumber(const char* str, int base) { |
+ static_assert( |
+ sizeof(long long int) >= sizeof(T), |
kwiberg-webrtc
2017/02/18 12:28:53
Compare std::numeric_limits<>::min() and ::max() i
|
+ "StringToNumber only supports signed integers as large as long long int"); |
+ RTC_DCHECK(str); |
+ char* end = nullptr; |
+ errno = 0; |
+ const long long int value = std::strtoll(str, &end, base); |
+ if (end && *end == 0 && end != str && errno == 0 && |
kwiberg-webrtc
2017/02/18 12:28:53
Style nit: *end == '\0'
|
+ value >= std::numeric_limits<T>::min() && |
+ value <= std::numeric_limits<T>::max()) { |
+ return rtc::Optional<T>(static_cast<T>(value)); |
+ } |
+ return rtc::Optional<T>(); |
+} |
+ |
+template <typename T> |
+typename std::enable_if<std::is_integral<T>::value && |
+ std::is_unsigned<T>::value, |
+ rtc::Optional<T>>::type |
+StringToNumber(const char* str, int base) { |
+ static_assert( |
+ sizeof(long long int) >= sizeof(T), |
+ "StringToNumber only supports unsigned integers as large as unsigned " |
+ "long long int"); |
+ RTC_DCHECK(str); |
+ // Explicitly discard negative values. std::strtoull parsing causes unsigned |
+ // wraparound. We cannot just reject values that start with -, though, since |
+ // -0 is perfectly fine, as is -0000000000000000000000000000000. |
+ bool is_negative = false; |
+ for (const char *ptr = str; isspace(*ptr) || *ptr == '-'; ++ptr) { |
+ if (*ptr == '-') { |
+ is_negative = true; |
+ } |
+ } |
+ char* end = nullptr; |
+ errno = 0; |
+ const unsigned long long int value = std::strtoull(str, &end, base); |
+ if (end && *end == 0 && end != str && errno == 0 && |
+ value >= std::numeric_limits<T>::min() && |
kwiberg-webrtc
2017/02/18 12:28:53
value and T are both unsigned, so this is triviall
|
+ value <= std::numeric_limits<T>::max() && (value == 0 || !is_negative)) { |
+ 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
|
+ } |
+ return rtc::Optional<T>(); |
+} |
+ |
+#define RTC_STN_INSTANCE(T) \ |
+ template rtc::Optional<T> StringToNumber<T>(const char*, int) |
+ |
+RTC_STN_INSTANCE(signed char); |
+RTC_STN_INSTANCE(unsigned char); |
+RTC_STN_INSTANCE(short); |
+RTC_STN_INSTANCE(unsigned short); |
+RTC_STN_INSTANCE(int); |
+RTC_STN_INSTANCE(unsigned int); |
+RTC_STN_INSTANCE(long); |
+RTC_STN_INSTANCE(unsigned long); |
+RTC_STN_INSTANCE(long long); |
+RTC_STN_INSTANCE(unsigned long long); |
+ |
+#undef RTC_STN_INSTANCE |
+ |
+} // namespace rtc |