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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
#pragma once
#include "zerocopy.h"
#include "zerocopy_output.h"
#include <util/generic/string.h>
#include <util/generic/noncopyable.h>
#include <util/generic/store_policy.h>
/**
* @addtogroup Streams_Strings
* @{
*/
/**
* Input stream for reading data from a string.
*/
class TStringInput: public IZeroCopyInputFastReadTo {
public:
/**
* Constructs a string input stream that reads character data from the
* provided string.
*
* Note that this stream keeps a reference to the provided string, so it's
* up to the user to make sure that the string doesn't get destroyed while
* this stream is in use.
*
* For reading data from `TStringBuf`s, see `TMemoryInput` (`util/stream/mem.h`).
*
* @param s String to read from.
*/
inline TStringInput(const TString& s Y_LIFETIME_BOUND) noexcept
: S_(&s)
, Pos_(0)
{
}
TStringInput(const TString&&) = delete;
~TStringInput() override;
TStringInput(TStringInput&&) noexcept = default;
TStringInput& operator=(TStringInput&&) noexcept = default;
inline void Swap(TStringInput& s) noexcept {
DoSwap(S_, s.S_);
DoSwap(Pos_, s.Pos_);
}
protected:
size_t DoNext(const void** ptr, size_t len) override;
void DoUndo(size_t len) override;
private:
const TString* S_;
size_t Pos_;
friend class TStringStream;
};
/**
* Stream for writing data into a string.
*/
class TStringOutput: public IZeroCopyOutput {
public:
/**
* Constructs a string output stream that appends character data to the
* provided string.
*
* Note that this stream keeps a reference to the provided string, so it's
* up to the user to make sure that the string doesn't get destroyed while
* this stream is in use.
*
* @param s String to append to.
*/
inline TStringOutput(TString& s Y_LIFETIME_BOUND) noexcept
: S_(&s)
{
}
TStringOutput(TStringOutput&& s) noexcept = default;
~TStringOutput() override;
/**
* @param size Number of additional characters to
* reserve in output string.
*/
inline void Reserve(size_t size) {
S_->reserve(S_->size() + size);
}
inline void Swap(TStringOutput& s) noexcept {
DoSwap(S_, s.S_);
}
protected:
size_t DoNext(void** ptr) override;
void DoUndo(size_t len) override;
void DoWrite(const void* buf, size_t len) override;
void DoWriteC(char c) override;
private:
TString* S_;
};
/**
* String input/output stream, similar to `std::stringstream`.
*/
class TStringStream: private TEmbedPolicy<TString>, public TStringInput, public TStringOutput {
using TEmbeddedString = TEmbedPolicy<TString>;
public:
inline TStringStream()
: TEmbeddedString()
, TStringInput(*TEmbeddedString::Ptr())
, TStringOutput(*TEmbeddedString::Ptr())
{
}
inline TStringStream(const TString& string)
: TEmbeddedString(string)
, TStringInput(*TEmbeddedString::Ptr())
, TStringOutput(*TEmbeddedString::Ptr())
{
}
inline TStringStream(TString&& string)
: TEmbeddedString(std::move(string))
, TStringInput(*TEmbeddedString::Ptr())
, TStringOutput(*TEmbeddedString::Ptr())
{
}
inline TStringStream(const TStringStream& other)
: TEmbeddedString(other.Str())
, TStringInput(*TEmbeddedString::Ptr())
, TStringOutput(*TEmbeddedString::Ptr())
{
}
inline TStringStream(TStringStream&& other)
: TEmbeddedString(std::move(other).Str())
, TStringInput(*TEmbeddedString::Ptr())
, TStringOutput(*TEmbeddedString::Ptr())
{
other.Pos_ = 0;
}
inline TStringStream& operator=(const TStringStream& other) {
// All references remain alive, we need to change position only
Str() = other.Str();
Pos_ = other.Pos_;
return *this;
}
inline TStringStream& operator=(TStringStream&& other) {
// All references remain alive, we need to change position only
Str() = std::move(other).Str();
Pos_ = other.Pos_;
other.Pos_ = 0;
return *this;
}
~TStringStream() override;
/**
* @returns Whether @c this contains any data
*/
explicit operator bool() const noexcept {
return !Empty();
}
/**
* @returns String that this stream is writing into.
*/
inline TString& Str() & noexcept {
return *Ptr();
}
/**
* @returns String that this stream is writing into.
*/
inline const TString& Str() const& noexcept {
return *Ptr();
}
/**
* @returns String that this stream is writing into.
*/
inline TString&& Str() && noexcept {
return std::move(*Ptr());
}
/**
* @returns Pointer to the character data contained
* in this stream. The data is guaranteed
* to be null-terminated.
*/
inline const char* Data() const noexcept {
return Ptr()->data();
}
/**
* @returns Total number of characters in this
* stream. Note that this is not the same
* as the total number of characters
* available for reading.
*/
inline size_t Size() const noexcept {
return Ptr()->size();
}
/**
* @returns Whether the string that this stream
* operates on is empty.
*/
Y_PURE_FUNCTION inline bool Empty() const noexcept {
return Str().empty();
}
using TStringOutput::Reserve;
/**
* Clears the string that this stream operates on and resets the
* read/write pointers.
*/
inline void Clear() {
Str().clear();
Pos_ = 0;
}
// TODO: compatibility with existing code, remove
Y_PURE_FUNCTION bool empty() const {
return Empty();
}
void clear() {
Clear();
}
};
/** @} */
|