Replace rtc::ToString() implementation with StrCat for most cases
Bug: None Change-Id: I23e76cbd531cd5ea2e10ea16763cbce5491c8f8d Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/364260 Commit-Queue: Harald Alvestrand <hta@webrtc.org> Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> Cr-Commit-Position: refs/heads/main@{#43125}
This commit is contained in:
parent
1fe80229cc
commit
cf796a2d2c
@ -601,6 +601,7 @@ rtc_library("stringutils") {
|
||||
":macromagic",
|
||||
":safe_minmax",
|
||||
"../api:array_view",
|
||||
"//third_party/abseil-cpp/absl/strings",
|
||||
"//third_party/abseil-cpp/absl/strings:string_view",
|
||||
]
|
||||
}
|
||||
|
||||
@ -10,6 +10,9 @@ specific_include_rules = {
|
||||
"+absl/strings/has_absl_stringify.h",
|
||||
"+absl/strings/str_cat.h",
|
||||
],
|
||||
"string_encode.h": [
|
||||
"+absl/strings/str_cat.h"
|
||||
],
|
||||
"protobuf_utils.h": [
|
||||
"+third_party/protobuf",
|
||||
],
|
||||
|
||||
@ -284,6 +284,7 @@ inline Val<LogArgType::kULongLong, unsigned long long> MakeVal(
|
||||
inline Val<LogArgType::kDouble, double> MakeVal(double x) {
|
||||
return {x};
|
||||
}
|
||||
|
||||
inline Val<LogArgType::kLongDouble, long double> MakeVal(long double x) {
|
||||
return {x};
|
||||
}
|
||||
|
||||
@ -187,88 +187,19 @@ std::vector<absl::string_view> split(absl::string_view source, char delimiter) {
|
||||
return fields;
|
||||
}
|
||||
|
||||
std::string ToString(const bool b) {
|
||||
template <>
|
||||
std::string ToString(bool b) {
|
||||
return b ? "true" : "false";
|
||||
}
|
||||
|
||||
std::string ToString(absl::string_view s) {
|
||||
return std::string(s);
|
||||
}
|
||||
|
||||
std::string ToString(const char* s) {
|
||||
return std::string(s);
|
||||
}
|
||||
|
||||
std::string ToString(const short s) {
|
||||
char buf[32];
|
||||
const int len = std::snprintf(&buf[0], arraysize(buf), "%hd", s);
|
||||
RTC_DCHECK_LE(len, arraysize(buf));
|
||||
return std::string(&buf[0], len);
|
||||
}
|
||||
std::string ToString(const unsigned short s) {
|
||||
char buf[32];
|
||||
const int len = std::snprintf(&buf[0], arraysize(buf), "%hu", s);
|
||||
RTC_DCHECK_LE(len, arraysize(buf));
|
||||
return std::string(&buf[0], len);
|
||||
}
|
||||
std::string ToString(const int s) {
|
||||
char buf[32];
|
||||
const int len = std::snprintf(&buf[0], arraysize(buf), "%d", s);
|
||||
RTC_DCHECK_LE(len, arraysize(buf));
|
||||
return std::string(&buf[0], len);
|
||||
}
|
||||
std::string ToString(const unsigned int s) {
|
||||
char buf[32];
|
||||
const int len = std::snprintf(&buf[0], arraysize(buf), "%u", s);
|
||||
RTC_DCHECK_LE(len, arraysize(buf));
|
||||
return std::string(&buf[0], len);
|
||||
}
|
||||
std::string ToString(const long int s) {
|
||||
char buf[32];
|
||||
const int len = std::snprintf(&buf[0], arraysize(buf), "%ld", s);
|
||||
RTC_DCHECK_LE(len, arraysize(buf));
|
||||
return std::string(&buf[0], len);
|
||||
}
|
||||
std::string ToString(const unsigned long int s) {
|
||||
char buf[32];
|
||||
const int len = std::snprintf(&buf[0], arraysize(buf), "%lu", s);
|
||||
RTC_DCHECK_LE(len, arraysize(buf));
|
||||
return std::string(&buf[0], len);
|
||||
}
|
||||
std::string ToString(const long long int s) {
|
||||
char buf[32];
|
||||
const int len = std::snprintf(&buf[0], arraysize(buf), "%lld", s);
|
||||
RTC_DCHECK_LE(len, arraysize(buf));
|
||||
return std::string(&buf[0], len);
|
||||
}
|
||||
std::string ToString(const unsigned long long int s) {
|
||||
char buf[32];
|
||||
const int len = std::snprintf(&buf[0], arraysize(buf), "%llu", s);
|
||||
RTC_DCHECK_LE(len, arraysize(buf));
|
||||
return std::string(&buf[0], len);
|
||||
}
|
||||
|
||||
std::string ToString(const double d) {
|
||||
char buf[32];
|
||||
const int len = std::snprintf(&buf[0], arraysize(buf), "%g", d);
|
||||
RTC_DCHECK_LE(len, arraysize(buf));
|
||||
return std::string(&buf[0], len);
|
||||
}
|
||||
|
||||
std::string ToString(const long double d) {
|
||||
template <>
|
||||
std::string ToString(long double d) {
|
||||
char buf[32];
|
||||
const int len = std::snprintf(&buf[0], arraysize(buf), "%Lg", d);
|
||||
RTC_DCHECK_LE(len, arraysize(buf));
|
||||
return std::string(&buf[0], len);
|
||||
}
|
||||
|
||||
std::string ToString(const void* const p) {
|
||||
char buf[32];
|
||||
const int len = std::snprintf(&buf[0], arraysize(buf), "%p", p);
|
||||
RTC_DCHECK_LE(len, arraysize(buf));
|
||||
return std::string(&buf[0], len);
|
||||
}
|
||||
|
||||
bool FromString(absl::string_view s, bool* b) {
|
||||
if (s == "false") {
|
||||
*b = false;
|
||||
|
||||
@ -18,6 +18,7 @@
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "api/array_view.h"
|
||||
#include "rtc_base/checks.h"
|
||||
@ -62,28 +63,32 @@ bool tokenize_first(absl::string_view source,
|
||||
std::string* token,
|
||||
std::string* rest);
|
||||
|
||||
// Convert arbitrary values to/from a string.
|
||||
// TODO(jonasolsson): Remove these when absl::StrCat becomes available.
|
||||
template <typename T,
|
||||
typename std::enable_if<
|
||||
!std::is_pointer<T>::value ||
|
||||
std::is_convertible<T, const char*>::value>::type* = nullptr>
|
||||
std::string ToString(T value) {
|
||||
return {absl::StrCat(value)};
|
||||
}
|
||||
|
||||
// Versions that behave differently from StrCat
|
||||
template <>
|
||||
std::string ToString(bool b);
|
||||
|
||||
std::string ToString(absl::string_view s);
|
||||
// The const char* overload is needed for correct overload resolution because of
|
||||
// the const void* version of ToString() below.
|
||||
std::string ToString(const char* s);
|
||||
|
||||
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);
|
||||
// Versions not supported by StrCat:
|
||||
template <>
|
||||
std::string ToString(long double t);
|
||||
|
||||
std::string ToString(const void* p);
|
||||
template <typename T,
|
||||
typename std::enable_if<
|
||||
std::is_pointer<T>::value &&
|
||||
!std::is_convertible<T, const char*>::value>::type* = nullptr>
|
||||
std::string ToString(T p) {
|
||||
char buf[32];
|
||||
const int len = std::snprintf(&buf[0], std::size(buf), "%p", p);
|
||||
RTC_DCHECK_LE(len, std::size(buf));
|
||||
return std::string(&buf[0], len);
|
||||
}
|
||||
|
||||
template <typename T,
|
||||
typename std::enable_if<std::is_arithmetic<T>::value &&
|
||||
|
||||
@ -263,6 +263,9 @@ TEST(ToString, SanityCheck) {
|
||||
const char* c = "message";
|
||||
EXPECT_EQ(ToString(c), c);
|
||||
EXPECT_EQ(ToString(std::string(c)), c);
|
||||
char nonconst_c[] = "message";
|
||||
EXPECT_EQ(ToString(nonconst_c), c);
|
||||
EXPECT_EQ(ToString(&nonconst_c[0]), c);
|
||||
|
||||
EXPECT_EQ(ToString(short{-123}), "-123");
|
||||
EXPECT_EQ(ToString((unsigned short)123), "123");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user