-
Notifications
You must be signed in to change notification settings - Fork 202
/
buffer.h
166 lines (136 loc) · 4.17 KB
/
buffer.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
#pragma once
#include <string.h>
#include <stdarg.h>
#include <stdio.h> // vsnprintf
NAMESPACE_BEGIN(NB_NAMESPACE)
NAMESPACE_BEGIN(detail)
struct Buffer {
public:
// Disable copy/move constructor and assignment
Buffer(const Buffer &) = delete;
Buffer(Buffer &&) = delete;
Buffer &operator=(const Buffer &) = delete;
Buffer &operator=(Buffer &&) = delete;
Buffer(size_t size = 0) : m_start((char *) malloc(size)) {
if (!m_start) {
fprintf(stderr, "Buffer::Buffer(): out of memory (unrecoverable error)!");
abort();
}
m_end = m_start + size;
if (size)
clear();
}
~Buffer() {
free(m_start);
}
/// Append a string with the specified length
void put(const char *str, size_t size) {
if (m_cur + size >= m_end)
expand(size + 1 - remain());
memcpy(m_cur, str, size);
m_cur += size;
*m_cur = '\0';
}
/// Append a string
template <size_t N> void put(const char (&str)[N]) {
put(str, N - 1);
}
/// Append a dynamic string
void put_dstr(const char *str) { put(str, strlen(str)); }
/// Append a single character to the buffer
void put(char c) {
if (m_cur + 1 >= m_end)
expand();
*m_cur++ = c;
*m_cur = '\0';
}
/// Append multiple copies of a single character to the buffer
void put(char c, size_t count) {
if (m_cur + count >= m_end)
expand(count + 1 - remain());
for (size_t i = 0; i < count; ++i)
*m_cur++ = c;
*m_cur = '\0';
}
/// Append a formatted (printf-style) string to the buffer
#if defined(__GNUC__)
__attribute__((__format__ (__printf__, 2, 3)))
#endif
size_t fmt(const char *format, ...) {
size_t written;
do {
size_t size = remain();
va_list args;
va_start(args, format);
written = (size_t) vsnprintf(m_cur, size, format, args);
va_end(args);
if (written + 1 < size) {
m_cur += written;
break;
}
expand();
} while (true);
return written;
}
const char *get() { return m_start; }
void clear() {
m_cur = m_start;
if (m_start != m_end)
m_start[0] = '\0';
}
/// Remove the last 'n' characters
void rewind(size_t n) {
if (m_cur < m_start + n)
m_cur = m_start;
else
m_cur -= n;
*m_cur = '\0';
}
/// Append an unsigned 32 bit integer
void put_uint32(uint32_t value) {
const int digits = 10;
const char *num = "0123456789";
char buf[digits];
size_t i = digits;
do {
buf[--i] = num[value % 10];
value /= 10;
} while (value);
return put(buf + i, digits - i);
}
char *copy(size_t offset = 0) const {
size_t copy_size = size() + 1 - offset;
char *tmp = (char *) malloc(copy_size);
if (!tmp) {
fprintf(stderr, "Buffer::copy(): out of memory (unrecoverable error)!");
abort();
}
memcpy(tmp, m_start + offset, copy_size);
return tmp;
}
size_t size() const { return (size_t) (m_cur - m_start); }
size_t remain() const { return (size_t) (m_end - m_cur); }
private:
NB_NOINLINE void expand(size_t minval = 2) {
size_t old_alloc_size = m_end - m_start,
new_alloc_size = 2 * old_alloc_size + minval,
used_size = (size_t) (m_cur - m_start),
copy_size = used_size + 1;
if (old_alloc_size < copy_size)
copy_size = old_alloc_size;
char *tmp = (char *) malloc(new_alloc_size);
if (!tmp) {
fprintf(stderr, "Buffer::expand(): out of memory (unrecoverable error)!");
abort();
}
memcpy(tmp, m_start, copy_size);
free(m_start);
m_start = tmp;
m_end = m_start + new_alloc_size;
m_cur = m_start + used_size;
}
private:
char *m_start{nullptr}, *m_cur{nullptr}, *m_end{nullptr};
};
NAMESPACE_END(detail)
NAMESPACE_END(NB_NAMESPACE)