Prism Ruby parser
Loading...
Searching...
No Matches
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
33typedef struct {
35 const uint8_t *source;
36
38 size_t length;
39
41 enum {
44
47
50
51#ifdef PRISM_HAS_MMAP
53 PM_STRING_MAPPED
54#endif
55 } type;
57
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
118
135
146
154
165int pm_string_compare(const pm_string_t *left, const pm_string_t *right);
166
174
181PRISM_EXPORTED_FUNCTION const uint8_t * pm_string_source(const pm_string_t *string);
182
189
190#endif
Macro definitions used throughout the prism library.
#define PRISM_EXPORTED_FUNCTION
By default, we compile with -fvisibility=hidden.
Definition defines.h:50
PRISM_EXPORTED_FUNCTION size_t pm_string_length(const pm_string_t *string)
Returns the length associated with the string.
Definition pm_string.c:353
void pm_string_ensure_owned(pm_string_t *string)
Ensure the string is owned.
Definition pm_string.c:316
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:42
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:30
PRISM_EXPORTED_FUNCTION const uint8_t * pm_string_source(const pm_string_t *string)
Returns the start pointer associated with the string.
Definition pm_string.c:361
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
Indicates that the string was successfully initialized.
Definition pm_string.h:107
@ 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
Indicates that the file that was attempted to be opened was a directory.
Definition pm_string.h:116
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:16
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...
Definition pm_string.c:118
PRISM_EXPORTED_FUNCTION void pm_string_free(pm_string_t *string)
Free the associated memory of the given string.
Definition pm_string.c:369
PRISM_EXPORTED_FUNCTION size_t pm_string_sizeof(void)
Returns the size of the pm_string_t struct.
Definition pm_string.c:8
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:336
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...
Definition pm_string.c:210
A generic string type that can have various ownership semantics.
Definition pm_string.h:33
const uint8_t * source
A pointer to the start of the string.
Definition pm_string.h:35
size_t length
The length of the string in bytes of memory.
Definition pm_string.h:38
@ PM_STRING_OWNED
This string owns its memory, and should be freed using pm_string_free.
Definition pm_string.h:49
@ PM_STRING_CONSTANT
This string is a constant string, and should not be freed.
Definition pm_string.h:43
@ PM_STRING_SHARED
This is a slice of another string, and should not be freed.
Definition pm_string.h:46