Ruby: prism/util/pm_buffer.h Source File

Ruby 3.4.8p72 (2025-12-17 revision 995b59f66677d44767ce9faac6957e5543617ff9)

pm_buffer.h
Go to the documentation of this file.
1
6#ifndef PRISM_BUFFER_H
7#define PRISM_BUFFER_H
8
9#include "prism/defines.h"
10#include "prism/util/pm_char.h"
11
12#include <assert.h>
13#include <stdbool.h>
14#include <stdint.h>
15#include <stdlib.h>
16#include <string.h>
17
22 typedef struct {
24 size_t length;
25
27 size_t capacity;
28
30 char *value;
31} pm_buffer_t;
32
38 PRISM_EXPORTED_FUNCTION size_t pm_buffer_sizeof(void);
39
47bool pm_buffer_init_capacity(pm_buffer_t *buffer, size_t capacity);
48
57 PRISM_EXPORTED_FUNCTION bool pm_buffer_init(pm_buffer_t *buffer);
58
67 PRISM_EXPORTED_FUNCTION char * pm_buffer_value(const pm_buffer_t *buffer);
68
77 PRISM_EXPORTED_FUNCTION size_t pm_buffer_length(const pm_buffer_t *buffer);
78
85void pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length);
86
94 void pm_buffer_append_format(pm_buffer_t *buffer, const char *format, ...) PRISM_ATTRIBUTE_FORMAT(2, 3);
95
103void pm_buffer_append_string(pm_buffer_t *buffer, const char *value, size_t length);
104
112void pm_buffer_append_bytes(pm_buffer_t *buffer, const uint8_t *value, size_t length);
113
120void pm_buffer_append_byte(pm_buffer_t *buffer, uint8_t value);
121
128void pm_buffer_append_varuint(pm_buffer_t *buffer, uint32_t value);
129
136void pm_buffer_append_varsint(pm_buffer_t *buffer, int32_t value);
137
144void pm_buffer_append_double(pm_buffer_t *buffer, double value);
145
154bool pm_buffer_append_unicode_codepoint(pm_buffer_t *buffer, uint32_t value);
155
160typedef enum {
161 PM_BUFFER_ESCAPING_RUBY,
162 PM_BUFFER_ESCAPING_JSON
163} pm_buffer_escaping_t;
164
173void pm_buffer_append_source(pm_buffer_t *buffer, const uint8_t *source, size_t length, pm_buffer_escaping_t escaping);
174
182void pm_buffer_prepend_string(pm_buffer_t *buffer, const char *value, size_t length);
183
190void pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source);
191
198void pm_buffer_clear(pm_buffer_t *buffer);
199
205void pm_buffer_rstrip(pm_buffer_t *buffer);
206
215size_t pm_buffer_index(const pm_buffer_t *buffer, char value);
216
225void pm_buffer_insert(pm_buffer_t *buffer, size_t index, const char *value, size_t length);
226
234 PRISM_EXPORTED_FUNCTION void pm_buffer_free(pm_buffer_t *buffer);
235
236#endif
assert.h
pm_buffer_free
void pm_buffer_free(pm_buffer_t *buffer)
Free the memory associated with the buffer.
Definition pm_buffer.c:355
pm_buffer_init
bool pm_buffer_init(pm_buffer_t *buffer)
Initialize a pm_buffer_t with its default values.
Definition pm_buffer.c:27
pm_buffer_length
size_t pm_buffer_length(const pm_buffer_t *buffer)
Return the length of the buffer.
Definition pm_buffer.c:43
pm_buffer_value
char * pm_buffer_value(const pm_buffer_t *buffer)
Return the value of the buffer.
Definition pm_buffer.c:35
pm_buffer_init_capacity
bool pm_buffer_init_capacity(pm_buffer_t *buffer, size_t capacity)
Initialize a pm_buffer_t with the given capacity.
Definition pm_buffer.c:15
pm_buffer_sizeof
PRISM_EXPORTED_FUNCTION size_t pm_buffer_sizeof(void)
Return the size of the pm_buffer_t struct.
Definition pm_buffer.c:7
pm_buffer_index
size_t pm_buffer_index(const pm_buffer_t *buffer, char value)
Checks if the buffer includes the given value.
Definition pm_buffer.c:330
pm_buffer_append_format
void pm_buffer_append_format(pm_buffer_t *buffer, const char *format,...) PRISM_ATTRIBUTE_FORMAT(2
Append a formatted string to the buffer.
pm_buffer_append_string
void void pm_buffer_append_string(pm_buffer_t *buffer, const char *value, size_t length)
Append a string to the buffer.
Definition pm_buffer.c:119
pm_buffer_escaping_t
pm_buffer_escaping_t
The different types of escaping that can be performed by the buffer when appending a slice of Ruby so...
Definition pm_buffer.h:160
pm_buffer_append_zeroes
void pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length)
Append the given amount of space as zeroes to the buffer.
Definition pm_buffer.c:86
pm_buffer_append_source
void pm_buffer_append_source(pm_buffer_t *buffer, const uint8_t *source, size_t length, pm_buffer_escaping_t escaping)
Append a slice of source code to the buffer.
Definition pm_buffer.c:219
pm_buffer_append_varuint
void pm_buffer_append_varuint(pm_buffer_t *buffer, uint32_t value)
Append a 32-bit unsigned integer to the buffer as a variable-length integer.
Definition pm_buffer.c:144
pm_buffer_append_byte
void pm_buffer_append_byte(pm_buffer_t *buffer, uint8_t value)
Append a single byte to the buffer.
Definition pm_buffer.c:135
pm_buffer_append_varsint
void pm_buffer_append_varsint(pm_buffer_t *buffer, int32_t value)
Append a 32-bit signed integer to the buffer as a variable-length integer.
Definition pm_buffer.c:161
pm_buffer_prepend_string
void pm_buffer_prepend_string(pm_buffer_t *buffer, const char *value, size_t length)
Prepend the given string to the buffer.
Definition pm_buffer.c:289
pm_buffer_rstrip
void pm_buffer_rstrip(pm_buffer_t *buffer)
Strip the whitespace from the end of the buffer.
Definition pm_buffer.c:320
pm_buffer_insert
void pm_buffer_insert(pm_buffer_t *buffer, size_t index, const char *value, size_t length)
Insert the given string into the buffer at the given index.
Definition pm_buffer.c:339
pm_buffer_append_unicode_codepoint
bool pm_buffer_append_unicode_codepoint(pm_buffer_t *buffer, uint32_t value)
Append a unicode codepoint to the buffer.
Definition pm_buffer.c:179
pm_buffer_append_bytes
void pm_buffer_append_bytes(pm_buffer_t *buffer, const uint8_t *value, size_t length)
Append a list of bytes to the buffer.
Definition pm_buffer.c:127
pm_buffer_append_double
void pm_buffer_append_double(pm_buffer_t *buffer, double value)
Append a double to the buffer.
Definition pm_buffer.c:170
pm_buffer_concat
void pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source)
Concatenate one buffer onto another.
Definition pm_buffer.c:301
pm_buffer_clear
void pm_buffer_clear(pm_buffer_t *buffer)
Clear the buffer by reducing its size to 0.
Definition pm_buffer.c:312
pm_char.h
Functions for working with characters and strings.
defines.h
Macro definitions used throughout the prism library.
PRISM_ATTRIBUTE_FORMAT
#define PRISM_ATTRIBUTE_FORMAT(string_index, argument_index)
Certain compilers support specifying that a function accepts variadic parameters that look like print...
Definition defines.h:67
PRISM_EXPORTED_FUNCTION
#define PRISM_EXPORTED_FUNCTION
By default, we compile with -fvisibility=hidden.
Definition defines.h:53
stdbool.h
C99 shim for <stdbool.h>
pm_buffer_t
A pm_buffer_t is a simple memory buffer that stores data in a contiguous block of memory.
Definition pm_buffer.h:22
pm_buffer_t::capacity
size_t capacity
The capacity of the buffer in bytes that has been allocated.
Definition pm_buffer.h:27
pm_buffer_t::length
size_t length
The length of the buffer in bytes.
Definition pm_buffer.h:24
pm_buffer_t::value
char * value
A pointer to the start of the buffer.
Definition pm_buffer.h:30