Ruby 4.1.0dev (2026-09-06 revision e536482403c19e68366c359a593e99e9514758d8)
char.c
1#include "prism/internal/char.h"
2
4#include "prism/internal/line_offset_list.h"
5
6#define PRISM_CHAR_BIT_REGEXP_OPTION (1 << 2)
7
8#define PRISM_NUMBER_BIT_BINARY_DIGIT (1 << 0)
9#define PRISM_NUMBER_BIT_BINARY_NUMBER (1 << 1)
10#define PRISM_NUMBER_BIT_OCTAL_DIGIT (1 << 2)
11#define PRISM_NUMBER_BIT_OCTAL_NUMBER (1 << 3)
12#define PRISM_NUMBER_BIT_DECIMAL_DIGIT (1 << 4)
13#define PRISM_NUMBER_BIT_DECIMAL_NUMBER (1 << 5)
14#define PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT (1 << 6)
15#define PRISM_NUMBER_BIT_HEXADECIMAL_NUMBER (1 << 7)
16
17const uint8_t pm_byte_table[256] = {
18// 0 1 2 3 4 5 6 7 8 9 A B C D E F
19 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, 0, 0, // 0x
20 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x
21 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x
22 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3x
23 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 4x
24 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, // 5x
25 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 6x
26 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, // 7x
27 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x
28 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x
29 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax
30 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx
31 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx
33 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx
35};
36
37static const uint8_t pm_number_table[256] = {
38 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
39 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 1x
41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 2x
42 0xff, 0xff, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xf0, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 3x
43 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 4x
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa, // 5x
45 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 6x
46 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 7x
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 8x
48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9x
49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Ax
50 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Bx
51 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Cx
52 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Dx
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Ex
54 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Fx
55};
56
61static PRISM_INLINE size_t
62pm_strspn_char_kind(const uint8_t *string, ptrdiff_t length, uint8_t kind) {
63 if (length <= 0) return 0;
64
65 size_t size = 0;
66 size_t maximum = (size_t) length;
67
68 while (size < maximum && (pm_byte_table[string[size]] & kind)) size++;
69 return size;
70}
71
76size_t
77pm_strspn_whitespace(const uint8_t *string, ptrdiff_t length) {
78 return pm_strspn_char_kind(string, length, PRISM_CHAR_BIT_WHITESPACE);
79}
80
86size_t
87pm_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) {
88 if (length <= 0) return 0;
89
90 uint32_t size = 0;
91 uint32_t maximum = (uint32_t) length;
92
93 while (size < maximum && (pm_byte_table[string[size]] & PRISM_CHAR_BIT_WHITESPACE)) {
94 if (string[size] == '\n') {
95 pm_line_offset_list_append(arena, line_offsets, start_offset + size + 1);
96 }
97
98 size++;
99 }
100
101 return size;
102}
103
108size_t
109pm_strspn_regexp_option(const uint8_t *string, ptrdiff_t length) {
110 return pm_strspn_char_kind(string, length, PRISM_CHAR_BIT_REGEXP_OPTION);
111}
112
113
119static PRISM_INLINE size_t
120pm_strspn_number_kind(const uint8_t *string, ptrdiff_t length, uint8_t kind) {
121 if (length <= 0) return 0;
122
123 size_t size = 0;
124 size_t maximum = (size_t) length;
125
126 while (size < maximum && (pm_number_table[string[size]] & kind)) size++;
127 return size;
128}
129
138static PRISM_INLINE size_t
139pm_strspn_number_kind_underscores(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid, uint8_t kind) {
140 if (length <= 0) return 0;
141
142 size_t size = 0;
143 size_t maximum = (size_t) length;
144
145 bool underscore = false;
146 while (size < maximum && (pm_number_table[string[size]] & kind)) {
147 if (string[size] == '_') {
148 if (underscore) *invalid = string + size;
149 underscore = true;
150 } else {
151 underscore = false;
152 }
153
154 size++;
155 }
156
157 if (size > 0 && string[size - 1] == '_') *invalid = string + size - 1;
158 return size;
159}
160
170size_t
171pm_strspn_binary_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid) {
172 return pm_strspn_number_kind_underscores(string, length, invalid, PRISM_NUMBER_BIT_BINARY_NUMBER);
173}
174
184size_t
185pm_strspn_octal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid) {
186 return pm_strspn_number_kind_underscores(string, length, invalid, PRISM_NUMBER_BIT_OCTAL_NUMBER);
187}
188
193size_t
194pm_strspn_decimal_digit(const uint8_t *string, ptrdiff_t length) {
195 return pm_strspn_number_kind(string, length, PRISM_NUMBER_BIT_DECIMAL_DIGIT);
196}
197
207size_t
208pm_strspn_decimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid) {
209 return pm_strspn_number_kind_underscores(string, length, invalid, PRISM_NUMBER_BIT_DECIMAL_NUMBER);
210}
211
217size_t
218pm_strspn_hexadecimal_digit(const uint8_t *string, ptrdiff_t length) {
219 return pm_strspn_number_kind(string, length, PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT);
220}
221
231size_t
232pm_strspn_hexadecimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid) {
233 return pm_strspn_number_kind_underscores(string, length, invalid, PRISM_NUMBER_BIT_HEXADECIMAL_NUMBER);
234}
235
239static PRISM_INLINE bool
240pm_char_is_number_kind(const uint8_t b, uint8_t kind) {
241 return (pm_number_table[b] & kind) != 0;
242}
243
247bool
248pm_char_is_binary_digit(const uint8_t b) {
249 return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_BINARY_DIGIT);
250}
251
255bool
256pm_char_is_octal_digit(const uint8_t b) {
257 return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_OCTAL_DIGIT);
258}
259
263bool
264pm_char_is_decimal_digit(const uint8_t b) {
265 return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_DECIMAL_DIGIT);
266}
267
271bool
272pm_char_is_hexadecimal_digit(const uint8_t b) {
273 return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT);
274}
#define PRISM_INLINE
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition inline.h:12
A list of offsets of the start of lines in a string.