diff options
author | babenko <babenko@yandex-team.ru> | 2022-02-10 16:49:19 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:19 +0300 |
commit | cec37806d8847aa3db53bafc9e251d4aaf325c12 (patch) | |
tree | 4a61c191e93e31d9ab423e258c71ab43550ee3d2 /library/cpp/yt/string/string_builder.h | |
parent | 58cd0b86ed99a72df22479e26a20bc1c1e57e65e (diff) | |
download | ydb-cec37806d8847aa3db53bafc9e251d4aaf325c12.tar.gz |
Restoring authorship annotation for <babenko@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/yt/string/string_builder.h')
-rw-r--r-- | library/cpp/yt/string/string_builder.h | 214 |
1 files changed, 107 insertions, 107 deletions
diff --git a/library/cpp/yt/string/string_builder.h b/library/cpp/yt/string/string_builder.h index 0e13e70904..6dac4a3eea 100644 --- a/library/cpp/yt/string/string_builder.h +++ b/library/cpp/yt/string/string_builder.h @@ -1,116 +1,116 @@ -#pragma once - -#include <util/generic/string.h> - -namespace NYT { - -//////////////////////////////////////////////////////////////////////////////// - -// Forward declarations. -class TStringBuilderBase; -class TStringBuilder; -class TDelimitedStringBuilderWrapper; - -template <size_t Length, class... TArgs> -void Format(TStringBuilderBase* builder, const char (&format)[Length], TArgs&&... args); -template <class... TArgs> -void Format(TStringBuilderBase* builder, TStringBuf format, TArgs&&... args); - -//////////////////////////////////////////////////////////////////////////////// - -//! A simple helper for constructing strings by a sequence of appends. -class TStringBuilderBase -{ -public: +#pragma once + +#include <util/generic/string.h> + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +// Forward declarations. +class TStringBuilderBase; +class TStringBuilder; +class TDelimitedStringBuilderWrapper; + +template <size_t Length, class... TArgs> +void Format(TStringBuilderBase* builder, const char (&format)[Length], TArgs&&... args); +template <class... TArgs> +void Format(TStringBuilderBase* builder, TStringBuf format, TArgs&&... args); + +//////////////////////////////////////////////////////////////////////////////// + +//! A simple helper for constructing strings by a sequence of appends. +class TStringBuilderBase +{ +public: virtual ~TStringBuilderBase() = default; - char* Preallocate(size_t size); - - size_t GetLength() const; - - TStringBuf GetBuffer() const; - - void Advance(size_t size); - - void AppendChar(char ch); - void AppendChar(char ch, int n); - - void AppendString(TStringBuf str); - void AppendString(const char* str); - - template <size_t Length, class... TArgs> - void AppendFormat(const char (&format)[Length], TArgs&&... args); - template <class... TArgs> - void AppendFormat(TStringBuf format, TArgs&&... args); - - void Reset(); - -protected: + char* Preallocate(size_t size); + + size_t GetLength() const; + + TStringBuf GetBuffer() const; + + void Advance(size_t size); + + void AppendChar(char ch); + void AppendChar(char ch, int n); + + void AppendString(TStringBuf str); + void AppendString(const char* str); + + template <size_t Length, class... TArgs> + void AppendFormat(const char (&format)[Length], TArgs&&... args); + template <class... TArgs> + void AppendFormat(TStringBuf format, TArgs&&... args); + + void Reset(); + +protected: char* Begin_ = nullptr; char* Current_ = nullptr; char* End_ = nullptr; - - virtual void DoReset() = 0; - virtual void DoPreallocate(size_t newLength) = 0; - - // -64 must account for any reasonable overhead in dynamic string allocation. - static constexpr size_t MinBufferLength = 1024 - 64; -}; - -//////////////////////////////////////////////////////////////////////////////// - -class TStringBuilder - : public TStringBuilderBase -{ -public: - TString Flush(); - -protected: - TString Buffer_; - + + virtual void DoReset() = 0; + virtual void DoPreallocate(size_t newLength) = 0; + + // -64 must account for any reasonable overhead in dynamic string allocation. + static constexpr size_t MinBufferLength = 1024 - 64; +}; + +//////////////////////////////////////////////////////////////////////////////// + +class TStringBuilder + : public TStringBuilderBase +{ +public: + TString Flush(); + +protected: + TString Buffer_; + void DoReset() override; void DoPreallocate(size_t size) override; -}; - -//////////////////////////////////////////////////////////////////////////////// - -template <class T> +}; + +//////////////////////////////////////////////////////////////////////////////// + +template <class T> TString ToStringViaBuilder(const T& value, TStringBuf spec = TStringBuf("v")); - -//////////////////////////////////////////////////////////////////////////////// - -//! Appends a certain delimiter starting from the second call. -class TDelimitedStringBuilderWrapper - : private TNonCopyable -{ -public: - TDelimitedStringBuilderWrapper( - TStringBuilderBase* builder, + +//////////////////////////////////////////////////////////////////////////////// + +//! Appends a certain delimiter starting from the second call. +class TDelimitedStringBuilderWrapper + : private TNonCopyable +{ +public: + TDelimitedStringBuilderWrapper( + TStringBuilderBase* builder, TStringBuf delimiter = TStringBuf(", ")) - : Builder_(builder) - , Delimiter_(delimiter) - { } - - TStringBuilderBase* operator->() - { - if (!FirstCall_) { - Builder_->AppendString(Delimiter_); - } - FirstCall_ = false; - return Builder_; - } - -private: - TStringBuilderBase* const Builder_; - const TStringBuf Delimiter_; - - bool FirstCall_ = true; -}; - -//////////////////////////////////////////////////////////////////////////////// - -} // namespace NYT - -#define STRING_BUILDER_INL_H_ -#include "string_builder-inl.h" -#undef STRING_BUILDER_INL_H_ + : Builder_(builder) + , Delimiter_(delimiter) + { } + + TStringBuilderBase* operator->() + { + if (!FirstCall_) { + Builder_->AppendString(Delimiter_); + } + FirstCall_ = false; + return Builder_; + } + +private: + TStringBuilderBase* const Builder_; + const TStringBuf Delimiter_; + + bool FirstCall_ = true; +}; + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT + +#define STRING_BUILDER_INL_H_ +#include "string_builder-inl.h" +#undef STRING_BUILDER_INL_H_ |