blob: 5254626da59dec07953512c3d8bb82e0888efb10 [file]
/*
* Copyright 2018 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef RTC_BASE_STRINGS_STRING_BUILDER_H_
#define RTC_BASE_STRINGS_STRING_BUILDER_H_
#include <cstdio>
#include <string>
#include <utility>
#include "absl/strings/has_absl_stringify.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "rtc_base/system/rtc_export.h"
namespace webrtc {
// A string builder that supports dynamic resizing while building a string.
// The class is based around an instance of std::string and allows moving
// ownership out of the class once the string has been built.
class RTC_EXPORT StringBuilder {
public:
StringBuilder() = default;
explicit StringBuilder(absl::string_view s) : str_(s) {}
StringBuilder(const StringBuilder&) = default;
StringBuilder& operator=(const StringBuilder&) = default;
StringBuilder& operator<<(absl::string_view str);
StringBuilder& operator<<(char c);
StringBuilder& operator<<(int i);
StringBuilder& operator<<(unsigned i);
StringBuilder& operator<<(long i); // NOLINT
StringBuilder& operator<<(long long i); // NOLINT
StringBuilder& operator<<(unsigned long i); // NOLINT
StringBuilder& operator<<(unsigned long long i); // NOLINT
StringBuilder& operator<<(float f);
StringBuilder& operator<<(double f);
template <typename T>
requires absl::HasAbslStringify<T>::value
StringBuilder& operator<<(const T& value) {
str_ += absl::StrCat(value);
return *this;
}
const std::string& str() const { return str_; }
void Clear() { str_.clear(); }
size_t size() const { return str_.size(); }
// Moves out the internal std::string.
std::string Release() { return std::move(str_); }
// Allows appending a printf style formatted string.
StringBuilder& AppendFormat(const char* fmt, ...)
#if defined(__GNUC__)
__attribute__((__format__(__printf__, 2, 3)))
#endif
;
private:
std::string str_;
};
} // namespace webrtc
#endif // RTC_BASE_STRINGS_STRING_BUILDER_H_