1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 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/str_cat.h"
#include "absl/strings/string_view.h"
#include "api/array_view.h"
#include "rtc_base/string_encode.h"
namespace webrtc {
// 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(ArrayView<char> buffer);
SimpleStringBuilder(const SimpleStringBuilder&) = delete;
SimpleStringBuilder& operator=(const SimpleStringBuilder&) = delete;
SimpleStringBuilder& operator<<(char ch);
SimpleStringBuilder& operator<<(absl::string_view 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, ...);
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 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_ += absl::StrCat(i);
return *this;
}
StringBuilder& operator<<(unsigned i) {
str_ += absl::StrCat(i);
return *this;
}
StringBuilder& operator<<(long i) { // NOLINT
str_ += absl::StrCat(i);
return *this;
}
StringBuilder& operator<<(long long i) { // NOLINT
str_ += absl::StrCat(i);
return *this;
}
StringBuilder& operator<<(unsigned long i) { // NOLINT
str_ += absl::StrCat(i);
return *this;
}
StringBuilder& operator<<(unsigned long long i) { // NOLINT
str_ += absl::StrCat(i);
return *this;
}
StringBuilder& operator<<(float f) {
str_ += absl::StrCat(f);
return *this;
}
StringBuilder& operator<<(double f) {
str_ += absl::StrCat(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 webrtc
// Re-export symbols from the webrtc namespace for backwards compatibility.
// TODO(bugs.webrtc.org/4222596): Remove once all references are updated.
#ifdef WEBRTC_ALLOW_DEPRECATED_NAMESPACES
namespace rtc {
using ::webrtc::SimpleStringBuilder;
using ::webrtc::StringBuilder;
} // namespace rtc
#endif // WEBRTC_ALLOW_DEPRECATED_NAMESPACES
#endif // RTC_BASE_STRINGS_STRING_BUILDER_H_
|