Coverage Report

Created: 2026-11-10 14:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
src/buffer.c
Line
Count
Source
1
#include "prism/internal/buffer.h"
2
3
#include "prism/compiler/inline.h"
4
5
#include "prism/internal/allocator.h"
6
#include "prism/internal/assert.h"
7
#include "prism/internal/char.h"
8
#include "prism/internal/oom.h"
9
10
#include <stdarg.h>
11
#include <stdlib.h>
12
#include <string.h>
13
14
/**
15
 * Initialize a pm_buffer_t with the given capacity.
16
 */
17
void
18
25.5k
pm_buffer_init(pm_buffer_t *buffer, size_t capacity) {
19
25.5k
    buffer->length = 0;
20
25.5k
    buffer->capacity = capacity;
21
22
25.5k
    buffer->value = (char *) xmalloc(capacity);
23
25.5k
    if (buffer->value == NULL) 
PRISM_OOM0
();
  Branch (23:9): [True: 0, False: 25.5k]
24
25.5k
}
25
26
/**
27
 * Allocate and initialize a new buffer.
28
 */
29
pm_buffer_t *
30
13.6k
pm_buffer_new(void) {
31
13.6k
    pm_buffer_t *buffer = (pm_buffer_t *) xmalloc(sizeof(pm_buffer_t));
32
13.6k
    if (buffer == NULL) 
PRISM_OOM0
();
  Branch (32:9): [True: 0, False: 13.6k]
33
34
13.6k
    pm_buffer_init(buffer, 1024);
35
13.6k
    return buffer;
36
13.6k
}
37
38
/**
39
 * Return the value of the buffer.
40
 */
41
char *
42
13.2k
pm_buffer_value(const pm_buffer_t *buffer) {
43
13.2k
    return buffer->value;
44
13.2k
}
45
46
/**
47
 * Return the length of the buffer.
48
 */
49
size_t
50
21.6k
pm_buffer_length(const pm_buffer_t *buffer) {
51
21.6k
    return buffer->length;
52
21.6k
}
53
54
/**
55
 * Append the given amount of space to the buffer. Returns false when there is
56
 * nothing to write because the length is zero. A zero-initialized buffer has a
57
 * NULL value until its first allocation, and passing NULL to
58
 * memcpy/memset/memmove is undefined behavior even for zero bytes, so callers
59
 * must not write in the zero case.
60
 *
61
 * The value and capacity are only updated together once the allocation has
62
 * succeeded, so the buffer remains valid to free if PRISM_OOM unwinds.
63
 */
64
static PRISM_INLINE bool
65
65.3M
pm_buffer_append_length(pm_buffer_t *buffer, size_t length) {
66
65.3M
    if (length == 0) 
return 738k
false738k
;
  Branch (66:9): [True: 738k, False: 64.5M]
67
68
64.5M
    size_t next_length = buffer->length + length;
69
64.5M
    if (next_length < buffer->length) 
PRISM_OOM0
();
  Branch (69:9): [True: 0, False: 64.5M]
70
71
64.5M
    if (next_length > buffer->capacity) {
  Branch (71:9): [True: 131k, False: 64.4M]
72
131k
        size_t next_capacity = buffer->capacity == 0 ? 
115.1k
:
buffer->capacity116k
;
  Branch (72:32): [True: 15.1k, False: 116k]
73
74
277k
        while (next_length > next_capacity) {
  Branch (74:16): [True: 145k, False: 131k]
75
145k
            if (next_capacity > SIZE_MAX / 2) 
PRISM_OOM0
();
  Branch (75:17): [True: 0, False: 145k]
76
145k
            next_capacity *= 2;
77
145k
        }
78
79
131k
        char *next_value = xrealloc_sized(buffer->value, next_capacity, buffer->capacity);
80
131k
        if (next_value == NULL) 
PRISM_OOM0
();
  Branch (80:13): [True: 0, False: 131k]
81
82
131k
        buffer->value = next_value;
83
131k
        buffer->capacity = next_capacity;
84
131k
    }
85
86
64.5M
    buffer->length = next_length;
87
64.5M
    return true;
88
64.5M
}
89
90
/**
91
 * Append a generic pointer to memory to the buffer.
92
 */
