blob: 7c45ac4ace4e68766f0867b6dd317b422aaa4b04 [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"
21#include "rtc_base/string_utils.h"
Tommifef05002018-02-27 13:51:08 +010022
23namespace rtc {
24
Karl Wiberg881f1682018-03-08 15:03:23 +010025// This is a minimalistic string builder class meant to cover the most cases of
26// when you might otherwise be tempted to use a stringstream (discouraged for
27// anything except logging). It uses a fixed-size buffer provided by the caller
28// and concatenates strings and numbers into it, allowing the results to be
29// read via |str()|.
Tommifef05002018-02-27 13:51:08 +010030class SimpleStringBuilder {
31 public:
Karl Wiberg881f1682018-03-08 15:03:23 +010032 explicit SimpleStringBuilder(rtc::ArrayView<char> buffer);
Tommifef05002018-02-27 13:51:08 +010033 SimpleStringBuilder(const SimpleStringBuilder&) = delete;
34 SimpleStringBuilder& operator=(const SimpleStringBuilder&) = delete;
35
Karl Wibergabff7dd2018-03-09 15:26:24 +010036 SimpleStringBuilder& operator<<(const char* str);
37 SimpleStringBuilder& operator<<(char ch);
38 SimpleStringBuilder& operator<<(const std::string& str);
39 SimpleStringBuilder& operator<<(int i);
40 SimpleStringBuilder& operator<<(unsigned i);
41 SimpleStringBuilder& operator<<(long i); // NOLINT
42 SimpleStringBuilder& operator<<(long long i); // NOLINT
43 SimpleStringBuilder& operator<<(unsigned long i); // NOLINT
44 SimpleStringBuilder& operator<<(unsigned long long i); // NOLINT
45 SimpleStringBuilder& operator<<(float f);
46 SimpleStringBuilder& operator<<(double f);
47 SimpleStringBuilder& operator<<(long double f);
Tommifef05002018-02-27 13:51:08 +010048
49 // Returns a pointer to the built string. The name |str()| is borrowed for
50 // compatibility reasons as we replace usage of stringstream throughout the
51 // code base.
Karl Wiberg881f1682018-03-08 15:03:23 +010052 const char* str() const { return buffer_.data(); }
Tommifef05002018-02-27 13:51:08 +010053
54 // Returns the length of the string. The name |size()| is picked for STL
55 // compatibility reasons.
56 size_t size() const { return size_; }
57
Yves Gerey665174f2018-06-19 15:03:05 +020058// Allows appending a printf style formatted string.
Karl Wiberg881f1682018-03-08 15:03:23 +010059#if defined(__GNUC__)
60 __attribute__((__format__(__printf__, 2, 3)))
61#endif
62 SimpleStringBuilder&
Karl Wibergabff7dd2018-03-09 15:26:24 +010063 AppendFormat(const char* fmt, ...);
Tommifef05002018-02-27 13:51:08 +010064
65 // An alternate way from operator<<() to append a string. This variant is
66 // slightly more efficient when the length of the string to append, is known.
Karl Wibergabff7dd2018-03-09 15:26:24 +010067 SimpleStringBuilder& Append(const char* str, size_t length = SIZE_UNKNOWN);
Tommifef05002018-02-27 13:51:08 +010068
69 private:
Karl Wiberg881f1682018-03-08 15:03:23 +010070 bool IsConsistent() const {
71 return size_ <= buffer_.size() - 1 && buffer_[size_] == '\0';
Tommifef05002018-02-27 13:51:08 +010072 }
73
Karl Wiberg881f1682018-03-08 15:03:23 +010074 // An always-zero-terminated fixed-size buffer that we write to. The fixed
75 // size allows the buffer to be stack allocated, which helps performance.
Tommifef05002018-02-27 13:51:08 +010076 // Having a fixed size is furthermore useful to avoid unnecessary resizing
77 // while building it.
Karl Wiberg881f1682018-03-08 15:03:23 +010078 const rtc::ArrayView<char> buffer_;
Tommifef05002018-02-27 13:51:08 +010079
80 // Represents the number of characters written to the buffer.
81 // This does not include the terminating '\0'.
82 size_t size_ = 0;
83};
84
Jonas Olsson88e18482018-09-03 10:15:08 +020085// A string builder that supports dynamic resizing while building a string.
86// The class is based around an instance of std::string and allows moving
87// ownership out of the class once the string has been built.
88// Note that this class uses the heap for allocations, so SimpleStringBuilder
89// might be more efficient for some use cases.
90class StringBuilder {
91 public:
92 StringBuilder() {}
93 explicit StringBuilder(absl::string_view s) : str_(s) {}
94
95 // TODO(tommi): Support construction from StringBuilder?
96 StringBuilder(const StringBuilder&) = delete;
97 StringBuilder& operator=(const StringBuilder&) = delete;
98
99 StringBuilder& operator<<(const absl::string_view str) {
100 str_.append(str.data(), str.length());
101 return *this;
102 }
103
104 StringBuilder& operator<<(char c) = delete;
105
106 StringBuilder& operator<<(int i) {
107 str_ += rtc::ToString(i);
108 return *this;
109 }
110
111 StringBuilder& operator<<(unsigned i) {
112 str_ += rtc::ToString(i);
113 return *this;
114 }
115
116 StringBuilder& operator<<(long i) { // NOLINT
117 str_ += rtc::ToString(i);
118 return *this;
119 }
120
121 StringBuilder& operator<<(long long i) { // NOLINT
122 str_ += rtc::ToString(i);
123 return *this;
124 }
125
126 StringBuilder& operator<<(unsigned long i) { // NOLINT
127 str_ += rtc::ToString(i);
128 return *this;
129 }
130
131 StringBuilder& operator<<(unsigned long long i) { // NOLINT
132 str_ += rtc::ToString(i);
133 return *this;
134 }
135
136 StringBuilder& operator<<(float f) {
137 str_ += rtc::ToString(f);
138 return *this;
139 }
140
141 StringBuilder& operator<<(double f) {
142 str_ += rtc::ToString(f);
143 return *this;
144 }
145
146 StringBuilder& operator<<(long double f) {
147 str_ += rtc::ToString(f);
148 return *this;
149 }
150
151 const std::string& str() const { return str_; }
152
153 void Clear() { str_.clear(); }
154
155 size_t size() const { return str_.size(); }
156
157 std::string Release() {
Jonas Olsson84df1c72018-09-14 16:59:32 +0200158 std::string ret = std::move(str_);
159 str_.clear();
Jonas Olsson88e18482018-09-03 10:15:08 +0200160 return ret;
161 }
162
163 // Allows appending a printf style formatted string.
164 StringBuilder& AppendFormat(const char* fmt, ...)
165#if defined(__GNUC__)
166 __attribute__((__format__(__printf__, 2, 3)))
167#endif
168 ;
169
170 private:
171 std::string str_;
172};
173
Tommifef05002018-02-27 13:51:08 +0100174} // namespace rtc
175
176#endif // RTC_BASE_STRINGS_STRING_BUILDER_H_