Update to current webrtc library
This is from the upstream library commit id 3326535126e435f1ba647885ce43a8f0f3d317eb, corresponding to Chromium 88.0.4290.1.
This commit is contained in:
141
webrtc/rtc_base/strings/string_builder.cc
Normal file
141
webrtc/rtc_base/strings/string_builder.cc
Normal file
@ -0,0 +1,141 @@
|
||||
/*
|
||||
* Copyright 2018 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 "rtc_base/strings/string_builder.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#include "rtc_base/checks.h"
|
||||
#include "rtc_base/numerics/safe_minmax.h"
|
||||
|
||||
namespace rtc {
|
||||
|
||||
SimpleStringBuilder::SimpleStringBuilder(rtc::ArrayView<char> buffer)
|
||||
: buffer_(buffer) {
|
||||
buffer_[0] = '\0';
|
||||
RTC_DCHECK(IsConsistent());
|
||||
}
|
||||
|
||||
SimpleStringBuilder& SimpleStringBuilder::operator<<(const char* str) {
|
||||
return Append(str, strlen(str));
|
||||
}
|
||||
|
||||
SimpleStringBuilder& SimpleStringBuilder::operator<<(char ch) {
|
||||
return Append(&ch, 1);
|
||||
}
|
||||
|
||||
SimpleStringBuilder& SimpleStringBuilder::operator<<(const std::string& str) {
|
||||
return Append(str.c_str(), str.length());
|
||||
}
|
||||
|
||||
// Numeric conversion routines.
|
||||
//
|
||||
// We use std::[v]snprintf instead of std::to_string because:
|
||||
// * std::to_string relies on the current locale for formatting purposes,
|
||||
// and therefore concurrent calls to std::to_string from multiple threads
|
||||
// may result in partial serialization of calls
|
||||
// * snprintf allows us to print the number directly into our buffer.
|
||||
// * avoid allocating a std::string (potential heap alloc).
|
||||
// TODO(tommi): Switch to std::to_chars in C++17.
|
||||
|
||||
SimpleStringBuilder& SimpleStringBuilder::operator<<(int i) {
|
||||
return AppendFormat("%d", i);
|
||||
}
|
||||
|
||||
SimpleStringBuilder& SimpleStringBuilder::operator<<(unsigned i) {
|
||||
return AppendFormat("%u", i);
|
||||
}
|
||||
|
||||
SimpleStringBuilder& SimpleStringBuilder::operator<<(long i) { // NOLINT
|
||||
return AppendFormat("%ld", i);
|
||||
}
|
||||
|
||||
SimpleStringBuilder& SimpleStringBuilder::operator<<(long long i) { // NOLINT
|
||||
return AppendFormat("%lld", i);
|
||||
}
|
||||
|
||||
SimpleStringBuilder& SimpleStringBuilder::operator<<(
|
||||
unsigned long i) { // NOLINT
|
||||
return AppendFormat("%lu", i);
|
||||
}
|
||||
|
||||
SimpleStringBuilder& SimpleStringBuilder::operator<<(
|
||||
unsigned long long i) { // NOLINT
|
||||
return AppendFormat("%llu", i);
|
||||
}
|
||||
|
||||
SimpleStringBuilder& SimpleStringBuilder::operator<<(float f) {
|
||||
return AppendFormat("%g", f);
|
||||
}
|
||||
|
||||
SimpleStringBuilder& SimpleStringBuilder::operator<<(double f) {
|
||||
return AppendFormat("%g", f);
|
||||
}
|
||||
|
||||
SimpleStringBuilder& SimpleStringBuilder::operator<<(long double f) {
|
||||
return AppendFormat("%Lg", f);
|
||||
}
|
||||
|
||||
SimpleStringBuilder& SimpleStringBuilder::AppendFormat(const char* fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
const int len =
|
||||
std::vsnprintf(&buffer_[size_], buffer_.size() - size_, fmt, args);
|
||||
if (len >= 0) {
|
||||
const size_t chars_added = rtc::SafeMin(len, buffer_.size() - 1 - size_);
|
||||
size_ += chars_added;
|
||||
RTC_DCHECK_EQ(len, chars_added) << "Buffer size was insufficient";
|
||||
} else {
|
||||
// This should never happen, but we're paranoid, so re-write the
|
||||
// terminator in case vsnprintf() overwrote it.
|
||||
RTC_NOTREACHED();
|
||||
buffer_[size_] = '\0';
|
||||
}
|
||||
va_end(args);
|
||||
RTC_DCHECK(IsConsistent());
|
||||
return *this;
|
||||
}
|
||||
|
||||
SimpleStringBuilder& SimpleStringBuilder::Append(const char* str,
|
||||
size_t length) {
|
||||
RTC_DCHECK_LT(size_ + length, buffer_.size())
|
||||
<< "Buffer size was insufficient";
|
||||
const size_t chars_added = rtc::SafeMin(length, buffer_.size() - size_ - 1);
|
||||
memcpy(&buffer_[size_], str, chars_added);
|
||||
size_ += chars_added;
|
||||
buffer_[size_] = '\0';
|
||||
RTC_DCHECK(IsConsistent());
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringBuilder& StringBuilder::AppendFormat(const char* fmt, ...) {
|
||||
va_list args, copy;
|
||||
va_start(args, fmt);
|
||||
va_copy(copy, args);
|
||||
const int predicted_length = std::vsnprintf(nullptr, 0, fmt, copy);
|
||||
va_end(copy);
|
||||
|
||||
RTC_DCHECK_GE(predicted_length, 0);
|
||||
if (predicted_length > 0) {
|
||||
const size_t size = str_.size();
|
||||
str_.resize(size + predicted_length);
|
||||
// Pass "+ 1" to vsnprintf to include space for the '\0'.
|
||||
const int actual_length =
|
||||
std::vsnprintf(&str_[size], predicted_length + 1, fmt, args);
|
||||
RTC_DCHECK_GE(actual_length, 0);
|
||||
}
|
||||
va_end(args);
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // namespace rtc
|
175
webrtc/rtc_base/strings/string_builder.h
Normal file
175
webrtc/rtc_base/strings/string_builder.h
Normal file
@ -0,0 +1,175 @@
|
||||
/*
|
||||
* Copyright 2018 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 RTC_BASE_STRINGS_STRING_BUILDER_H_
|
||||
#define RTC_BASE_STRINGS_STRING_BUILDER_H_
|
||||
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "api/array_view.h"
|
||||
#include "rtc_base/string_encode.h"
|
||||
|
||||
namespace rtc {
|
||||
|
||||
// This is a minimalistic string builder class meant to cover the most cases of
|
||||
// when you might otherwise be tempted to use a stringstream (discouraged for
|
||||
// anything except logging). It uses a fixed-size buffer provided by the caller
|
||||
// and concatenates strings and numbers into it, allowing the results to be
|
||||
// read via |str()|.
|
||||
class SimpleStringBuilder {
|
||||
public:
|
||||
explicit SimpleStringBuilder(rtc::ArrayView<char> buffer);
|
||||
SimpleStringBuilder(const SimpleStringBuilder&) = delete;
|
||||
SimpleStringBuilder& operator=(const SimpleStringBuilder&) = delete;
|
||||
|
||||
SimpleStringBuilder& operator<<(const char* str);
|
||||
SimpleStringBuilder& operator<<(char ch);
|
||||
SimpleStringBuilder& operator<<(const std::string& str);
|
||||
SimpleStringBuilder& operator<<(int i);
|
||||
SimpleStringBuilder& operator<<(unsigned i);
|
||||
SimpleStringBuilder& operator<<(long i); // NOLINT
|
||||
SimpleStringBuilder& operator<<(long long i); // NOLINT
|
||||
SimpleStringBuilder& operator<<(unsigned long i); // NOLINT
|
||||
SimpleStringBuilder& operator<<(unsigned long long i); // NOLINT
|
||||
SimpleStringBuilder& operator<<(float f);
|
||||
SimpleStringBuilder& operator<<(double f);
|
||||
SimpleStringBuilder& operator<<(long double f);
|
||||
|
||||
// Returns a pointer to the built string. The name |str()| is borrowed for
|
||||
// compatibility reasons as we replace usage of stringstream throughout the
|
||||
// code base.
|
||||
const char* str() const { return buffer_.data(); }
|
||||
|
||||
// Returns the length of the string. The name |size()| is picked for STL
|
||||
// compatibility reasons.
|
||||
size_t size() const { return size_; }
|
||||
|
||||
// Allows appending a printf style formatted string.
|
||||
#if defined(__GNUC__)
|
||||
__attribute__((__format__(__printf__, 2, 3)))
|
||||
#endif
|
||||
SimpleStringBuilder&
|
||||
AppendFormat(const char* fmt, ...);
|
||||
|
||||
// An alternate way from operator<<() to append a string. This variant is
|
||||
// slightly more efficient when the length of the string to append, is known.
|
||||
SimpleStringBuilder& Append(const char* str, size_t length);
|
||||
|
||||
private:
|
||||
bool IsConsistent() const {
|
||||
return size_ <= buffer_.size() - 1 && buffer_[size_] == '\0';
|
||||
}
|
||||
|
||||
// An always-zero-terminated fixed-size buffer that we write to. The fixed
|
||||
// size allows the buffer to be stack allocated, which helps performance.
|
||||
// Having a fixed size is furthermore useful to avoid unnecessary resizing
|
||||
// while building it.
|
||||
const rtc::ArrayView<char> buffer_;
|
||||
|
||||
// Represents the number of characters written to the buffer.
|
||||
// This does not include the terminating '\0'.
|
||||
size_t size_ = 0;
|
||||
};
|
||||
|
||||
// A string builder that supports dynamic resizing while building a string.
|
||||
// The class is based around an instance of std::string and allows moving
|
||||
// ownership out of the class once the string has been built.
|
||||
// Note that this class uses the heap for allocations, so SimpleStringBuilder
|
||||
// might be more efficient for some use cases.
|
||||
class StringBuilder {
|
||||
public:
|
||||
StringBuilder() {}
|
||||
explicit StringBuilder(absl::string_view s) : str_(s) {}
|
||||
|
||||
// TODO(tommi): Support construction from StringBuilder?
|
||||
StringBuilder(const StringBuilder&) = delete;
|
||||
StringBuilder& operator=(const StringBuilder&) = delete;
|
||||
|
||||
StringBuilder& operator<<(const absl::string_view str) {
|
||||
str_.append(str.data(), str.length());
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringBuilder& operator<<(char c) = delete;
|
||||
|
||||
StringBuilder& operator<<(int i) {
|
||||
str_ += rtc::ToString(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringBuilder& operator<<(unsigned i) {
|
||||
str_ += rtc::ToString(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringBuilder& operator<<(long i) { // NOLINT
|
||||
str_ += rtc::ToString(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringBuilder& operator<<(long long i) { // NOLINT
|
||||
str_ += rtc::ToString(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringBuilder& operator<<(unsigned long i) { // NOLINT
|
||||
str_ += rtc::ToString(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringBuilder& operator<<(unsigned long long i) { // NOLINT
|
||||
str_ += rtc::ToString(i);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringBuilder& operator<<(float f) {
|
||||
str_ += rtc::ToString(f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringBuilder& operator<<(double f) {
|
||||
str_ += rtc::ToString(f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
StringBuilder& operator<<(long double f) {
|
||||
str_ += rtc::ToString(f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
const std::string& str() const { return str_; }
|
||||
|
||||
void Clear() { str_.clear(); }
|
||||
|
||||
size_t size() const { return str_.size(); }
|
||||
|
||||
std::string Release() {
|
||||
std::string ret = std::move(str_);
|
||||
str_.clear();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Allows appending a printf style formatted string.
|
||||
StringBuilder& AppendFormat(const char* fmt, ...)
|
||||
#if defined(__GNUC__)
|
||||
__attribute__((__format__(__printf__, 2, 3)))
|
||||
#endif
|
||||
;
|
||||
|
||||
private:
|
||||
std::string str_;
|
||||
};
|
||||
|
||||
} // namespace rtc
|
||||
|
||||
#endif // RTC_BASE_STRINGS_STRING_BUILDER_H_
|
Reference in New Issue
Block a user