blob: 915277518a939a4809e29d02305cf436074d2487 (
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
|
#pragma once
#include "cescape.h"
#include "fail.h"
#include "stream_counter.h"
#include <library/cpp/yson_pull/input.h>
namespace NYsonPull {
namespace NDetail {
template <class StreamCounter>
class byte_reader {
NYsonPull::NInput::IStream& stream_;
StreamCounter stream_counter_;
public:
byte_reader(NYsonPull::NInput::IStream& stream)
: stream_(stream)
{
}
// const-ness added to prevent direct stream mutation
const NYsonPull::NInput::IStream& stream() {
return stream_;
}
template <typename... Args>
ATTRIBUTE(noinline, cold)
void fail[[noreturn]](const char* msg, Args&&... args) {
NYsonPull::NDetail::fail(
stream_counter_.info(),
msg,
std::forward<Args>(args)...);
}
template <bool AllowFinish>
void fill_buffer() {
stream_.fill_buffer();
if (!AllowFinish) {
auto& buf = stream_.buffer();
if (Y_UNLIKELY(buf.is_empty() && stream_.at_end())) {
fail("Premature end of stream");
}
}
}
void fill_buffer() {
return fill_buffer<true>();
}
template <bool AllowFinish>
ui8 get_byte() {
fill_buffer<AllowFinish>();
auto& buf = stream_.buffer();
return !buf.is_empty()
? *buf.pos()
: ui8{'\0'};
}
ui8 get_byte() {
return get_byte<true>();
}
void advance(size_t bytes) {
auto& buf = stream_.buffer();
stream_counter_.update(
buf.pos(),
buf.pos() + bytes);
buf.advance(bytes);
}
};
}
}
|