93
static PRISM_INLINE void
94
60.1M
pm_buffer_append(pm_buffer_t *buffer, const void *source, size_t length) {
95
60.1M
    size_t cursor = buffer->length;
96
60.1M
    if (pm_buffer_append_length(buffer, length)) {
  Branch (96:9): [True: 59.5M, False: 656k]
97
59.5M
        memcpy(buffer->value + cursor, source, length);
98
59.5M
    }
99
60.1M
}
100
101
/**
102
 * Append the given amount of space as zeroes to the buffer.
103
 */
104
void
105
49.6k
pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length) {
106
49.6k
    size_t cursor = buffer->length;
107
49.6k
    if (pm_buffer_append_length(buffer, length)) {
  Branch (107:9): [True: 49.1k, False: 471]
108
49.1k
        memset(buffer->value + cursor, 0, length);
109
49.1k
    }
110
49.6k
}
111
112
/**
113
 * Append the given byte to the buffer the given number of times.
114
 */
115
void
116
304k
pm_buffer_append_repeated(pm_buffer_t *buffer, uint8_t value, size_t length) {
117
304k
    size_t cursor = buffer->length;
118
304k
    if (pm_buffer_append_length(buffer, length)) {
  Branch (118:9): [True: 222k, False: 81.6k]
119
222k
        memset(buffer->value + cursor, value, length);
120
222k
    }
121
304k
}
122
123
/**
124
 * Append a formatted string to the buffer.
125
 */
126
void
127
4.78M
pm_buffer_append_format(pm_buffer_t *buffer, const char *format, ...) {
128
4.78M
    va_list arguments;
129
4.78M
    va_start(arguments, format);
130
4.78M
    int result = vsnprintf(NULL, 0, format, arguments);
131
4.78M
    va_end(arguments);
132
133
4.78M
    if (result < 0) 
return0
;
  Branch (133:9): [True: 0, False: 4.78M]
134
4.78M
    size_t length = (size_t) (result + 1);
135
136
4.78M
    size_t cursor = buffer->length;
137
4.78M
    if (pm_buffer_append_length(buffer, length)) {
  Branch (137:9): [True: 4.78M, False: 0]
138
4.78M
        va_start(arguments, format);
139
4.78M
        vsnprintf(buffer->value + cursor, length, format, arguments);
140
4.78M
        va_end(arguments);
141
4.78M
        buffer->length--;
142
4.78M
    }
143
4.78M
}
144
145
/**
146
 * Append a string to the buffer.
147
 */
148
void
149
23.3M
pm_buffer_append_string(pm_buffer_t *buffer, const char *value, size_t length) {
150
23.3M
    pm_buffer_append(buffer, value, length);
151
23.3M
}
152
153
/**
154
 * Append a list of bytes to the buffer.
155
 */
156
void
157
288k
pm_buffer_append_bytes(pm_buffer_t *buffer, const uint8_t *value, size_t length) {
158
288k
    pm_buffer_append(buffer, (const char *) value, length);
159
288k
}
160
161
/**
162
 * Append a single byte to the buffer.
163
 */
164
void
165
31.3M
pm_buffer_append_byte(pm_buffer_t *buffer, uint8_t value) {
166
31.3M
    const void *source = &value;
167
31.3M
    pm_buffer_append(buffer, source, sizeof(uint8_t));
168
31.3M
}
169
170
/**
171
 * Append a 32-bit unsigned integer to the buffer as a variable-length integer.
172
 */
173
void
174
9.24M
pm_buffer_append_varuint(pm_buffer_t *buffer, uint32_t value) {
175
9.24M
    if (value < 128) {
  Branch (175:9): [True: 7.10M, False: 2.13M]
176
7.10M
        pm_buffer_append_byte(buffer, (uint8_t) value);
177
7.10M
    } else {
178
2.13M
        uint32_t n = value;
179
4.28M
        while (n >= 128) {
  Branch (179:16): [True: 2.14M, False: 2.13M]
180
2.14M
            pm_buffer_append_byte(buffer, (uint8_t) (n | 128));
181
2.14M
            n >>= 7;
182
2.14M
        }
183
2.13M
        pm_buffer_append_byte(buffer, (uint8_t) n);
184
2.13M
    }
185
9.24M
}
186
187
/**
188
 * Append a 32-bit signed integer to the buffer as a variable-length integer.
189
 */
