1#include "prism/internal/strpbrk.h"
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"
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]);
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);
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) {
41 }
else if (parser->explicit_encoding == PM_ENCODING_UTF_8_ENTRY) {
44 pm_diagnostic_list_append_format(&parser->metadata_arena, &parser->error_list, start, length, PM_ERR_MIXED_ENCODING, parser->encoding->name);
47 PRISM_ASSERT(
false &&
"unreachable");
51 parser->explicit_encoding = parser->encoding;
70#if defined(PRISM_HAS_NEON) || defined(PRISM_HAS_SSE2) || defined(PRISM_HAS_SWAR)
83pm_strpbrk_cache_update(
pm_parser_t *parser,
const uint8_t *charset) {
87 if (memcmp(parser->strpbrk_cache.charset, charset,
sizeof(parser->strpbrk_cache.charset)) == 0)
return;
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));
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;
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);
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);
116#if defined(PRISM_HAS_NEON)
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);
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);
130 while (idx + 16 <= maximum) {
131 uint8x16_t v = vld1q_u8(source + idx);
135 if (vmaxvq_u8(vandq_u8(v, mask_80)) != 0)
break;
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);
141 if (vmaxvq_u8(matched) == 0) {
147 uint64_t lo64 = vgetq_lane_u64(vreinterpretq_u64_u8(matched), 0);
149 *index = idx + pm_ctzll(lo64) / 8;
152 uint64_t hi64 = vgetq_lane_u64(vreinterpretq_u64_u8(matched), 1);
153 *index = idx + 8 + pm_ctzll(hi64) / 8;
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))) {
171#elif defined(PRISM_HAS_SSE2)
172#include <emmintrin.h>
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);
184 __m128i needles[
sizeof(parser->strpbrk_cache.charset)];
185 size_t needles_size = 0;
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]);
191 const __m128i zero = _mm_setzero_si128();
194 while (idx + 16 <= maximum) {
195 __m128i v = _mm_loadu_si128((
const __m128i *) (source + idx));
198 if (_mm_movemask_epi8(v) != 0)
break;
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]));
206 int mask = _mm_movemask_epi8(matched);
214 *index = idx + pm_ctzll((uint64_t) (mask & 0xFFFF));
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))) {
232#elif defined(PRISM_HAS_SWAR)
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);
238 static const uint64_t highs = 0x8080808080808080ULL;
241 while (idx + 8 <= maximum) {
243 memcpy(&word, source + idx, 8);
246 if (word & highs)
break;
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))) {
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))) {
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;
294 if (source[index] < 0x80) {
297 size_t width = pm_encoding_utf_8_char_width(source + index, (ptrdiff_t) (maximum - index));
301 }
else if (mode == PM_STRPBRK_RAW) {
303 }
else if (mode == PM_STRPBRK_REGEXP) {
304 pm_strpbrk_invalid_multibyte_char(parser, source + index);
311 const size_t start = index;
315 }
while (index < maximum && pm_encoding_utf_8_char_width(source + index, (ptrdiff_t) (maximum - index)) == 0);
317 pm_strpbrk_invalid_multibyte_character(parser, (uint32_t) ((source + start) - parser->start), (uint32_t) (index - start));
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;
335 if (mode == PM_STRPBRK_STRING && source[index] >= 0x80) pm_strpbrk_explicit_encoding_set(parser, (uint32_t) (source + index - parser->start), 1);
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) {
349 while (index < maximum) {
350 if (strchr((
const char *) charset, source[index]) != NULL) {
351 return source + index;
354 if (source[index] < 0x80) {
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));
362 }
else if (mode == PM_STRPBRK_RAW) {
364 }
else if (mode == PM_STRPBRK_REGEXP) {
365 pm_strpbrk_invalid_multibyte_char(parser, source + index);
372 const size_t start = index;
376 }
while (index < maximum && pm_encoding_char_width(encoding, source + index, (ptrdiff_t) (maximum - index)) == 0);
378 pm_strpbrk_invalid_multibyte_character(parser, (uint32_t) ((source + start) - parser->start), (uint32_t) (index - start));
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) {
394 while (index < maximum) {
395 if (strchr((
const char *) charset, source[index]) != NULL) {
396 return source + index;
399 if (source[index] < 0x80 || mode == PM_STRPBRK_RAW) {
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));
407 }
else if (mode == PM_STRPBRK_REGEXP) {
408 pm_strpbrk_invalid_multibyte_char(parser, source + index);
415 const size_t start = index;
419 }
while (index < maximum && pm_encoding_char_width(encoding, source + index, (ptrdiff_t) (maximum - index)) == 0);
421 pm_strpbrk_invalid_multibyte_character(parser, (uint32_t) ((source + start) - parser->start), (uint32_t) (index - start));
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;
452 size_t maximum = (size_t) length;
454 if (scan_strpbrk_ascii(parser, source, maximum, charset, &index))
return source + index;
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);
463 return pm_strpbrk_single_byte(parser, source, charset, index, maximum, mode);
#define PRISM_INLINE
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
C99 shim for <stdbool.h>.
#define PRISM_UNUSED
GCC will warn if you specify a function or parameter that is unused at runtime.