aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/buffer.h
blob: 9cfebdf0096ac80863542cbdbbef6b11016b3bc1 (plain) (blame)
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
#pragma once

#include "utility.h"

#include <util/generic/fwd.h>
#include <util/system/align.h>
#include <util/system/yassert.h>

#include <cstring>

class TBuffer {
public:
    using TIterator = char*;
    using TConstIterator = const char*;

    TBuffer(size_t len = 0);
    TBuffer(const char* buf, size_t len);

    TBuffer(const TBuffer& b)
        : Data_(nullptr)
        , Len_(0)
        , Pos_(0)
    {
        *this = b;
    }

    TBuffer(TBuffer&& b) noexcept;

    TBuffer& operator=(TBuffer&& b) noexcept;

    TBuffer& operator=(const TBuffer& b) {
        if (this != &b) {
            Assign(b.Data(), b.Size());
        }
        return *this;
    }

    ~TBuffer();

    inline void Clear() noexcept {
        Pos_ = 0;
    }

    inline void EraseBack(size_t n) noexcept {
        Y_ASSERT(n <= Pos_);
        Pos_ -= n;
    }

    inline void Reset() noexcept {
        TBuffer().Swap(*this);
    }
 
    inline void Assign(const char* data, size_t len) {
        Clear();
        Append(data, len);
    }

    inline void Assign(const char* b, const char* e) {
        Assign(b, e - b);
    }

    inline char* Data() noexcept {
        return Data_;
    }

    inline const char* Data() const noexcept {
        return Data_;
    }

    inline char* Pos() noexcept {
        return Data_ + Pos_;
    }

    inline const char* Pos() const noexcept {
        return Data_ + Pos_;
    }

    /// Used space in bytes (do not mix with Capacity!)
    inline size_t Size() const noexcept {
        return Pos_;
    }

    Y_PURE_FUNCTION inline bool Empty() const noexcept {
        return !Size();
    }

    inline explicit operator bool() const noexcept {
        return Size();
    }

    inline size_t Avail() const noexcept {
        return Len_ - Pos_;
    }

    void Append(const char* buf, size_t len);

    inline void Append(const char* b, const char* e) {
        Append(b, e - b);
    }

    inline void Append(char ch) {
        if (Len_ == Pos_) {
            Reserve(Len_ + 1);
        }

        *(Data() + Pos_++) = ch;
    }

    void Fill(char ch, size_t len);

    // Method is useful when first messages from buffer are processed, and
    // the last message in buffer is incomplete, so we need to move partial
    // message to the begin of the buffer and continue filling the buffer
    // from the network.
    inline void Chop(size_t pos, size_t count) {
        const auto end = pos + count;
        Y_ASSERT(end <= Pos_);

        if (count == 0) {
            return;
        } else if (count == Pos_) {
            Pos_ = 0;
        } else {
            memmove(Data_ + pos, Data_ + end, Pos_ - end);
            Pos_ -= count;
        }
    }

    inline void ChopHead(size_t count) {
        Chop(0U, count);
    }

    inline void Proceed(size_t pos) {
        //Y_ASSERT(pos <= Len_); // see discussion in REVIEW:29021
        Resize(pos);
    }

    inline void Advance(size_t len) {
        Resize(Pos_ + len);
    }

    inline void Reserve(size_t len) {
        if (len > Len_) {
            DoReserve(len);
        }
    }

    inline void ReserveExactNeverCallMeInSaneCode(size_t len) {
        if (len > Len_) {
            Realloc(len);
        }
    }

    inline void ShrinkToFit() {
        if (Pos_ < Len_) {
            Realloc(Pos_);
        }
    }

    inline void Resize(size_t len) {
        Reserve(len);
        Pos_ = len;
    }

    // Method works like Resize, but allocates exact specified number of bytes
    // rather than rounded up to next power of 2
    // Use with care
    inline void ResizeExactNeverCallMeInSaneCode(size_t len) {
        ReserveExactNeverCallMeInSaneCode(len);
        Pos_ = len;
    }

    inline size_t Capacity() const noexcept {
        return Len_;
    }

    inline void AlignUp(size_t align, char fillChar = '\0') {
        size_t diff = ::AlignUp(Pos_, align) - Pos_;
        while (diff-- > 0) {
            Append(fillChar);
        }
    }
 
    inline char* data() noexcept {
        return Data();
    }

    inline const char* data() const noexcept {
        return Data();
    }

    inline size_t size() const noexcept {
        return Size();
    }

    inline void Swap(TBuffer& r) noexcept {
        DoSwap(Data_, r.Data_);
        DoSwap(Len_, r.Len_);
        DoSwap(Pos_, r.Pos_);
    }

    /*
     * after this call buffer becomes empty
     */
    void AsString(TString& s);

    /*
     * iterator-like interface
     */
    inline TIterator Begin() noexcept {
        return Data();
    }

    inline TIterator End() noexcept {
        return Begin() + Size();
    }

    inline TConstIterator Begin() const noexcept {
        return Data();
    }

    inline TConstIterator End() const noexcept {
        return Begin() + Size();
    }

    bool operator==(const TBuffer& other) const noexcept {
        if (Empty()) {
            return other.Empty();
        }
        return Size() == other.Size() && 0 == std::memcmp(Data(), other.Data(), Size());
    }

    bool operator!=(const TBuffer& other) const noexcept {
        return !(*this == other);
    }

private:
    void DoReserve(size_t len);
    void Realloc(size_t len);

private:
    char* Data_;
    size_t Len_;
    size_t Pos_;
};