Reimplement rtc::ToString and rtc::FromString without streams.
Bug: webrtc:8982
Change-Id: I3977435b035fdebef449732301d6e77fc899e7ba
Reviewed-on: https://webrtc-review.googlesource.com/86941
Commit-Queue: Jonas Olsson <jonasolsson@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24319}
diff --git a/rtc_base/stringencode.h b/rtc_base/stringencode.h
index 7042c4a..830ea77 100644
--- a/rtc_base/stringencode.h
+++ b/rtc_base/stringencode.h
@@ -16,6 +16,7 @@
#include <vector>
#include "rtc_base/checks.h"
+#include "rtc_base/string_to_number.h"
namespace rtc {
@@ -147,32 +148,40 @@
std::string* rest);
// Convert arbitrary values to/from a string.
+// TODO(jonasolsson): Remove these when absl::StrCat becomes available.
+std::string ToString(bool b);
-template <class T>
-static bool ToString(const T& t, std::string* s) {
- RTC_DCHECK(s);
- std::ostringstream oss;
- oss << std::boolalpha << t;
- *s = oss.str();
- return !oss.fail();
-}
+std::string ToString(const char* s);
+std::string ToString(std::string t);
-template <class T>
+std::string ToString(short s);
+std::string ToString(unsigned short s);
+std::string ToString(int s);
+std::string ToString(unsigned int s);
+std::string ToString(long int s);
+std::string ToString(unsigned long int s);
+std::string ToString(long long int s);
+std::string ToString(unsigned long long int s);
+
+std::string ToString(double t);
+
+std::string ToString(const void* p);
+
+template <typename T,
+ typename std::enable_if<std::is_arithmetic<T>::value &&
+ !std::is_same<T, bool>::value,
+ int>::type = 0>
static bool FromString(const std::string& s, T* t) {
RTC_DCHECK(t);
- std::istringstream iss(s);
- iss >> std::boolalpha >> *t;
- return !iss.fail();
+ absl::optional<T> result = StringToNumber<T>(s);
+
+ if (result)
+ *t = *result;
+
+ return result.has_value();
}
-// Inline versions of the string conversion routines.
-
-template <typename T>
-static inline std::string ToString(const T& val) {
- std::string str;
- ToString(val, &str);
- return str;
-}
+bool FromString(const std::string& s, bool* b);
template <typename T>
static inline T FromString(const std::string& str) {
@@ -181,13 +190,6 @@
return val;
}
-template <typename T>
-static inline T FromString(const T& defaultValue, const std::string& str) {
- T val(defaultValue);
- FromString(str, &val);
- return val;
-}
-
//////////////////////////////////////////////////////////////////////
} // namespace rtc