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> |
Karl Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 15 | #include <cstring> |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 16 | #include <string> |
| 17 | |
Karl Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 18 | #include "api/array_view.h" |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
Karl Wiberg | 881f168 | 2018-03-08 15:03:23 +0100 | [diff] [blame] | 20 | #include "rtc_base/numerics/safe_minmax.h" |
Tommi | fef0500 | 2018-02-27 13:51:08 +0100 | [diff] [blame] | 21 | #include "rtc_base/stringutils.h" |
| 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 | |
| 85 | } // namespace rtc |
| 86 | |
| 87 | #endif // RTC_BASE_STRINGS_STRING_BUILDER_H_ |