Ruby: prism/util/pm_string.h Source File

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

pm_string.h
Go to the documentation of this file.
1
6#ifndef PRISM_STRING_H
7#define PRISM_STRING_H
8
9#include "prism/defines.h"
10
11#include <assert.h>
12#include <errno.h>
13#include <stdbool.h>
14#include <stddef.h>
15#include <stdlib.h>
16#include <string.h>
17
18// The following headers are necessary to read files using demand paging.
19#ifdef _WIN32
20#include <windows.h>
21#elif defined(_POSIX_MAPPED_FILES)
22#include <fcntl.h>
23#include <sys/mman.h>
24#include <sys/stat.h>
25#elif defined(PRISM_HAS_FILESYSTEM)
26#include <fcntl.h>
27#include <sys/stat.h>
28#endif
29
33 typedef struct {
35 const uint8_t *source;
36
38 size_t length;
39
41 enum {
43 PM_STRING_CONSTANT,
44
46 PM_STRING_SHARED,
47
49 PM_STRING_OWNED,
50
51#ifdef PRISM_HAS_MMAP
53 PM_STRING_MAPPED
54#endif
55 } type;
56} pm_string_t;
57
64 PRISM_EXPORTED_FUNCTION size_t pm_string_sizeof(void);
65
70#define PM_STRING_EMPTY ((pm_string_t) { .type = PM_STRING_CONSTANT, .source = NULL, .length = 0 })
71
79void pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end);
80
88void pm_string_owned_init(pm_string_t *string, uint8_t *source, size_t length);
89
97void pm_string_constant_init(pm_string_t *string, const char *source, size_t length);
98
105 typedef enum {
107 PM_STRING_INIT_SUCCESS = 0,
112 PM_STRING_INIT_ERROR_GENERIC = 1,
116 PM_STRING_INIT_ERROR_DIRECTORY = 2
117} pm_string_init_result_t;
118
136 PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_mapped_init(pm_string_t *string, const char *filepath);
137
149 PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_file_init(pm_string_t *string, const char *filepath);
150
157void pm_string_ensure_owned(pm_string_t *string);
158
169int pm_string_compare(const pm_string_t *left, const pm_string_t *right);
170
179 PRISM_EXPORTED_FUNCTION size_t pm_string_length(const pm_string_t *string);
180
189 PRISM_EXPORTED_FUNCTION const uint8_t * pm_string_source(const pm_string_t *string);
190
198 PRISM_EXPORTED_FUNCTION void pm_string_free(pm_string_t *string);
199
200#endif
assert.h
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
pm_string_ensure_owned
void pm_string_ensure_owned(pm_string_t *string)
Ensure the string is owned.
Definition pm_string.c:314
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.
Definition pm_string.c:44
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.
Definition pm_string.c:32
pm_string_init_result_t
pm_string_init_result_t
Represents the result of calling pm_string_mapped_init or pm_string_file_init.
Definition pm_string.h:105
PM_STRING_INIT_SUCCESS
@ PM_STRING_INIT_SUCCESS
Indicates that the string was successfully initialized.
Definition pm_string.h:107
PM_STRING_INIT_ERROR_GENERIC
@ PM_STRING_INIT_ERROR_GENERIC
Indicates a generic error from a string_*_init function, where the type of error should be read from ...
Definition pm_string.h:112
PM_STRING_INIT_ERROR_DIRECTORY
@ PM_STRING_INIT_ERROR_DIRECTORY
Indicates that the file that was attempted to be opened was a directory.
Definition pm_string.h:116
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.
Definition pm_string.c:18
pm_string_sizeof
PRISM_EXPORTED_FUNCTION size_t pm_string_sizeof(void)
Returns the size of the pm_string_t struct.
Definition pm_string.c:10
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.
Definition pm_string.c:334
defines.h
Macro definitions used throughout the prism library.
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_string_t
A generic string type that can have various ownership semantics.
Definition pm_string.h:33
pm_string_t::source
const uint8_t * source
A pointer to the start of the string.
Definition pm_string.h:35
pm_string_t::length
size_t length
The length of the string in bytes of memory.
Definition pm_string.h:38
pm_string_t::PM_STRING_OWNED
@ PM_STRING_OWNED
This string owns its memory, and should be freed using pm_string_free().
Definition pm_string.h:49
pm_string_t::PM_STRING_CONSTANT
@ PM_STRING_CONSTANT
This string is a constant string, and should not be freed.
Definition pm_string.h:43
pm_string_t::PM_STRING_SHARED
@ PM_STRING_SHARED
This is a slice of another string, and should not be freed.
Definition pm_string.h:46