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