Ruby 4.1.0dev (2026-03-20 revision eb04ab9117336f2b3613244cfe8a528c52faf6d6)
Macros | Functions | Variables
pm_char.h File Reference

(eb04ab9117336f2b3613244cfe8a528c52faf6d6)

Functions for working with characters and strings. More...

#include "prism/defines.h"
#include "prism/util/pm_line_offset_list.h"
#include <stdbool.h>
#include <stddef.h>
Include dependency graph for pm_char.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PRISM_CHAR_BIT_WHITESPACE   (1 << 0)
 Bit flag for whitespace characters in pm_byte_table.
 
#define PRISM_CHAR_BIT_INLINE_WHITESPACE   (1 << 1)
 Bit flag for inline whitespace characters in pm_byte_table.
 

Functions

static PRISM_FORCE_INLINE bool pm_char_is_whitespace (const uint8_t b)
 Returns true if the given character is a whitespace character.
 
static PRISM_FORCE_INLINE bool pm_char_is_inline_whitespace (const uint8_t b)
 Returns true if the given character is an inline whitespace character.
 
static PRISM_FORCE_INLINE 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 inline whitespace (space/tab).
 
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.
 
size_t pm_strspn_whitespace_newlines (const uint8_t *string, ptrdiff_t length, pm_arena_t *arena, pm_line_offset_list_t *line_offsets, uint32_t start_offset)
 Returns the number of characters at the start of the string that are whitespace while also tracking the location of each newline.
 
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 decimal 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 hexadecimal digits.
 
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 octal 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 decimal 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 hexadecimal digits or underscores.
 
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 regexp options.
 
size_t pm_strspn_binary_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 binary digits or underscores.
 
bool pm_char_is_binary_digit (const uint8_t b)
 Returns true if the given character is a binary digit.
 
bool pm_char_is_octal_digit (const uint8_t b)
 Returns true if the given character is an octal digit.
 
bool pm_char_is_decimal_digit (const uint8_t b)
 Returns true if the given character is a decimal digit.
 
bool pm_char_is_hexadecimal_digit (const uint8_t b)
 Returns true if the given character is a hexadecimal digit.
 

Variables

const uint8_t pm_byte_table [256]
 A lookup table for classifying bytes.
 

Detailed Description

Functions for working with characters and strings.

Definition in file pm_char.h.

Macro Definition Documentation

◆ PRISM_CHAR_BIT_INLINE_WHITESPACE

#define PRISM_CHAR_BIT_INLINE_WHITESPACE   (1 << 1)

Bit flag for inline whitespace characters in pm_byte_table.

Definition at line 19 of file pm_char.h.

◆ PRISM_CHAR_BIT_WHITESPACE

#define PRISM_CHAR_BIT_WHITESPACE   (1 << 0)

Bit flag for whitespace characters in pm_byte_table.

Definition at line 16 of file pm_char.h.

Function Documentation

◆ pm_char_is_binary_digit()

bool pm_char_is_binary_digit ( const uint8_t  b)

Returns true if the given character is a binary digit.

Parameters
bThe character to check.
Returns
True if the given character is a binary digit.

Definition at line 245 of file pm_char.c.

◆ pm_char_is_decimal_digit()

bool pm_char_is_decimal_digit ( const uint8_t  b)

Returns true if the given character is a decimal digit.

Parameters
bThe character to check.
Returns
True if the given character is a decimal digit.

Definition at line 261 of file pm_char.c.

◆ pm_char_is_hexadecimal_digit()

bool pm_char_is_hexadecimal_digit ( const uint8_t  b)

Returns true if the given character is a hexadecimal digit.

Parameters
bThe character to check.
Returns
True if the given character is a hexadecimal digit.

Definition at line 269 of file pm_char.c.

◆ pm_char_is_inline_whitespace()

static PRISM_FORCE_INLINE bool pm_char_is_inline_whitespace ( const uint8_t  b)
static

Returns true if the given character is an inline whitespace character.

Parameters
bThe character to check.
Returns
True if the given character is an inline whitespace character.

Definition at line 45 of file pm_char.h.

◆ pm_char_is_octal_digit()

bool pm_char_is_octal_digit ( const uint8_t  b)

Returns true if the given character is an octal digit.

Parameters
bThe character to check.
Returns
True if the given character is an octal digit.

Definition at line 253 of file pm_char.c.

