string_builder.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #ifndef RTC_BASE_STRINGS_STRING_BUILDER_H_
  11. #define RTC_BASE_STRINGS_STRING_BUILDER_H_
  12. #include <cstdio>
  13. #include <string>
  14. #include <utility>
  15. #include "absl/strings/string_view.h"
  16. #include "api/array_view.h"
  17. #include "rtc_base/string_encode.h"
  18. namespace rtc {
  19. // This is a minimalistic string builder class meant to cover the most cases of
  20. // when you might otherwise be tempted to use a stringstream (discouraged for
  21. // anything except logging). It uses a fixed-size buffer provided by the caller
  22. // and concatenates strings and numbers into it, allowing the results to be
  23. // read via |str()|.
  24. class SimpleStringBuilder {
  25. public:
  26. explicit SimpleStringBuilder(rtc::ArrayView<char> buffer);
  27. SimpleStringBuilder(const SimpleStringBuilder&) = delete;
  28. SimpleStringBuilder& operator=(const SimpleStringBuilder&) = delete;
  29. SimpleStringBuilder& operator<<(const char* str);
  30. SimpleStringBuilder& operator<<(char ch);
  31. SimpleStringBuilder& operator<<(const std::string& str);
  32. SimpleStringBuilder& operator<<(int i);
  33. SimpleStringBuilder& operator<<(unsigned i);
  34. SimpleStringBuilder& operator<<(long i); // NOLINT
  35. SimpleStringBuilder& operator<<(long long i); // NOLINT
  36. SimpleStringBuilder& operator<<(unsigned long i); // NOLINT
  37. SimpleStringBuilder& operator<<(unsigned long long i); // NOLINT
  38. SimpleStringBuilder& operator<<(float f);
  39. SimpleStringBuilder& operator<<(double f);
  40. SimpleStringBuilder& operator<<(long double f);
  41. // Returns a pointer to the built string. The name |str()| is borrowed for
  42. // compatibility reasons as we replace usage of stringstream throughout the
  43. // code base.
  44. const char* str() const { return buffer_.data(); }
  45. // Returns the length of the string. The name |size()| is picked for STL
  46. // compatibility reasons.
  47. size_t size() const { return size_; }
  48. // Allows appending a printf style formatted string.
  49. #if defined(__GNUC__)
  50. __attribute__((__format__(__printf__, 2, 3)))
  51. #endif
  52. SimpleStringBuilder&
  53. AppendFormat(const char* fmt, ...);
  54. // An alternate way from operator<<() to append a string. This variant is
  55. // slightly more efficient when the length of the string to append, is known.
  56. SimpleStringBuilder& Append(const char* str, size_t length);
  57. private:
  58. bool IsConsistent() const {
  59. return size_ <= buffer_.size() - 1 && buffer_[size_] == '\0';
  60. }
  61. // An always-zero-terminated fixed-size buffer that we write to. The fixed
  62. // size allows the buffer to be stack allocated, which helps performance.
  63. // Having a fixed size is furthermore useful to avoid unnecessary resizing
  64. // while building it.
  65. const rtc::ArrayView<char> buffer_;
  66. // Represents the number of characters written to the buffer.
  67. // This does not include the terminating '\0'.
  68. size_t size_ = 0;
  69. };
  70. // A string builder that supports dynamic resizing while building a string.
  71. // The class is based around an instance of std::string and allows moving
  72. // ownership out of the class once the string has been built.
  73. // Note that this class uses the heap for allocations, so SimpleStringBuilder
  74. // might be more efficient for some use cases.
  75. class StringBuilder {
  76. public:
  77. StringBuilder() {}
  78. explicit StringBuilder(absl::string_view s) : str_(s) {}
  79. // TODO(tommi): Support construction from StringBuilder?
  80. StringBuilder(const StringBuilder&) = delete;
  81. StringBuilder& operator=(const StringBuilder&) = delete;
  82. StringBuilder& operator<<(const absl::string_view str) {
  83. str_.append(str.data(), str.length());
  84. return *this;
  85. }
  86. StringBuilder& operator<<(char c) = delete;
  87. StringBuilder& operator<<(int i) {
  88. str_ += rtc::ToString(i);
  89. return *this;
  90. }
  91. StringBuilder& operator<<(unsigned i) {
  92. str_ += rtc::ToString(i);
  93. return *this;
  94. }
  95. StringBuilder& operator<<(long i) { // NOLINT
  96. str_ += rtc::ToString(i);
  97. return *this;
  98. }
  99. StringBuilder& operator<<(long long i) { // NOLINT
  100. str_ += rtc::ToString(i);
  101. return *this;
  102. }
  103. StringBuilder& operator<<(unsigned long i) { // NOLINT
  104. str_ += rtc::ToString(i);
  105. return *this;
  106. }
  107. StringBuilder& operator<<(unsigned long long i) { // NOLINT
  108. str_ += rtc::ToString(i);
  109. return *this;
  110. }
  111. StringBuilder& operator<<(float f) {
  112. str_ += rtc::ToString(f);
  113. return *this;
  114. }
  115. StringBuilder& operator<<(double f) {
  116. str_ += rtc::ToString(f);
  117. return *this;
  118. }
  119. StringBuilder& operator<<(long double f) {
  120. str_ += rtc::ToString(f);
  121. return *this;
  122. }
  123. const std::string& str() const { return str_; }
  124. void Clear() { str_.clear(); }
  125. size_t size() const { return str_.size(); }
  126. std::string Release() {
  127. std::string ret = std::move(str_);
  128. str_.clear();
  129. return ret;
  130. }
  131. // Allows appending a printf style formatted string.
  132. StringBuilder& AppendFormat(const char* fmt, ...)
  133. #if defined(__GNUC__)
  134. __attribute__((__format__(__printf__, 2, 3)))
  135. #endif
  136. ;
  137. private:
  138. std::string str_;
  139. };
  140. } // namespace rtc
  141. #endif // RTC_BASE_STRINGS_STRING_BUILDER_H_