190
void
191
13.6k
pm_buffer_append_varsint(pm_buffer_t *buffer, int32_t value) {
192
13.6k
    uint32_t unsigned_int = ((uint32_t)(value) << 1) ^ ((uint32_t)(value >> 31));
193
13.6k
    pm_buffer_append_varuint(buffer, unsigned_int);
194
13.6k
}
195
196
/**
197
 * Append a double to the buffer.
198
 */
199
void
200
2.59k
pm_buffer_append_double(pm_buffer_t *buffer, double value) {
201
2.59k
    const void *source = &value;
202
2.59k
    pm_buffer_append(buffer, source, sizeof(double));
203
2.59k
}
204
205
/**
206
 * Append a 64-bit unsigned integer to the buffer in native endian order.
207
 */
208
void
209
13.6k
pm_buffer_append_uint64(pm_buffer_t *buffer, uint64_t value) {
210
13.6k
    const void *source = &value;
211
13.6k
    pm_buffer_append(buffer, source, sizeof(uint64_t));
212
13.6k
}
213
214
/**
215
 * Append a unicode codepoint to the buffer.
216
 */
217
bool
218
551
pm_buffer_append_unicode_codepoint(pm_buffer_t *buffer, uint32_t value) {
219
551
    if (value <= 0x7F) {
  Branch (219:9): [True: 418, False: 133]
220
418
        pm_buffer_append_byte(buffer, (uint8_t) value); // 0xxxxxxx
221
418
        return true;
222
418
    } else 
if (133
value <= 0x7FF133
) {
  Branch (222:16): [True: 44, False: 89]
223
44
        uint8_t bytes[] = {
224
44
            (uint8_t) (0xC0 | ((value >> 6) & 0x3F)), // 110xxxxx
225
44
            (uint8_t) (0x80 | (value & 0x3F))         // 10xxxxxx
226
44
        };
227
228
44
        pm_buffer_append_bytes(buffer, bytes, 2);
229
44
        return true;
230
89
    } else if (value <= 0xFFFF) {
  Branch (230:16): [True: 64, False: 25]
231
64
        uint8_t bytes[] = {
232
64
            (uint8_t) (0xE0 | ((value >> 12) & 0x3F)), // 1110xxxx
233
64
            (uint8_t) (0x80 | ((value >> 6) & 0x3F)),  // 10xxxxxx
234
64
            (uint8_t) (0x80 | (value & 0x3F))          // 10xxxxxx
235
64
        };
236
237
64
        pm_buffer_append_bytes(buffer, bytes, 3);
238
64
        return true;
239
64
    } else 
if (25
value <= 0x10FFFF25
) {
  Branch (239:16): [True: 12, False: 13]
240
12
        uint8_t bytes[] = {
241
12
            (uint8_t) (0xF0 | ((value >> 18) & 0x3F)), // 11110xxx
242
12
            (uint8_t) (0x80 | ((value >> 12) & 0x3F)), // 10xxxxxx
243
12
            (uint8_t) (0x80 | ((value >> 6) & 0x3F)),  // 10xxxxxx
244
12
            (uint8_t) (0x80 | (value & 0x3F))          // 10xxxxxx
245
12
        };
246
247
12
        pm_buffer_append_bytes(buffer, bytes, 4);
248
12
        return true;
249
13
    } else {
250
13
        return false;
251
13
    }
252
551
}
253
254
/**
255
 * Append a slice of source code to the buffer.
256
 */
