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> |
| 16 | |
| 17 | #include "rtc_base/checks.h" |
| 18 | #include "rtc_base/stringutils.h" |
| 19 | |
| 20 | namespace rtc { |
| 21 | |
| 22 | // This is a minimalistic string builder class meant to cover the most cases |
| 23 | // of when you might otherwise be tempted to use a stringstream (discouraged |
| 24 | // for anything except logging). |
| 25 | // This class allocates a fixed size buffer on the stack and concatenates |
| 26 | // strings and numbers into it, allowing the results to be read via |str()|. |
| 27 | template <size_t buffer_size> |
| 28 | class SimpleStringBuilder { |
| 29 | public: |
| 30 | SimpleStringBuilder() { buffer_[0] = '\0'; } |
| 31 | SimpleStringBuilder(const SimpleStringBuilder&) = delete; |
| 32 | SimpleStringBuilder& operator=(const SimpleStringBuilder&) = delete; |
| 33 | |
| 34 | SimpleStringBuilder& operator<<(const char* str) { return Append(str); } |
| 35 | |
| 36 | SimpleStringBuilder& operator<<(char ch) { return Append(&ch, 1); } |
| 37 | |
| 38 | SimpleStringBuilder& operator<<(const std::string& str) { |
| 39 | return Append(str.c_str(), str.length()); |
| 40 | } |
| 41 | |
| 42 | // Numeric conversion routines. |
| 43 | // |
| 44 | // We use std::[v]snprintf instead of std::to_string because: |
| 45 | // * std::to_string relies on the current locale for formatting purposes, |
| 46 | // and therefore concurrent calls to std::to_string from multiple threads |
| 47 | // may result in partial serialization of calls |
| 48 | // * snprintf allows us to print the number directly into our buffer. |
| 49 | // * avoid allocating a std::string (potential heap alloc). |
| 50 | // TODO(tommi): Switch to std::to_chars in C++17. |
| 51 | |
| 52 | SimpleStringBuilder& operator<<(int i) { return AppendFormat("%d", i); } |
| 53 | |
| 54 | SimpleStringBuilder& operator<<(unsigned i) { return AppendFormat("%u", i); } |
| 55 | |
| 56 | SimpleStringBuilder& operator<<(long i) { // NOLINT |
| 57 | return AppendFormat("%ld", i); |
| 58 | } |
| 59 | |
| 60 | SimpleStringBuilder& operator<<(long long i) { // NOLINT |
| 61 | return AppendFormat("%lld", i); |
| 62 | } |
| 63 | |
| 64 | SimpleStringBuilder& operator<<(unsigned long i) { // NOLINT |
| 65 | return AppendFormat("%lu", i); |
| 66 | } |
| 67 | |
| 68 | SimpleStringBuilder& operator<<(unsigned long long i) { // NOLINT |
| 69 | return AppendFormat("%llu", i); |
| 70 | } |
| 71 | |
| 72 | SimpleStringBuilder& operator<<(float f) { return AppendFormat("%f", f); } |
| 73 | |
| 74 | SimpleStringBuilder& operator<<(double f) { return AppendFormat("%f", f); } |
| 75 | |
| 76 | SimpleStringBuilder& operator<<(long double f) { |
| 77 | return AppendFormat("%Lf", f); |
| 78 | } |
| 79 | |
| 80 | // Returns a pointer to the built string. The name |str()| is borrowed for |
| 81 | // compatibility reasons as we replace usage of stringstream throughout the |
| 82 | // code base. |
| 83 | const char* str() const { return &buffer_[0]; } |
| 84 | |
| 85 | // Returns the length of the string. The name |size()| is picked for STL |
| 86 | // compatibility reasons. |
| 87 | size_t size() const { return size_; } |
| 88 | |
| 89 | // Allows appending a printf style formatted string. |
| 90 | SimpleStringBuilder& AppendFormat(const char* fmt, ...) { |
| 91 | va_list args; |
| 92 | va_start(args, fmt); |
| 93 | int len = std::vsnprintf(&buffer_[size_], buffer_size - size_, fmt, args); |
| 94 | RTC_DCHECK_GE(len, 0); |
| 95 | // Negative values are likely programmer error, but let's not update the |
| 96 | // length if so. |
| 97 | if (len > 0) |
| 98 | AddToLength(len); |
| 99 | va_end(args); |
| 100 | return *this; |
| 101 | } |
| 102 | |
| 103 | // An alternate way from operator<<() to append a string. This variant is |
| 104 | // slightly more efficient when the length of the string to append, is known. |
| 105 | SimpleStringBuilder& Append(const char* str, size_t length = SIZE_UNKNOWN) { |
| 106 | AddToLength( |
| 107 | rtc::strcpyn(&buffer_[size_], buffer_size - size_, str, length)); |
| 108 | return *this; |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | void AddToLength(size_t chars_added) { |
| 113 | size_ += chars_added; |
| 114 | RTC_DCHECK_EQ('\0', buffer_[size_]); |
| 115 | RTC_DCHECK_LE(size_, buffer_size - 1) |
| 116 | << "Buffer size limit reached (" << buffer_size << ")"; |
| 117 | } |
| 118 | |
| 119 | // An always-zero-terminated fixed buffer that we write to. |
| 120 | // Assuming the SimpleStringBuilder instance lives on the stack, this |
| 121 | // buffer will be stack allocated, which is done for performance reasons. |
| 122 | // Having a fixed size is furthermore useful to avoid unnecessary resizing |
| 123 | // while building it. |
| 124 | char buffer_[buffer_size]; // NOLINT |
| 125 | |
| 126 | // Represents the number of characters written to the buffer. |
| 127 | // This does not include the terminating '\0'. |
| 128 | size_t size_ = 0; |
| 129 | }; |
| 130 | |
| 131 | } // namespace rtc |
| 132 | |
| 133 | #endif // RTC_BASE_STRINGS_STRING_BUILDER_H_ |