-
Notifications
You must be signed in to change notification settings - Fork 40
/
buffer.c
353 lines (285 loc) · 6.05 KB
/
buffer.c
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//
// buffer.c
//
// Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca>
//
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include "buffer.h"
// TODO: shared with reference counting
// TODO: linked list for append/prepend etc
/*
* Compute the nearest multiple of `a` from `b`.
*/
#define nearest_multiple_of(a, b) \
(((b) + ((a) - 1)) & ~((a) - 1))
/*
* Allocate a new buffer with BUFFER_DEFAULT_SIZE.
*/
buffer_t *
buffer_new() {
return buffer_new_with_size(BUFFER_DEFAULT_SIZE);
}
/*
* Allocate a new buffer with `n` bytes.
*/
buffer_t *
buffer_new_with_size(size_t n) {
buffer_t *self = malloc(sizeof(buffer_t));
if (!self) return NULL;
self->len = n;
self->data = self->alloc = calloc(n + 1, 1);
return self;
}
/*
* Allocate a new buffer with `str`.
*/
buffer_t *
buffer_new_with_string(char *str) {
return buffer_new_with_string_length(str, strlen(str));
}
/*
* Allocate a new buffer with `str` and `len`.
*/
buffer_t *
buffer_new_with_string_length(char *str, size_t len) {
buffer_t *self = malloc(sizeof(buffer_t));
if (!self) return NULL;
self->len = len;
self->data = self->alloc = str;
return self;
}
/*
* Allocate a new buffer with a copy of `str`.
*/
buffer_t *
buffer_new_with_copy(char *str) {
size_t len = strlen(str);
buffer_t *self = buffer_new_with_size(len);
if (!self) return NULL;
memcpy(self->alloc, str, len);
self->data = self->alloc;
return self;
}
/*
* Deallocate excess memory, the number
* of bytes removed or -1.
*/
ssize_t
buffer_compact(buffer_t *self) {
size_t len = buffer_length(self);
size_t rem = self->len - len;
char *buf = calloc(len + 1, 1);
if (!buf) return -1;
memcpy(buf, self->data, len);
free(self->alloc);
self->len = len;
self->data = self->alloc = buf;
return rem;
}
/*
* Free the buffer.
*/
void
buffer_free(buffer_t *self) {
free(self->alloc);
free(self);
}
/*
* Return buffer size.
*/
size_t
buffer_size(buffer_t *self) {
return self->len;
}
/*
* Return string length.
*/
size_t
buffer_length(buffer_t *self) {
return strlen(self->data);
}
/*
* Resize to hold `n` bytes.
*/
int
buffer_resize(buffer_t *self, size_t n) {
n = nearest_multiple_of(1024, n);
self->len = n;
self->alloc = self->data = realloc(self->alloc, n + 1);
if (!self->alloc) return -1;
self->alloc[n] = '\0';
return 0;
}
/*
* Append a printf-style formatted string to the buffer.
*/
int buffer_appendf(buffer_t *self, const char *format, ...) {
va_list ap;
va_list tmpa;
char *dst = NULL;
int length = 0;
int required = 0;
int bytes = 0;
va_start(ap, format);
length = buffer_length(self);
// First, we compute how many bytes are needed
// for the formatted string and allocate that
// much more space in the buffer.
va_copy(tmpa, ap);
required = vsnprintf(NULL, 0, format, tmpa);
va_end(tmpa);
if (-1 == buffer_resize(self, length + required)) {
va_end(ap);
return -1;
}
// Next format the string into the space that we
// have made room for.
dst = self->data + length;
bytes = vsnprintf(dst, 1 + required, format, ap);
va_end(ap);
return bytes < 0
? -1
: 0;
}
/*
* Append `str` to `self` and return 0 on success, -1 on failure.
*/
int
buffer_append(buffer_t *self, const char *str) {
return buffer_append_n(self, str, strlen(str));
}
/*
* Append the first `len` bytes from `str` to `self` and
* return 0 on success, -1 on failure.
*/
int
buffer_append_n(buffer_t *self, const char *str, size_t len) {
size_t prev = strlen(self->data);
size_t needed = len + prev;
// enough space
if (self->len > needed) {
strncat(self->data, str, len);
return 0;
}
// resize
int ret = buffer_resize(self, needed);
if (-1 == ret) return -1;
strncat(self->data, str, len);
return 0;
}
/*
* Prepend `str` to `self` and return 0 on success, -1 on failure.
*/
int
buffer_prepend(buffer_t *self, char *str) {
size_t len = strlen(str);
size_t prev = strlen(self->data);
size_t needed = len + prev;
// enough space
if (self->len > needed) goto move;
// resize
int ret = buffer_resize(self, needed);
if (-1 == ret) return -1;
// move
move:
memmove(self->data + len, self->data, prev + 1);
memcpy(self->data, str, len);
return 0;
}
/*
* Return a new buffer based on the `from..to` slice of `buf`,
* or NULL on error.
*/
buffer_t *
buffer_slice(buffer_t *buf, size_t from, ssize_t to) {
size_t len = strlen(buf->data);
// bad range
if (to < from) return NULL;
// relative to end
if (to < 0) to = len - ~to;
// cap end
if (to > len) to = len;
size_t n = to - from;
buffer_t *self = buffer_new_with_size(n);
memcpy(self->data, buf->data + from, n);
return self;
}
/*
* Return 1 if the buffers contain equivalent data.
*/
int
buffer_equals(buffer_t *self, buffer_t *other) {
return 0 == strcmp(self->data, other->data);
}
/*
* Return the index of the substring `str`, or -1 on failure.
*/
ssize_t
buffer_indexof(buffer_t *self, char *str) {
char *sub = strstr(self->data, str);
if (!sub) return -1;
return sub - self->data;
}
/*
* Trim leading whitespace.
*/
void
buffer_trim_left(buffer_t *self) {
int c;
while ((c = *self->data) && isspace(c)) {
++self->data;
}
}
/*
* Trim trailing whitespace.
*/
void
buffer_trim_right(buffer_t *self) {
int c;
size_t i = buffer_length(self) - 1;
while ((c = self->data[i]) && isspace(c)) {
self->data[i--] = 0;
}
}
/*
* Trim trailing and leading whitespace.
*/
void
buffer_trim(buffer_t *self) {
buffer_trim_left(self);
buffer_trim_right(self);
}
/*
* Fill the buffer with `c`.
*/
void
buffer_fill(buffer_t *self, int c) {
memset(self->data, c, self->len);
}
/*
* Fill the buffer with 0.
*/
void
buffer_clear(buffer_t *self) {
buffer_fill(self, 0);
}
/*
* Print a hex dump of the buffer.
*/
void
buffer_print(buffer_t *self) {
int i;
size_t len = self->len;
printf("\n ");
// hex
for (i = 0; i < len; ++i) {
printf(" %02x", self->alloc[i]);
if ((i + 1) % 8 == 0) printf("\n ");
}
printf("\n");
}