diff options
Diffstat (limited to 'prism/util')
| -rw-r--r-- | prism/util/pm_buffer.c | 179 | ||||
| -rw-r--r-- | prism/util/pm_buffer.h | 146 | ||||
| -rw-r--r-- | prism/util/pm_char.c | 318 | ||||
| -rw-r--r-- | prism/util/pm_char.h | 205 | ||||
| -rw-r--r-- | prism/util/pm_constant_pool.c | 296 | ||||
| -rw-r--r-- | prism/util/pm_constant_pool.h | 191 | ||||
| -rw-r--r-- | prism/util/pm_list.c | 49 | ||||
| -rw-r--r-- | prism/util/pm_list.h | 97 | ||||
| -rw-r--r-- | prism/util/pm_memchr.c | 35 | ||||
| -rw-r--r-- | prism/util/pm_memchr.h | 29 | ||||
| -rw-r--r-- | prism/util/pm_newline_list.c | 96 | ||||
| -rw-r--r-- | prism/util/pm_newline_list.h | 104 | ||||
| -rw-r--r-- | prism/util/pm_state_stack.c | 25 | ||||
| -rw-r--r-- | prism/util/pm_state_stack.h | 42 | ||||
| -rw-r--r-- | prism/util/pm_string.c | 210 | ||||
| -rw-r--r-- | prism/util/pm_string.h | 150 | ||||
| -rw-r--r-- | prism/util/pm_string_list.c | 28 | ||||
| -rw-r--r-- | prism/util/pm_string_list.h | 44 | ||||
| -rw-r--r-- | prism/util/pm_strncasecmp.c | 24 | ||||
| -rw-r--r-- | prism/util/pm_strncasecmp.h | 32 | ||||
| -rw-r--r-- | prism/util/pm_strpbrk.c | 72 | ||||
| -rw-r--r-- | prism/util/pm_strpbrk.h | 43 |
22 files changed, 0 insertions, 2415 deletions
diff --git a/prism/util/pm_buffer.c b/prism/util/pm_buffer.c deleted file mode 100644 index 307b55d030..0000000000 --- a/prism/util/pm_buffer.c +++ /dev/null @@ -1,179 +0,0 @@ -#include "prism/util/pm_buffer.h" - -/** - * Return the size of the pm_buffer_t struct. - */ -size_t -pm_buffer_sizeof(void) { - return sizeof(pm_buffer_t); -} - -/** - * Initialize a pm_buffer_t with the given capacity. - */ -bool -pm_buffer_init_capacity(pm_buffer_t *buffer, size_t capacity) { - buffer->length = 0; - buffer->capacity = capacity; - - buffer->value = (char *) malloc(capacity); - return buffer->value != NULL; -} - -/** - * Initialize a pm_buffer_t with its default values. - */ -bool -pm_buffer_init(pm_buffer_t *buffer) { - return pm_buffer_init_capacity(buffer, 1024); -} - -/** - * Return the value of the buffer. - */ -char * -pm_buffer_value(pm_buffer_t *buffer) { - return buffer->value; -} - -/** - * Return the length of the buffer. - */ -size_t -pm_buffer_length(pm_buffer_t *buffer) { - return buffer->length; -} - -/** - * Append the given amount of space to the buffer. - */ -static inline void -pm_buffer_append_length(pm_buffer_t *buffer, size_t length) { - size_t next_length = buffer->length + length; - - if (next_length > buffer->capacity) { - if (buffer->capacity == 0) { - buffer->capacity = 1; - } - - while (next_length > buffer->capacity) { - buffer->capacity *= 2; - } - - buffer->value = realloc(buffer->value, buffer->capacity); - } - - buffer->length = next_length; -} - -/** - * Append a generic pointer to memory to the buffer. - */ -static inline void -pm_buffer_append(pm_buffer_t *buffer, const void *source, size_t length) { - size_t cursor = buffer->length; - pm_buffer_append_length(buffer, length); - memcpy(buffer->value + cursor, source, length); -} - -/** - * Append the given amount of space as zeroes to the buffer. - */ -void -pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length) { - size_t cursor = buffer->length; - pm_buffer_append_length(buffer, length); - memset(buffer->value + cursor, 0, length); -} - -/** - * Append a formatted string to the buffer. - */ -void -pm_buffer_append_format(pm_buffer_t *buffer, const char *format, ...) { - va_list arguments; - va_start(arguments, format); - int result = vsnprintf(NULL, 0, format, arguments); - va_end(arguments); - - if (result < 0) return; - size_t length = (size_t) (result + 1); - - size_t cursor = buffer->length; - pm_buffer_append_length(buffer, length); - - va_start(arguments, format); - vsnprintf(buffer->value + cursor, length, format, arguments); - va_end(arguments); - - buffer->length--; -} - -/** - * Append a string to the buffer. - */ -void -pm_buffer_append_string(pm_buffer_t *buffer, const char *value, size_t length) { - pm_buffer_append(buffer, value, length); -} - -/** - * Append a list of bytes to the buffer. - */ -void -pm_buffer_append_bytes(pm_buffer_t *buffer, const uint8_t *value, size_t length) { - pm_buffer_append(buffer, (const char *) value, length); -} - -/** - * Append a single byte to the buffer. - */ -void -pm_buffer_append_byte(pm_buffer_t *buffer, uint8_t value) { - const void *source = &value; - pm_buffer_append(buffer, source, sizeof(uint8_t)); -} - -/** - * Append a 32-bit unsigned integer to the buffer as a variable-length integer. - */ -void -pm_buffer_append_varuint(pm_buffer_t *buffer, uint32_t value) { - if (value < 128) { - pm_buffer_append_byte(buffer, (uint8_t) value); - } else { - uint32_t n = value; - while (n >= 128) { - pm_buffer_append_byte(buffer, (uint8_t) (n | 128)); - n >>= 7; - } - pm_buffer_append_byte(buffer, (uint8_t) n); - } -} - -/** - * Append a 32-bit signed integer to the buffer as a variable-length integer. - */ -void -pm_buffer_append_varsint(pm_buffer_t *buffer, int32_t value) { - uint32_t unsigned_int = ((uint32_t)(value) << 1) ^ ((uint32_t)(value >> 31)); - pm_buffer_append_varuint(buffer, unsigned_int); -} - -/** - * Concatenate one buffer onto another. - */ -void -pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source) { - if (source->length > 0) { - pm_buffer_append(destination, source->value, source->length); - } -} - -/** - * Free the memory associated with the buffer. - */ -void -pm_buffer_free(pm_buffer_t *buffer) { - free(buffer->value); -} diff --git a/prism/util/pm_buffer.h b/prism/util/pm_buffer.h deleted file mode 100644 index ec11d05e9b..0000000000 --- a/prism/util/pm_buffer.h +++ /dev/null @@ -1,146 +0,0 @@ -/** - * @file pm_buffer.h - * - * A wrapper around a contiguous block of allocated memory. - */ -#ifndef PRISM_BUFFER_H -#define PRISM_BUFFER_H - -#include "prism/defines.h" - -#include <assert.h> -#include <stdbool.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> - -/** - * A pm_buffer_t is a simple memory buffer that stores data in a contiguous - * block of memory. - */ -typedef struct { - /** The length of the buffer in bytes. */ - size_t length; - - /** The capacity of the buffer in bytes that has been allocated. */ - size_t capacity; - - /** A pointer to the start of the buffer. */ - char *value; -} pm_buffer_t; - -/** - * Return the size of the pm_buffer_t struct. - * - * @returns The size of the pm_buffer_t struct. - */ -PRISM_EXPORTED_FUNCTION size_t pm_buffer_sizeof(void); - -/** - * Initialize a pm_buffer_t with the given capacity. - * - * @param buffer The buffer to initialize. - * @param capacity The capacity of the buffer. - * @returns True if the buffer was initialized successfully, false otherwise. - */ -bool pm_buffer_init_capacity(pm_buffer_t *buffer, size_t capacity); - -/** - * Initialize a pm_buffer_t with its default values. - * - * @param buffer The buffer to initialize. - * @returns True if the buffer was initialized successfully, false otherwise. - */ -PRISM_EXPORTED_FUNCTION bool pm_buffer_init(pm_buffer_t *buffer); - -/** - * Return the value of the buffer. - * - * @param buffer The buffer to get the value of. - * @returns The value of the buffer. - */ -PRISM_EXPORTED_FUNCTION char * pm_buffer_value(pm_buffer_t *buffer); - -/** - * Return the length of the buffer. - * - * @param buffer The buffer to get the length of. - * @returns The length of the buffer. - */ -PRISM_EXPORTED_FUNCTION size_t pm_buffer_length(pm_buffer_t *buffer); - -/** - * Append the given amount of space as zeroes to the buffer. - * - * @param buffer The buffer to append to. - * @param length The amount of space to append and zero. - */ -void pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length); - -/** - * Append a formatted string to the buffer. - * - * @param buffer The buffer to append to. - * @param format The format string to append. - * @param ... The arguments to the format string. - */ -void pm_buffer_append_format(pm_buffer_t *buffer, const char *format, ...) PRISM_ATTRIBUTE_FORMAT(2, 3); - -/** - * Append a string to the buffer. - * - * @param buffer The buffer to append to. - * @param value The string to append. - * @param length The length of the string to append. - */ -void pm_buffer_append_string(pm_buffer_t *buffer, const char *value, size_t length); - -/** - * Append a list of bytes to the buffer. - * - * @param buffer The buffer to append to. - * @param value The bytes to append. - * @param length The length of the bytes to append. - */ -void pm_buffer_append_bytes(pm_buffer_t *buffer, const uint8_t *value, size_t length); - -/** - * Append a single byte to the buffer. - * - * @param buffer The buffer to append to. - * @param value The byte to append. - */ -void pm_buffer_append_byte(pm_buffer_t *buffer, uint8_t value); - -/** - * Append a 32-bit unsigned integer to the buffer as a variable-length integer. - * - * @param buffer The buffer to append to. - * @param value The integer to append. - */ -void pm_buffer_append_varuint(pm_buffer_t *buffer, uint32_t value); - -/** - * Append a 32-bit signed integer to the buffer as a variable-length integer. - * - * @param buffer The buffer to append to. - * @param value The integer to append. - */ -void pm_buffer_append_varsint(pm_buffer_t *buffer, int32_t value); - -/** - * Concatenate one buffer onto another. - * - * @param destination The buffer to concatenate onto. - * @param source The buffer to concatenate. - */ -void pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source); - -/** - * Free the memory associated with the buffer. - * - * @param buffer The buffer to free. - */ -PRISM_EXPORTED_FUNCTION void pm_buffer_free(pm_buffer_t *buffer); - -#endif diff --git a/prism/util/pm_char.c b/prism/util/pm_char.c deleted file mode 100644 index 13eddbba48..0000000000 --- a/prism/util/pm_char.c +++ /dev/null @@ -1,318 +0,0 @@ -#include "prism/util/pm_char.h" - -#define PRISM_CHAR_BIT_WHITESPACE (1 << 0) -#define PRISM_CHAR_BIT_INLINE_WHITESPACE (1 << 1) -#define PRISM_CHAR_BIT_REGEXP_OPTION (1 << 2) - -#define PRISM_NUMBER_BIT_BINARY_DIGIT (1 << 0) -#define PRISM_NUMBER_BIT_BINARY_NUMBER (1 << 1) -#define PRISM_NUMBER_BIT_OCTAL_DIGIT (1 << 2) -#define PRISM_NUMBER_BIT_OCTAL_NUMBER (1 << 3) -#define PRISM_NUMBER_BIT_DECIMAL_DIGIT (1 << 4) -#define PRISM_NUMBER_BIT_DECIMAL_NUMBER (1 << 5) -#define PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT (1 << 6) -#define PRISM_NUMBER_BIT_HEXADECIMAL_NUMBER (1 << 7) - -static const uint8_t pm_byte_table[256] = { -// 0 1 2 3 4 5 6 7 8 9 A B C D E F - 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, 0, 0, // 0x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x - 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 4x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 5x - 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 4, 4, 4, // 6x - 0, 0, 0, 4, 0, 4, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, // 7x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx -}; - -static const uint8_t pm_number_table[256] = { - // 0 1 2 3 4 5 6 7 8 9 A B C D E F - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 1x - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 2x - 0xff, 0xff, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xf0, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 3x - 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 4x - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, // 5x - 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 6x - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 7x - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 8x - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9x - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Ax - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Bx - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Cx - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Dx - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Ex - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Fx -}; - -/** - * Returns the number of characters at the start of the string that match the - * given kind. Disallows searching past the given maximum number of characters. - */ -static inline size_t -pm_strspn_char_kind(const uint8_t *string, ptrdiff_t length, uint8_t kind) { - if (length <= 0) return 0; - - size_t size = 0; - size_t maximum = (size_t) length; - - while (size < maximum && (pm_byte_table[string[size]] & kind)) size++; - return size; -} - -/** - * Returns the number of characters at the start of the string that are - * whitespace. Disallows searching past the given maximum number of characters. - */ -size_t -pm_strspn_whitespace(const uint8_t *string, ptrdiff_t length) { - return pm_strspn_char_kind(string, length, PRISM_CHAR_BIT_WHITESPACE); -} - -/** - * Returns the number of characters at the start of the string that are - * whitespace while also tracking the location of each newline. Disallows - * searching past the given maximum number of characters. - */ -size_t -pm_strspn_whitespace_newlines(const uint8_t *string, ptrdiff_t length, pm_newline_list_t *newline_list) { - if (length <= 0) return 0; - - size_t size = 0; - size_t maximum = (size_t) length; - - while (size < maximum && (pm_byte_table[string[size]] & PRISM_CHAR_BIT_WHITESPACE)) { - if (string[size] == '\n') { - pm_newline_list_append(newline_list, string + size); - } - - size++; - } - - return size; -} - -/** - * Returns the number of characters at the start of the string that are inline - * whitespace. Disallows searching past the given maximum number of characters. - */ -size_t -pm_strspn_inline_whitespace(const uint8_t *string, ptrdiff_t length) { - return pm_strspn_char_kind(string, length, PRISM_CHAR_BIT_INLINE_WHITESPACE); -} - -/** - * Returns the number of characters at the start of the string that are regexp - * options. Disallows searching past the given maximum number of characters. - */ -size_t -pm_strspn_regexp_option(const uint8_t *string, ptrdiff_t length) { - return pm_strspn_char_kind(string, length, PRISM_CHAR_BIT_REGEXP_OPTION); -} - -/** - * Returns true if the given character matches the given kind. - */ -static inline bool -pm_char_is_char_kind(const uint8_t b, uint8_t kind) { - return (pm_byte_table[b] & kind) != 0; -} - -/** - * Returns true if the given character is a whitespace character. - */ -bool -pm_char_is_whitespace(const uint8_t b) { - return pm_char_is_char_kind(b, PRISM_CHAR_BIT_WHITESPACE); -} - -/** - * Returns true if the given character is an inline whitespace character. - */ -bool -pm_char_is_inline_whitespace(const uint8_t b) { - return pm_char_is_char_kind(b, PRISM_CHAR_BIT_INLINE_WHITESPACE); -} - -/** - * Scan through the string and return the number of characters at the start of - * the string that match the given kind. Disallows searching past the given - * maximum number of characters. - */ -static inline size_t -pm_strspn_number_kind(const uint8_t *string, ptrdiff_t length, uint8_t kind) { - if (length <= 0) return 0; - - size_t size = 0; - size_t maximum = (size_t) length; - - while (size < maximum && (pm_number_table[string[size]] & kind)) size++; - return size; -} - -/** - * Scan through the string and return the number of characters at the start of - * the string that match the given kind. Disallows searching past the given - * maximum number of characters. - * - * Additionally, report the location of the last invalid underscore character - * found in the string through the out invalid parameter. - */ -static inline size_t -pm_strspn_number_kind_underscores(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid, uint8_t kind) { - if (length <= 0) return 0; - - size_t size = 0; - size_t maximum = (size_t) length; - - bool underscore = false; - while (size < maximum && (pm_number_table[string[size]] & kind)) { - if (string[size] == '_') { - if (underscore) *invalid = string + size; - underscore = true; - } else { - underscore = false; - } - - size++; - } - - if (string[size - 1] == '_') *invalid = string + size - 1; - return size; -} - -/** - * Returns the number of characters at the start of the string that are binary - * digits or underscores. Disallows searching past the given maximum number of - * characters. - * - * If multiple underscores are found in a row or if an underscore is - * found at the end of the number, then the invalid pointer is set to the index - * of the first invalid underscore. - */ -size_t -pm_strspn_binary_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid) { - return pm_strspn_number_kind_underscores(string, length, invalid, PRISM_NUMBER_BIT_BINARY_NUMBER); -} - -/** - * Returns the number of characters at the start of the string that are octal - * digits or underscores. Disallows searching past the given maximum number of - * characters. - * - * If multiple underscores are found in a row or if an underscore is - * found at the end of the number, then the invalid pointer is set to the index - * of the first invalid underscore. - */ -size_t -pm_strspn_octal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid) { - return pm_strspn_number_kind_underscores(string, length, invalid, PRISM_NUMBER_BIT_OCTAL_NUMBER); -} - -/** - * Returns the number of characters at the start of the string that are decimal - * digits. Disallows searching past the given maximum number of characters. - */ -size_t -pm_strspn_decimal_digit(const uint8_t *string, ptrdiff_t length) { - return pm_strspn_number_kind(string, length, PRISM_NUMBER_BIT_DECIMAL_DIGIT); -} - -/** - * Returns the number of characters at the start of the string that are decimal - * digits or underscores. Disallows searching past the given maximum number of - * characters. - * - * If multiple underscores are found in a row or if an underscore is - * found at the end of the number, then the invalid pointer is set to the index - * of the first invalid underscore - */ -size_t -pm_strspn_decimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid) { - return pm_strspn_number_kind_underscores(string, length, invalid, PRISM_NUMBER_BIT_DECIMAL_NUMBER); -} - -/** - * Returns the number of characters at the start of the string that are - * hexadecimal digits. Disallows searching past the given maximum number of - * characters. - */ -size_t -pm_strspn_hexadecimal_digit(const uint8_t *string, ptrdiff_t length) { - return pm_strspn_number_kind(string, length, PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT); -} - -/** - * Returns the number of characters at the start of the string that are - * hexadecimal digits or underscores. Disallows searching past the given maximum - * number of characters. - * - * If multiple underscores are found in a row or if an underscore is - * found at the end of the number, then the invalid pointer is set to the index - * of the first invalid underscore. - */ -size_t -pm_strspn_hexadecimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid) { - return pm_strspn_number_kind_underscores(string, length, invalid, PRISM_NUMBER_BIT_HEXADECIMAL_NUMBER); -} - -/** - * Returns true if the given character matches the given kind. - */ -static inline bool -pm_char_is_number_kind(const uint8_t b, uint8_t kind) { - return (pm_number_table[b] & kind) != 0; -} - -/** - * Returns true if the given character is a binary digit. - */ -bool -pm_char_is_binary_digit(const uint8_t b) { - return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_BINARY_DIGIT); -} - -/** - * Returns true if the given character is an octal digit. - */ -bool -pm_char_is_octal_digit(const uint8_t b) { - return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_OCTAL_DIGIT); -} - -/** - * Returns true if the given character is a decimal digit. - */ -bool -pm_char_is_decimal_digit(const uint8_t b) { - return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_DECIMAL_DIGIT); -} - -/** - * Returns true if the given character is a hexadecimal digit. - */ -bool -pm_char_is_hexadecimal_digit(const uint8_t b) { - return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT); -} - -#undef PRISM_CHAR_BIT_WHITESPACE -#undef PRISM_CHAR_BIT_INLINE_WHITESPACE -#undef PRISM_CHAR_BIT_REGEXP_OPTION - -#undef PRISM_NUMBER_BIT_BINARY_DIGIT -#undef PRISM_NUMBER_BIT_BINARY_NUMBER -#undef PRISM_NUMBER_BIT_OCTAL_DIGIT -#undef PRISM_NUMBER_BIT_OCTAL_NUMBER -#undef PRISM_NUMBER_BIT_DECIMAL_DIGIT -#undef PRISM_NUMBER_BIT_DECIMAL_NUMBER -#undef PRISM_NUMBER_BIT_HEXADECIMAL_NUMBER -#undef PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT diff --git a/prism/util/pm_char.h b/prism/util/pm_char.h deleted file mode 100644 index 32f698a42b..0000000000 --- a/prism/util/pm_char.h +++ /dev/null @@ -1,205 +0,0 @@ -/** - * @file pm_char.h - * - * Functions for working with characters and strings. - */ -#ifndef PRISM_CHAR_H -#define PRISM_CHAR_H - -#include "prism/defines.h" -#include "prism/util/pm_newline_list.h" - -#include <stdbool.h> -#include <stddef.h> - -/** - * Returns the number of characters at the start of the string that are - * whitespace. Disallows searching past the given maximum number of characters. - * - * @param string The string to search. - * @param length The maximum number of characters to search. - * @return The number of characters at the start of the string that are - * whitespace. - */ -size_t pm_strspn_whitespace(const uint8_t *string, ptrdiff_t length); - -/** - * Returns the number of characters at the start of the string that are - * whitespace while also tracking the location of each newline. Disallows - * searching past the given maximum number of characters. - * - * @param string The string to search. - * @param length The maximum number of characters to search. - * @param newline_list The list of newlines to populate. - * @return The number of characters at the start of the string that are - * whitespace. - */ -size_t -pm_strspn_whitespace_newlines(const uint8_t *string, ptrdiff_t length, pm_newline_list_t *newline_list); - -/** - * Returns the number of characters at the start of the string that are inline - * whitespace. Disallows searching past the given maximum number of characters. - * - * @param string The string to search. - * @param length The maximum number of characters to search. - * @return The number of characters at the start of the string that are inline - * whitespace. - */ -size_t pm_strspn_inline_whitespace(const uint8_t *string, ptrdiff_t length); - -/** - * Returns the number of characters at the start of the string that are decimal - * digits. Disallows searching past the given maximum number of characters. - * - * @param string The string to search. - * @param length The maximum number of characters to search. - * @return The number of characters at the start of the string that are decimal - * digits. - */ -size_t pm_strspn_decimal_digit(const uint8_t *string, ptrdiff_t length); - -/** - * Returns the number of characters at the start of the string that are - * hexadecimal digits. Disallows searching past the given maximum number of - * characters. - * - * @param string The string to search. - * @param length The maximum number of characters to search. - * @return The number of characters at the start of the string that are - * hexadecimal digits. - */ -size_t pm_strspn_hexadecimal_digit(const uint8_t *string, ptrdiff_t length); - -/** - * Returns the number of characters at the start of the string that are octal - * digits or underscores. Disallows searching past the given maximum number of - * characters. - * - * If multiple underscores are found in a row or if an underscore is - * found at the end of the number, then the invalid pointer is set to the index - * of the first invalid underscore. - * - * @param string The string to search. - * @param length The maximum number of characters to search. - * @param invalid The pointer to set to the index of the first invalid - * underscore. - * @return The number of characters at the start of the string that are octal - * digits or underscores. - */ -size_t pm_strspn_octal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid); - -/** - * Returns the number of characters at the start of the string that are decimal - * digits or underscores. Disallows searching past the given maximum number of - * characters. - * - * If multiple underscores are found in a row or if an underscore is - * found at the end of the number, then the invalid pointer is set to the index - * of the first invalid underscore. - * - * @param string The string to search. - * @param length The maximum number of characters to search. - * @param invalid The pointer to set to the index of the first invalid - * underscore. - * @return The number of characters at the start of the string that are decimal - * digits or underscores. - */ -size_t pm_strspn_decimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid); - -/** - * Returns the number of characters at the start of the string that are - * hexadecimal digits or underscores. Disallows searching past the given maximum - * number of characters. - * - * If multiple underscores are found in a row or if an underscore is - * found at the end of the number, then the invalid pointer is set to the index - * of the first invalid underscore. - * - * @param string The string to search. - * @param length The maximum number of characters to search. - * @param invalid The pointer to set to the index of the first invalid - * underscore. - * @return The number of characters at the start of the string that are - * hexadecimal digits or underscores. - */ -size_t pm_strspn_hexadecimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid); - -/** - * Returns the number of characters at the start of the string that are regexp - * options. Disallows searching past the given maximum number of characters. - * - * @param string The string to search. - * @param length The maximum number of characters to search. - * @return The number of characters at the start of the string that are regexp - * options. - */ -size_t pm_strspn_regexp_option(const uint8_t *string, ptrdiff_t length); - -/** - * Returns the number of characters at the start of the string that are binary - * digits or underscores. Disallows searching past the given maximum number of - * characters. - * - * If multiple underscores are found in a row or if an underscore is - * found at the end of the number, then the invalid pointer is set to the index - * of the first invalid underscore. - * - * @param string The string to search. - * @param length The maximum number of characters to search. - * @param invalid The pointer to set to the index of the first invalid - * underscore. - * @return The number of characters at the start of the string that are binary - * digits or underscores. - */ -size_t pm_strspn_binary_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid); - -/** - * Returns true if the given character is a whitespace character. - * - * @param b The character to check. - * @return True if the given character is a whitespace character. - */ -bool pm_char_is_whitespace(const uint8_t b); - -/** - * Returns true if the given character is an inline whitespace character. - * - * @param b The character to check. - * @return True if the given character is an inline whitespace character. - */ -bool pm_char_is_inline_whitespace(const uint8_t b); - -/** - * Returns true if the given character is a binary digit. - * - * @param b The character to check. - * @return True if the given character is a binary digit. - */ -bool pm_char_is_binary_digit(const uint8_t b); - -/** - * Returns true if the given character is an octal digit. - * - * @param b The character to check. - * @return True if the given character is an octal digit. - */ -bool pm_char_is_octal_digit(const uint8_t b); - -/** - * Returns true if the given character is a decimal digit. - * - * @param b The character to check. - * @return True if the given character is a decimal digit. - */ -bool pm_char_is_decimal_digit(const uint8_t b); - -/** - * Returns true if the given character is a hexadecimal digit. - * - * @param b The character to check. - * @return True if the given character is a hexadecimal digit. - */ -bool pm_char_is_hexadecimal_digit(const uint8_t b); - -#endif diff --git a/prism/util/pm_constant_pool.c b/prism/util/pm_constant_pool.c deleted file mode 100644 index e06682eb48..0000000000 --- a/prism/util/pm_constant_pool.c +++ /dev/null @@ -1,296 +0,0 @@ -#include "prism/util/pm_constant_pool.h" - -/** - * Initialize a list of constant ids. - */ -void -pm_constant_id_list_init(pm_constant_id_list_t *list) { - list->ids = NULL; - list->size = 0; - list->capacity = 0; -} - -/** - * Append a constant id to a list of constant ids. Returns false if any - * potential reallocations fail. - */ -bool -pm_constant_id_list_append(pm_constant_id_list_t *list, pm_constant_id_t id) { - if (list->size >= list->capacity) { - list->capacity = list->capacity == 0 ? 8 : list->capacity * 2; - list->ids = (pm_constant_id_t *) realloc(list->ids, sizeof(pm_constant_id_t) * list->capacity); - if (list->ids == NULL) return false; - } - - list->ids[list->size++] = id; - return true; -} - -/** - * Checks if the current constant id list includes the given constant id. - */ -bool -pm_constant_id_list_includes(pm_constant_id_list_t *list, pm_constant_id_t id) { - for (size_t index = 0; index < list->size; index++) { - if (list->ids[index] == id) return true; - } - return false; -} - -/** - * Get the memory size of a list of constant ids. - */ -size_t -pm_constant_id_list_memsize(pm_constant_id_list_t *list) { - return sizeof(pm_constant_id_list_t) + (list->capacity * sizeof(pm_constant_id_t)); -} - -/** - * Free the memory associated with a list of constant ids. - */ -void -pm_constant_id_list_free(pm_constant_id_list_t *list) { - if (list->ids != NULL) { - free(list->ids); - } -} - -/** - * A relatively simple hash function (djb2) that is used to hash strings. We are - * optimizing here for simplicity and speed. - */ -static inline uint32_t -pm_constant_pool_hash(const uint8_t *start, size_t length) { - // This is a prime number used as the initial value for the hash function. - uint32_t value = 5381; - - for (size_t index = 0; index < length; index++) { - value = ((value << 5) + value) + start[index]; - } - - return value; -} - -/** - * https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 - */ -static uint32_t -next_power_of_two(uint32_t v) { - // Avoid underflow in subtraction on next line. - if (v == 0) { - // 1 is the nearest power of 2 to 0 (2^0) - return 1; - } - v--; - v |= v >> 1; - v |= v >> 2; - v |= v >> 4; - v |= v >> 8; - v |= v >> 16; - v++; - return v; -} - -#ifndef NDEBUG -static bool -is_power_of_two(uint32_t size) { - return (size & (size - 1)) == 0; -} -#endif - -/** - * Resize a constant pool to a given capacity. - */ -static inline bool -pm_constant_pool_resize(pm_constant_pool_t *pool) { - assert(is_power_of_two(pool->capacity)); - - uint32_t next_capacity = pool->capacity * 2; - if (next_capacity < pool->capacity) return false; - - const uint32_t mask = next_capacity - 1; - const size_t element_size = sizeof(pm_constant_pool_bucket_t) + sizeof(pm_constant_t); - - void *next = calloc(next_capacity, element_size); - if (next == NULL) return false; - - pm_constant_pool_bucket_t *next_buckets = next; - pm_constant_t *next_constants = (void *)(((char *) next) + next_capacity * sizeof(pm_constant_pool_bucket_t)); - - // For each bucket in the current constant pool, find the index in the - // next constant pool, and insert it. - for (uint32_t index = 0; index < pool->capacity; index++) { - pm_constant_pool_bucket_t *bucket = &pool->buckets[index]; - - // If an id is set on this constant, then we know we have content here. - // In this case we need to insert it into the next constant pool. - if (bucket->id != 0) { - uint32_t next_index = bucket->hash & mask; - - // This implements linear scanning to find the next available slot - // in case this index is already taken. We don't need to bother - // comparing the values since we know that the hash is unique. - while (next_buckets[next_index].id != 0) { - next_index = (next_index + 1) & mask; - } - - // Here we copy over the entire bucket, which includes the id so - // that they are consistent between resizes. - next_buckets[next_index] = *bucket; - } - } - - // The constants are stable with respect to hash table resizes. - memcpy(next_constants, pool->constants, pool->size * sizeof(pm_constant_t)); - - // pool->constants and pool->buckets are allocated out of the same chunk - // of memory, with the buckets coming first. - free(pool->buckets); - pool->constants = next_constants; - pool->buckets = next_buckets; - pool->capacity = next_capacity; - return true; -} - -/** - * Initialize a new constant pool with a given capacity. - */ -bool -pm_constant_pool_init(pm_constant_pool_t *pool, uint32_t capacity) { - const uint32_t maximum = (~((uint32_t) 0)); - if (capacity >= ((maximum / 2) + 1)) return false; - - capacity = next_power_of_two(capacity); - const size_t element_size = sizeof(pm_constant_pool_bucket_t) + sizeof(pm_constant_t); - void *memory = calloc(capacity, element_size); - if (memory == NULL) return false; - - pool->buckets = memory; - pool->constants = (void *)(((char *)memory) + capacity * sizeof(pm_constant_pool_bucket_t)); - pool->size = 0; - pool->capacity = capacity; - return true; -} - -/** - * Return a pointer to the constant indicated by the given constant id. - */ -pm_constant_t * -pm_constant_pool_id_to_constant(const pm_constant_pool_t *pool, pm_constant_id_t constant_id) { - assert(constant_id > 0 && constant_id <= pool->size); - return &pool->constants[constant_id - 1]; -} - -/** - * Insert a constant into a constant pool and return its index in the pool. - */ -static inline pm_constant_id_t -pm_constant_pool_insert(pm_constant_pool_t *pool, const uint8_t *start, size_t length, pm_constant_pool_bucket_type_t type) { - if (pool->size >= (pool->capacity / 4 * 3)) { - if (!pm_constant_pool_resize(pool)) return 0; - } - - assert(is_power_of_two(pool->capacity)); - const uint32_t mask = pool->capacity - 1; - - uint32_t hash = pm_constant_pool_hash(start, length); - uint32_t index = hash & mask; - pm_constant_pool_bucket_t *bucket; - - while (bucket = &pool->buckets[index], bucket->id != 0) { - // If there is a collision, then we need to check if the content is the - // same as the content we are trying to insert. If it is, then we can - // return the id of the existing constant. - pm_constant_t *constant = &pool->constants[bucket->id - 1]; - - if ((constant->length == length) && memcmp(constant->start, start, length) == 0) { - // Since we have found a match, we need to check if this is - // attempting to insert a shared or an owned constant. We want to - // prefer shared constants since they don't require allocations. - if (type == PM_CONSTANT_POOL_BUCKET_OWNED) { - // If we're attempting to insert an owned constant and we have - // an existing constant, then either way we don't want the given - // memory. Either it's duplicated with the existing constant or - // it's not necessary because we have a shared version. - free((void *) start); - } else if (bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED) { - // If we're attempting to insert a shared constant and the - // existing constant is owned, then we can free the owned - // constant and replace it with the shared constant. - free((void *) constant->start); - constant->start = start; - bucket->type = (unsigned int) (PM_CONSTANT_POOL_BUCKET_DEFAULT & 0x3); - } - - return bucket->id; - } - - index = (index + 1) & mask; - } - - // IDs are allocated starting at 1, since the value 0 denotes a non-existant - // constant. - uint32_t id = ++pool->size; - assert(pool->size < ((uint32_t) (1 << 30))); - - *bucket = (pm_constant_pool_bucket_t) { - .id = (unsigned int) (id & 0x3fffffff), - .type = (unsigned int) (type & 0x3), - .hash = hash - }; - - pool->constants[id - 1] = (pm_constant_t) { - .start = start, - .length = length, - }; - - return id; -} - -/** - * Insert a constant into a constant pool. Returns the id of the constant, or 0 - * if any potential calls to resize fail. - */ -pm_constant_id_t -pm_constant_pool_insert_shared(pm_constant_pool_t *pool, const uint8_t *start, size_t length) { - return pm_constant_pool_insert(pool, start, length, PM_CONSTANT_POOL_BUCKET_DEFAULT); -} - -/** - * Insert a constant into a constant pool from memory that is now owned by the - * constant pool. Returns the id of the constant, or 0 if any potential calls to - * resize fail. - */ -pm_constant_id_t -pm_constant_pool_insert_owned(pm_constant_pool_t *pool, const uint8_t *start, size_t length) { - return pm_constant_pool_insert(pool, start, length, PM_CONSTANT_POOL_BUCKET_OWNED); -} - -/** - * Insert a constant into a constant pool from memory that is constant. Returns - * the id of the constant, or 0 if any potential calls to resize fail. - */ -pm_constant_id_t -pm_constant_pool_insert_constant(pm_constant_pool_t *pool, const uint8_t *start, size_t length) { - return pm_constant_pool_insert(pool, start, length, PM_CONSTANT_POOL_BUCKET_CONSTANT); -} - -/** - * Free the memory associated with a constant pool. - */ -void -pm_constant_pool_free(pm_constant_pool_t *pool) { - // For each constant in the current constant pool, free the contents if the - // contents are owned. - for (uint32_t index = 0; index < pool->capacity; index++) { - pm_constant_pool_bucket_t *bucket = &pool->buckets[index]; - - // If an id is set on this constant, then we know we have content here. - if (bucket->id != 0 && bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED) { - pm_constant_t *constant = &pool->constants[bucket->id - 1]; - free((void *) constant->start); - } - } - - free(pool->buckets); -} diff --git a/prism/util/pm_constant_pool.h b/prism/util/pm_constant_pool.h deleted file mode 100644 index ae5a88fbde..0000000000 --- a/prism/util/pm_constant_pool.h +++ /dev/null @@ -1,191 +0,0 @@ -/** - * @file pm_constant_pool.h - * - * A data structure that stores a set of strings. - * - * Each string is assigned a unique id, which can be used to compare strings for - * equality. This comparison ends up being much faster than strcmp, since it - * only requires a single integer comparison. - */ -#ifndef PRISM_CONSTANT_POOL_H -#define PRISM_CONSTANT_POOL_H - -#include "prism/defines.h" - -#include <assert.h> -#include <stdbool.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> - -/** - * A constant id is a unique identifier for a constant in the constant pool. - */ -typedef uint32_t pm_constant_id_t; - -/** - * A list of constant IDs. Usually used to represent a set of locals. - */ -typedef struct { - /** The number of constant ids in the list. */ - size_t size; - - /** The number of constant ids that have been allocated in the list. */ - size_t capacity; - - /** The constant ids in the list. */ - pm_constant_id_t *ids; -} pm_constant_id_list_t; - -/** - * Initialize a list of constant ids. - * - * @param list The list to initialize. - */ -void pm_constant_id_list_init(pm_constant_id_list_t *list); - -/** - * Append a constant id to a list of constant ids. Returns false if any - * potential reallocations fail. - * - * @param list The list to append to. - * @param id The id to append. - * @return Whether the append succeeded. - */ -bool pm_constant_id_list_append(pm_constant_id_list_t *list, pm_constant_id_t id); - -/** - * Checks if the current constant id list includes the given constant id. - * - * @param list The list to check. - * @param id The id to check for. - * @return Whether the list includes the given id. - */ -bool pm_constant_id_list_includes(pm_constant_id_list_t *list, pm_constant_id_t id); - -/** - * Get the memory size of a list of constant ids. - * - * @param list The list to get the memory size of. - * @return The memory size of the list. - */ -size_t pm_constant_id_list_memsize(pm_constant_id_list_t *list); - -/** - * Free the memory associated with a list of constant ids. - * - * @param list The list to free. - */ -void pm_constant_id_list_free(pm_constant_id_list_t *list); - -/** - * The type of bucket in the constant pool hash map. This determines how the - * bucket should be freed. - */ -typedef unsigned int pm_constant_pool_bucket_type_t; - -/** By default, each constant is a slice of the source. */ -static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_DEFAULT = 0; - -/** An owned constant is one for which memory has been allocated. */ -static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_OWNED = 1; - -/** A constant constant is known at compile time. */ -static const pm_constant_pool_bucket_type_t PM_CONSTANT_POOL_BUCKET_CONSTANT = 2; - -/** A bucket in the hash map. */ -typedef struct { - /** The incremental ID used for indexing back into the pool. */ - unsigned int id: 30; - - /** The type of the bucket, which determines how to free it. */ - pm_constant_pool_bucket_type_t type: 2; - - /** The hash of the bucket. */ - uint32_t hash; -} pm_constant_pool_bucket_t; - -/** A constant in the pool which effectively stores a string. */ -typedef struct { - /** A pointer to the start of the string. */ - const uint8_t *start; - - /** The length of the string. */ - size_t length; -} pm_constant_t; - -/** The overall constant pool, which stores constants found while parsing. */ -typedef struct { - /** The buckets in the hash map. */ - pm_constant_pool_bucket_t *buckets; - - /** The constants that are stored in the buckets. */ - pm_constant_t *constants; - - /** The number of buckets in the hash map. */ - uint32_t size; - - /** The number of buckets that have been allocated in the hash map. */ - uint32_t capacity; -} pm_constant_pool_t; - -/** - * Initialize a new constant pool with a given capacity. - * - * @param pool The pool to initialize. - * @param capacity The initial capacity of the pool. - * @return Whether the initialization succeeded. - */ -bool pm_constant_pool_init(pm_constant_pool_t *pool, uint32_t capacity); - -/** - * Return a pointer to the constant indicated by the given constant id. - * - * @param pool The pool to get the constant from. - * @param constant_id The id of the constant to get. - * @return A pointer to the constant. - */ -pm_constant_t * pm_constant_pool_id_to_constant(const pm_constant_pool_t *pool, pm_constant_id_t constant_id); - -/** - * Insert a constant into a constant pool that is a slice of a source string. - * Returns the id of the constant, or 0 if any potential calls to resize fail. - * - * @param pool The pool to insert the constant into. - * @param start A pointer to the start of the constant. - * @param length The length of the constant. - * @return The id of the constant. - */ -pm_constant_id_t pm_constant_pool_insert_shared(pm_constant_pool_t *pool, const uint8_t *start, size_t length); - -/** - * Insert a constant into a constant pool from memory that is now owned by the - * constant pool. Returns the id of the constant, or 0 if any potential calls to - * resize fail. - * - * @param pool The pool to insert the constant into. - * @param start A pointer to the start of the constant. - * @param length The length of the constant. - * @return The id of the constant. - */ -pm_constant_id_t pm_constant_pool_insert_owned(pm_constant_pool_t *pool, const uint8_t *start, size_t length); - -/** - * Insert a constant into a constant pool from memory that is constant. Returns - * the id of the constant, or 0 if any potential calls to resize fail. - * - * @param pool The pool to insert the constant into. - * @param start A pointer to the start of the constant. - * @param length The length of the constant. - * @return The id of the constant. - */ -pm_constant_id_t pm_constant_pool_insert_constant(pm_constant_pool_t *pool, const uint8_t *start, size_t length); - -/** - * Free the memory associated with a constant pool. - * - * @param pool The pool to free. - */ -void pm_constant_pool_free(pm_constant_pool_t *pool); - -#endif diff --git a/prism/util/pm_list.c b/prism/util/pm_list.c deleted file mode 100644 index 62cfe47cfa..0000000000 --- a/prism/util/pm_list.c +++ /dev/null @@ -1,49 +0,0 @@ -#include "prism/util/pm_list.h" - -/** - * Returns true if the given list is empty. - */ -PRISM_EXPORTED_FUNCTION bool -pm_list_empty_p(pm_list_t *list) { - return list->head == NULL; -} - -/** - * Returns the size of the list. - */ -PRISM_EXPORTED_FUNCTION size_t -pm_list_size(pm_list_t *list) { - return list->size; -} - -/** - * Append a node to the given list. - */ -void -pm_list_append(pm_list_t *list, pm_list_node_t *node) { - if (list->head == NULL) { - list->head = node; - } else { - list->tail->next = node; - } - - list->tail = node; - list->size++; -} - -/** - * Deallocate the internal state of the given list. - */ -PRISM_EXPORTED_FUNCTION void -pm_list_free(pm_list_t *list) { - pm_list_node_t *node = list->head; - pm_list_node_t *next; - - while (node != NULL) { - next = node->next; - free(node); - node = next; - } - - list->size = 0; -} diff --git a/prism/util/pm_list.h b/prism/util/pm_list.h deleted file mode 100644 index d29fe07c52..0000000000 --- a/prism/util/pm_list.h +++ /dev/null @@ -1,97 +0,0 @@ -/** - * @file pm_list.h - * - * An abstract linked list. - */ -#ifndef PRISM_LIST_H -#define PRISM_LIST_H - -#include "prism/defines.h" - -#include <stdbool.h> -#include <stddef.h> -#include <stdint.h> -#include <stdlib.h> - -/** - * This struct represents an abstract linked list that provides common - * functionality. It is meant to be used any time a linked list is necessary to - * store data. - * - * The linked list itself operates off a set of pointers. Because the pointers - * are not necessarily sequential, they can be of any size. We use this fact to - * allow the consumer of this linked list to extend the node struct to include - * any data they want. This is done by using the pm_list_node_t as the first - * member of the struct. - * - * For example, if we want to store a list of integers, we can do the following: - * - * ```c - * typedef struct { - * pm_list_node_t node; - * int value; - * } pm_int_node_t; - * - * pm_list_t list = { 0 }; - * pm_int_node_t *node = malloc(sizeof(pm_int_node_t)); - * node->value = 5; - * - * pm_list_append(&list, &node->node); - * ``` - * - * The pm_list_t struct is used to represent the overall linked list. It - * contains a pointer to the head and tail of the list. This allows for easy - * iteration and appending of new nodes. - */ -typedef struct pm_list_node { - /** A pointer to the next node in the list. */ - struct pm_list_node *next; -} pm_list_node_t; - -/** - * This represents the overall linked list. It keeps a pointer to the head and - * tail so that iteration is easy and pushing new nodes is easy. - */ -typedef struct { - /** The size of the list. */ - size_t size; - - /** A pointer to the head of the list. */ - pm_list_node_t *head; - - /** A pointer to the tail of the list. */ - pm_list_node_t *tail; -} pm_list_t; - -/** - * Returns true if the given list is empty. - * - * @param list The list to check. - * @return True if the given list is empty, otherwise false. - */ -PRISM_EXPORTED_FUNCTION bool pm_list_empty_p(pm_list_t *list); - -/** - * Returns the size of the list. - * - * @param list The list to check. - * @return The size of the list. - */ -PRISM_EXPORTED_FUNCTION size_t pm_list_size(pm_list_t *list); - -/** - * Append a node to the given list. - * - * @param list The list to append to. - * @param node The node to append. - */ -void pm_list_append(pm_list_t *list, pm_list_node_t *node); - -/** - * Deallocate the internal state of the given list. - * - * @param list The list to free. - */ -PRISM_EXPORTED_FUNCTION void pm_list_free(pm_list_t *list); - -#endif diff --git a/prism/util/pm_memchr.c b/prism/util/pm_memchr.c deleted file mode 100644 index 7ea20ace6d..0000000000 --- a/prism/util/pm_memchr.c +++ /dev/null @@ -1,35 +0,0 @@ -#include "prism/util/pm_memchr.h" - -#define PRISM_MEMCHR_TRAILING_BYTE_MINIMUM 0x40 - -/** - * We need to roll our own memchr to handle cases where the encoding changes and - * we need to search for a character in a buffer that could be the trailing byte - * of a multibyte character. - */ -void * -pm_memchr(const void *memory, int character, size_t number, bool encoding_changed, const pm_encoding_t *encoding) { - if (encoding_changed && encoding->multibyte && character >= PRISM_MEMCHR_TRAILING_BYTE_MINIMUM) { - const uint8_t *source = (const uint8_t *) memory; - size_t index = 0; - - while (index < number) { - if (source[index] == character) { - return (void *) (source + index); - } - - size_t width = encoding->char_width(source + index, (ptrdiff_t) (number - index)); - if (width == 0) { - return NULL; - } - - index += width; - } - - return NULL; - } else { - return memchr(memory, character, number); - } -} - -#undef PRISM_MEMCHR_TRAILING_BYTE_MINIMUM diff --git a/prism/util/pm_memchr.h b/prism/util/pm_memchr.h deleted file mode 100644 index e0671eaed3..0000000000 --- a/prism/util/pm_memchr.h +++ /dev/null @@ -1,29 +0,0 @@ -/** - * @file pm_memchr.h - * - * A custom memchr implementation. - */ -#ifndef PRISM_MEMCHR_H -#define PRISM_MEMCHR_H - -#include "prism/defines.h" -#include "prism/encoding.h" - -#include <stddef.h> - -/** - * We need to roll our own memchr to handle cases where the encoding changes and - * we need to search for a character in a buffer that could be the trailing byte - * of a multibyte character. - * - * @param source The source string. - * @param character The character to search for. - * @param number The maximum number of bytes to search. - * @param encoding_changed Whether the encoding changed. - * @param encoding A pointer to the encoding. - * @return A pointer to the first occurrence of the character in the source - * string, or NULL if no such character exists. - */ -void * pm_memchr(const void *source, int character, size_t number, bool encoding_changed, const pm_encoding_t *encoding); - -#endif diff --git a/prism/util/pm_newline_list.c b/prism/util/pm_newline_list.c deleted file mode 100644 index f27bb75b63..0000000000 --- a/prism/util/pm_newline_list.c +++ /dev/null @@ -1,96 +0,0 @@ -#include "prism/util/pm_newline_list.h" - -/** - * Initialize a new newline list with the given capacity. Returns true if the - * allocation of the offsets succeeds, otherwise returns false. - */ -bool -pm_newline_list_init(pm_newline_list_t *list, const uint8_t *start, size_t capacity) { - list->offsets = (size_t *) calloc(capacity, sizeof(size_t)); - if (list->offsets == NULL) return false; - - list->start = start; - - // This is 1 instead of 0 because we want to include the first line of the - // file as having offset 0, which is set because of calloc. - list->size = 1; - list->capacity = capacity; - - return true; -} - -/** - * Append a new offset to the newline list. Returns true if the reallocation of - * the offsets succeeds (if one was necessary), otherwise returns false. - */ -bool -pm_newline_list_append(pm_newline_list_t *list, const uint8_t *cursor) { - if (list->size == list->capacity) { - size_t *original_offsets = list->offsets; - - list->capacity = (list->capacity * 3) / 2; - list->offsets = (size_t *) calloc(list->capacity, sizeof(size_t)); - memcpy(list->offsets, original_offsets, list->size * sizeof(size_t)); - free(original_offsets); - if (list->offsets == NULL) return false; - } - - assert(*cursor == '\n'); - assert(cursor >= list->start); - size_t newline_offset = (size_t) (cursor - list->start + 1); - - assert(list->size == 0 || newline_offset > list->offsets[list->size - 1]); - list->offsets[list->size++] = newline_offset; - - return true; -} - -/** - * Conditionally append a new offset to the newline list, if the value passed in - * is a newline. - */ -bool -pm_newline_list_check_append(pm_newline_list_t *list, const uint8_t *cursor) { - if (*cursor != '\n') { - return true; - } - return pm_newline_list_append(list, cursor); -} - -/** - * Returns the line and column of the given offset. If the offset is not in the - * list, the line and column of the closest offset less than the given offset - * are returned. - */ -pm_line_column_t -pm_newline_list_line_column(const pm_newline_list_t *list, const uint8_t *cursor) { - assert(cursor >= list->start); - size_t offset = (size_t) (cursor - list->start); - - size_t left = 0; - size_t right = list->size - 1; - - while (left <= right) { - size_t mid = left + (right - left) / 2; - - if (list->offsets[mid] == offset) { - return ((pm_line_column_t) { mid, 0 }); - } - - if (list->offsets[mid] < offset) { - left = mid + 1; - } else { - right = mid - 1; - } - } - - return ((pm_line_column_t) { left - 1, offset - list->offsets[left - 1] }); -} - -/** - * Free the internal memory allocated for the newline list. - */ -void -pm_newline_list_free(pm_newline_list_t *list) { - free(list->offsets); -} diff --git a/prism/util/pm_newline_list.h b/prism/util/pm_newline_list.h deleted file mode 100644 index a31051f4e0..0000000000 --- a/prism/util/pm_newline_list.h +++ /dev/null @@ -1,104 +0,0 @@ -/** - * @file pm_newline_list.h - * - * A list of byte offsets of newlines in a string. - * - * When compiling the syntax tree, it's necessary to know the line and column - * of many nodes. This is necessary to support things like error messages, - * tracepoints, etc. - * - * It's possible that we could store the start line, start column, end line, and - * end column on every node in addition to the offsets that we already store, - * but that would be quite a lot of memory overhead. - */ -#ifndef PRISM_NEWLINE_LIST_H -#define PRISM_NEWLINE_LIST_H - -#include "prism/defines.h" - -#include <assert.h> -#include <stdbool.h> -#include <stddef.h> -#include <stdlib.h> - -/** - * A list of offsets of newlines in a string. The offsets are assumed to be - * sorted/inserted in ascending order. - */ -typedef struct { - /** A pointer to the start of the source string. */ - const uint8_t *start; - - /** The number of offsets in the list. */ - size_t size; - - /** The capacity of the list that has been allocated. */ - size_t capacity; - - /** The list of offsets. */ - size_t *offsets; -} pm_newline_list_t; - -/** - * A line and column in a string. - */ -typedef struct { - /** The line number. */ - size_t line; - - /** The column number. */ - size_t column; -} pm_line_column_t; - -/** - * Initialize a new newline list with the given capacity. Returns true if the - * allocation of the offsets succeeds, otherwise returns false. - * - * @param list The list to initialize. - * @param start A pointer to the start of the source string. - * @param capacity The initial capacity of the list. - * @return True if the allocation of the offsets succeeds, otherwise false. - */ -bool pm_newline_list_init(pm_newline_list_t *list, const uint8_t *start, size_t capacity); - -/** - * Append a new offset to the newline list. Returns true if the reallocation of - * the offsets succeeds (if one was necessary), otherwise returns false. - * - * @param list The list to append to. - * @param cursor A pointer to the offset to append. - * @return True if the reallocation of the offsets succeeds (if one was - * necessary), otherwise false. - */ -bool pm_newline_list_append(pm_newline_list_t *list, const uint8_t *cursor); - -/** - * Conditionally append a new offset to the newline list, if the value passed in - * is a newline. - * - * @param list The list to append to. - * @param cursor A pointer to the offset to append. - * @return True if the reallocation of the offsets succeeds (if one was - * necessary), otherwise false. - */ -bool pm_newline_list_check_append(pm_newline_list_t *list, const uint8_t *cursor); - -/** - * Returns the line and column of the given offset. If the offset is not in the - * list, the line and column of the closest offset less than the given offset - * are returned. - * - * @param list The list to search. - * @param cursor A pointer to the offset to search for. - * @return The line and column of the given offset. - */ -pm_line_column_t pm_newline_list_line_column(const pm_newline_list_t *list, const uint8_t *cursor); - -/** - * Free the internal memory allocated for the newline list. - * - * @param list The list to free. - */ -void pm_newline_list_free(pm_newline_list_t *list); - -#endif diff --git a/prism/util/pm_state_stack.c b/prism/util/pm_state_stack.c deleted file mode 100644 index 2a424b4c03..0000000000 --- a/prism/util/pm_state_stack.c +++ /dev/null @@ -1,25 +0,0 @@ -#include "prism/util/pm_state_stack.h" - -/** - * Pushes a value onto the stack. - */ -void -pm_state_stack_push(pm_state_stack_t *stack, bool value) { - *stack = (*stack << 1) | (value & 1); -} - -/** - * Pops a value off the stack. - */ -void -pm_state_stack_pop(pm_state_stack_t *stack) { - *stack >>= 1; -} - -/** - * Returns the value at the top of the stack. - */ -bool -pm_state_stack_p(pm_state_stack_t *stack) { - return *stack & 1; -} diff --git a/prism/util/pm_state_stack.h b/prism/util/pm_state_stack.h deleted file mode 100644 index 1ce57a2209..0000000000 --- a/prism/util/pm_state_stack.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * @file pm_state_stack.h - * - * A stack of boolean values. - */ -#ifndef PRISM_STATE_STACK_H -#define PRISM_STATE_STACK_H - -#include "prism/defines.h" - -#include <stdbool.h> -#include <stdint.h> - -/** - * A struct that represents a stack of boolean values. - */ -typedef uint32_t pm_state_stack_t; - -/** - * Pushes a value onto the stack. - * - * @param stack The stack to push the value onto. - * @param value The value to push onto the stack. - */ -void pm_state_stack_push(pm_state_stack_t *stack, bool value); - -/** - * Pops a value off the stack. - * - * @param stack The stack to pop the value off of. - */ -void pm_state_stack_pop(pm_state_stack_t *stack); - -/** - * Returns the value at the top of the stack. - * - * @param stack The stack to get the value from. - * @return The value at the top of the stack. - */ -bool pm_state_stack_p(pm_state_stack_t *stack); - -#endif diff --git a/prism/util/pm_string.c b/prism/util/pm_string.c deleted file mode 100644 index f4d3033a1b..0000000000 --- a/prism/util/pm_string.c +++ /dev/null @@ -1,210 +0,0 @@ -#include "prism/util/pm_string.h" - -/** - * Returns the size of the pm_string_t struct. This is necessary to allocate the - * correct amount of memory in the FFI backend. - */ -PRISM_EXPORTED_FUNCTION size_t -pm_string_sizeof(void) { - return sizeof(pm_string_t); -} - -/** - * Initialize a shared string that is based on initial input. - */ -void -pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end) { - assert(start <= end); - - *string = (pm_string_t) { - .type = PM_STRING_SHARED, - .source = start, - .length = (size_t) (end - start) - }; -} - -/** - * Initialize an owned string that is responsible for freeing allocated memory. - */ -void -pm_string_owned_init(pm_string_t *string, uint8_t *source, size_t length) { - *string = (pm_string_t) { - .type = PM_STRING_OWNED, - .source = source, - .length = length - }; -} - -/** - * Initialize a constant string that doesn't own its memory source. - */ -void -pm_string_constant_init(pm_string_t *string, const char *source, size_t length) { - *string = (pm_string_t) { - .type = PM_STRING_CONSTANT, - .source = (const uint8_t *) source, - .length = length - }; -} - -/** - * 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`. - */ -bool -pm_string_mapped_init(pm_string_t *string, const char *filepath) { -#ifdef _WIN32 - // Open the file for reading. - HANDLE file = CreateFile(filepath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - - if (file == INVALID_HANDLE_VALUE) { - perror("CreateFile failed"); - return false; - } - - // Get the file size. - DWORD file_size = GetFileSize(file, NULL); - if (file_size == INVALID_FILE_SIZE) { - CloseHandle(file); - perror("GetFileSize failed"); - return false; - } - - // If the file is empty, then we don't need to do anything else, we'll set - // the source to a constant empty string and return. - if (file_size == 0) { - CloseHandle(file); - const uint8_t source[] = ""; - *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 }; - return true; - } - - // Create a mapping of the file. - HANDLE mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL); - if (mapping == NULL) { - CloseHandle(file); - perror("CreateFileMapping failed"); - return false; - } - - // Map the file into memory. - uint8_t *source = (uint8_t *) MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0); - CloseHandle(mapping); - CloseHandle(file); - - if (source == NULL) { - perror("MapViewOfFile failed"); - return false; - } - - *string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = (size_t) file_size }; - return true; -#else - // Open the file for reading - int fd = open(filepath, O_RDONLY); - if (fd == -1) { - perror("open"); - return false; - } - - // Stat the file to get the file size - struct stat sb; - if (fstat(fd, &sb) == -1) { - close(fd); - perror("fstat"); - return false; - } - - // mmap the file descriptor to virtually get the contents - size_t size = (size_t) sb.st_size; - uint8_t *source = NULL; - - if (size == 0) { - close(fd); - const uint8_t source[] = ""; - *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 }; - return true; - } - - source = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); - if (source == MAP_FAILED) { - perror("Map failed"); - return false; - } - - close(fd); - *string = (pm_string_t) { .type = PM_STRING_MAPPED, .source = source, .length = size }; - return true; -#endif -} - -/** - * Returns the memory size associated with the string. - */ -size_t -pm_string_memsize(const pm_string_t *string) { - size_t size = sizeof(pm_string_t); - if (string->type == PM_STRING_OWNED) { - size += string->length; - } - return size; -} - -/** - * Ensure the string is owned. If it is not, then reinitialize it as owned and - * copy over the previous source. - */ -void -pm_string_ensure_owned(pm_string_t *string) { - if (string->type == PM_STRING_OWNED) return; - - size_t length = pm_string_length(string); - const uint8_t *source = pm_string_source(string); - - uint8_t *memory = malloc(length); - if (!memory) return; - - pm_string_owned_init(string, memory, length); - memcpy((void *) string->source, source, length); -} - -/** - * Returns the length associated with the string. - */ -PRISM_EXPORTED_FUNCTION size_t -pm_string_length(const pm_string_t *string) { - return string->length; -} - -/** - * Returns the start pointer associated with the string. - */ -PRISM_EXPORTED_FUNCTION const uint8_t * -pm_string_source(const pm_string_t *string) { - return string->source; -} - -/** - * Free the associated memory of the given string. - */ -PRISM_EXPORTED_FUNCTION void -pm_string_free(pm_string_t *string) { - void *memory = (void *) string->source; - - if (string->type == PM_STRING_OWNED) { - free(memory); - } else if (string->type == PM_STRING_MAPPED && string->length) { -#if defined(_WIN32) - UnmapViewOfFile(memory); -#else - munmap(memory, string->length); -#endif - } -} diff --git a/prism/util/pm_string.h b/prism/util/pm_string.h deleted file mode 100644 index ddb153784f..0000000000 --- a/prism/util/pm_string.h +++ /dev/null @@ -1,150 +0,0 @@ -/** - * @file pm_string.h - * - * A generic string type that can have various ownership semantics. - */ -#ifndef PRISM_STRING_H -#define PRISM_STRING_H - -#include "prism/defines.h" - -#include <assert.h> -#include <stdbool.h> -#include <stddef.h> -#include <stdlib.h> -#include <string.h> - -// The following headers are necessary to read files using demand paging. -#ifdef _WIN32 -#include <windows.h> -#else -#include <fcntl.h> -#include <sys/mman.h> -#include <sys/stat.h> -#include <unistd.h> -#endif - -/** - * A generic string type that can have various ownership semantics. - */ -typedef struct { - /** A pointer to the start of the string. */ - const uint8_t *source; - - /** The length of the string in bytes of memory. */ - size_t length; - - /** The type of the string. This field determines how the string should be freed. */ - enum { - /** This string is a constant string, and should not be freed. */ - PM_STRING_CONSTANT, - - /** This is a slice of another string, and should not be freed. */ - PM_STRING_SHARED, - - /** This string owns its memory, and should be freed using `pm_string_free`. */ - PM_STRING_OWNED, - - /** This string is a memory-mapped file, and should be freed using `pm_string_free`. */ - PM_STRING_MAPPED - } type; -} pm_string_t; - -/** - * Returns the size of the pm_string_t struct. This is necessary to allocate the - * correct amount of memory in the FFI backend. - * - * @return The size of the pm_string_t struct. - */ -PRISM_EXPORTED_FUNCTION size_t pm_string_sizeof(void); - -/** - * Defines an empty string. This is useful for initializing a string that will - * be filled in later. - */ -#define PM_STRING_EMPTY ((pm_string_t) { .type = PM_STRING_CONSTANT, .source = NULL, .length = 0 }) - -/** - * Initialize a shared string that is based on initial input. - * - * @param string The string to initialize. - * @param start The start of the string. - * @param end The end of the string. - */ -void pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end); - -/** - * Initialize an owned string that is responsible for freeing allocated memory. - * - * @param string The string to initialize. - * @param source The source of the string. - * @param length The length of the string. - */ -void pm_string_owned_init(pm_string_t *string, uint8_t *source, size_t length); - -/** - * Initialize a constant string that doesn't own its memory source. - * - * @param string The string to initialize. - * @param source The source of the string. - * @param length The length of the string. - */ -void pm_string_constant_init(pm_string_t *string, const char *source, size_t length); - -/** - * 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`. - * - * @param string The string to initialize. - * @param filepath The filepath to read. - * @return Whether or not the file was successfully mapped. - */ -PRISM_EXPORTED_FUNCTION bool pm_string_mapped_init(pm_string_t *string, const char *filepath); - -/** - * Returns the memory size associated with the string. - * - * @param string The string to get the memory size of. - * @return The size of the memory associated with the string. - */ -size_t pm_string_memsize(const pm_string_t *string); - -/** - * Ensure the string is owned. If it is not, then reinitialize it as owned and - * copy over the previous source. - * - * @param string The string to ensure is owned. - */ -void pm_string_ensure_owned(pm_string_t *string); - -/** - * Returns the length associated with the string. - * - * @param string The string to get the length of. - * @return The length of the string. - */ -PRISM_EXPORTED_FUNCTION size_t pm_string_length(const pm_string_t *string); - -/** - * Returns the start pointer associated with the string. - * - * @param string The string to get the start pointer of. - * @return The start pointer of the string. - */ -PRISM_EXPORTED_FUNCTION const uint8_t * pm_string_source(const pm_string_t *string); - -/** - * Free the associated memory of the given string. - * - * @param string The string to free. - */ -PRISM_EXPORTED_FUNCTION void pm_string_free(pm_string_t *string); - -#endif diff --git a/prism/util/pm_string_list.c b/prism/util/pm_string_list.c deleted file mode 100644 index d49e4ed734..0000000000 --- a/prism/util/pm_string_list.c +++ /dev/null @@ -1,28 +0,0 @@ -#include "prism/util/pm_string_list.h" - -/** - * Append a pm_string_t to the given string list. - */ -void -pm_string_list_append(pm_string_list_t *string_list, pm_string_t *string) { - if (string_list->length + 1 > string_list->capacity) { - if (string_list->capacity == 0) { - string_list->capacity = 1; - } else { - string_list->capacity *= 2; - } - - string_list->strings = realloc(string_list->strings, string_list->capacity * sizeof(pm_string_t)); - if (string_list->strings == NULL) abort(); - } - - string_list->strings[string_list->length++] = *string; -} - -/** - * Free the memory associated with the string list - */ -void -pm_string_list_free(pm_string_list_t *string_list) { - free(string_list->strings); -} diff --git a/prism/util/pm_string_list.h b/prism/util/pm_string_list.h deleted file mode 100644 index 0d406cc5d8..0000000000 --- a/prism/util/pm_string_list.h +++ /dev/null @@ -1,44 +0,0 @@ -/** - * @file pm_string_list.h - * - * A list of strings. - */ -#ifndef PRISM_STRING_LIST_H -#define PRISM_STRING_LIST_H - -#include "prism/defines.h" -#include "prism/util/pm_string.h" - -#include <stddef.h> -#include <stdlib.h> - -/** - * A list of strings. - */ -typedef struct { - /** The length of the string list. */ - size_t length; - - /** The capacity of the string list that has been allocated. */ - size_t capacity; - - /** A pointer to the start of the string list. */ - pm_string_t *strings; -} pm_string_list_t; - -/** - * Append a pm_string_t to the given string list. - * - * @param string_list The string list to append to. - * @param string The string to append. - */ -void pm_string_list_append(pm_string_list_t *string_list, pm_string_t *string); - -/** - * Free the memory associated with the string list. - * - * @param string_list The string list to free. - */ -PRISM_EXPORTED_FUNCTION void pm_string_list_free(pm_string_list_t *string_list); - -#endif diff --git a/prism/util/pm_strncasecmp.c b/prism/util/pm_strncasecmp.c deleted file mode 100644 index 2240bf8110..0000000000 --- a/prism/util/pm_strncasecmp.c +++ /dev/null @@ -1,24 +0,0 @@ -#include "prism/util/pm_strncasecmp.h" - -/** - * Compare two strings, ignoring case, up to the given length. Returns 0 if the - * strings are equal, a negative number if string1 is less than string2, or a - * positive number if string1 is greater than string2. - * - * Note that this is effectively our own implementation of strncasecmp, but it's - * not available on all of the platforms we want to support so we're rolling it - * here. - */ -int -pm_strncasecmp(const uint8_t *string1, const uint8_t *string2, size_t length) { - size_t offset = 0; - int difference = 0; - - while (offset < length && string1[offset] != '\0') { - if (string2[offset] == '\0') return string1[offset]; - if ((difference = tolower(string1[offset]) - tolower(string2[offset])) != 0) return difference; - offset++; - } - - return difference; -} diff --git a/prism/util/pm_strncasecmp.h b/prism/util/pm_strncasecmp.h deleted file mode 100644 index 5cb88cb5eb..0000000000 --- a/prism/util/pm_strncasecmp.h +++ /dev/null @@ -1,32 +0,0 @@ -/** - * @file pm_strncasecmp.h - * - * A custom strncasecmp implementation. - */ -#ifndef PRISM_STRNCASECMP_H -#define PRISM_STRNCASECMP_H - -#include "prism/defines.h" - -#include <ctype.h> -#include <stddef.h> -#include <stdint.h> - -/** - * Compare two strings, ignoring case, up to the given length. Returns 0 if the - * strings are equal, a negative number if string1 is less than string2, or a - * positive number if string1 is greater than string2. - * - * Note that this is effectively our own implementation of strncasecmp, but it's - * not available on all of the platforms we want to support so we're rolling it - * here. - * - * @param string1 The first string to compare. - * @param string2 The second string to compare - * @param length The maximum number of characters to compare. - * @return 0 if the strings are equal, a negative number if string1 is less than - * string2, or a positive number if string1 is greater than string2. - */ -int pm_strncasecmp(const uint8_t *string1, const uint8_t *string2, size_t length); - -#endif diff --git a/prism/util/pm_strpbrk.c b/prism/util/pm_strpbrk.c deleted file mode 100644 index 115eba1fd2..0000000000 --- a/prism/util/pm_strpbrk.c +++ /dev/null @@ -1,72 +0,0 @@ -#include "prism/util/pm_strpbrk.h" - -/** - * This is the slow path that does care about the encoding. - */ -static inline const uint8_t * -pm_strpbrk_multi_byte(const pm_parser_t *parser, const uint8_t *source, const uint8_t *charset, size_t maximum) { - size_t index = 0; - - while (index < maximum) { - if (strchr((const char *) charset, source[index]) != NULL) { - return source + index; - } - - size_t width = parser->encoding->char_width(source + index, (ptrdiff_t) (maximum - index)); - if (width == 0) { - return NULL; - } - - index += width; - } - - return NULL; -} - -/** - * This is the fast path that does not care about the encoding. - */ -static inline const uint8_t * -pm_strpbrk_single_byte(const uint8_t *source, const uint8_t *charset, size_t maximum) { - size_t index = 0; - - while (index < maximum) { - if (strchr((const char *) charset, source[index]) != NULL) { - return source + index; - } - - index++; - } - - return NULL; -} - -/** - * Here we have rolled our own version of strpbrk. The standard library strpbrk - * has undefined behavior when the source string is not null-terminated. We want - * to support strings that are not null-terminated because pm_parse does not - * have the contract that the string is null-terminated. (This is desirable - * because it means the extension can call pm_parse with the result of a call to - * mmap). - * - * The standard library strpbrk also does not support passing a maximum length - * to search. We want to support this for the reason mentioned above, but we - * also don't want it to stop on null bytes. Ruby actually allows null bytes - * within strings, comments, regular expressions, etc. So we need to be able to - * skip past them. - * - * Finally, we want to support encodings wherein the charset could contain - * characters that are trailing bytes of multi-byte characters. For example, in - * Shift-JIS, the backslash character can be a trailing byte. In that case we - * need to take a slower path and iterate one multi-byte character at a time. - */ -const uint8_t * -pm_strpbrk(const pm_parser_t *parser, const uint8_t *source, const uint8_t *charset, ptrdiff_t length) { - if (length <= 0) { - return NULL; - } else if (parser->encoding_changed && parser->encoding->multibyte) { - return pm_strpbrk_multi_byte(parser, source, charset, (size_t) length); - } else { - return pm_strpbrk_single_byte(source, charset, (size_t) length); - } -} diff --git a/prism/util/pm_strpbrk.h b/prism/util/pm_strpbrk.h deleted file mode 100644 index c1cf0d54db..0000000000 --- a/prism/util/pm_strpbrk.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * @file pm_strpbrk.h - * - * A custom strpbrk implementation. - */ -#ifndef PRISM_STRPBRK_H -#define PRISM_STRPBRK_H - -#include "prism/defines.h" -#include "prism/parser.h" - -#include <stddef.h> -#include <string.h> - -/** - * Here we have rolled our own version of strpbrk. The standard library strpbrk - * has undefined behavior when the source string is not null-terminated. We want - * to support strings that are not null-terminated because pm_parse does not - * have the contract that the string is null-terminated. (This is desirable - * because it means the extension can call pm_parse with the result of a call to - * mmap). - * - * The standard library strpbrk also does not support passing a maximum length - * to search. We want to support this for the reason mentioned above, but we - * also don't want it to stop on null bytes. Ruby actually allows null bytes - * within strings, comments, regular expressions, etc. So we need to be able to - * skip past them. - * - * Finally, we want to support encodings wherein the charset could contain - * characters that are trailing bytes of multi-byte characters. For example, in - * Shift-JIS, the backslash character can be a trailing byte. In that case we - * need to take a slower path and iterate one multi-byte character at a time. - * - * @param parser The parser. - * @param source The source to search. - * @param charset The charset to search for. - * @param length The maximum number of bytes to search. - * @return A pointer to the first character in the source string that is in the - * charset, or NULL if no such character exists. - */ -const uint8_t * pm_strpbrk(const pm_parser_t *parser, const uint8_t *source, const uint8_t *charset, ptrdiff_t length); - -#endif |
