1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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:
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* 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_;
void DoReset() override;
void DoPreallocate(size_t size) override;
};
////////////////////////////////////////////////////////////////////////////////
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,
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_
|