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

Unified Diff: webrtc/base/string_to_number.h

Issue 2696003004: Added integer parsing functions in base/string_to_number.h (Closed)
Patch Set: Added more debug printing to invalid inputs tests. 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 side-by-side diff with in-line comments
Download patch
Index: webrtc/base/string_to_number.h
diff --git a/webrtc/base/string_to_number.h b/webrtc/base/string_to_number.h
new file mode 100644
index 0000000000000000000000000000000000000000..f54cd26238979771df2265955d99c45ce6bd2e70
--- /dev/null
+++ b/webrtc/base/string_to_number.h
@@ -0,0 +1,138 @@
+/*
+ * 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.
+ */
+
+#ifndef WEBRTC_BASE_STRING_TO_NUMBER_H_
+#define WEBRTC_BASE_STRING_TO_NUMBER_H_
+
+#include <string>
+#include <limits>
+
+#include "webrtc/base/optional.h"
+
+namespace rtc {
+
+// This file declares two families of functions to parse numbers from strings,
+// one set for integers and one set for floats. The standard C library functions
+// either fail to indicate errors (atoi, etc.) or are a hassle to work with
+// (strtol, sscanf, etc.). The standard C++ library functions (std::stoi, etc.)
+// indicate errors by throwing exceptions, which are disabled in WebRTC.
+//
+// Integers are parsed using one of the following functions:
+// rtc::Optional<int-type> StringToNumber(const char* str, int base = 10);
+// rtc::Optional<int-type> StringToNumber(const std::string& str,
+// int base = 10);
+//
+// These functions parse a value from the beginning of a string into one of the
+// fundamental integer types, or returns an empty Optional if parsing
+// failed. Values outside of the range supported by the type will be
+// rejected. By setting base to 0, one of octal, decimal or hexadecimal will be
+// detected from the string's prefix (0, nothing or 0x, respectively).
+// If non-zero, base can be set to a value between 2 and 36 inclusively.
+//
+// Floating-point values are parsed similarly to integers, but don't support
+// explicitly specifying the base:
+// rtc::Optional<float-type> StringToNumber(const char* str);
+// rtc::Optional<float-type> StringToNumber(const std::string& str);
+
+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 = 10) {
kwiberg-webrtc 2017/02/16 00:27:24 You probably want to static_assert that long long
ossu 2017/02/16 10:19:26 Makes sense. The tests should catch any case where
kwiberg-webrtc 2017/02/16 11:59:42 Yes, but it's easier for a human to see that the s
+ RTC_DCHECK(str);
+ char* end = nullptr;
+ errno = 0;
+ const long long int value = std::strtoll(str, &end, base);
+ if (end != str && errno == 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 = 10) {
+ 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;
+ }
+ }
kwiberg-webrtc 2017/02/16 00:27:23 You allow multiple minus signs---that's probably n
ossu 2017/02/16 10:19:26 I should check, but I'd expect stroull would rejec
kwiberg-webrtc 2017/02/16 11:59:42 Oh, I see. I thought you were discarding the minus
+ char* end = nullptr;
+ errno = 0;
+ const unsigned long long int value = std::strtoull(str, &end, base);
+ if (end != str && errno == 0 && value >= std::numeric_limits<T>::min() &&
+ value <= std::numeric_limits<T>::max() && (value == 0 || !is_negative)) {
+ return rtc::Optional<T>(static_cast<T>(value));
+ }
+ return rtc::Optional<T>();
+}
+
+template <typename T>
+typename std::enable_if<std::is_floating_point<T>::value,
+ rtc::Optional<T>>::type
+StringToNumber(const char* str);
+
+template<>
+rtc::Optional<float> StringToNumber<float>(const char* str) {
+ char *end = nullptr;
+ errno = 0;
+ const float value = std::strtof(str, &end);
+ if (end != str && errno == 0) {
+ return rtc::Optional<float>(value);
+ }
+ return rtc::Optional<float>();
+}
+
+template<>
+rtc::Optional<double> StringToNumber<double>(const char* str) {
+ char *end = nullptr;
+ errno = 0;
+ const double value = std::strtod(str, &end);
+ if (end != str && errno == 0) {
+ return rtc::Optional<double>(value);
+ }
+ return rtc::Optional<double>();
+}
+
+template<>
+rtc::Optional<long double> StringToNumber<long double>(const char* str) {
+ char *end = nullptr;
+ errno = 0;
+ const long double value = std::strtold(str, &end);
+ if (end != str && errno == 0) {
+ return rtc::Optional<long double>(value);
+ }
+ return rtc::Optional<long double>();
+}
kwiberg-webrtc 2017/02/16 00:27:23 You could probably unify these three if you made a
ossu 2017/02/16 10:19:26 Yeah, that's true. I'll look into that. As-is, the
kwiberg-webrtc 2017/02/16 11:59:42 Sounds good.
+
+// The std::string overloads only exists if there is a matching const char*
+// version.
+template <typename T>
+auto StringToNumber(const std::string& str, int base)
+ -> decltype(StringToNumber<T>(str.c_str(), base)) {
+ return StringToNumber<T>(str.c_str(), base);
+}
+
+template <typename T>
+auto StringToNumber(const std::string& str)
+ -> decltype(StringToNumber<T>(str.c_str())) {
+ return StringToNumber<T>(str.c_str());
+}
kwiberg-webrtc 2017/02/16 00:27:23 Could you ditch this one if you defaulted base to
ossu 2017/02/16 10:19:26 Alas, no, there's no base parameter to the floatin
+
+} // namespace rtc
+
+#endif // WEBRTC_BASE_STRING_TO_NUMBER_H_

Powered by Google App Engine
This is Rietveld 408576698