Ruby: prism/util/pm_strpbrk.c Source File

Ruby 3.3.5p100 (2024-09-03 revision ef084cc8f4958c1b6e4ead99136631bef6d8ddba)

pm_strpbrk.c
1#include "prism/util/pm_strpbrk.h"
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}
pm_strpbrk.h
A custom strpbrk implementation.
pm_encoding_t::char_width
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
pm_encoding_t::multibyte
bool multibyte
Return true if the encoding is a multibyte encoding.
Definition encoding.h:61
pm_parser
This struct represents the overall parser.
Definition parser.h:489
pm_parser::encoding
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
pm_parser::encoding_changed
bool encoding_changed
Whether or not the encoding has been changed by a magic comment.
Definition parser.h:682