257
void
258
1.38M
pm_buffer_append_source(pm_buffer_t *buffer, const uint8_t *source, size_t length, pm_buffer_escaping_t escaping) {
259
5.83M
    for (size_t index = 0; index < length; 
index++4.45M
) {
  Branch (259:28): [True: 4.45M, False: 1.38M]
260
4.45M
        const uint8_t byte = source[index];
261
262
4.45M
        if ((byte <= 0x06) || 
(4.43M
byte >= 0x0E4.43M
&&
byte <= 0x1F4.25M
) ||
(byte >= 0x7F)4.42M
) {
  Branch (262:13): [True: 22.9k, False: 4.43M]
  Branch (262:32): [True: 4.25M, False: 172k]
  Branch (262:48): [True: 6.85k, False: 4.25M]
  Branch (262:65): [True: 125k, False: 4.30M]
263
154k
            if (escaping == PM_BUFFER_ESCAPING_RUBY) {
  Branch (263:17): [True: 106k, False: 48.6k]
264
106k
                pm_buffer_append_format(buffer, "\\x%02X", byte);
265
106k
            } else {
266
48.6k
                pm_buffer_append_format(buffer, "\\u%04X", byte);
267
48.6k
            }
268
4.30M
        } else {
269
4.30M
            switch (byte) {
270
504
                case '\a':
  Branch (270:17): [True: 504, False: 4.29M]
271
504
                    if (escaping == PM_BUFFER_ESCAPING_RUBY) {
  Branch (271:25): [True: 290, False: 214]
272
290
                        pm_buffer_append_string(buffer, "\\a", 2);
273
290
                    } else {
274
214
                        pm_buffer_append_format(buffer, "\\u%04X", byte);
275
214
                    }
276
504
                    break;
277
364
                case '\b':
  Branch (277:17): [True: 364, False: 4.29M]
278
364
                    pm_buffer_append_string(buffer, "\\b", 2);
279
364
                    break;
280
1.53k
                case '\t':
  Branch (280:17): [True: 1.53k, False: 4.29M]
281
1.53k
                    pm_buffer_append_string(buffer, "\\t", 2);
282
1.53k
                    break;
283
159k
                case '\n':
  Branch (283:17): [True: 159k, False: 4.14M]
284
159k
                    pm_buffer_append_string(buffer, "\\n", 2);
285
159k
                    break;
286
1.12k
                case '\v':
  Branch (286:17): [True: 1.12k, False: 4.29M]
287
1.12k
                    if (escaping == PM_BUFFER_ESCAPING_RUBY) {
  Branch (287:25): [True: 824, False: 304]
288
824
                        pm_buffer_append_string(buffer, "\\v", 2);
289
824
                    } else {
290
304
                        pm_buffer_append_format(buffer, "\\u%04X", byte);
291
304
                    }
292
1.12k
                    break;
293
720
                case '\f':
  Branch (293:17): [True: 720, False: 4.29M]
294
720
                    pm_buffer_append_string(buffer, "\\f", 2);
295
720
                    break;
296
8.42k
                case '\r':
  Branch (296:17): [True: 8.42k, False: 4.29M]
297
8.42k
                    pm_buffer_append_string(buffer, "\\r", 2);
298
8.42k
                    break;
299
49.9k
                case '"':
  Branch (299:17): [True: 49.9k, False: 4.25M]
300
49.9k
                    pm_buffer_append_string(buffer, "\\\"", 2);
301
49.9k
                    break;
302
28.2k
                case '#': {
  Branch (302:17): [True: 28.2k, False: 4.27M]
303
28.2k
                    if (escaping == PM_BUFFER_ESCAPING_RUBY && 
index + 1 < length21.8k
) {
  Branch (303:25): [True: 21.8k, False: 6.42k]
  Branch (303:64): [True: 19.4k, False: 2.35k]
304
19.4k
                        const uint8_t next_byte = source[index + 1];
305
19.4k
                        if (next_byte == '{' || 
next_byte == '@'9.93k
||
next_byte == '$'7.77k
) {
  Branch (305:29): [True: 9.53k, False: 9.93k]
  Branch (305:49): [True: 2.15k, False: 7.77k]
  Branch (305:69): [True: 641, False: 7.13k]
306
12.3k
                            pm_buffer_append_byte(buffer, '\\');
307
12.3k
                        }
308
19.4k
                    }
309
310
28.2k
                    pm_buffer_append_byte(buffer, '#');
311
28.2k
                    break;
312
0
                }
313
43.8k
                case '\\':
  Branch (313:17): [True: 43.8k, False: 4.25M]
314
43.8k
                    pm_buffer_append_string(buffer, "\\\\", 2);
315
43.8k
                    break;
316
4.00M
                default:
  Branch (316:17): [True: 4.00M, False: 294k]
317
4.00M
                    pm_buffer_append_byte(buffer, byte);
318
4.00M
                    break;
319
4.30M
            }
320
4.30M
        }
321
4.45M
    }
322
1.38M
}
323
324
/**
325
 * Prepend the given string to the buffer.
326
 */
