blob: 00986371d3697ff47e242c18bfce236dcf496c48 [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
Tommifef05002018-02-27 13:51:08 +010063 private:
Karl Wiberg881f1682018-03-08 15:03:23 +010064 bool IsConsistent() const {
65 return size_ <= buffer_.size() - 1 && buffer_[size_] == '\0';
Tommifef05002018-02-27 13:51:08 +010066 }
67
Karl Wiberg881f1682018-03-08 15:03:23 +010068 // An always-zero-terminated fixed-size buffer that we write to. The fixed
69 // size allows the buffer to be stack allocated, which helps performance.
Tommifef05002018-02-27 13:51:08 +010070 // Having a fixed size is furthermore useful to avoid unnecessary resizing
71 // while building it.
Karl Wiberg881f1682018-03-08 15:03:23 +010072 const rtc::ArrayView<char> buffer_;
Tommifef05002018-02-27 13:51:08 +010073
74 // Represents the number of characters written to the buffer.
75 // This does not include the terminating '\0'.
76 size_t size_ = 0;
77};
78
Jonas Olsson88e18482018-09-03 10:15:08 +020079// A string builder that supports dynamic resizing while building a string.
80// The class is based around an instance of std::string and allows moving
81// ownership out of the class once the string has been built.
82// Note that this class uses the heap for allocations, so SimpleStringBuilder
83// might be more efficient for some use cases.
84class StringBuilder {
85 public:
86 StringBuilder() {}
87 explicit StringBuilder(absl::string_view s) : str_(s) {}
88
89 // TODO(tommi): Support construction from StringBuilder?
90 StringBuilder(const StringBuilder&) = delete;
91 StringBuilder& operator=(const StringBuilder&) = delete;
92
93 StringBuilder& operator<<(const absl::string_view str) {
94 str_.append(str.data(), str.length());
95 return *this;
96 }
97
98 StringBuilder& operator<<(char c) = delete;
99
100 StringBuilder& operator<<(int i) {
101 str_ += rtc::ToString(i);
102 return *this;
103 }
104
105 StringBuilder& operator<<(unsigned i) {
106 str_ += rtc::ToString(i);
107 return *this;
108 }
109
110 StringBuilder& operator<<(long i) { // NOLINT
111 str_ += rtc::ToString(i);
112 return *this;
113 }
114
115 StringBuilder& operator<<(long long i) { // NOLINT
116 str_ += rtc::ToString(i);
117 return *this;
118 }
119
120 StringBuilder& operator<<(unsigned long i) { // NOLINT
121 str_ += rtc::ToString(i);
122 return *this;
123 }
124
125 StringBuilder& operator<<(unsigned long long i) { // NOLINT
126 str_ += rtc::ToString(i);
127 return *this;
128 }
129
130 StringBuilder& operator<<(float f) {
131 str_ += rtc::ToString(f);
132 return *this;
133 }
134
135 StringBuilder& operator<<(double f) {
136 str_ += rtc::ToString(f);
137 return *this;
138 }
139
140 StringBuilder& operator<<(long double f) {
141 str_ += rtc::ToString(f);
142 return *this;
143 }
144
145 const std::string& str() const { return str_; }
146
147 void Clear() { str_.clear(); }
148
149 size_t size() const { return str_.size(); }
150
151 std::string Release() {
Jonas Olsson84df1c72018-09-14 16:59:32 +0200152 std::string ret = std::move(str_);
153 str_.clear();
Jonas Olsson88e18482018-09-03 10:15:08 +0200154 return ret;
155 }
156
157 // Allows appending a printf style formatted string.
158 StringBuilder& AppendFormat(const char* fmt, ...)
159#if defined(__GNUC__)
160 __attribute__((__format__(__printf__, 2, 3)))
161#endif
162 ;
163
164 private:
165 std::string str_;
166};
167
Tommifef05002018-02-27 13:51:08 +0100168} // namespace rtc
169
170#endif // RTC_BASE_STRINGS_STRING_BUILDER_H_