-
Notifications
You must be signed in to change notification settings - Fork 2
/
buffer.c
262 lines (211 loc) · 7.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
#include <assert.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "buffer.h"
void range_skip(range_t *range, size_t amount) {
assert(range->size >= amount && "invalid skip");
range->size -= amount;
range->ptr += amount;
}
void range_shorten(range_t *range, size_t amount) {
assert(range->size >= amount && "invalid amount");
range->size -= amount;
}
range_t range_subs(range_t *range, size_t size) {
assert(range->size >= size && "invalid size");
return (range_t){.ptr = range->ptr, .size = size};
}
range_t range_sub(range_t *range, size_t start, size_t size) {
assert(range->size > start && "invalid start");
assert(range->size - start >= size && "invalid size");
return (range_t){.ptr = range->ptr + start, .size = size};
}
// Please use the RANGE_STRING if the string is a constant.
range_t range_fromString(char *string) {
return (range_t){.ptr = string, .size = strlen(string)};
}
int range_cmp(range_t a, range_t b) {
if (a.size != b.size)
return 0;
return memcmp(a.ptr, b.ptr, a.size) == 0;
}
range_t range_copy(range_t *range) {
char *buffer = dmalloc(range->size);
return (range_t){.ptr = buffer, .size = range->size};
}
int range_parseInt(range_t range) {
assert(range.size != 0 && "Empty range");
// Signed int overflow is undefined behaviour.
long result = 0;
int sign = 1;
if (*range.ptr == '-') {
range_skip(&range, 1);
sign = -1;
} else if (*range.ptr == '+') {
range_skip(&range, 1);
}
for (int i = 0; i < range.size; i++) {
result *= 10;
result += range.ptr[i] - '0';
if (sign == 1 && result >= INT_MAX)
break;
else if (sign == -1 && result - 1 >= INT_MAX)
break;
// FIXME: Error handling.
}
return (int)(sign == -1 ? -result : result);
}
void position_ingest(position_t *position, char c) {
position->offset++;
if (c == '\n') {
position->line++;
position->col = 0;
} else if (c != '\r') {
position->col++;
}
}
void position_dump(position_t *position) {
printf("Line: %u, Column: %u, Offset: %u\n", position->line, position->col,
position->offset);
}
void dbuffer_write(dbuffer_t *dbuffer, FILE *file) {
fwrite(dbuffer->buffer, 1, dbuffer->usage, file);
}
void dbuffer_initSize(dbuffer_t *dbuffer, size_t size) {
dbuffer->usage = 0;
dbuffer->capacity = size;
dbuffer->buffer = dmalloc(size);
}
void dbuffer_init(dbuffer_t *dbuffer) {
dbuffer_initSize(dbuffer, DBUFFER_INIT);
}
#define max(a, b) ((a) > (b) ? (a) : (b))
void dbuffer_ensureCap(dbuffer_t *dbuffer, size_t size) {
if (size > dbuffer->capacity - dbuffer->usage) {
size_t newSize =
max(dbuffer->capacity * 2, dbuffer->capacity * 3 / 2 + size);
void *newBuffer = dmalloc(newSize);
memcpy(newBuffer, dbuffer->buffer, dbuffer->usage);
free(dbuffer->buffer);
dbuffer->buffer = newBuffer;
}
}
void dbuffer_removeRange(dbuffer_t *dbuffer, size_t offset, size_t size) {
assert(dbuffer->usage >= size && "Invalid size");
assert(offset < dbuffer->usage && "Invalid offset");
size_t offsetEnd = offset + size;
// Move memory that comes after the range to the begining.
memmove(dbuffer->buffer + offset, dbuffer->buffer + offsetEnd,
dbuffer->usage - offsetEnd);
// Reduce the usage since we removed @size bytes.
dbuffer->usage -= size;
}
void dbuffer_pushData(dbuffer_t *dbuffer, void *buffer, size_t size) {
dbuffer_ensureCap(dbuffer, size);
memcpy(dbuffer->buffer + dbuffer->usage, buffer, size);
dbuffer->usage += size;
}
void dbuffer_pushStr(dbuffer_t *dbuffer, void *str) {
size_t size = strlen(str);
dbuffer_pushData(dbuffer, str, size);
}
void dbuffer_pushChar(dbuffer_t *dbuffer, unsigned char ch) {
dbuffer_pushData(dbuffer, &ch, 1);
}
void dbuffer_pushLong(dbuffer_t *dbuffer, unsigned long num, size_t bytes) {
for (int i = 0; i < bytes; i++) {
dbuffer_pushChar(dbuffer, num & 0xFF);
num = num >> 8;
}
}
// Push litle endian bytes
void dbuffer_pushInt(dbuffer_t *dbuffer, unsigned int num, size_t bytes) {
assert(bytes <= sizeof(size_t) && "invalid int size");
// num = num << (4 - bytes) * 8;
for (int i = 0; i < bytes; i++) {
dbuffer_pushChar(dbuffer, num & 0xFF);
num = num >> 8;
}
}
void dbuffer_push(dbuffer_t *dbuffer, size_t bytes, ...) {
va_list list;
va_start(list, bytes);
unsigned char buffer[bytes];
for (size_t i = 0; i < bytes; i++)
buffer[i] = (unsigned char)va_arg(list, unsigned int);
dbuffer_pushData(dbuffer, buffer, bytes);
va_end(list);
}
void dbuffer_pushChars(dbuffer_t *dbuffer, char c, size_t size) {
dbuffer_ensureCap(dbuffer, size);
memset(dbuffer->buffer + dbuffer->usage, c, size);
dbuffer->usage += size;
}
void dbuffer_pushRange(dbuffer_t *dbuffer, range_t *range) {
dbuffer_pushData(dbuffer, range->ptr, range->size);
}
void dbuffer_pushPtr(dbuffer_t *dbuffer, void *pointer) {
dbuffer_ensureCap(dbuffer, sizeof(void *));
dbuffer_pushData(dbuffer, &pointer, sizeof(void *));
}
void dbuffer_popPtr(dbuffer_t *dbuffer) { dbuffer->usage -= sizeof(void *); }
void *dbuffer_getLastPtr(dbuffer_t *dbuffer) {
return ((void **)dbuffer->buffer)[dbuffer->usage / sizeof(void *) - 1];
}
void dbuffer_pushUIntHexStr(dbuffer_t *dbuffer, unsigned int number) {
size_t size = 0;
do {
char f = number % 16;
if (f < 10)
f += '0';
else
f += 'A';
dbuffer_pushChar(dbuffer, '0' + number % 10);
number /= 10;
size++;
} while (number != 0);
char *begin = dbuffer->buffer + dbuffer->usage - size;
for (size_t i = 0, count = size / 2; i < count; i++) {
char tmp = begin[i];
begin[i] = begin[size - i - 1];
begin[size - i - 1] = tmp;
}
}
void dbuffer_pushUIntStr(dbuffer_t *dbuffer, unsigned int number) {
size_t size = 0;
do {
dbuffer_pushChar(dbuffer, '0' + number % 10);
number /= 10;
size++;
} while (number != 0);
char *begin = dbuffer->buffer + dbuffer->usage - size;
for (size_t i = 0, count = size / 2; i < count; i++) {
char tmp = begin[i];
begin[i] = begin[size - i - 1];
begin[size - i - 1] = tmp;
}
}
void dbuffer_pushIntStr(dbuffer_t *dbuffer, int number) {
if (number < 0) {
dbuffer_pushChar(dbuffer, '-');
number = -number;
}
dbuffer_pushUIntStr(dbuffer, (unsigned int)number);
}
range_t dbuffer_asRange(dbuffer_t *dbuffer) {
return (range_t){.ptr = dbuffer->buffer, .size = dbuffer->usage};
}
void **dbuffer_asPtrArray(dbuffer_t *dbuffer, size_t *size) {
*size = dbuffer->usage / sizeof(void *);
return (void **)dbuffer->buffer;
}
void dbuffer_swap(dbuffer_t *a, dbuffer_t *b) {
dbuffer_t bk = *a;
*a = *b;
*b = bk;
}
void dbuffer_clear(dbuffer_t *dbuffer) { dbuffer->usage = 0; }
void dbuffer_free(dbuffer_t *dbuffer) { free(dbuffer->buffer); }