diff options
author | arkady-e1ppa <arkady-e1ppa@yandex-team.com> | 2024-07-02 15:28:09 +0300 |
---|---|---|
committer | arkady-e1ppa <arkady-e1ppa@yandex-team.com> | 2024-07-02 15:40:39 +0300 |
commit | 3336f268eb26aa0318cd3fa3f97116c43545f87b (patch) | |
tree | 047395a3b7bf0751a86c5676a9c103e168e47e3c /library/cpp/yt/string/format.h | |
parent | aa95a328ada306559bd361a96c5348829d0e562e (diff) | |
download | ydb-3336f268eb26aa0318cd3fa3f97116c43545f87b.tar.gz |
YT-21868: Reorganize files to prevent unclear missing include errors
Moving files around so that include of either `string_builder.h` or `format.h` would result in being able to use both `Format` and `TStringBuilder`
8a2abbea2ae7027c1cd3a243ab3de55bdd5d3d27
Diffstat (limited to 'library/cpp/yt/string/format.h')
-rw-r--r-- | library/cpp/yt/string/format.h | 75 |
1 files changed, 72 insertions, 3 deletions
diff --git a/library/cpp/yt/string/format.h b/library/cpp/yt/string/format.h index 9e12d286b7..a6ab1f76f4 100644 --- a/library/cpp/yt/string/format.h +++ b/library/cpp/yt/string/format.h @@ -1,6 +1,8 @@ #pragma once -#include "string_builder.h" +#include "format_string.h" + +#include <util/generic/string.h> namespace NYT { @@ -55,12 +57,79 @@ namespace NYT { */ template <class... TArgs> -void Format(TStringBuilderBase* builder, TFormatString<TArgs...> format, TArgs&&... args); -template <class... TArgs> TString Format(TFormatString<TArgs...> format, TArgs&&... args); //////////////////////////////////////////////////////////////////////////////// +// StringBuilder(Base) definition. + +//! A simple helper for constructing strings by a sequence of appends. +class TStringBuilderBase +{ +public: + virtual ~TStringBuilderBase() = default; + + char* Preallocate(size_t size); + + void Reserve(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 DoReserve(size_t newLength) = 0; + + static constexpr size_t MinBufferLength = 128; +}; + +//////////////////////////////////////////////////////////////////////////////// + +class TStringBuilder + : public TStringBuilderBase +{ +public: + TString Flush(); + +protected: + TString Buffer_; + + void DoReset() override; + void DoReserve(size_t size) override; +}; + +//////////////////////////////////////////////////////////////////////////////// + +template <class... TArgs> +void Format(TStringBuilderBase* builder, TFormatString<TArgs...> format, TArgs&&... args); + +//////////////////////////////////////////////////////////////////////////////// + +template <class T> +TString ToStringViaBuilder(const T& value, TStringBuf spec = TStringBuf("v")); + +//////////////////////////////////////////////////////////////////////////////// + template <class TRange, class TFormatter> struct TFormattableView { |