Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 1 | /* |
| 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 Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 16 | #include <utility> |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 17 | |
Jonas Olsson | 88e1848 | 2018-09-03 10:15:08 +0200 | [diff] [blame] | 18 | #include "absl/strings/string_view.h" |
Karl Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 19 | #include "api/array_view.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 20 | #include "rtc_base/string_encode.h" |
| 21 | #include "rtc_base/string_utils.h" |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 22 | |
| 23 | namespace rtc { |
| 24 | |
Karl Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 25 | // 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()|. |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 30 | class SimpleStringBuilder { |
| 31 | public: |
Karl Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 32 | explicit SimpleStringBuilder(rtc::ArrayView<char> buffer); |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 33 | SimpleStringBuilder(const SimpleStringBuilder&) = delete; |
| 34 | SimpleStringBuilder& operator=(const SimpleStringBuilder&) = delete; |
| 35 | |
Karl Wiberg | abff7dd | 2018-03-09 15:26:24 +0100 | [diff] [blame] | 36 | 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); |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 48 | |
| 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 Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 52 | const char* str() const { return buffer_.data(); } |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 53 | |
| 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 58 | // Allows appending a printf style formatted string. |
Karl Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 59 | #if defined(__GNUC__) |
| 60 | __attribute__((__format__(__printf__, 2, 3))) |
| 61 | #endif |
| 62 | SimpleStringBuilder& |
Karl Wiberg | abff7dd | 2018-03-09 15:26:24 +0100 | [diff] [blame] | 63 | AppendFormat(const char* fmt, ...); |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 64 | |
| 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 Wiberg | abff7dd | 2018-03-09 15:26:24 +0100 | [diff] [blame] | 67 | SimpleStringBuilder& Append(const char* str, size_t length = SIZE_UNKNOWN); |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 68 | |
| 69 | private: |
Karl Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 70 | bool IsConsistent() const { |
| 71 | return size_ <= buffer_.size() - 1 && buffer_[size_] == '\0'; |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Karl Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 74 | // 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. |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 76 | // Having a fixed size is furthermore useful to avoid unnecessary resizing |
| 77 | // while building it. |
Karl Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 78 | const rtc::ArrayView<char> buffer_; |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 79 | |
| 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 Olsson | 88e1848 | 2018-09-03 10:15:08 +0200 | [diff] [blame] | 85 | // 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. |
| 90 | class 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 Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 158 | std::string ret = std::move(str_); |
| 159 | str_.clear(); |
Jonas Olsson | 88e1848 | 2018-09-03 10:15:08 +0200 | [diff] [blame] | 160 | 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 | |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 174 | } // namespace rtc |
| 175 | |
| 176 | #endif // RTC_BASE_STRINGS_STRING_BUILDER_H_ |