Ruby 4.1.0dev (2026-11-19 revision b3e890bf959ffb14f55ecd016717417a62ebd04e)
strpbrk.c
1#include "prism/internal/strpbrk.h"
2
6
7#include "prism/internal/assert.h"
8#include "prism/internal/bit.h"
9#include "prism/internal/diagnostic.h"
10#include "prism/internal/encoding.h"
11#include "prism/internal/parser.h"
12
13#include <stdbool.h>
14#include <string.h>
15
19static PRISM_INLINE void
20pm_strpbrk_invalid_multibyte_character(pm_parser_t *parser, uint32_t start, uint32_t length) {
21 pm_diagnostic_list_append_format(&parser->metadata_arena, &parser->error_list, start, length, PM_ERR_INVALID_MULTIBYTE_CHARACTER, parser->start[start]);
22}
23
28static PRISM_INLINE void
29pm_strpbrk_invalid_multibyte_char(pm_parser_t *parser, const uint8_t *source) {
30 pm_diagnostic_list_append_format(&parser->metadata_arena, &parser->error_list, (uint32_t) (source - parser->start), 1, PM_ERR_INVALID_MULTIBYTE_CHAR, parser->encoding->name);
31}
32
36static PRISM_INLINE void
37pm_strpbrk_explicit_encoding_set(pm_parser_t *parser, uint32_t start, uint32_t length) {
38 if (parser->explicit_encoding != NULL) {
39 if (parser->explicit_encoding == parser->encoding) {
40 // Okay, we already locked to this encoding.
41 } else if (parser->explicit_encoding == PM_ENCODING_UTF_8_ENTRY) {
42 // Not okay, we already found a Unicode escape sequence and this
43 // conflicts.
44 pm_diagnostic_list_append_format(&parser->metadata_arena, &parser->error_list, start, length, PM_ERR_MIXED_ENCODING, parser->encoding->name);
45 } else {
46 // Should not be anything else.
47 PRISM_ASSERT(false && "unreachable");
48 }
49 }
50
51 parser->explicit_encoding = parser->encoding;
52}
53
69
70#if defined(PRISM_HAS_NEON) || defined(PRISM_HAS_SSE2) || defined(PRISM_HAS_SWAR)
71
82static PRISM_INLINE void
83pm_strpbrk_cache_update(pm_parser_t *parser, const uint8_t *charset) {
84 // The cache key is the full charset buffer (PM_STRPBRK_CACHE_SIZE bytes).
85 // Since it is always NUL-padded, a fixed-size comparison covers both
86 // content and length.
87 if (memcmp(parser->strpbrk_cache.charset, charset, sizeof(parser->strpbrk_cache.charset)) == 0) return;
88
89 memset(parser->strpbrk_cache.low_lut, 0, sizeof(parser->strpbrk_cache.low_lut));
90 memset(parser->strpbrk_cache.high_lut, 0, sizeof(parser->strpbrk_cache.high_lut));
91 memset(parser->strpbrk_cache.table, 0, sizeof(parser->strpbrk_cache.table));
92
93 // Always include NUL in the tables. The slow path uses strchr, which
94 // always matches NUL (it finds the C string terminator), so NUL is
95 // effectively always a breakpoint. Replicating that here lets the fast
96 // scanner handle NUL at full speed instead of bailing to the slow path.
97 parser->strpbrk_cache.low_lut[0x00] |= (uint8_t) (1 << 0);
98 parser->strpbrk_cache.high_lut[0x00] = (uint8_t) (1 << 0);
99 parser->strpbrk_cache.table[0] |= (uint64_t) 1;
100
101 size_t charset_len = 0;
102 for (const uint8_t *c = charset; *c != '\0'; c++) {
103 parser->strpbrk_cache.low_lut[*c & 0x0F] |= (uint8_t) (1 << (*c >> 4));
104 parser->strpbrk_cache.high_lut[*c >> 4] = (uint8_t) (1 << (*c >> 4));
105 parser->strpbrk_cache.table[*c >> 6] |= (uint64_t) 1 << (*c & 0x3F);
106 charset_len++;
107 }
108
109 // Store the new charset key, NUL-padded to the full buffer size.
110 memcpy(parser->strpbrk_cache.charset, charset, charset_len + 1);
111 memset(parser->strpbrk_cache.charset + charset_len + 1, 0, sizeof(parser->strpbrk_cache.charset) - charset_len - 1);
112}
113
114#endif
115
116#if defined(PRISM_HAS_NEON)
117#include <arm_neon.h>
118
119static PRISM_INLINE bool
120scan_strpbrk_ascii(pm_parser_t *parser, const uint8_t *source, size_t maximum, const uint8_t *charset, size_t *index) {
121 pm_strpbrk_cache_update(parser, charset);
122
123 uint8x16_t low_lut = vld1q_u8(parser->strpbrk_cache.low_lut);
124 uint8x16_t high_lut = vld1q_u8(parser->strpbrk_cache.high_lut);
125 uint8x16_t mask_0f = vdupq_n_u8(0x0F);
126 uint8x16_t mask_80 = vdupq_n_u8(0x80);
127
128 size_t idx = 0;
129
130 while (idx + 16 <= maximum) {
131 uint8x16_t v = vld1q_u8(source + idx);
132
133 // If any byte has the high bit set, we have non-ASCII data.
134 // Return to let the caller's encoding-aware loop handle it.
135 if (vmaxvq_u8(vandq_u8(v, mask_80)) != 0) break;
136
137 uint8x16_t lo_class = vqtbl1q_u8(low_lut, vandq_u8(v, mask_0f));
138 uint8x16_t hi_class = vqtbl1q_u8(high_lut, vshrq_n_u8(v, 4));
139 uint8x16_t matched = vtstq_u8(lo_class, hi_class);
140
141 if (vmaxvq_u8(matched) == 0) {
142 idx += 16;
143 continue;
144 }
145
146 // Find the position of the first matching byte.
147 uint64_t lo64 = vgetq_lane_u64(vreinterpretq_u64_u8(matched), 0);
148 if (lo64 != 0) {
149 *index = idx + pm_ctzll(lo64) / 8;
150 return true;
151 }
152 uint64_t hi64 = vgetq_lane_u64(vreinterpretq_u64_u8(matched), 1);
153 *index = idx + 8 + pm_ctzll(hi64) / 8;
154 return true;
155 }
156
157 // Scalar tail for remaining < 16 ASCII bytes.
158 while (idx < maximum && source[idx] < 0x80) {
159 uint8_t byte = source[idx];
160 if (parser->strpbrk_cache.table[byte >> 6] & ((uint64_t) 1 << (byte & 0x3F))) {
161 *index = idx;
162 return true;
163 }
164 idx++;
165 }
166
167 *index = idx;
168 return false;
169}
170
171#elif defined(PRISM_HAS_SSE2)
172#include <emmintrin.h>
173
174static PRISM_INLINE bool
175scan_strpbrk_ascii(pm_parser_t *parser, const uint8_t *source, size_t maximum, const uint8_t *charset, size_t *index) {
176 pm_strpbrk_cache_update(parser, charset);
177
178 /*
179 * SSE2 has no byte shuffle, so the nibble lookup used by NEON is not
180 * available. Instead, each charset byte is broadcast into its own vector
181 * and compared against the input directly. Charsets are short, so this is
182 * a handful of compares per 16 bytes.
183 */
184 __m128i needles[sizeof(parser->strpbrk_cache.charset)];
185 size_t needles_size = 0;
186
187 for (size_t i = 0; i < sizeof(parser->strpbrk_cache.charset) && parser->strpbrk_cache.charset[i] != '\0'; i++) {
188 needles[needles_size++] = _mm_set1_epi8((char) parser->strpbrk_cache.charset[i]);
189 }
190
191 const __m128i zero = _mm_setzero_si128();
192 size_t idx = 0;
193
194 while (idx + 16 <= maximum) {
195 __m128i v = _mm_loadu_si128((const __m128i *) (source + idx));
196
197 /* If any byte has the high bit set, stop. */
198 if (_mm_movemask_epi8(v) != 0) break;
199
200 /* NUL is always a breakpoint, matching pm_strpbrk_cache_update. */
201 __m128i matched = _mm_cmpeq_epi8(v, zero);
202 for (size_t i = 0; i < needles_size; i++) {
203 matched = _mm_or_si128(matched, _mm_cmpeq_epi8(v, needles[i]));
204 }
205
206 int mask = _mm_movemask_epi8(matched);
207
208 if (mask == 0) {
209 idx += 16;
210 continue;
211 }
212
213 /* Find the first matching byte. */
214 *index = idx + pm_ctzll((uint64_t) (mask & 0xFFFF));
215 return true;
216 }
217
218 /* Scalar tail. */
219 while (idx < maximum && source[idx] < 0x80) {
220 uint8_t byte = source[idx];
221 if (parser->strpbrk_cache.table[byte >> 6] & ((uint64_t) 1 << (byte & 0x3F))) {
222 *index = idx;
223 return true;
224 }
225 idx++;
226 }
227
228 *index = idx;
229 return false;
230}
231
232#elif defined(PRISM_HAS_SWAR)
233
234static PRISM_INLINE bool
235scan_strpbrk_ascii(pm_parser_t *parser, const uint8_t *source, size_t maximum, const uint8_t *charset, size_t *index) {
236 pm_strpbrk_cache_update(parser, charset);
237
238 static const uint64_t highs = 0x8080808080808080ULL;
239 size_t idx = 0;
240
241 while (idx + 8 <= maximum) {
242 uint64_t word;
243 memcpy(&word, source + idx, 8);
244
245 // Bail on any non-ASCII byte.
246 if (word & highs) break;
247
248 // Check each byte against the charset table.
249 for (size_t j = 0; j < 8; j++) {
250 uint8_t byte = source[idx + j];
251 if (parser->strpbrk_cache.table[byte >> 6] & ((uint64_t) 1 << (byte & 0x3F))) {
252 *index = idx + j;
253 return true;
254 }
255 }
256
257 idx += 8;
258 }
259
260 // Scalar tail.
261 while (idx < maximum && source[idx] < 0x80) {
262 uint8_t byte = source[idx];
263 if (parser->strpbrk_cache.table[byte >> 6] & ((uint64_t) 1 << (byte & 0x3F))) {
264 *index = idx;
265 return true;
266 }
267 idx++;
268 }
269
270 *index = idx;
271 return false;
272}
273
274#else
275
276static PRISM_INLINE bool
277scan_strpbrk_ascii(PRISM_UNUSED pm_parser_t *parser, PRISM_UNUSED const uint8_t *source, PRISM_UNUSED size_t maximum, PRISM_UNUSED const uint8_t *charset, size_t *index) {
278 *index = 0;
279 return false;
280}
281
282#endif
283
287static PRISM_INLINE const uint8_t *
288pm_strpbrk_utf8(pm_parser_t *parser, const uint8_t *source, const uint8_t *charset, size_t index, size_t maximum, pm_strpbrk_mode_t mode) {
289 while (index < maximum) {
290 if (strchr((const char *) charset, source[index]) != NULL) {
291 return source + index;
292 }
293
294 if (source[index] < 0x80) {
295 index++;
296 } else {
297 size_t width = pm_encoding_utf_8_char_width(source + index, (ptrdiff_t) (maximum - index));
298
299 if (width > 0) {
300 index += width;
301 } else if (mode == PM_STRPBRK_RAW) {
302 index++;
303 } else if (mode == PM_STRPBRK_REGEXP) {
304 pm_strpbrk_invalid_multibyte_char(parser, source + index);
305 index++;
306 } else {
307 // At this point we know we have an invalid multibyte character.
308 // We'll walk forward as far as we can until we find the next
309 // valid character so that we don't spam the user with a ton of
310 // the same kind of error.
311 const size_t start = index;
312
313 do {
314 index++;
315 } while (index < maximum && pm_encoding_utf_8_char_width(source + index, (ptrdiff_t) (maximum - index)) == 0);
316
317 pm_strpbrk_invalid_multibyte_character(parser, (uint32_t) ((source + start) - parser->start), (uint32_t) (index - start));
318 }
319 }
320 }
321
322 return NULL;
323}
324
328static PRISM_INLINE const uint8_t *
329pm_strpbrk_ascii_8bit(pm_parser_t *parser, const uint8_t *source, const uint8_t *charset, size_t index, size_t maximum, pm_strpbrk_mode_t mode) {
330 while (index < maximum) {
331 if (strchr((const char *) charset, source[index]) != NULL) {
332 return source + index;
333 }
334
335 if (mode == PM_STRPBRK_STRING && source[index] >= 0x80) pm_strpbrk_explicit_encoding_set(parser, (uint32_t) (source + index - parser->start), 1);
336 index++;
337 }
338
339 return NULL;
340}
341
345static PRISM_INLINE const uint8_t *
346pm_strpbrk_multi_byte(pm_parser_t *parser, const uint8_t *source, const uint8_t *charset, size_t index, size_t maximum, pm_strpbrk_mode_t mode) {
347 const pm_encoding_t *encoding = parser->encoding;
348
349 while (index < maximum) {
350 if (strchr((const char *) charset, source[index]) != NULL) {
351 return source + index;
352 }
353
354 if (source[index] < 0x80) {
355 index++;
356 } else {
357 size_t width = pm_encoding_char_width(encoding, source + index, (ptrdiff_t) (maximum - index));
358 if (mode == PM_STRPBRK_STRING) pm_strpbrk_explicit_encoding_set(parser, (uint32_t) (source + index - parser->start), (uint32_t) (width == 0 ? 1 : width));
359
360 if (width > 0) {
361 index += width;
362 } else if (mode == PM_STRPBRK_RAW) {
363 index++;
364 } else if (mode == PM_STRPBRK_REGEXP) {
365 pm_strpbrk_invalid_multibyte_char(parser, source + index);
366 index++;
367 } else {
368 // At this point we know we have an invalid multibyte character.
369 // We'll walk forward as far as we can until we find the next
370 // valid character so that we don't spam the user with a ton of
371 // the same kind of error.
372 const size_t start = index;
373
374 do {
375 index++;
376 } while (index < maximum && pm_encoding_char_width(encoding, source + index, (ptrdiff_t) (maximum - index)) == 0);
377
378 pm_strpbrk_invalid_multibyte_character(parser, (uint32_t) ((source + start) - parser->start), (uint32_t) (index - start));
379 }
380 }
381 }
382
383 return NULL;
384}
385
390static PRISM_INLINE const uint8_t *
391pm_strpbrk_single_byte(pm_parser_t *parser, const uint8_t *source, const uint8_t *charset, size_t index, size_t maximum, pm_strpbrk_mode_t mode) {
392 const pm_encoding_t *encoding = parser->encoding;
393
394 while (index < maximum) {
395 if (strchr((const char *) charset, source[index]) != NULL) {
396 return source + index;
397 }
398
399 if (source[index] < 0x80 || mode == PM_STRPBRK_RAW) {
400 index++;
401 } else {
402 size_t width = pm_encoding_char_width(encoding, source + index, (ptrdiff_t) (maximum - index));
403 if (mode == PM_STRPBRK_STRING) pm_strpbrk_explicit_encoding_set(parser, (uint32_t) (source + index - parser->start), (uint32_t) (width == 0 ? 1 : width));
404
405 if (width > 0) {
406 index += width;
407 } else if (mode == PM_STRPBRK_REGEXP) {
408 pm_strpbrk_invalid_multibyte_char(parser, source + index);
409 index++;
410 } else {
411 // At this point we know we have an invalid multibyte character.
412 // We'll walk forward as far as we can until we find the next
413 // valid character so that we don't spam the user with a ton of
414 // the same kind of error.
415 const size_t start = index;
416
417 do {
418 index++;
419 } while (index < maximum && pm_encoding_char_width(encoding, source + index, (ptrdiff_t) (maximum - index)) == 0);
420
421 pm_strpbrk_invalid_multibyte_character(parser, (uint32_t) ((source + start) - parser->start), (uint32_t) (index - start));
422 }
423 }
424 }
425
426 return NULL;
427}
428
448const uint8_t *
449pm_strpbrk(pm_parser_t *parser, const uint8_t *source, const uint8_t *charset, ptrdiff_t length, pm_strpbrk_mode_t mode) {
450 if (length <= 0) return NULL;
451
452 size_t maximum = (size_t) length;
453 size_t index = 0;
454 if (scan_strpbrk_ascii(parser, source, maximum, charset, &index)) return source + index;
455
456 if (!parser->encoding_changed) {
457 return pm_strpbrk_utf8(parser, source, charset, index, maximum, mode);
458 } else if (parser->encoding == PM_ENCODING_ASCII_8BIT_ENTRY) {
459 return pm_strpbrk_ascii_8bit(parser, source, charset, index, maximum, mode);
460 } else if (pm_encoding_multibyte_p(parser->encoding)) {
461 return pm_strpbrk_multi_byte(parser, source, charset, index, maximum, mode);
462 } else {
463 return pm_strpbrk_single_byte(parser, source, charset, index, maximum, mode);
464 }
465}
#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
C99 shim for <stdbool.h>.
#define PRISM_UNUSED
GCC will warn if you specify a function or parameter that is unused at runtime.
Definition unused.h:13