blob: e528cf2f0e853a7488b3ee7632e6667d4104e845 [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
28// 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<<(const char* str);
36 SimpleStringBuilder& operator<<(char ch);
37 SimpleStringBuilder& operator<<(const std::string& str);
38 SimpleStringBuilder& operator<<(int i);
39 SimpleStringBuilder& operator<<(unsigned i);
40 SimpleStringBuilder& operator<<(long i); // NOLINT
41 SimpleStringBuilder& operator<<(long long i); // NOLINT
42 SimpleStringBuilder& operator<<(unsigned long i); // NOLINT
43 SimpleStringBuilder& operator<<(unsigned long long i); // NOLINT
44 SimpleStringBuilder& operator<<(float f);
45 SimpleStringBuilder& operator<<(double f);
46 SimpleStringBuilder& operator<<(long double f);
Tommifef05002018-02-27 13:51:08 +010047
48 // Returns a pointer to the built string. The name |str()| is borrowed for
49 // compatibility reasons as we replace usage of stringstream throughout the
50 // code base.
Karl Wiberg881f1682018-03-08 15:03:23 +010051 const char* str() const { return buffer_.data(); }
Tommifef05002018-02-27 13:51:08 +010052
53 // Returns the length of the string. The name |size()| is picked for STL
54 // compatibility reasons.
55 size_t size() const { return size_; }
56
Yves Gerey665174f2018-06-19 15:03:05 +020057// Allows appending a printf style formatted string.
Karl Wiberg881f1682018-03-08 15:03:23 +010058#if defined(__GNUC__)
59 __attribute__((__format__(__printf__, 2, 3)))
60#endif
61 SimpleStringBuilder&
Karl Wibergabff7dd2018-03-09 15:26:24 +010062 AppendFormat(const char* fmt, ...);
Tommifef05002018-02-27 13:51:08 +010063
64 // An alternate way from operator<<() to append a string. This variant is
65 // slightly more efficient when the length of the string to append, is known.
Niels Möller198cf002019-05-10 12:29:13 +020066 SimpleStringBuilder& Append(const char* str, size_t length);
Tommifef05002018-02-27 13:51:08 +010067
68 private:
Karl Wiberg881f1682018-03-08 15:03:23 +010069 bool IsConsistent() const {
70 return size_ <= buffer_.size() - 1 && buffer_[size_] == '\0';
Tommifef05002018-02-27 13:51:08 +010071 }
72
Karl Wiberg881f1682018-03-08 15:03:23 +010073 // An always-zero-terminated fixed-size buffer that we write to. The fixed
74 // size allows the buffer to be stack allocated, which helps performance.
Tommifef05002018-02-27 13:51:08 +010075 // Having a fixed size is furthermore useful to avoid unnecessary resizing
76 // while building it.
Karl Wiberg881f1682018-03-08 15:03:23 +010077 const rtc::ArrayView<char> buffer_;
Tommifef05002018-02-27 13:51:08 +010078
79 // Represents the number of characters written to the buffer.
80 // This does not include the terminating '\0'.
81 size_t size_ = 0;
82};
83
Jonas Olsson88e18482018-09-03 10:15:08 +020084// A string builder that supports dynamic resizing while building a string.
85// The class is based around an instance of std::string and allows moving
86// ownership out of the class once the string has been built.
87// Note that this class uses the heap for allocations, so SimpleStringBuilder
88// might be more efficient for some use cases.
89class StringBuilder {
90 public:
91 StringBuilder() {}
92 explicit StringBuilder(absl::string_view s) : str_(s) {}
93
94 // TODO(tommi): Support construction from StringBuilder?
95 StringBuilder(const StringBuilder&) = delete;
96 StringBuilder& operator=(const StringBuilder&) = delete;
97
98 StringBuilder& operator<<(const absl::string_view str) {
99 str_.append(str.data(), str.length());
100 return *this;
101 }
102
103 StringBuilder& operator<<(char c) = delete;
104
105 StringBuilder& operator<<(int i) {
106 str_ += rtc::ToString(i);
107 return *this;
108 }
109
110 StringBuilder& operator<<(unsigned i) {
111 str_ += rtc::ToString(i);
112 return *this;
113 }
114
115 StringBuilder& operator<<(long i) { // NOLINT
116 str_ += rtc::ToString(i);
117 return *this;
118 }
119
120 StringBuilder& operator<<(long long i) { // NOLINT
121 str_ += rtc::ToString(i);
122 return *this;
123 }
124
125 StringBuilder& operator<<(unsigned long i) { // NOLINT
126 str_ += rtc::ToString(i);
127 return *this;
128 }
129
130 StringBuilder& operator<<(unsigned long long i) { // NOLINT
131 str_ += rtc::ToString(i);
132 return *this;
133 }
134
135 StringBuilder& operator<<(float f) {
136 str_ += rtc::ToString(f);
137 return *this;
138 }
139
140 StringBuilder& operator<<(double f) {
141 str_ += rtc::ToString(f);
142 return *this;
143 }
144
145 StringBuilder& operator<<(long double f) {
146 str_ += rtc::ToString(f);
147 return *this;
148 }
149
150 const std::string& str() const { return str_; }
151
152 void Clear() { str_.clear(); }
153
154 size_t size() const { return str_.size(); }
155
156 std::string Release() {
Jonas Olsson84df1c72018-09-14 16:59:32 +0200157 std::string ret = std::move(str_);
158 str_.clear();
Jonas Olsson88e18482018-09-03 10:15:08 +0200159 return ret;
160 }
161
162 // Allows appending a printf style formatted string.
163 StringBuilder& AppendFormat(const char* fmt, ...)
164#if defined(__GNUC__)
165 __attribute__((__format__(__printf__, 2, 3)))
166#endif
167 ;
168
169 private:
170 std::string str_;
171};
172
Tommifef05002018-02-27 13:51:08 +0100173} // namespace rtc
174
175#endif // RTC_BASE_STRINGS_STRING_BUILDER_H_