Ruby  3.4.0dev (2024-12-06 revision 892c46283a5ea4179500d951c9d4866c0051f27b)
Data Structures | Macros | Enumerations | Functions
pm_string.h File Reference

(892c46283a5ea4179500d951c9d4866c0051f27b)

A generic string type that can have various ownership semantics. More...

#include "prism/defines.h"
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
Include dependency graph for pm_string.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  pm_string_t
 A generic string type that can have various ownership semantics. More...
 

Macros

#define PM_STRING_EMPTY   ((pm_string_t) { .type = PM_STRING_CONSTANT, .source = NULL, .length = 0 })
 Defines an empty string. More...
 

Enumerations

enum  pm_string_init_result_t { PM_STRING_INIT_SUCCESS = 0 , PM_STRING_INIT_ERROR_GENERIC = 1 , PM_STRING_INIT_ERROR_DIRECTORY = 2 }
 Represents the result of calling pm_string_mapped_init or pm_string_file_init. More...
 

Functions

PRISM_EXPORTED_FUNCTION size_t pm_string_sizeof (void)
 Returns the size of the pm_string_t struct. More...
 
void pm_string_shared_init (pm_string_t *string, const uint8_t *start, const uint8_t *end)
 Initialize a shared string that is based on initial input. More...
 
void pm_string_owned_init (pm_string_t *string, uint8_t *source, size_t length)
 Initialize an owned string that is responsible for freeing allocated memory. More...
 
void pm_string_constant_init (pm_string_t *string, const char *source, size_t length)
 Initialize a constant string that doesn't own its memory source. More...
 
PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_mapped_init (pm_string_t *string, const char *filepath)
 Read the file indicated by the filepath parameter into source and load its contents and size into the given pm_string_t. More...
 
PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_file_init (pm_string_t *string, const char *filepath)
 Read the file indicated by the filepath parameter into source and load its contents and size into the given pm_string_t. More...
 
void pm_string_ensure_owned (pm_string_t *string)
 Ensure the string is owned. More...
 
int pm_string_compare (const pm_string_t *left, const pm_string_t *right)
 Compare the underlying lengths and bytes of two strings. More...
 
PRISM_EXPORTED_FUNCTION size_t pm_string_length (const pm_string_t *string)
 Returns the length associated with the string. More...
 
PRISM_EXPORTED_FUNCTION const uint8_t * pm_string_source (const pm_string_t *string)
 Returns the start pointer associated with the string. More...
 
PRISM_EXPORTED_FUNCTION void pm_string_free (pm_string_t *string)
 Free the associated memory of the given string. More...
 

Detailed Description

A generic string type that can have various ownership semantics.

Definition in file pm_string.h.

Macro Definition Documentation

◆ PM_STRING_EMPTY

#define PM_STRING_EMPTY   ((pm_string_t) { .type = PM_STRING_CONSTANT, .source = NULL, .length = 0 })

Defines an empty string.

This is useful for initializing a string that will be filled in later.

Definition at line 70 of file pm_string.h.

Enumeration Type Documentation

◆ pm_string_init_result_t

Represents the result of calling pm_string_mapped_init or pm_string_file_init.

We need this additional information because there is not a platform-agnostic way to indicate that the file that was attempted to be opened was a directory.

Enumerator
PM_STRING_INIT_SUCCESS 

Indicates that the string was successfully initialized.

PM_STRING_INIT_ERROR_GENERIC 

Indicates a generic error from a string_*_init function, where the type of error should be read from errno or GetLastError().

PM_STRING_INIT_ERROR_DIRECTORY 

Indicates that the file that was attempted to be opened was a directory.

Definition at line 105 of file pm_string.h.

Function Documentation

◆ pm_string_compare()

int pm_string_compare ( const pm_string_t left,
const pm_string_t right 
)

Compare the underlying lengths and bytes of two strings.

Returns 0 if the strings are equal, a negative number if the left string is less than the right string, and a positive number if the left string is greater than the right string.

Parameters
leftThe left string to compare.
rightThe right string to compare.
Returns
The comparison result.

Returns 0 if the strings are equal, a negative number if the left string is less than the right string, and a positive number if the left string is greater than the right string.

Definition at line 336 of file pm_string.c.

◆ pm_string_constant_init()

void pm_string_constant_init ( pm_string_t string,
const char *  source,
size_t  length 
)

