aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/str.cpp
blob: 13f0e8ef2892c4e33fb1b916349fe6a9d6aa5810 (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
#include "str.h"

static constexpr size_t MIN_BUFFER_GROW_SIZE = 16;

TStringInput::~TStringInput() = default;

size_t TStringInput::DoNext(const void** ptr, size_t len) {
    len = Min(len, S_->size() - Pos_);
    *ptr = S_->data() + Pos_;
    Pos_ += len;
    return len;
}

void TStringInput::DoUndo(size_t len) {
    Y_VERIFY(len <= Pos_);
    Pos_ -= len;
}

TStringOutput::~TStringOutput() = default;

size_t TStringOutput::DoNext(void** ptr) {
    if (S_->size() == S_->capacity()) {
        S_->reserve(FastClp2(S_->capacity() + MIN_BUFFER_GROW_SIZE));
    }
    size_t previousSize = S_->size();
    ResizeUninitialized(*S_, S_->capacity());
    *ptr = S_->begin() + previousSize;
    return S_->size() - previousSize;
}

void TStringOutput::DoUndo(size_t len) {
    Y_VERIFY(len <= S_->size(), "trying to undo more bytes than actually written");
    S_->resize(S_->size() - len);
}

void TStringOutput::DoWrite(const void* buf, size_t len) {
    S_->append((const char*)buf, len);
}

void TStringOutput::DoWriteC(char c) {
    S_->push_back(c);
}

TStringStream::~TStringStream() = default;