327
void
328
pm_buffer_prepend_string(pm_buffer_t *buffer, const char *value, size_t length) {
329
    size_t cursor = buffer->length;
330
    if (pm_buffer_append_length(buffer, length)) {
331
        memmove(buffer->value + length, buffer->value, cursor);
332
        memcpy(buffer->value, value, length);
333
    }
334
}
335
336
/**
337
 * Concatenate one buffer onto another.
338
 */
339
void
340
5.11M
pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source) {
341
5.11M
    if (source->length > 0) {
  Branch (341:9): [True: 5.08M, False: 27.3k]
342
5.08M
        pm_buffer_append(destination, source->value, source->length);
343
5.08M
    }
344
5.11M
}
345
346
/**
347
 * Clear the buffer by reducing its size to 0. This does not free the allocated
348
 * memory, but it does allow the buffer to be reused.
349
 */
350
void
351
53.3k
pm_buffer_clear(pm_buffer_t *buffer) {
352
53.3k
    buffer->length = 0;
353
53.3k
}
354
355
/**
356
 * Strip the whitespace from the end of the buffer.
357
 */
358
void
359
pm_buffer_rstrip(pm_buffer_t *buffer) {
360
    while (buffer->length > 0 && pm_char_is_whitespace((uint8_t) buffer->value[buffer->length - 1])) {
361
        buffer->length--;
362
    }
363
}
364
365
/**
366
 * Checks if the buffer includes the given value.
367
 */
368
size_t
369
26
pm_buffer_index(const pm_buffer_t *buffer, char value) {
370
26
    const char *first = memchr(buffer->value, value, buffer->length);
371
26
    return (first == NULL) ? SIZE_MAX : 
(size_t) (first - buffer->value)0
;
  Branch (371:12): [True: 26, False: 0]
372
26
}
373
374
/**
375
 * Insert the given string into the buffer at the given index.
376
 */
377
void
378
13
pm_buffer_insert(pm_buffer_t *buffer, size_t index, const char *value, size_t length) {
379
13
    PRISM_ASSERT(index <= buffer->length);
  Branch (379:5): [True: 0, False: 13]
  Branch (379:5): [True: 13, False: 0]
380
381
13
    if (index == buffer->length) {
  Branch (381:9): [True: 13, False: 0]
382
13
        pm_buffer_append_string(buffer, value, length);
383
13
    } else {
384
0
        pm_buffer_append_zeroes(buffer, length);
385
0
        memmove(buffer->value + index + length, buffer->value + index, buffer->length - length - index);
386
0
        memcpy(buffer->value + index, value, length);
387
0
    }
388
13
}
389
390
/**
391
 * Free the memory held by the buffer.
392
 */
393
void
394
61.6k
pm_buffer_cleanup(pm_buffer_t *buffer) {
395
61.6k
    xfree_sized(buffer->value, buffer->capacity);
396
61.6k
}
397
398
/**
399
 * Free both the memory held by the buffer and the buffer itself.
400
 */
401
void
402
13.6k
pm_buffer_free(pm_buffer_t *buffer) {
403
13.6k
    pm_buffer_cleanup(buffer);
404
13.6k
    xfree_sized(buffer, sizeof(pm_buffer_t));
405
13.6k
}