Ruby 3.3.2p78 (2024-05-30 revision e5a195edf62fe1bf7146a191da13fa1c4fecbd71)
pm_strpbrk.c
2
6static inline const uint8_t *
7pm_strpbrk_multi_byte(const pm_parser_t *parser, const uint8_t *source, const uint8_t *charset, size_t maximum) {
8 size_t index = 0;
9
10 while (index < maximum) {
11 if (strchr((const char *) charset, source[index]) != NULL) {
12 return source + index;
13 }
14
15 size_t width = parser->encoding->char_width(source + index, (ptrdiff_t) (maximum - index));
16 if (width == 0) {
17 return NULL;
18 }
19
20 index += width;
21 }
22
23 return NULL;
24}
25
29static inline const uint8_t *
30pm_strpbrk_single_byte(const uint8_t *source, const uint8_t *charset, size_t maximum) {
31 size_t index = 0;
32
33 while (index < maximum) {
34 if (strchr((const char *) charset, source[index]) != NULL) {
35 return source + index;
36 }
37
38 index++;
39 }
40
41 return NULL;
42}
43
63const uint8_t *
64pm_strpbrk(const pm_parser_t *parser, const uint8_t *source, const uint8_t *charset, ptrdiff_t length) {
65 if (length <= 0) {
66 return NULL;
67 } else if (parser->encoding_changed && parser->encoding->multibyte) {
68 return pm_strpbrk_multi_byte(parser, source, charset, (size_t) length);
69 } else {
70 return pm_strpbrk_single_byte(source, charset, (size_t) length);
71 }
72}
A custom strpbrk implementation.
size_t(* char_width)(const uint8_t *b, ptrdiff_t n)
Return the number of bytes that the next character takes if it is valid in the encoding.
Definition encoding.h:29
bool multibyte
Return true if the encoding is a multibyte encoding.
Definition encoding.h:61
This struct represents the overall parser.
Definition parser.h:489
const pm_encoding_t * encoding
The encoding functions for the current file is attached to the parser as it's parsing so that it can ...
Definition parser.h:584
bool encoding_changed
Whether or not the encoding has been changed by a magic comment.
Definition parser.h:682