Ruby 3.4.6p54 (2025-09-16 revision dbd83256b1cec76c69756ecb8758b9e1079833de)
pm_buffer.h File Reference

A wrapper around a contiguous block of allocated memory. More...

#include "prism/defines.h"
#include "prism/util/pm_char.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Data Structures

struct  pm_buffer_t
 A pm_buffer_t is a simple memory buffer that stores data in a contiguous block of memory. More...

Enumerations

enum  pm_buffer_escaping_t { PM_BUFFER_ESCAPING_RUBY , PM_BUFFER_ESCAPING_JSON }
 The different types of escaping that can be performed by the buffer when appending a slice of Ruby source code. More...

Functions

PRISM_EXPORTED_FUNCTION size_t pm_buffer_sizeof (void)
 Return the size of the pm_buffer_t struct.
bool pm_buffer_init_capacity (pm_buffer_t *buffer, size_t capacity)
 Initialize a pm_buffer_t with the given capacity.
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_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.
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_byte (pm_buffer_t *buffer, uint8_t value)
 Append a single byte 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_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_append_double (pm_buffer_t *buffer, double value)
 Append a double to the buffer.
bool pm_buffer_append_unicode_codepoint (pm_buffer_t *buffer, uint32_t value)
 Append a unicode codepoint 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_prepend_string (pm_buffer_t *buffer, const char *value, size_t length)
 Prepend the given string 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.
void pm_buffer_rstrip (pm_buffer_t *buffer)
 Strip the whitespace from the end of the buffer.
size_t pm_buffer_index (const pm_buffer_t *buffer, char value)
 Checks if the buffer includes the given value.
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.

Detailed Description

A wrapper around a contiguous block of allocated memory.

Definition in file pm_buffer.h.

Enumeration Type Documentation

◆ pm_buffer_escaping_t

The different types of escaping that can be performed by the buffer when appending a slice of Ruby source code.

Definition at line 160 of file pm_buffer.h.

Function Documentation

◆ pm_buffer_append_byte()

void pm_buffer_append_byte ( pm_buffer_t * buffer,
uint8_t value )

Append a single byte to the buffer.

Parameters
bufferThe buffer to append to.
valueThe byte to append.

Definition at line 135 of file pm_buffer.c.

◆ 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.

Parameters
bufferThe buffer to append to.
valueThe bytes to append.
lengthThe length of the bytes to append.

Definition at line 127 of file pm_buffer.c.

◆ pm_buffer_append_double()

void pm_buffer_append_double ( pm_buffer_t * buffer,
double value )

Append a double to the buffer.

Parameters
bufferThe buffer to append to.
valueThe double to append.

Definition at line 170 of file pm_buffer.c.

◆ pm_buffer_append_format()

void pm_buffer_append_format ( pm_buffer_t * buffer,
const char * format,
... )

Append a formatted string to the buffer.

Parameters
bufferThe buffer to append to.
formatThe format string to append.
...The arguments to the format string.

◆ 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.

Parameters
bufferThe buffer to append to.
sourceThe source code to append.
lengthThe length of the source code to append.
escapingThe type of escaping to perform.

Definition at line 219 of file pm_buffer.c.

◆ 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.

Parameters
bufferThe buffer to append to.
valueThe string to append.
lengthThe length of the string to append.

Definition at line 119 of file pm_buffer.c.

◆ 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.

Parameters
bufferThe buffer to append to.
valueThe character to append.
Returns
True if the codepoint was valid and appended successfully, false otherwise.

Definition at line 179 of file pm_buffer.c.

◆ 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.

Parameters
bufferThe buffer to append to.
valueThe integer to append.

Definition at line 161 of file pm_buffer.c.

◆ 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.

Parameters
bufferThe buffer to append to.
valueThe integer to append.

Definition at line 144 of file pm_buffer.c.

◆ 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.

Parameters
bufferThe buffer to append to.
lengthThe amount of space to append and zero.

Definition at line 86 of file pm_buffer.c.

◆ pm_buffer_clear()

void pm_buffer_clear ( pm_buffer_t * buffer)

Clear the buffer by reducing its size to 0.

This does not free the allocated memory, but it does allow the buffer to be reused.

Parameters
bufferThe buffer to clear.

This does not free the allocated memory, but it does allow the buffer to be reused.

Definition at line 312 of file pm_buffer.c.

◆ pm_buffer_concat()

void pm_buffer_concat ( pm_buffer_t * destination,
const pm_buffer_t * source )

Concatenate one buffer onto another.

Parameters
destinationThe buffer to concatenate onto.
sourceThe buffer to concatenate.

Definition at line 301 of file pm_buffer.c.

◆ pm_buffer_index()

size_t pm_buffer_index ( const pm_buffer_t * buffer,
char value )

Checks if the buffer includes the given value.

Parameters
bufferThe buffer to check.
valueThe value to check for.
Returns
The index of the first occurrence of the value in the buffer, or SIZE_MAX if the value is not found.

Definition at line 330 of file pm_buffer.c.

◆ 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.

Parameters
bufferThe buffer to initialize.
capacityThe capacity of the buffer.
Returns
True if the buffer was initialized successfully, false otherwise.

Definition at line 15 of file pm_buffer.c.

Referenced by pm_buffer_t::pm_buffer_init().

◆ 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.

Parameters
bufferThe buffer to insert into.
indexThe index to insert at.
valueThe string to insert.
lengthThe length of the string to insert.

Definition at line 339 of file pm_buffer.c.

◆ 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.

Parameters
bufferThe buffer to prepend to.
valueThe string to prepend.
lengthThe length of the string to prepend.

Definition at line 289 of file pm_buffer.c.

◆ pm_buffer_rstrip()

void pm_buffer_rstrip ( pm_buffer_t * buffer)

Strip the whitespace from the end of the buffer.

Parameters
bufferThe buffer to strip.

Definition at line 320 of file pm_buffer.c.

◆ pm_buffer_sizeof()

PRISM_EXPORTED_FUNCTION size_t pm_buffer_sizeof ( void )

Return the size of the pm_buffer_t struct.

Returns
The size of the pm_buffer_t struct.

Definition at line 7 of file pm_buffer.c.