-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.h
More file actions
122 lines (102 loc) · 1.82 KB
/
Copy pathbuffer.h
File metadata and controls
122 lines (102 loc) · 1.82 KB
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
#pragma once
#include <vector>
#include <cstring>
#include <utility>
#include <cassert>
namespace fs
{
class buffer
{
public:
explicit buffer(size_t size) :
data_(size),
begin_(0),
end_(0)
{
}
buffer(buffer&& other) :
data_(std::move(other.data_)),
begin_(other.begin_),
end_(other.end_)
{
}
buffer& operator=(buffer&& other)
{
data_ = std::move(other.data_);
begin_ = other.begin_;
end_ = other.end_;
}
const char* data_to_read() const
{
assert(!empty_to_read());
return data_.data() + begin_;
}
const char* data_to_read_end() const
{
return data_.data() + end_;
}
bool empty_to_read() const
{
return begin_ == end_;
}
size_t available_to_read() const
{
return end_ - begin_;
}
void read(size_t cnt)
{
assert(begin_ + cnt <= end_);
begin_ += cnt;
if (begin_ == end_)
clear();
}
void clear()
{
begin_ = 0;
end_ = 0;
}
char* data_to_write()
{
assert(!empty_to_write());
return data_.data() + end_;
}
bool empty_to_write() const
{
return end_ == data_.size();
}
size_t available_to_write() const
{
return data_.size() - end_;
}
void write(size_t cnt)
{
assert(end_ + cnt <= data_.size());
end_ += cnt;
}
const char* first_string_end() const
{
auto end = static_cast<const char*>(memchr(data_to_read(), '\n', available_to_read()));
return end ? end + 1 : nullptr;
}
void terminate_with_newline_if_necessary()
{
// If it is empty, full or already terminates than do nothing
if (empty_to_read() || end_ == data_.size() || data_[end_ - 1] == '\n')
return;
data_[end_ - 1] = '\n';
}
void move_data_to_begin()
{
if (empty_to_read())
return;
const auto sz = available_to_read();
memmove(data_.data(), data_to_read(), sz);
begin_ = 0;
end_ = sz;
}
private:
std::vector<char> data_;
size_t begin_;
size_t end_;
};
}