aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/zerocopy.cpp
blob: dffd211c1af0d9e56a0ef4be130375e5f69a6e06 (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
#include "zerocopy.h"
#include "output.h" 
 
IZeroCopyInput::~IZeroCopyInput() = default;
 
size_t IZeroCopyInput::DoRead(void* buf, size_t len) {
    const void* ptr;
    size_t result = DoNext(&ptr, len);
 
    if (result) { 
        memcpy(buf, ptr, result); 
    } 
 
    return result;
}

ui64 IZeroCopyInput::DoReadAll(IOutputStream& out) {
    ui64 result = 0;
    const void* ptr; 

    while (size_t len = Next(&ptr)) {
        out.Write(ptr, len);
        result += len;
    }

    return result;
}

size_t IZeroCopyInput::DoSkip(size_t len) {
    const void* ptr;
 
    return DoNext(&ptr, len);
}

IZeroCopyInputFastReadTo::~IZeroCopyInputFastReadTo() = default;

size_t IZeroCopyInputFastReadTo::DoReadTo(TString& st, char ch) {
    const char* ptr;
    size_t len = Next(&ptr);
    if (!len) {
        return 0;
    }
    size_t result = 0;
    st.clear();
    do {
        if (const char* pos = (const char*)memchr(ptr, ch, len)) {
            size_t bytesRead = (pos - ptr) + 1;
            if (bytesRead > 1) {
                st.append(ptr, pos);
            }
            Undo(len - bytesRead);
            result += bytesRead;
            return result;
        } else {
            result += len;
            st.append(ptr, len);
        }
    } while (len = Next(&ptr));
    return result;
}