-
Notifications
You must be signed in to change notification settings - Fork 716
/
buffer.inl.hh
202 lines (169 loc) · 4.91 KB
/
buffer.inl.hh
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
#ifndef buffer_inl_h_INCLUDED
#define buffer_inl_h_INCLUDED
#include "assert.hh"
namespace Kakoune
{
[[gnu::always_inline]]
inline const char& Buffer::byte_at(BufferCoord c) const
{
kak_assert(c.line < line_count() and c.column < m_lines[c.line].length());
return m_lines[c.line][c.column];
}
inline BufferCoord Buffer::next(BufferCoord coord) const
{
if (coord.column < m_lines[coord.line].length() - 1)
return {coord.line, coord.column + 1};
return { coord.line + 1, 0 };
}
inline BufferCoord Buffer::prev(BufferCoord coord) const
{
if (coord.column == 0)
return { coord.line - 1, m_lines[coord.line - 1].length() - 1 };
return { coord.line, coord.column - 1 };
}
inline ByteCount Buffer::distance(BufferCoord begin, BufferCoord end) const
{
if (begin > end)
return -distance(end, begin);
if (begin.line == end.line)
return end.column - begin.column;
ByteCount res = m_lines[begin.line].length() - begin.column;
for (LineCount l = begin.line+1; l < end.line; ++l)
res += m_lines[l].length();
res += end.column;
return res;
}
inline bool Buffer::is_valid(BufferCoord c) const
{
return (c.line >= 0 and c.column >= 0) and
((c.line < line_count() and c.column < m_lines[c.line].length()) or
(c.line == line_count() and c.column == 0));
}
inline bool Buffer::is_end(BufferCoord c) const
{
return c >= end_coord();
}
inline BufferIterator Buffer::begin() const
{
return {*this, { 0, 0 }};
}
inline BufferIterator Buffer::end() const
{
return {*this, end_coord()};
}
[[gnu::always_inline]]
inline LineCount Buffer::line_count() const
{
return LineCount{(int)m_lines.size()};
}
inline size_t Buffer::timestamp() const
{
return m_changes.size();
}
inline StringView Buffer::substr(BufferCoord begin, BufferCoord end) const
{
kak_assert(begin.line == end.line);
return m_lines[begin.line].substr(begin.column, end.column - begin.column);
}
inline ConstArrayView<Buffer::Change> Buffer::changes_since(size_t timestamp) const
{
if (timestamp < m_changes.size())
return { m_changes.data() + timestamp,
m_changes.data() + m_changes.size() };
return {};
}
inline BufferCoord Buffer::back_coord() const
{
return { line_count() - 1, m_lines.back().length() - 1 };
}
inline BufferCoord Buffer::end_coord() const
{
return line_count();
}
inline BufferIterator::BufferIterator(const Buffer& buffer, BufferCoord coord) noexcept
: m_buffer{&buffer}, m_coord{coord},
m_line{coord.line < buffer.line_count() ? (*m_buffer)[coord.line] : StringView{}} {}
inline bool BufferIterator::operator==(const BufferIterator& iterator) const noexcept
{
kak_assert(m_buffer == iterator.m_buffer);
return m_coord == iterator.m_coord;
}
inline auto BufferIterator::operator<=>(const BufferIterator& iterator) const noexcept
{
kak_assert(m_buffer == iterator.m_buffer);
return (m_coord <=> iterator.m_coord);
}
inline bool BufferIterator::operator==(const BufferCoord& coord) const noexcept
{
return m_coord == coord;
}
[[gnu::always_inline]]
inline const char& BufferIterator::operator*() const noexcept
{
return m_line[m_coord.column];
}
inline const char& BufferIterator::operator[](size_t n) const noexcept
{
return m_buffer->byte_at(m_buffer->advance(m_coord, n));
}
inline size_t BufferIterator::operator-(const BufferIterator& iterator) const
{
kak_assert(m_buffer == iterator.m_buffer);
return (size_t)m_buffer->distance(iterator.m_coord, m_coord);
}
inline BufferIterator BufferIterator::operator+(ByteCount size) const
{
kak_assert(m_buffer);
return { *m_buffer, m_buffer->advance(m_coord, size) };
}
inline BufferIterator BufferIterator::operator-(ByteCount size) const
{
return { *m_buffer, m_buffer->advance(m_coord, -size) };
}
inline BufferIterator& BufferIterator::operator+=(ByteCount size)
{
m_coord = m_buffer->advance(m_coord, size);
m_line = (*m_buffer)[m_coord.line];
return *this;
}
inline BufferIterator& BufferIterator::operator-=(ByteCount size)
{
m_coord = m_buffer->advance(m_coord, -size);
m_line = (*m_buffer)[m_coord.line];
return *this;
}
inline BufferIterator& BufferIterator::operator++()
{
if (++m_coord.column == m_line.length())
{
m_line = (++m_coord.line < m_buffer->line_count()) ?
(*m_buffer)[m_coord.line] : StringView{};
m_coord.column = 0;
}
return *this;
}
inline BufferIterator& BufferIterator::operator--()
{
if (m_coord.column == 0)
{
m_line = (*m_buffer)[--m_coord.line];
m_coord.column = m_line.length() - 1;
}
else
--m_coord.column;
return *this;
}
inline BufferIterator BufferIterator::operator++(int)
{
BufferIterator save = *this;
++*this;
return save;
}
inline BufferIterator BufferIterator::operator--(int)
{
BufferIterator save = *this;
--*this;
return save;
}
}
#endif // buffer_inl_h_INCLUDED