20 return buffer->
value != NULL;
51 pm_buffer_append_length(
pm_buffer_t *buffer,
size_t length) {
52 size_t next_length = buffer->
length + length;
54 if (next_length > buffer->
capacity) {
59 while (next_length > buffer->
capacity) {
64 if (buffer->
value == NULL)
return false;
67 buffer->
length = next_length;
75 pm_buffer_append(
pm_buffer_t *buffer,
const void *source,
size_t length) {
76 size_t cursor = buffer->
length;
77 if (pm_buffer_append_length(buffer, length)) {
78 memcpy(buffer->
value + cursor, source, length);
87 size_t cursor = buffer->
length;
88 if (pm_buffer_append_length(buffer, length)) {
89 memset(buffer->
value + cursor, 0, length);
99 va_start(arguments, format);
100 int result = vsnprintf(NULL, 0, format, arguments);
103 if (result < 0)
return;
104 size_t length = (size_t) (result + 1);
106 size_t cursor = buffer->
length;
107 if (pm_buffer_append_length(buffer, length)) {
108 va_start(arguments, format);
109 vsnprintf(buffer->
value + cursor, length, format, arguments);
120 pm_buffer_append(buffer, value, length);
128 pm_buffer_append(buffer, (
const char *) value, length);
136 const void *source = &value;
137 pm_buffer_append(buffer, source,
sizeof(uint8_t));
162 uint32_t unsigned_int = ((uint32_t)(value) << 1) ^ ((uint32_t)(value >> 31));
171 const void *source = &value;
172 pm_buffer_append(buffer, source,
sizeof(
double));
180 for (
size_t index = 0; index < length; index++) {
181 const uint8_t
byte = source[index];
183 if ((
byte <= 0x06) || (
byte >= 0x0E &&
byte <= 0x1F) || (
byte >= 0x7F)) {
184 if (escaping == PM_BUFFER_ESCAPING_RUBY) {
192 if (escaping == PM_BUFFER_ESCAPING_RUBY) {
208 if (escaping == PM_BUFFER_ESCAPING_RUBY) {
224 if (escaping == PM_BUFFER_ESCAPING_RUBY && index + 1 < length) {
225 const uint8_t next_byte = source[index + 1];
226 if (next_byte ==
'{' || next_byte ==
'@' || next_byte ==
'$') {
250 size_t cursor = buffer->
length;
251 if (pm_buffer_append_length(buffer, length)) {
252 memmove(buffer->
value + length, buffer->
value, cursor);
253 memcpy(buffer->
value, value, length);
263 pm_buffer_append(destination, source->
value, source->
length);
291 const char *first = memchr(buffer->
value, value, buffer->
length);
292 return (first == NULL) ? SIZE_MAX : (size_t) (first - buffer->
value);
300 assert(index <= buffer->length);
302 if (index == buffer->
length) {
306 memmove(buffer->
value + index + length, buffer->
value + index, buffer->
length - length - index);
307 memcpy(buffer->
value + index, value, length);
#define xfree
Old name of ruby_xfree.
#define xrealloc
Old name of ruby_xrealloc.
#define xmalloc
Old name of ruby_xmalloc.
A wrapper around a contiguous block of allocated memory.
bool pm_buffer_init_capacity(pm_buffer_t *buffer, size_t capacity)
Initialize a pm_buffer_t with the given capacity.
PRISM_EXPORTED_FUNCTION size_t pm_buffer_sizeof(void)
Return the size of the pm_buffer_t struct.
size_t pm_buffer_index(const pm_buffer_t *buffer, char value)
Checks if the buffer includes the given value.
void pm_buffer_append_format(pm_buffer_t *buffer, const char *format,...) PRISM_ATTRIBUTE_FORMAT(2
Append a formatted string to the buffer.
void void pm_buffer_append_string(pm_buffer_t *buffer, const char *value, size_t length)
Append a string to the buffer.
PRISM_EXPORTED_FUNCTION size_t pm_buffer_length(const pm_buffer_t *buffer)
Return the length of the buffer.
pm_buffer_escaping_t
The different types of escaping that can be performed by the buffer when appending a slice of Ruby so...
void pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length)
Append the given amount of space as zeroes to the buffer.
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.
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.
void pm_buffer_append_byte(pm_buffer_t *buffer, uint8_t value)
Append a single byte to the buffer.
PRISM_EXPORTED_FUNCTION bool pm_buffer_init(pm_buffer_t *buffer)
Initialize a pm_buffer_t with its default values.
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.
void pm_buffer_prepend_string(pm_buffer_t *buffer, const char *value, size_t length)
Prepend the given string to the buffer.
void pm_buffer_rstrip(pm_buffer_t *buffer)
Strip the whitespace from the end of the buffer.
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.
PRISM_EXPORTED_FUNCTION char * pm_buffer_value(const pm_buffer_t *buffer)
Return the value of the buffer.
PRISM_EXPORTED_FUNCTION void pm_buffer_free(pm_buffer_t *buffer)
Free the memory associated with the buffer.
void pm_buffer_append_bytes(pm_buffer_t *buffer, const uint8_t *value, size_t length)
Append a list of bytes to the buffer.
void pm_buffer_append_double(pm_buffer_t *buffer, double value)
Append a double to the buffer.
void pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source)
Concatenate one buffer onto another.
void pm_buffer_clear(pm_buffer_t *buffer)
Clear the buffer by reducing its size to 0.
bool pm_char_is_whitespace(const uint8_t b)
Returns true if the given character is a whitespace character.
A pm_buffer_t is a simple memory buffer that stores data in a contiguous block of memory.
size_t capacity
The capacity of the buffer in bytes that has been allocated.
size_t length
The length of the buffer in bytes.
char * value
A pointer to the start of the buffer.