Ruby 4.1.0dev (2026-11-10 revision d9bbf7af11227361911849907728f055a9e49db0)
buffer.h
1#ifndef PRISM_INTERNAL_BUFFER_H
2#define PRISM_INTERNAL_BUFFER_H
3
5
6#include "prism/buffer.h"
7
8#include <stdbool.h>
9#include <stdint.h>
10
11/*
12 * A simple memory buffer that stores data in a contiguous block of memory.
13 */
15 /* The length of the buffer in bytes. */
16 size_t length;
17
18 /* The capacity of the buffer in bytes that has been allocated. */
19 size_t capacity;
20
21 /* A pointer to the start of the buffer. */
22 char *value;
23};
24
25/* Initialize a pm_buffer_t with the given capacity. */
26void pm_buffer_init(pm_buffer_t *buffer, size_t capacity);
27
28/* Free the memory held by the buffer. */
29void pm_buffer_cleanup(pm_buffer_t *buffer);
30
31/* Append the given amount of space as zeroes to the buffer. */
32void pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length);
33
34/* Append the given byte to the buffer the given number of times. */
35void pm_buffer_append_repeated(pm_buffer_t *buffer, uint8_t value, size_t length);
36
37/* Append a formatted string to the buffer. */
38void pm_buffer_append_format(pm_buffer_t *buffer, const char *format, ...) PRISM_ATTRIBUTE_FORMAT(2, 3);
39
40/* Append a string to the buffer. */
41void pm_buffer_append_string(pm_buffer_t *buffer, const char *value, size_t length);
42
43/*
44 * Append a string literal to the buffer, with its length computed at compile
45 * time. Concatenating with "" means anything other than a string literal fails
46 * to compile, instead of having its pointer size taken as its length.
47 */
48#define PM_BUFFER_APPEND_LITERAL(buffer, literal) pm_buffer_append_string((buffer), "" literal, sizeof(literal) - 1)
49
50/* Append a list of bytes to the buffer. */
51void pm_buffer_append_bytes(pm_buffer_t *buffer, const uint8_t *value, size_t length);
52
53/* Append a single byte to the buffer. */
54void pm_buffer_append_byte(pm_buffer_t *buffer, uint8_t value);
55
56/* Append a 32-bit unsigned integer to the buffer as a variable-length integer. */
57void pm_buffer_append_varuint(pm_buffer_t *buffer, uint32_t value);
58
59/* Append a 32-bit signed integer to the buffer as a variable-length integer. */
60void pm_buffer_append_varsint(pm_buffer_t *buffer, int32_t value);
61
62/* Append a double to the buffer. */
63void pm_buffer_append_double(pm_buffer_t *buffer, double value);
64
65/* Append a 64-bit unsigned integer to the buffer in native endian order. */
66void pm_buffer_append_uint64(pm_buffer_t *buffer, uint64_t value);
67
68/* Append a unicode codepoint to the buffer. */
69bool pm_buffer_append_unicode_codepoint(pm_buffer_t *buffer, uint32_t value);
70
71/*
72 * The different types of escaping that can be performed by the buffer when
73 * appending a slice of Ruby source code.
74 */
75typedef enum {
76 PM_BUFFER_ESCAPING_RUBY,
77 PM_BUFFER_ESCAPING_JSON
78} pm_buffer_escaping_t;
79
80/* Append a slice of source code to the buffer. */
81void pm_buffer_append_source(pm_buffer_t *buffer, const uint8_t *source, size_t length, pm_buffer_escaping_t escaping);
82
83/* Prepend the given string to the buffer. */
84void pm_buffer_prepend_string(pm_buffer_t *buffer, const char *value, size_t length);
85
86/* Concatenate one buffer onto another. */
87void pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source);
88
89/*
90 * Clear the buffer by reducing its size to 0. This does not free the allocated
91 * memory, but it does allow the buffer to be reused.
92 */
93void pm_buffer_clear(pm_buffer_t *buffer);
94
95/* Strip the whitespace from the end of the buffer. */
96void pm_buffer_rstrip(pm_buffer_t *buffer);
97
98/* Checks if the buffer includes the given value. */
99size_t pm_buffer_index(const pm_buffer_t *buffer, char value);
100
101/* Insert the given string into the buffer at the given index. */
102void pm_buffer_insert(pm_buffer_t *buffer, size_t index, const char *value, size_t length);
103
104#endif
A wrapper around a contiguous block of allocated memory.
#define PRISM_ATTRIBUTE_FORMAT(fmt_idx_, arg_idx_)
Certain compilers support specifying that a function accepts variadic parameters that look like print...
Definition format.h:19
C99 shim for <stdbool.h>