Line | Count | Source |
1 | | #include "prism/internal/char.h" |
2 | | |
3 | | #include "prism/compiler/inline.h" |
4 | | #include "prism/internal/line_offset_list.h" |
5 | | |
6 | 6.62k | #define PRISM_CHAR_BIT_REGEXP_OPTION (1 << 2) |
7 | | |
8 | 217 | #define PRISM_NUMBER_BIT_BINARY_DIGIT (1 << 0) |
9 | 193 | #define PRISM_NUMBER_BIT_BINARY_NUMBER (1 << 1) |
10 | 905 | #define PRISM_NUMBER_BIT_OCTAL_DIGIT (1 << 2) |
11 | 1.39k | #define PRISM_NUMBER_BIT_OCTAL_NUMBER (1 << 3) |
12 | 17.6k | #define PRISM_NUMBER_BIT_DECIMAL_DIGIT (1 << 4) |
13 | 2.55k | #define PRISM_NUMBER_BIT_DECIMAL_NUMBER (1 << 5) |
14 | 3.59k | #define PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT (1 << 6) |
15 | 171 | #define PRISM_NUMBER_BIT_HEXADECIMAL_NUMBER (1 << 7) |
16 | | |
17 | | const 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 | | |
37 | | static 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 | | |
57 | | /** |
58 | | * Returns the number of characters at the start of the string that match the |
59 | | * given kind. Disallows searching past the given maximum number of characters. |
60 | | */ |
61 | | static PRISM_INLINE size_t |
62 | 9.71k | pm_strspn_char_kind(const uint8_t *string, ptrdiff_t length, uint8_t kind) { |
63 | 9.71k | if (length <= 0) return 0145 ; Branch (63:9): [True: 145, False: 9.57k]
|
64 | | |
65 | 9.57k | size_t size = 0; |
66 | 9.57k | size_t maximum = (size_t) length; |
67 | | |
68 | 16.1k | while (size < maximum && (pm_byte_table[string[size]] & kind)16.1k ) size++6.61k ; Branch (68:12): [True: 16.1k, False: 8]
Branch (68:30): [True: 6.61k, False: 9.56k]
|
69 | 9.57k | return size; |
70 | 9.71k | } |
71 | | |
72 | | /** |
73 | | * Returns the number of characters at the start of the string that are |
74 | | * whitespace. Disallows searching past the given maximum number of characters. |
75 | | */ |
76 | | size_t |
77 | 3.09k | pm_strspn_whitespace(const uint8_t *string, ptrdiff_t length) { |
78 | 3.09k | return pm_strspn_char_kind(string, length, PRISM_CHAR_BIT_WHITESPACE); |
79 | 3.09k | } |
80 | | |
81 | | /** |
82 | | * Returns the number of characters at the end of the string that are |
83 | | * whitespace. Disallows searching past the given maximum number of characters. |
84 | | */ |
85 | | size_t |
86 | 144 | pm_strrspn_whitespace(const uint8_t *string, ptrdiff_t length) { |
87 | 144 | if (length <= 0) return 00 ; Branch (87:9): [True: 0, False: 144]
|
88 | | |
89 | 144 | size_t size = 0; |
90 | 144 | size_t maximum = (size_t) length; |
91 | | |
92 | 376 | while (size < maximum && (pm_byte_table[string[maximum - size - 1]] & PRISM_CHAR_BIT_WHITESPACE)) size++232 ; Branch (92:12): [True: 376, False: 0]
Branch (92:30): [True: 232, False: 144]
|
93 | 144 | return size; |
94 | 144 | } |
95 | | |
96 | | /** |
97 | | * Returns the number of characters at the start of the string that are |
98 | | * whitespace while also tracking the location of each newline. Disallows |
99 | | * searching past the given maximum number of characters. |
100 | | */ |
101 | | size_t |
102 | 52.0k | 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) { |
103 | 52.0k | if (length <= 0) return 0343 ; Branch (103:9): [True: 343, False: 51.7k]
|
104 | | |
105 | 51.7k | uint32_t size = 0; |
106 | 51.7k | uint32_t maximum = (uint32_t) length; |
107 | | |
108 | 83.2k | while (size < maximum && (pm_byte_table[string[size]] & 83.0k PRISM_CHAR_BIT_WHITESPACE83.0k )) { Branch (108:12): [True: 83.0k, False: 182]
Branch (108:30): [True: 31.5k, False: 51.5k]
|
109 | 31.5k | if (string[size] == '\n') { Branch (109:13): [True: 13.5k, False: 18.0k]
|
110 | 13.5k | pm_line_offset_list_append(arena, line_offsets, start_offset + size + 1); |
111 | 13.5k | } |
112 | | |
113 | 31.5k | size++; |
114 | 31.5k | } |
115 | | |
116 | 51.7k | return size; |
117 | 52.0k | } |
118 | | |
119 | | /** |
120 | | * Returns the number of characters at the start of the string that are regexp |
121 | | * options. Disallows searching past the given maximum number of characters. |
122 | | */ |
123 | | size_t |
124 | 6.62k | pm_strspn_regexp_option(const uint8_t *string, ptrdiff_t length) { |
125 | 6.62k | return pm_strspn_char_kind(string, length, PRISM_CHAR_BIT_REGEXP_OPTION); |
126 | 6.62k | } |
127 | | |
128 | | |
129 | | /** |
130 | | * Scan through the string and return the number of characters at the start of |
131 | | * the string that match the given kind. Disallows searching past the given |
132 | | * maximum number of characters. |
133 | | */ |
134 | | static PRISM_INLINE size_t |
135 | 1.69k | pm_strspn_number_kind(const uint8_t *string, ptrdiff_t length, uint8_t kind) { |
136 | 1.69k | if (length <= 0) return 01 ; Branch (136:9): [True: 1, False: 1.69k]
|
137 | | |
138 | 1.69k | size_t size = 0; |
139 | 1.69k | size_t maximum = (size_t) length; |
140 | | |
141 | 7.69k | while (size < maximum && (pm_number_table[string[size]] & kind)7.54k ) size++5.99k ; Branch (141:12): [True: 7.54k, False: 144]
Branch (141:30): [True: 5.99k, False: 1.55k]
|
142 | 1.69k | return size; |
143 | 1.69k | } |
144 | | |
145 | | /** |
146 | | * Scan through the string and return the number of characters at the start of |
147 | | * the string that match the given kind. Disallows searching past the given |
148 | | * maximum number of characters. |
149 | | * |
150 | | * Additionally, report the location of the last invalid underscore character |
151 | | * found in the string through the out invalid parameter. |
152 | | */ |
153 | | static PRISM_INLINE size_t |
154 | 4.31k | pm_strspn_number_kind_underscores(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid, uint8_t kind) { |
155 | 4.31k | if (length <= 0) return 02 ; Branch (155:9): [True: 2, False: 4.31k]
|
156 | | |
157 | 4.31k | size_t size = 0; |
158 | 4.31k | size_t maximum = (size_t) length; |
159 | | |
160 | 4.31k | bool underscore = false; |
161 | 21.0k | while (size < maximum && (pm_number_table[string[size]] & kind)21.0k ) { Branch (161:12): [True: 21.0k, False: 18]
Branch (161:30): [True: 16.7k, False: 4.29k]
|
162 | 16.7k | if (string[size] == '_') { Branch (162:13): [True: 2.13k, False: 14.6k]
|
163 | 2.13k | if (underscore) *invalid = string + size1.78k ; Branch (163:17): [True: 1.78k, False: 351]
|
164 | 2.13k | underscore = true; |
165 | 14.6k | } else { |
166 | 14.6k | underscore = false; |
167 | 14.6k | } |
168 | | |
169 | 16.7k | size++; |
170 | 16.7k | } |
171 | | |
172 | 4.31k | if (size > 0 && string[size - 1] == '_'2.67k ) *invalid = string + size - 1117 ; Branch (172:9): [True: 2.67k, False: 1.64k]
Branch (172:21): [True: 117, False: 2.55k]
|
173 | 4.31k | return size; |
174 | 4.31k | } |
175 | | |
176 | | /** |
177 | | * Returns the number of characters at the start of the string that are binary |
178 | | * digits or underscores. Disallows searching past the given maximum number of |
179 | | * characters. |
180 | | * |
181 | | * If multiple underscores are found in a row or if an underscore is |
182 | | * found at the end of the number, then the invalid pointer is set to the index |
183 | | * of the first invalid underscore. |
184 | | */ |
185 | | size_t |
186 | 193 | pm_strspn_binary_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid) { |
187 | 193 | return pm_strspn_number_kind_underscores(string, length, invalid, PRISM_NUMBER_BIT_BINARY_NUMBER); |
188 | 193 | } |
189 | | |
190 | | /** |
191 | | * Returns the number of characters at the start of the string that are octal |
192 | | * digits or underscores. Disallows searching past the given maximum number of |
193 | | * characters. |
194 | | * |
195 | | * If multiple underscores are found in a row or if an underscore is |
196 | | * found at the end of the number, then the invalid pointer is set to the index |
197 | | * of the first invalid underscore. |
198 | | */ |
199 | | size_t |
200 | 1.39k | pm_strspn_octal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid) { |
201 | 1.39k | return pm_strspn_number_kind_underscores(string, length, invalid, PRISM_NUMBER_BIT_OCTAL_NUMBER); |
202 | 1.39k | } |
203 | | |
204 | | /** |
205 | | * Returns the number of characters at the start of the string that are decimal |
206 | | * digits. Disallows searching past the given maximum number of characters. |
207 | | */ |
208 | | size_t |
209 | 525 | pm_strspn_decimal_digit(const uint8_t *string, ptrdiff_t length) { |
210 | 525 | return pm_strspn_number_kind(string, length, PRISM_NUMBER_BIT_DECIMAL_DIGIT); |
211 | 525 | } |
212 | | |
213 | | /** |
214 | | * Returns the number of characters at the start of the string that are decimal |
215 | | * digits or underscores. Disallows searching past the given maximum number of |
216 | | * characters. |
217 | | * |
218 | | * If multiple underscores are found in a row or if an underscore is |
219 | | * found at the end of the number, then the invalid pointer is set to the index |
220 | | * of the first invalid underscore |
221 | | */ |
222 | | size_t |
223 | 2.55k | pm_strspn_decimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid) { |
224 | 2.55k | return pm_strspn_number_kind_underscores(string, length, invalid, PRISM_NUMBER_BIT_DECIMAL_NUMBER); |
225 | 2.55k | } |
226 | | |
227 | | /** |
228 | | * Returns the number of characters at the start of the string that are |
229 | | * hexadecimal digits. Disallows searching past the given maximum number of |
230 | | * characters. |
231 | | */ |
232 | | size_t |
233 | 1.17k | pm_strspn_hexadecimal_digit(const uint8_t *string, ptrdiff_t length) { |
234 | 1.17k | return pm_strspn_number_kind(string, length, PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT); |
235 | 1.17k | } |
236 | | |
237 | | /** |
238 | | * Returns the number of characters at the start of the string that are |
239 | | * hexadecimal digits or underscores. Disallows searching past the given maximum |
240 | | * number of characters. |
241 | | * |
242 | | * If multiple underscores are found in a row or if an underscore is |
243 | | * found at the end of the number, then the invalid pointer is set to the index |
244 | | * of the first invalid underscore. |
245 | | */ |
246 | | size_t |
247 | 171 | pm_strspn_hexadecimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid) { |
248 | 171 | return pm_strspn_number_kind_underscores(string, length, invalid, PRISM_NUMBER_BIT_HEXADECIMAL_NUMBER); |
249 | 171 | } |
250 | | |
251 | | /** |
252 | | * Returns true if the given character matches the given kind. |
253 | | */ |
254 | | static PRISM_INLINE bool |
255 | 20.6k | pm_char_is_number_kind(const uint8_t b, uint8_t kind) { |
256 | 20.6k | return (pm_number_table[b] & kind) != 0; |
257 | 20.6k | } |
258 | | |
259 | | /** |
260 | | * Returns true if the given character is a binary digit. |
261 | | */ |
262 | | bool |
263 | 217 | pm_char_is_binary_digit(const uint8_t b) { |
264 | 217 | return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_BINARY_DIGIT); |
265 | 217 | } |
266 | | |
267 | | /** |
268 | | * Returns true if the given character is an octal digit. |
269 | | */ |
270 | | bool |
271 | 905 | pm_char_is_octal_digit(const uint8_t b) { |
272 | 905 | return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_OCTAL_DIGIT); |
273 | 905 | } |
274 | | |
275 | | /** |
276 | | * Returns true if the given character is a decimal digit. |
277 | | */ |
278 | | bool |
279 | 17.0k | pm_char_is_decimal_digit(const uint8_t b) { |
280 | 17.0k | return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_DECIMAL_DIGIT); |
281 | 17.0k | } |
282 | | |
283 | | /** |
284 | | * Returns true if the given character is a hexadecimal digit. |
285 | | */ |
286 | | bool |
287 | 2.42k | pm_char_is_hexadecimal_digit(const uint8_t b) { |
288 | 2.42k | return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT); |
289 | 2.42k | } |