-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRingBuffer.h
244 lines (192 loc) · 5 KB
/
RingBuffer.h
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#ifndef __STRING_BUFFER_H__
#define __STRING_BUFFER_H__
#include <Arduino.h>
/*
* DECLARATION
*/
template<uint16_t Size, typename T>
class RingBuffer {
public:
RingBuffer();
uint16_t append(T c);
bool full() const;
bool empty() const;
void clear();
uint16_t length() const;
static uint16_t capacity();
const T operator[](uint16_t idx) const;
T pop_first();
uint16_t pop_firsts(uint16_t n);
T pop_last();
const T* buffer();
static const uint16_t END = -1;
protected:
bool _is_continuous() const;
void _make_continuous();
T _buffer[Size];
uint16_t _start, _end;
uint16_t _length;
uint16_t _capacity;
};
template<uint16_t Size, typename T>
const uint16_t RingBuffer<Size, T>::END;
template<uint16_t Size>
class StringBuffer: public RingBuffer<Size, char> {
public:
using RingBuffer<Size, char>::END;
using RingBuffer<Size, char>::append;
uint16_t append(const char* str);
uint16_t index_of(const char* substr, uint16_t offset = 0);
bool starts_with(const char* substr);
bool pop_until(const char* substr);
bool pop_while(const char subchr);
};
/*
* IMPLEMENTATION
*/
template<uint16_t Size, typename T>
RingBuffer<Size, T>::RingBuffer() :
_start(0), _end(0), _length(0), _capacity(Size) {
}
template<uint16_t Size, typename T>
uint16_t RingBuffer<Size, T>::append(T c) {
if (full())
return 0;
_buffer[_end] = c;
_end = (_end + 1) % capacity();
++_length;
return 1;
}
template<uint16_t Size, typename T>
bool RingBuffer<Size, T>::full() const {
return capacity() == length();
}
template<uint16_t Size, typename T>
bool RingBuffer<Size, T>::empty() const {
return length() == 0;
}
template<uint16_t Size, typename T>
void RingBuffer<Size, T>::clear() {
_start = 0;
_end = 0;
_length = 0;
}
template<uint16_t Size, typename T>
uint16_t RingBuffer<Size, T>::length() const {
return _length;
}
template<uint16_t Size, typename T>
uint16_t RingBuffer<Size, T>::capacity() {
return Size;
}
template<uint16_t Size, typename T>
const/* can't modify the returned value */T RingBuffer<Size, T>::operator[](
uint16_t idx) const {
if (idx >= length())
return _buffer[0]; // declared as undefined behavior for the user
return _buffer[(_start + idx) % capacity()];
}
template<uint16_t Size, typename T>
T RingBuffer<Size, T>::pop_first() {
if (empty())
return _buffer[0];
uint8_t t = _start;
_start = (_start + 1) % capacity();
--_length;
return _buffer[t];
}
template<uint16_t Size, typename T>
uint16_t RingBuffer<Size, T>::pop_firsts(uint16_t n) {
uint16_t count = 0;
while (!empty() && n--) {
pop_first();
++count;
}
return count;
}
template<uint16_t Size, typename T>
T RingBuffer<Size, T>::pop_last() {
if (empty())
return _buffer[0];
--_length;
_end = (_start + length()) % capacity();
return _buffer[_end];
}
/* can cause a call to memmove */
template<uint16_t Size, typename T>
const T* RingBuffer<Size, T>::buffer() {
if (!_is_continuous())
_make_continuous();
return _buffer + _start;
}
template<uint16_t Size, typename T>
bool RingBuffer<Size, T>::_is_continuous() const {
/* is empty */
bool res = empty();
/* content is somewhere in the middle of buffer */
res = res || _start < _end;
/* buffer has not cyclied yet */
res = res || _end == 0;
return res;
}
template<uint16_t Size, typename T>
void RingBuffer<Size, T>::_make_continuous() {
while (!_is_continuous()) {
T tmp = _buffer[0];
memmove(&(_buffer[0]), &(_buffer[1]), (_end - 1) * sizeof (T));
memmove(&(_buffer[_start - 1]), &(_buffer[_start]), (capacity() - _start) * sizeof (T));
_buffer[capacity() - 1] = tmp;
_start = (_start - 1) % capacity();
_end = (_start + length()) % capacity();
}
}
template<uint16_t Size>
uint16_t StringBuffer<Size>::append(const char* str) {
uint16_t count = 0;
size_t str_length = strlen(str);
for (uint16_t i = 0; i < str_length; ++i) {
if (!this->append(str[i])) {
return count;
}
++count;
}
return count;
}
template<uint16_t Size>
uint16_t StringBuffer<Size>::index_of(const char* substr, uint16_t offset) {
if (this->empty())
return StringBuffer<Size>::END;
size_t substr_length = strlen(substr);
if (substr_length + offset > this->length())
return StringBuffer<Size>::END;
for (uint16_t i = offset; i <= this->length() - substr_length; ++i) {
bool match = true;
for (uint16_t j = 0; j < substr_length && match; ++j) {
match = match && ((*this)[i + j] == substr[j]);
}
if (match)
return i;
}
return StringBuffer<Size>::END;
}
template<uint16_t Size>
bool StringBuffer<Size>::starts_with(const char* substr) {
return (this->index_of(substr) == 0);
}
template<uint16_t Size>
bool StringBuffer<Size>::pop_until(const char* substr) {
uint16_t offset = index_of(substr);
if (offset == StringBuffer<Size>::END)
return false;
this->pop_firsts(offset + strlen(substr));
return true;
}
template<uint16_t Size>
bool StringBuffer<Size>::pop_while(const char subchr) {
if (this->empty() || (*this)[0] != subchr)
return false;
while (!this->empty() && (*this)[0] == subchr)
this->pop_first();
return true;
}
#endif