Ruby 4.1.0dev (2026-11-25 revision f46d41a54ce53b8bc6d86f3571999bdc5075fee2)
buffer.h
1#ifndef PRISM_INTERNAL_BUFFER_H
2#define PRISM_INTERNAL_BUFFER_H
3
4#include "prism/buffer.h"
5
6#include <stdbool.h>
7#include <stdint.h>
8
9/*
10 * A simple memory buffer that stores data in a contiguous block of memory.
11 */
13 /* The length of the buffer in bytes. */
14 size_t length;
15
16 /* The capacity of the buffer in bytes that has been allocated. */
17 size_t capacity;
18
19 /* A pointer to the start of the buffer. */
20 char *value;
21};
22
23/* Initialize a pm_buffer_t with the given capacity. */
24void pm_buffer_init(pm_buffer_t *buffer, size_t capacity);
25
26/* Free the memory held by the buffer. */
27void pm_buffer_cleanup(pm_buffer_t *buffer);
28
29/* Append the given amount of space as zeroes to the buffer. */
30void pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length);
31
32/* Append the given byte to the buffer the given number of times. */
33void pm_buffer_append_repeated(pm_buffer_t *buffer, uint8_t value, size_t length);
34
35/* Append the decimal digits of an unsigned integer without leading zeros. */
36void pm_buffer_append_decimal(pm_buffer_t *buffer, uint64_t value);
37
38/* Append uppercase hexadecimal digits, padded with zeros to the given width. */
39void pm_buffer_append_hexadecimal(pm_buffer_t *buffer, uint32_t value, uint8_t width);
40
41/* Write both uppercase hexadecimal digits of a byte. */
42void pm_buffer_hexadecimal_byte_digits(uint8_t byte, char digits[2]);
43
44/* Append a byte as a \xHH escape. */
45void pm_buffer_append_byte_escape(pm_buffer_t *buffer, uint8_t byte);
46
47/* Return the decimal width of a line number, including its sign. */
48uint8_t pm_buffer_line_number_width(int32_t line);
49
50/* Write a line number at the end of digits and return its length. */
51uint8_t pm_buffer_line_number_digits(int32_t line, char digits[11]);
52
53/* Append a line number, padded on the left with spaces to the given width. */
54void pm_buffer_append_line_number(pm_buffer_t *buffer, int32_t line, uint8_t width);
55
56/* Append a string to the buffer. */
57void pm_buffer_append_string(pm_buffer_t *buffer, const char *value, size_t length);
58
59/*
60 * Append a string literal to the buffer, with its length computed at compile
61 * time. Concatenating with "" means anything other than a string literal fails
62 * to compile, instead of having its pointer size taken as its length.
63 */
64#define PM_BUFFER_APPEND_LITERAL(buffer, literal) pm_buffer_append_string((buffer), "" literal, sizeof(literal) - 1)
65
66/* Append a list of bytes to the buffer. */
67void pm_buffer_append_bytes(pm_buffer_t *buffer, const uint8_t *value, size_t length);
68
69/* Append a single byte to the buffer. */
70void pm_buffer_append_byte(pm_buffer_t *buffer, uint8_t value);
71
72/* Append a 32-bit unsigned integer to the buffer as a variable-length integer. */
73void pm_buffer_append_varuint(pm_buffer_t *buffer, uint32_t value);
74
75/* Append a 32-bit signed integer to the buffer as a variable-length integer. */
76void pm_buffer_append_varsint(pm_buffer_t *buffer, int32_t value);
77
78/* Append a double to the buffer. */
79void pm_buffer_append_double(pm_buffer_t *buffer, double value);
80
81/* Append a 64-bit unsigned integer to the buffer in native endian order. */
82void pm_buffer_append_uint64(pm_buffer_t *buffer, uint64_t value);
83
84/* Append a unicode codepoint to the buffer. */
85bool pm_buffer_append_unicode_codepoint(pm_buffer_t *buffer, uint32_t value);
86
87/*
88 * The different types of escaping that can be performed by the buffer when
89 * appending a slice of Ruby source code.
90 */
91typedef enum {
92 PM_BUFFER_ESCAPING_RUBY,
93 PM_BUFFER_ESCAPING_JSON
94} pm_buffer_escaping_t;
95
96/* Append a slice of source code to the buffer. */
97void pm_buffer_append_source(pm_buffer_t *buffer, const uint8_t *source, size_t length, pm_buffer_escaping_t escaping);
98
99/* Concatenate one buffer onto another. */
100void pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source);
101
102/*
103 * Clear the buffer by reducing its size to 0. This does not free the allocated
104 * memory, but it does allow the buffer to be reused.
105 */
106void pm_buffer_clear(pm_buffer_t *buffer);
107
108/* Strip the whitespace from the end of the buffer. */
109void pm_buffer_rstrip(pm_buffer_t *buffer);
110
111#endif
A wrapper around a contiguous block of allocated memory.
C99 shim for <stdbool.h>.