Ruby 4.1.0dev (2026-09-14 revision 6ca2b168f3790e4972a01686f16f6b82524c699a)
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 start of the string that are
58 * whitespace while also tracking the location of each newline. Disallows
59 * searching past the given maximum number of characters.
60 */
61size_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);
62
63/*
64 * Returns the number of characters at the start of the string that are decimal
65 * digits. Disallows searching past the given maximum number of characters.
66 */
67size_t pm_strspn_decimal_digit(const uint8_t *string, ptrdiff_t length);
68
69/*
70 * Returns the number of characters at the start of the string that are
71 * hexadecimal digits. Disallows searching past the given maximum number of
72 * characters.
73 */
74size_t pm_strspn_hexadecimal_digit(const uint8_t *string, ptrdiff_t length);
75
76/*
77 * Returns the number of characters at the start of the string that are octal
78 * digits or underscores. Disallows searching past the given maximum number of
79 * characters.
80 *
81 * If multiple underscores are found in a row or if an underscore is
82 * found at the end of the number, then the invalid pointer is set to the index
83 * of the first invalid underscore.
84 */
85size_t pm_strspn_octal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
86
87/*
88 * Returns the number of characters at the start of the string that are decimal
89 * digits or underscores. Disallows searching past the given maximum number of
90 * characters.
91 *
92 * If multiple underscores are found in a row or if an underscore is
93 * found at the end of the number, then the invalid pointer is set to the index
94 * of the first invalid underscore.
95 */
96size_t pm_strspn_decimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
97
98/*
99 * Returns the number of characters at the start of the string that are
100 * hexadecimal digits or underscores. Disallows searching past the given maximum
101 * number of characters.
102 *
103 * If multiple underscores are found in a row or if an underscore is
104 * found at the end of the number, then the invalid pointer is set to the index
105 * of the first invalid underscore.
106 */
107size_t pm_strspn_hexadecimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
108
109/*
110 * Returns the number of characters at the start of the string that are regexp
111 * options. Disallows searching past the given maximum number of characters.
112 */
113size_t pm_strspn_regexp_option(const uint8_t *string, ptrdiff_t length);
114
115/*
116 * Returns the number of characters at the start of the string that are binary
117 * digits or underscores. Disallows searching past the given maximum number of
118 * characters.
119 *
120 * If multiple underscores are found in a row or if an underscore is
121 * found at the end of the number, then the invalid pointer is set to the index
122 * of the first invalid underscore.
123 */
124size_t pm_strspn_binary_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
125
126
127/* Returns true if the given character is a binary digit. */
128bool pm_char_is_binary_digit(const uint8_t b);
129
130/* Returns true if the given character is an octal digit. */
131bool pm_char_is_octal_digit(const uint8_t b);
132
133/* Returns true if the given character is a decimal digit. */
134bool pm_char_is_decimal_digit(const uint8_t b);
135
136/* Returns true if the given character is a hexadecimal digit. */
137bool pm_char_is_hexadecimal_digit(const uint8_t b);
138
139#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.