blob: b35b7f1867be93303806ccc81009a8b9d98c7b0b [file] [log] [blame]
Tommifef05002018-02-27 13:51:08 +01001/*
2 * Copyright 2018 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef RTC_BASE_STRINGS_STRING_BUILDER_H_
12#define RTC_BASE_STRINGS_STRING_BUILDER_H_
13
14#include <cstdio>
15#include <string>
Yves Gerey2e00abc2018-10-05 15:39:24 +020016#include <utility>
Tommifef05002018-02-27 13:51:08 +010017
Jonas Olsson88e18482018-09-03 10:15:08 +020018#include "absl/strings/string_view.h"
Karl Wiberg881f1682018-03-08 15:03:23 +010019#include "api/array_view.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/string_encode.h"
Tommifef05002018-02-27 13:51:08 +010021
22namespace rtc {
23
Karl Wiberg881f1682018-03-08 15:03:23 +010024// This is a minimalistic string builder class meant to cover the most cases of
25// when you might otherwise be tempted to use a stringstream (discouraged for
26// anything except logging). It uses a fixed-size buffer provided by the caller
27// and concatenates strings and numbers into it, allowing the results to be
Artem Titov96e3b992021-07-26 16:03:14 +020028// read via `str()`.
Tommifef05002018-02-27 13:51:08 +010029class SimpleStringBuilder {
30 public:
Karl Wiberg881f1682018-03-08 15:03:23 +010031 explicit SimpleStringBuilder(rtc::ArrayView<char> buffer);
Tommifef05002018-02-27 13:51:08 +010032 SimpleStringBuilder(const SimpleStringBuilder&) = delete;
33 SimpleStringBuilder& operator=(const SimpleStringBuilder&) = delete;
34
Karl Wibergabff7dd2018-03-09 15:26:24 +010035 SimpleStringBuilder& operator<<(char ch);
Ali Tofigh7fa90572022-03-17 15:47:49 +010036 SimpleStringBuilder& operator<<(absl::string_view str);
Karl Wibergabff7dd2018-03-09 15:26:24 +010037 SimpleStringBuilder& operator<<(int i);
38 SimpleStringBuilder& operator<<(unsigned i);
39 SimpleStringBuilder& operator<<(long i); // NOLINT
40 SimpleStringBuilder& operator<<(long long i); // NOLINT
41 SimpleStringBuilder& operator<<(unsigned long i); // NOLINT
42 SimpleStringBuilder& operator<<(unsigned long long i); // NOLINT
43 SimpleStringBuilder& operator<<(float f);
44 SimpleStringBuilder& operator<<(double f);
45 SimpleStringBuilder& operator<<(long double f);
Tommifef05002018-02-27 13:51:08 +010046
Artem Titov96e3b992021-07-26 16:03:14 +020047 // Returns a pointer to the built string. The name `str()` is borrowed for
Tommifef05002018-02-27 13:51:08 +010048 // compatibility reasons as we replace usage of stringstream throughout the
49 // code base.
Karl Wiberg881f1682018-03-08 15:03:23 +010050 const char* str() const { return buffer_.data(); }
Tommifef05002018-02-27 13:51:08 +010051
Artem Titov96e3b992021-07-26 16:03:14 +020052 // Returns the length of the string. The name `size()` is picked for STL
Tommifef05002018-02-27 13:51:08 +010053 // compatibility reasons.
54 size_t size() const { return size_; }
55
Yves Gerey665174f2018-06-19 15:03:05 +020056// Allows appending a printf style formatted string.
Karl Wiberg881f1682018-03-08 15:03:23 +010057#if defined(__GNUC__)
58 __attribute__((__format__(__printf__, 2, 3)))
59#endif
60 SimpleStringBuilder&
Karl Wibergabff7dd2018-03-09 15:26:24 +010061 AppendFormat(const char* fmt, ...);
Tommifef05002018-02-27 13:51:08 +010062
63 // An alternate way from operator<<() to append a string. This variant is
64 // slightly more efficient when the length of the string to append, is known.
Niels Möller198cf002019-05-10 12:29:13 +020065 SimpleStringBuilder& Append(const char* str, size_t length);
Tommifef05002018-02-27 13:51:08 +010066
67 private:
Karl Wiberg881f1682018-03-08 15:03:23 +010068 bool IsConsistent() const {
69 return size_ <= buffer_.size() - 1 && buffer_[size_] == '\0';
Tommifef05002018-02-27 13:51:08 +010070 }
71
Karl Wiberg881f1682018-03-08 15:03:23 +010072 // An always-zero-terminated fixed-size buffer that we write to. The fixed
73 // size allows the buffer to be stack allocated, which helps performance.
Tommifef05002018-02-27 13:51:08 +010074 // Having a fixed size is furthermore useful to avoid unnecessary resizing
75 // while building it.
Karl Wiberg881f1682018-03-08 15:03:23 +010076 const rtc::ArrayView<char> buffer_;
Tommifef05002018-02-27 13:51:08 +010077
78 // Represents the number of characters written to the buffer.
79 // This does not include the terminating '\0'.
80 size_t size_ = 0;
81};
82
Jonas Olsson88e18482018-09-03 10:15:08 +020083// A string builder that supports dynamic resizing while building a string.
84// The class is based around an instance of std::string and allows moving
85// ownership out of the class once the string has been built.
86// Note that this class uses the heap for allocations, so SimpleStringBuilder
87// might be more efficient for some use cases.
88class StringBuilder {
89 public:
90 StringBuilder() {}
91 explicit StringBuilder(absl::string_view s) : str_(s) {}
92
93 // TODO(tommi): Support construction from StringBuilder?
94 StringBuilder(const StringBuilder&) = delete;
95 StringBuilder& operator=(const StringBuilder&) = delete;
96
97 StringBuilder& operator<<(const absl::string_view str) {
98 str_.append(str.data(), str.length());
99 return *this;
100 }
101
102 StringBuilder& operator<<(char c) = delete;
103
104 StringBuilder& operator<<(int i) {
105 str_ += rtc::ToString(i);
106 return *this;
107 }
108
109 StringBuilder& operator<<(unsigned i) {
110 str_ += rtc::ToString(i);
111 return *this;
112 }
113
114 StringBuilder& operator<<(long i) { // NOLINT
115 str_ += rtc::ToString(i);
116 return *this;
117 }
118
119 StringBuilder& operator<<(long long i) { // NOLINT
120 str_ += rtc::ToString(i);
121 return *this;
122 }
123
124 StringBuilder& operator<<(unsigned long i) { // NOLINT
125 str_ += rtc::ToString(i);
126 return *this;
127 }
128
129 StringBuilder& operator<<(unsigned long long i) { // NOLINT
130 str_ += rtc::ToString(i);
131 return *this;
132 }
133
134 StringBuilder& operator<<(float f) {
135 str_ += rtc::ToString(f);
136 return *this;
137 }
138
139 StringBuilder& operator<<(double f) {
140 str_ += rtc::ToString(f);
141 return *this;
142 }
143
144 StringBuilder& operator<<(long double f) {
145 str_ += rtc::ToString(f);
146 return *this;
147 }
148
149 const std::string& str() const { return str_; }
150
151 void Clear() { str_.clear(); }
152
153 size_t size() const { return str_.size(); }
154
155 std::string Release() {
Jonas Olsson84df1c72018-09-14 16:59:32 +0200156 std::string ret = std::move(str_);
157 str_.clear();
Jonas Olsson88e18482018-09-03 10:15:08 +0200158 return ret;
159 }
160
161 // Allows appending a printf style formatted string.
162 StringBuilder& AppendFormat(const char* fmt, ...)
163#if defined(__GNUC__)
164 __attribute__((__format__(__printf__, 2, 3)))
165#endif
166 ;
167
168 private:
169 std::string str_;
170};
171
Tommifef05002018-02-27 13:51:08 +0100172} // namespace rtc
173
174#endif // RTC_BASE_STRINGS_STRING_BUILDER_H_