◆ pm_char_is_whitespace()

static PRISM_FORCE_INLINE bool pm_char_is_whitespace ( const uint8_t  b)
static

Returns true if the given character is a whitespace character.

Parameters
bThe character to check.
Returns
True if the given character is a whitespace character.

Definition at line 34 of file pm_char.h.

Referenced by pm_buffer_rstrip().

◆ pm_strspn_binary_number()

size_t pm_strspn_binary_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 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.

Parameters
stringThe string to search.
lengthThe maximum number of characters to search.
invalidThe pointer to set to the index of the first invalid underscore.
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.

Definition at line 168 of file pm_char.c.

◆ pm_strspn_decimal_digit()

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 decimal digits.

Disallows searching past the given maximum number of characters.

Parameters
stringThe string to search.
lengthThe maximum number of characters to search.
Returns
The number of characters at the start of the string that are decimal digits.

Disallows searching past the given maximum number of characters.

Definition at line 191 of file pm_char.c.

◆ pm_strspn_decimal_number()

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

Parameters
stringThe string to search.
lengthThe maximum number of characters to search.
invalidThe pointer to set to the index of the first invalid underscore.
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

Definition at line 205 of file pm_char.c.

◆ pm_strspn_hexadecimal_digit()

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 hexadecimal digits.

Disallows searching past the given maximum number of characters.

Parameters
stringThe string to search.
lengthThe maximum number of characters to search.
Returns
The number of characters at the start of the string that are hexadecimal digits.

Disallows searching past the given maximum number of characters.

Definition at line 215 of file pm_char.c.

◆ pm_strspn_hexadecimal_number()

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

Parameters
stringThe string to search.
lengthThe maximum number of characters to search.
invalidThe pointer to set to the index of the first invalid underscore.
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.

Definition at line 229 of file pm_char.c.

◆ pm_strspn_inline_whitespace()

static PRISM_FORCE_INLINE size_t pm_strspn_inline_whitespace ( const uint8_t *  string,
ptrdiff_t  length 
)
static

Returns the number of characters at the start of the string that are inline whitespace (space/tab).

Scans the byte table directly for use in hot paths.

Parameters
stringThe string to search.
lengthThe maximum number of characters to search.
Returns
The number of characters at the start of the string that are inline whitespace.

Definition at line 59 of file pm_char.h.

Referenced by pm_parser::pm_parser_init().

◆ pm_strspn_octal_number()

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

Parameters
stringThe string to search.
lengthThe maximum number of characters to search.
invalidThe pointer to set to the index of the first invalid underscore.
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.

Definition at line 182 of file pm_char.c.

◆ pm_strspn_regexp_option()

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 regexp options.

Disallows searching past the given maximum number of characters.

Parameters
stringThe string to search.
lengthThe maximum number of characters to search.
Returns
The number of characters at the start of the string that are regexp options.

Disallows searching past the given maximum number of characters.

Definition at line 106 of file pm_char.c.

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

Disallows searching past the given maximum number of characters.

Parameters
stringThe string to search.
lengthThe maximum number of characters to search.
Returns
The number of characters at the start of the string that are whitespace.

Disallows searching past the given maximum number of characters.

Definition at line 74 of file pm_char.c.

◆ pm_strspn_whitespace_newlines()

size_t pm_strspn_whitespace_newlines ( const uint8_t *  string,
ptrdiff_t  length,
pm_arena_t *  arena,
pm_line_offset_list_t *  line_offsets,
uint32_t  start_offset 
)

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.

Parameters
stringThe string to search.
lengthThe maximum number of characters to search.
arenaThe arena to allocate from when appending to line_offsets.
line_offsetsThe list of newlines to populate.
start_offsetThe offset at which the string occurs in the source, for the purpose of tracking newlines.
Returns
The number of characters at the start of the string that are whitespace.

Disallows searching past the given maximum number of characters.

Definition at line 84 of file pm_char.c.

Variable Documentation

◆ pm_byte_table

const uint8_t pm_byte_table[256]
extern

A lookup table for classifying bytes.

Each entry is a bitfield of PRISM_CHAR_BIT_* flags. Defined in pm_char.c.

Definition at line 14 of file pm_char.c.

Referenced by pm_char_is_inline_whitespace(), pm_char_is_whitespace(), and pm_strspn_inline_whitespace().