Debian Codegraph @

include/prism/util/pm_buffer.h

raw symbols SCIP
1 /**
2 * @file pm_buffer.h
3 *
4 * A wrapper around a contiguous block of allocated memory.
5 */
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
18 /**
19 * A pm_buffer_t is a simple memory buffer that stores data in a contiguous
20 * block of memory.
21 */
22 typedef struct {
23 /** The length of the buffer in bytes. */
24 size_t length;
25
26 /** The capacity of the buffer in bytes that has been allocated. */
27 size_t capacity;
28
29 /** A pointer to the start of the buffer. */
30 char *value;
31 } pm_buffer_t;
32
33 /**
34 * Return the size of the pm_buffer_t struct.
35 *
36 * @returns The size of the pm_buffer_t struct.
37 */
38 PRISM_EXPORTED_FUNCTION size_t pm_buffer_sizeof(void);
39
40 /**
41 * Initialize a pm_buffer_t with the given capacity.
42 *
43 * @param buffer The buffer to initialize.
44 * @param capacity The capacity of the buffer.
45 * @returns True if the buffer was initialized successfully, false otherwise.
46 */
47 bool pm_buffer_init_capacity(pm_buffer_t *buffer, size_t capacity);
48
49 /**
50 * Initialize a pm_buffer_t with its default values.
51 *
52 * @param buffer The buffer to initialize.
53 * @returns True if the buffer was initialized successfully, false otherwise.
54 *
55 * \public \memberof pm_buffer_t
56 */
57 PRISM_EXPORTED_FUNCTION bool pm_buffer_init(pm_buffer_t *buffer);
58
59 /**
60 * Return the value of the buffer.
61 *
62 * @param buffer The buffer to get the value of.
63 * @returns The value of the buffer.
64 *
65 * \public \memberof pm_buffer_t
66 */
67 PRISM_EXPORTED_FUNCTION char * pm_buffer_value(const pm_buffer_t *buffer);
68
69 /**
70 * Return the length of the buffer.
71 *
72 * @param buffer The buffer to get the length of.
73 * @returns The length of the buffer.
74 *
75 * \public \memberof pm_buffer_t
76 */
77 PRISM_EXPORTED_FUNCTION size_t pm_buffer_length(const pm_buffer_t *buffer);
78
79 /**
80 * Append the given amount of space as zeroes to the buffer.
81 *
82 * @param buffer The buffer to append to.
83 * @param length The amount of space to append and zero.
84 */
85 void pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length);
86
87 /**
88 * Append a formatted string to the buffer.
89 *
90 * @param buffer The buffer to append to.
91 * @param format The format string to append.
92 * @param ... The arguments to the format string.
93 */
94 void pm_buffer_append_format(pm_buffer_t *buffer, const char *format, ...) PRISM_ATTRIBUTE_FORMAT(2, 3);
95
96 /**
97 * Append a string to the buffer.
98 *
99 * @param buffer The buffer to append to.
100 * @param value The string to append.
101 * @param length The length of the string to append.
102 */
103 void pm_buffer_append_string(pm_buffer_t *buffer, const char *value, size_t length);
104
105 /**
106 * Append a list of bytes to the buffer.
107 *
108 * @param buffer The buffer to append to.
109 * @param value The bytes to append.
110 * @param length The length of the bytes to append.
111 */
112 void pm_buffer_append_bytes(pm_buffer_t *buffer, const uint8_t *value, size_t length);
113
114 /**
115 * Append a single byte to the buffer.
116 *
117 * @param buffer The buffer to append to.
118 * @param value The byte to append.
119 */
120 void pm_buffer_append_byte(pm_buffer_t *buffer, uint8_t value);
121
122 /**
123 * Append a 32-bit unsigned integer to the buffer as a variable-length integer.
124 *
125 * @param buffer The buffer to append to.
126 * @param value The integer to append.
127 */
128 void pm_buffer_append_varuint(pm_buffer_t *buffer, uint32_t value);
129
130 /**
131 * Append a 32-bit signed integer to the buffer as a variable-length integer.
132 *
133 * @param buffer The buffer to append to.
134 * @param value The integer to append.
135 */
136 void pm_buffer_append_varsint(pm_buffer_t *buffer, int32_t value);
137
138 /**
139 * Append a double to the buffer.
140 *
141 * @param buffer The buffer to append to.
142 * @param value The double to append.
143 */
144 void pm_buffer_append_double(pm_buffer_t *buffer, double value);
145
146 /**
147 * Append a unicode codepoint to the buffer.
148 *
149 * @param buffer The buffer to append to.
150 * @param value The character to append.
151 * @returns True if the codepoint was valid and appended successfully, false
152 * otherwise.
153 */
154 bool pm_buffer_append_unicode_codepoint(pm_buffer_t *buffer, uint32_t value);
155
156 /**
157 * The different types of escaping that can be performed by the buffer when
158 * appending a slice of Ruby source code.
159 */
160 typedef enum {
161 PM_BUFFER_ESCAPING_RUBY,
162 PM_BUFFER_ESCAPING_JSON
163 } pm_buffer_escaping_t;
164
165 /**
166 * Append a slice of source code to the buffer.
167 *
168 * @param buffer The buffer to append to.
169 * @param source The source code to append.
170 * @param length The length of the source code to append.
171 * @param escaping The type of escaping to perform.
172 */
173 void pm_buffer_append_source(pm_buffer_t *buffer, const uint8_t *source, size_t length, pm_buffer_escaping_t escaping);
174
175 /**
176 * Prepend the given string to the buffer.
177 *
178 * @param buffer The buffer to prepend to.
179 * @param value The string to prepend.
180 * @param length The length of the string to prepend.
181 */
182 void pm_buffer_prepend_string(pm_buffer_t *buffer, const char *value, size_t length);
183
184 /**
185 * Concatenate one buffer onto another.
186 *
187 * @param destination The buffer to concatenate onto.
188 * @param source The buffer to concatenate.
189 */
190 void pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source);
191
192 /**
193 * Clear the buffer by reducing its size to 0. This does not free the allocated
194 * memory, but it does allow the buffer to be reused.
195 *
196 * @param buffer The buffer to clear.
197 */
198 void pm_buffer_clear(pm_buffer_t *buffer);
199
200 /**
201 * Strip the whitespace from the end of the buffer.
202 *
203 * @param buffer The buffer to strip.
204 */
205 void pm_buffer_rstrip(pm_buffer_t *buffer);
206
207 /**
208 * Checks if the buffer includes the given value.
209 *
210 * @param buffer The buffer to check.
211 * @param value The value to check for.
212 * @returns The index of the first occurrence of the value in the buffer, or
213 * SIZE_MAX if the value is not found.
214 */
215 size_t pm_buffer_index(const pm_buffer_t *buffer, char value);
216
217 /**
218 * Insert the given string into the buffer at the given index.
219 *
220 * @param buffer The buffer to insert into.
221 * @param index The index to insert at.
222 * @param value The string to insert.
223 * @param length The length of the string to insert.
224 */
225 void pm_buffer_insert(pm_buffer_t *buffer, size_t index, const char *value, size_t length);
226
227 /**
228 * Free the memory associated with the buffer.
229 *
230 * @param buffer The buffer to free.
231 *
232 * \public \memberof pm_buffer_t
233 */
234 PRISM_EXPORTED_FUNCTION void pm_buffer_free(pm_buffer_t *buffer);
235
236 #endif