Ruby 4.1.0dev (2026-03-27 revision 6d4681d2262bc50a46e4811b11ca52e6bf6d0728)
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 a formatted string to the buffer. */
35void pm_buffer_append_format(pm_buffer_t *buffer, const char *format, ...) PRISM_ATTRIBUTE_FORMAT(2, 3);
36
37/* Append a string to the buffer. */
38void pm_buffer_append_string(pm_buffer_t *buffer, const char *value, size_t length);
39
40/* Append a list of bytes to the buffer. */
41void pm_buffer_append_bytes(pm_buffer_t *buffer, const uint8_t *value, size_t length);
42
43/* Append a single byte to the buffer. */
44void pm_buffer_append_byte(pm_buffer_t *buffer, uint8_t value);
45
46/* Append a 32-bit unsigned integer to the buffer as a variable-length integer. */
47void pm_buffer_append_varuint(pm_buffer_t *buffer, uint32_t value);
48
49/* Append a 32-bit signed integer to the buffer as a variable-length integer. */
50void pm_buffer_append_varsint(pm_buffer_t *buffer, int32_t value);
51
52/* Append a double to the buffer. */
53void pm_buffer_append_double(pm_buffer_t *buffer, double value);
54
55/* Append a unicode codepoint to the buffer. */
56bool pm_buffer_append_unicode_codepoint(pm_buffer_t *buffer, uint32_t value);
57
58/*
59 * The different types of escaping that can be performed by the buffer when
60 * appending a slice of Ruby source code.
61 */
62typedef enum {
63 PM_BUFFER_ESCAPING_RUBY,
64 PM_BUFFER_ESCAPING_JSON
65} pm_buffer_escaping_t;
66
67/* Append a slice of source code to the buffer. */
68void pm_buffer_append_source(pm_buffer_t *buffer, const uint8_t *source, size_t length, pm_buffer_escaping_t escaping);
69
70/* Prepend the given string to the buffer. */
71void pm_buffer_prepend_string(pm_buffer_t *buffer, const char *value, size_t length);
72
73/* Concatenate one buffer onto another. */
74void pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source);
75
76/*
77 * Clear the buffer by reducing its size to 0. This does not free the allocated
78 * memory, but it does allow the buffer to be reused.
79 */
80void pm_buffer_clear(pm_buffer_t *buffer);
81
82/* Strip the whitespace from the end of the buffer. */
83void pm_buffer_rstrip(pm_buffer_t *buffer);
84
85/* Checks if the buffer includes the given value. */
86size_t pm_buffer_index(const pm_buffer_t *buffer, char value);
87
88/* Insert the given string into the buffer at the given index. */
89void pm_buffer_insert(pm_buffer_t *buffer, size_t index, const char *value, size_t length);
90
91#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:17
C99 shim for <stdbool.h>