Initialize a constant string that doesn't own its memory source.

Parameters
stringThe string to initialize.
sourceThe source of the string.
lengthThe length of the string.

Definition at line 42 of file pm_string.c.

Referenced by pm_options_encoding_set(), pm_options_filepath_set(), and pm_options_read().

◆ pm_string_ensure_owned()

void pm_string_ensure_owned ( pm_string_t string)

Ensure the string is owned.

If it is not, then reinitialize it as owned and copy over the previous source.

Parameters
stringThe string to ensure is owned.

If it is not, then reinitialize it as owned and copy over the previous source.

Definition at line 316 of file pm_string.c.

◆ pm_string_file_init()

PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_file_init ( pm_string_t string,
const char *  filepath 
)

Read the file indicated by the filepath parameter into source and load its contents and size into the given pm_string_t.

The given pm_string_t should be freed using pm_string_free when it is no longer used.

Parameters
stringThe string to initialize.
filepathThe filepath to read.
Returns
The success of the read, indicated by the value of the enum.

The given pm_string_t should be freed using pm_string_free when it is no longer used.

Definition at line 210 of file pm_string.c.

Referenced by pm_string_mapped_init().

◆ pm_string_free()

PRISM_EXPORTED_FUNCTION void pm_string_free ( pm_string_t string)

Free the associated memory of the given string.

Parameters
stringThe string to free.

Definition at line 369 of file pm_string.c.

Referenced by pm_node_destroy(), pm_options_free(), and pm_parser_free().

◆ pm_string_length()

PRISM_EXPORTED_FUNCTION size_t pm_string_length ( const pm_string_t string)

Returns the length associated with the string.

Parameters
stringThe string to get the length of.
Returns
The length of the string.

Definition at line 353 of file pm_string.c.

Referenced by pm_dump_json(), pm_string_compare(), and pm_string_ensure_owned().

◆ pm_string_mapped_init()

PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_mapped_init ( pm_string_t string,
const char *  filepath 
)

Read the file indicated by the filepath parameter into source and load its contents and size into the given pm_string_t.

The given pm_string_t should be freed using pm_string_free when it is no longer used.

We want to use demand paging as much as possible in order to avoid having to read the entire file into memory (which could be detrimental to performance for large files). This means that if we're on windows we'll use MapViewOfFile, on POSIX systems that have access to mmap we'll use mmap, and on other POSIX systems we'll use read.

Parameters
stringThe string to initialize.
filepathThe filepath to read.
Returns
The success of the read, indicated by the value of the enum.

The given pm_string_t should be freed using pm_string_free when it is no longer used.

We want to use demand paging as much as possible in order to avoid having to read the entire file into memory (which could be detrimental to performance for large files). This means that if we're on windows we'll use MapViewOfFile, on POSIX systems that have access to mmap we'll use mmap, and on other POSIX systems we'll use read.

Definition at line 118 of file pm_string.c.

◆ pm_string_owned_init()

void pm_string_owned_init ( pm_string_t string,
uint8_t *  source,
size_t  length 
)

Initialize an owned string that is responsible for freeing allocated memory.

Parameters
stringThe string to initialize.
sourceThe source of the string.
lengthThe length of the string.

Definition at line 30 of file pm_string.c.

Referenced by pm_string_ensure_owned().

◆ pm_string_shared_init()

void pm_string_shared_init ( pm_string_t string,
const uint8_t *  start,
const uint8_t *  end 
)

Initialize a shared string that is based on initial input.

Parameters
stringThe string to initialize.
startThe start of the string.
endThe end of the string.

Definition at line 16 of file pm_string.c.

◆ pm_string_sizeof()

PRISM_EXPORTED_FUNCTION size_t pm_string_sizeof ( void  )

Returns the size of the pm_string_t struct.

This is necessary to allocate the correct amount of memory in the FFI backend.

Returns
The size of the pm_string_t struct.

This is necessary to allocate the correct amount of memory in the FFI backend.

Definition at line 8 of file pm_string.c.

◆ pm_string_source()

PRISM_EXPORTED_FUNCTION const uint8_t* pm_string_source ( const pm_string_t string)

Returns the start pointer associated with the string.

Parameters
stringThe string to get the start pointer of.
Returns
The start pointer of the string.

Definition at line 361 of file pm_string.c.

Referenced by pm_dump_json(), pm_string_compare(), and pm_string_ensure_owned().