Make SimpleStringBuilder into a non-template

So that future CLs can de-inline its methods.

We do this by asking the caller to allocate the buffer instead of
having it as a data member.

Bug: webrtc:8982
Change-Id: I246b0973e54510fdd880c3b6875336c31334d008
Reviewed-on: https://webrtc-review.googlesource.com/60000
Commit-Queue: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Fredrik Solenberg <solenberg@webrtc.org>
Reviewed-by: Tommi <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22355}
This commit is contained in:
Karl Wiberg
2018-03-08 15:03:23 +01:00
committed by Commit Bot
parent 4c6a30c1bf
commit 881f16891b
8 changed files with 173 additions and 38 deletions

View File

@ -12,22 +12,24 @@
#define RTC_BASE_STRINGS_STRING_BUILDER_H_
#include <cstdio>
#include <cstring>
#include <string>
#include "api/array_view.h"
#include "rtc_base/checks.h"
#include "rtc_base/numerics/safe_minmax.h"
#include "rtc_base/stringutils.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).
// This class allocates a fixed size buffer on the stack and concatenates
// strings and numbers into it, allowing the results to be read via |str()|.
template <size_t buffer_size>
// 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:
SimpleStringBuilder() { buffer_[0] = '\0'; }
explicit SimpleStringBuilder(rtc::ArrayView<char> buffer);
SimpleStringBuilder(const SimpleStringBuilder&) = delete;
SimpleStringBuilder& operator=(const SimpleStringBuilder&) = delete;
@ -80,48 +82,60 @@ class SimpleStringBuilder {
// 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_[0]; }
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.
SimpleStringBuilder& AppendFormat(const char* fmt, ...) {
#if defined(__GNUC__)
__attribute__((__format__(__printf__, 2, 3)))
#endif
SimpleStringBuilder&
AppendFormat(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
int len = std::vsnprintf(&buffer_[size_], buffer_size - size_, fmt, args);
RTC_DCHECK_GE(len, 0);
// Negative values are likely programmer error, but let's not update the
// length if so.
if (len > 0)
AddToLength(len);
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;
}
// 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 = SIZE_UNKNOWN) {
AddToLength(
rtc::strcpyn(&buffer_[size_], buffer_size - size_, str, length));
const size_t chars_added =
rtc::strcpyn(&buffer_[size_], buffer_.size() - size_, str, length);
size_ += chars_added;
RTC_DCHECK_EQ(chars_added,
length == SIZE_UNKNOWN ? std::strlen(str) : length)
<< "Buffer size was insufficient";
RTC_DCHECK(IsConsistent());
return *this;
}
private:
void AddToLength(size_t chars_added) {
size_ += chars_added;
RTC_DCHECK_EQ('\0', buffer_[size_]);
RTC_DCHECK_LE(size_, buffer_size - 1)
<< "Buffer size limit reached (" << buffer_size << ")";
bool IsConsistent() const {
return size_ <= buffer_.size() - 1 && buffer_[size_] == '\0';
}
// An always-zero-terminated fixed buffer that we write to.
// Assuming the SimpleStringBuilder instance lives on the stack, this
// buffer will be stack allocated, which is done for performance reasons.
// 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.
char buffer_[buffer_size]; // NOLINT
const rtc::ArrayView<char> buffer_;
// Represents the number of characters written to the buffer.
// This does not include the terminating '\0'.