Ruby 4.1.0dev (2026-11-18 revision 197b8f0103fb351bbe2909735c07b5cf907405c6)
char.h
1#ifndef PRISM_INTERNAL_CHAR_H
2#define PRISM_INTERNAL_CHAR_H
3
5
6#include "prism/arena.h"
8
9#include <stdbool.h>
10#include <stddef.h>
11#include <stdint.h>
12
13/* Bit flag for whitespace characters in pm_byte_table. */
14#define PRISM_CHAR_BIT_WHITESPACE (1 << 0)
15
16/* Bit flag for inline whitespace characters in pm_byte_table. */
17#define PRISM_CHAR_BIT_INLINE_WHITESPACE (1 << 1)
18
19/*
20 * A lookup table for classifying bytes. Each entry is a bitfield of
21 * PRISM_CHAR_BIT_* flags. Defined in char.c.
22 */
23extern const uint8_t pm_byte_table[256];
24
25/* Returns true if the given character is a whitespace character. */
26static PRISM_FORCE_INLINE bool
27pm_char_is_whitespace(const uint8_t b) {
28 return (pm_byte_table[b] & PRISM_CHAR_BIT_WHITESPACE) != 0;
29}
30
31/* Returns true if the given character is an inline whitespace character. */
32static PRISM_FORCE_INLINE bool
33pm_char_is_inline_whitespace(const uint8_t b) {
34 return (pm_byte_table[b] & PRISM_CHAR_BIT_INLINE_WHITESPACE) != 0;
35}
36
37/*
38 * Returns the number of characters at the start of the string that are inline
39 * whitespace (space/tab). Scans the byte table directly for use in hot paths.
40 */
41static PRISM_FORCE_INLINE size_t
42pm_strspn_inline_whitespace(const uint8_t *string, ptrdiff_t length) {
43 if (length <= 0) return 0;
44 size_t size = 0;
45 size_t maximum = (size_t) length;
46 while (size < maximum && (pm_byte_table[string[size]] & PRISM_CHAR_BIT_INLINE_WHITESPACE)) size++;
47 return size;
48}
49
50/*
51 * Returns the number of characters at the start of the string that are
52 * whitespace. Disallows searching past the given maximum number of characters.
53 */
54size_t pm_strspn_whitespace(const uint8_t *string, ptrdiff_t length);
55
56/*
57 * Returns the number of characters at the end of the string that are
58 * whitespace. Disallows searching past the given maximum number of characters.
59 */
60size_t pm_strrspn_whitespace(const uint8_t *string, ptrdiff_t length);
61
62/*
63 * Returns the number of characters at the start of the string that are
64 * whitespace while also tracking the location of each newline. Disallows
65 * searching past the given maximum number of characters.
66 */
67size_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);
68
69/*
70 * Returns the number of characters at the start of the string that are decimal
71 * digits. Disallows searching past the given maximum number of characters.
72 */
73size_t pm_strspn_decimal_digit(const uint8_t *string, ptrdiff_t length);
74
75/*
76 * Returns the number of characters at the start of the string that are
77 * hexadecimal digits. Disallows searching past the given maximum number of
78 * characters.
79 */
80size_t pm_strspn_hexadecimal_digit(const uint8_t *string, ptrdiff_t length);
81
82/*
83 * Returns the number of characters at the start of the string that are octal
84 * digits or underscores. Disallows searching past the given maximum number of
85 * characters.
86 *
87 * If multiple underscores are found in a row or if an underscore is
88 * found at the end of the number, then the invalid pointer is set to the index
89 * of the first invalid underscore.
90 */
91size_t pm_strspn_octal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
92
93/*
94 * Returns the number of characters at the start of the string that are decimal
95 * digits or underscores. Disallows searching past the given maximum number of
96 * characters.
97 *
98 * If multiple underscores are found in a row or if an underscore is
99 * found at the end of the number, then the invalid pointer is set to the index
100 * of the first invalid underscore.
101 */
102size_t pm_strspn_decimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
103
104/*
105 * Returns the number of characters at the start of the string that are
106 * hexadecimal digits or underscores. Disallows searching past the given maximum
107 * number of characters.
108 *
109 * If multiple underscores are found in a row or if an underscore is
110 * found at the end of the number, then the invalid pointer is set to the index
111 * of the first invalid underscore.
112 */
113size_t pm_strspn_hexadecimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
114
115/*
116 * Returns the number of characters at the start of the string that are regexp
117 * options. Disallows searching past the given maximum number of characters.
118 */
119size_t pm_strspn_regexp_option(const uint8_t *string, ptrdiff_t length);
120
121/*
122 * Returns the number of characters at the start of the string that are binary
123 * digits or underscores. Disallows searching past the given maximum number of
124 * characters.
125 *
126 * If multiple underscores are found in a row or if an underscore is
127 * found at the end of the number, then the invalid pointer is set to the index
128 * of the first invalid underscore.
129 */
130size_t pm_strspn_binary_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
131
132
133/* Returns true if the given character is a binary digit. */
134bool pm_char_is_binary_digit(const uint8_t b);
135
136/* Returns true if the given character is an octal digit. */
137bool pm_char_is_octal_digit(const uint8_t b);
138
139/* Returns true if the given character is a decimal digit. */
140bool pm_char_is_decimal_digit(const uint8_t b);
141
142/* Returns true if the given character is a hexadecimal digit. */
143bool pm_char_is_hexadecimal_digit(const uint8_t b);
144
145#endif
A bump allocator for the prism parser.
#define PRISM_FORCE_INLINE
Force a function to be inlined at every call site.
A list of byte offsets of newlines in a string.
C99 shim for <stdbool.h>.
A list of offsets of the start of lines in a string.