Debian Codegraph @

src/util/pm_strpbrk.c

raw symbols SCIP
1 #cxx . . $ `<file>/src/util/pm_strpbrk.c`/include "prism/util/pm_strpbrk.h"cxx . . $ `<file>/include/prism/util/pm_strpbrk.h`/
2
3 /**
4 * Add an invalid multibyte character error to the parser.
5 */
6 static inline void
7 pm_strpbrk_invalid_multibyte_charactercxx . . $ pm_strpbrk_invalid_multibyte_character(7dcff9168bda8dc3).(pm_parser_tcxx . . $ pm_parser_t# *parser, const uint8_tcxx . . $ uint8_t# *start, const uint8_tcxx . . $ uint8_t# *end) {
8 pm_diagnostic_list_append_formatcxx . . $ pm_diagnostic_list_append_format(80bb1ec643042d9).(&parser->error_listcxx . . $ pm_parser#error_list., start, end, PM_ERR_INVALID_MULTIBYTE_CHARACTERcxx . . $ PM_ERR_INVALID_MULTIBYTE_CHARACTER., *start);
9 }
10
11 /**
12 * Set the explicit encoding for the parser to the current encoding.
13 */
14 static inline void
15 pm_strpbrk_explicit_encoding_setcxx . . $ pm_strpbrk_explicit_encoding_set(65ec33e39fc07236).(pm_parser_tcxx . . $ pm_parser_t# *parser, const uint8_tcxx . . $ uint8_t# *source, size_t width) {
16 if (parser->explicit_encodingcxx . . $ pm_parser#explicit_encoding. != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
17 if (parser->explicit_encodingcxx . . $ pm_parser#explicit_encoding. == parser->encodingcxx . . $ pm_parser#encoding.) {
18 // Okay, we already locked to this encoding.
19 } else if (parser->explicit_encodingcxx . . $ pm_parser#explicit_encoding. == PM_ENCODING_UTF_8_ENTRYcxx . . $ `include/prism/encoding.h:245:9`!) {
20 // Not okay, we already found a Unicode escape sequence and this
21 // conflicts.
22 pm_diagnostic_list_append_formatcxx . . $ pm_diagnostic_list_append_format(80bb1ec643042d9).(&parser->error_listcxx . . $ pm_parser#error_list., source, source + width, PM_ERR_MIXED_ENCODINGcxx . . $ PM_ERR_MIXED_ENCODING., parser->encodingcxx . . $ pm_parser#encoding.->namecxx . . $ $anonymous_type_f1726b61bf0350fa_0#name.);
23 } else {
24 // Should not be anything else.
25 assertcxx . . $ `/usr/include/assert.h:108:11`!(falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`! && "unreachable");
26 }
27 }
28
29 parser->explicit_encodingcxx . . $ pm_parser#explicit_encoding. = parser->encodingcxx . . $ pm_parser#encoding.;
30 }
31
32 /**
33 * This is the default path.
34 */
35 static inline const uint8_tcxx . . $ uint8_t# *
36 pm_strpbrk_utf8cxx . . $ pm_strpbrk_utf8(b327dff00a4ff2b2).(pm_parser_tcxx . . $ pm_parser_t# *parser, const uint8_tcxx . . $ uint8_t# *source, const uint8_tcxx . . $ uint8_t# *charset, size_t maximum, boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! validate) {
37 size_t index = 0;
38
39 while (index < maximum) {
40 if (strchrcxx . . $ strchr(2050848944e00d54).((const char *) charset, source[index]) != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
41 return source + index;
42 }
43
44 if (source[index] < 0x80) {
45 index++;
46 } else {
47 size_t width = pm_encoding_utf_8_char_widthcxx . . $ pm_encoding_utf_8_char_width(dc9dc2cf7fc514b1).(source + index, (ptrdiff_t) (maximum - index));
48
49 if (width > 0) {
50 index += width;
51 } else if (!validate) {
52 index++;
53 } else {
54 // At this point we know we have an invalid multibyte character.
55 // We'll walk forward as far as we can until we find the next
56 // valid character so that we don't spam the user with a ton of
57 // the same kind of error.
58 const size_t start = index;
59
60 do {
61 index++;
62 } while (index < maximum && pm_encoding_utf_8_char_widthcxx . . $ pm_encoding_utf_8_char_width(dc9dc2cf7fc514b1).(source + index, (ptrdiff_t) (maximum - index)) == 0);
63
64 pm_strpbrk_invalid_multibyte_charactercxx . . $ pm_strpbrk_invalid_multibyte_character(7dcff9168bda8dc3).(parser, source + start, source + index);
65 }
66 }
67 }
68
69 return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
70 }
71
72 /**
73 * This is the path when the encoding is ASCII-8BIT.
74 */
75 static inline const uint8_tcxx . . $ uint8_t# *
76 pm_strpbrk_ascii_8bitcxx . . $ pm_strpbrk_ascii_8bit(b327dff00a4ff2b2).(pm_parser_tcxx . . $ pm_parser_t# *parser, const uint8_tcxx . . $ uint8_t# *source, const uint8_tcxx . . $ uint8_t# *charset, size_t maximum, boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! validate) {
77 size_t index = 0;
78
79 while (index < maximum) {
80 if (strchrcxx . . $ strchr(2050848944e00d54).((const char *) charset, source[index]) != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
81 return source + index;
82 }
83
84 if (validate && source[index] >= 0x80) pm_strpbrk_explicit_encoding_setcxx . . $ pm_strpbrk_explicit_encoding_set(65ec33e39fc07236).(parser, source, 1);
85 index++;
86 }
87
88 return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
89 }
90
91 /**
92 * This is the slow path that does care about the encoding.
93 */
94 static inline const uint8_tcxx . . $ uint8_t# *
95 pm_strpbrk_multi_bytecxx . . $ pm_strpbrk_multi_byte(b327dff00a4ff2b2).(pm_parser_tcxx . . $ pm_parser_t# *parser, const uint8_tcxx . . $ uint8_t# *source, const uint8_tcxx . . $ uint8_t# *charset, size_t maximum, boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! validate) {
96 size_t index = 0;
97 const pm_encoding_tcxx . . $ pm_encoding_t# *encoding = parser->encodingcxx . . $ pm_parser#encoding.;
98
99 while (index < maximum) {
100 if (strchrcxx . . $ strchr(2050848944e00d54).((const char *) charset, source[index]) != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
101 return source + index;
102 }
103
104 if (source[index] < 0x80) {
105 index++;
106 } else {
107 size_t width = encoding->char_widthcxx . . $ $anonymous_type_f1726b61bf0350fa_0#char_width.(source + index, (ptrdiff_t) (maximum - index));
108 if (validate) pm_strpbrk_explicit_encoding_setcxx . . $ pm_strpbrk_explicit_encoding_set(65ec33e39fc07236).(parser, source, width);
109
110 if (width > 0) {
111 index += width;
112 } else if (!validate) {
113 index++;
114 } else {
115 // At this point we know we have an invalid multibyte character.
116 // We'll walk forward as far as we can until we find the next
117 // valid character so that we don't spam the user with a ton of
118 // the same kind of error.
119 const size_t start = index;
120
121 do {
122 index++;
123 } while (index < maximum && encoding->char_widthcxx . . $ $anonymous_type_f1726b61bf0350fa_0#char_width.(source + index, (ptrdiff_t) (maximum - index)) == 0);
124
125 pm_strpbrk_invalid_multibyte_charactercxx . . $ pm_strpbrk_invalid_multibyte_character(7dcff9168bda8dc3).(parser, source + start, source + index);
126 }
127 }
128 }
129
130 return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
131 }
132
133 /**
134 * This is the fast path that does not care about the encoding because we know
135 * the encoding only supports single-byte characters.
136 */
137 static inline const uint8_tcxx . . $ uint8_t# *
138 pm_strpbrk_single_bytecxx . . $ pm_strpbrk_single_byte(b327dff00a4ff2b2).(pm_parser_tcxx . . $ pm_parser_t# *parser, const uint8_tcxx . . $ uint8_t# *source, const uint8_tcxx . . $ uint8_t# *charset, size_t maximum, boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! validate) {
139 size_t index = 0;
140 const pm_encoding_tcxx . . $ pm_encoding_t# *encoding = parser->encodingcxx . . $ pm_parser#encoding.;
141
142 while (index < maximum) {
143 if (strchrcxx . . $ strchr(2050848944e00d54).((const char *) charset, source[index]) != NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!) {
144 return source + index;
145 }
146
147 if (source[index] < 0x80 || !validate) {
148 index++;
149 } else {
150 size_t width = encoding->char_widthcxx . . $ $anonymous_type_f1726b61bf0350fa_0#char_width.(source + index, (ptrdiff_t) (maximum - index));
151 pm_strpbrk_explicit_encoding_setcxx . . $ pm_strpbrk_explicit_encoding_set(65ec33e39fc07236).(parser, source, width);
152
153 if (width > 0) {
154 index += width;
155 } else {
156 // At this point we know we have an invalid multibyte character.
157 // We'll walk forward as far as we can until we find the next
158 // valid character so that we don't spam the user with a ton of
159 // the same kind of error.
160 const size_t start = index;
161
162 do {
163 index++;
164 } while (index < maximum && encoding->char_widthcxx . . $ $anonymous_type_f1726b61bf0350fa_0#char_width.(source + index, (ptrdiff_t) (maximum - index)) == 0);
165
166 pm_strpbrk_invalid_multibyte_charactercxx . . $ pm_strpbrk_invalid_multibyte_character(7dcff9168bda8dc3).(parser, source + start, source + index);
167 }
168 }
169 }
170
171 return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
172 }
173
174 /**
175 * Here we have rolled our own version of strpbrk. The standard library strpbrk
176 * has undefined behavior when the source string is not null-terminated. We want
177 * to support strings that are not null-terminated because pm_parse does not
178 * have the contract that the string is null-terminated. (This is desirable
179 * because it means the extension can call pm_parse with the result of a call to
180 * mmap).
181 *
182 * The standard library strpbrk also does not support passing a maximum length
183 * to search. We want to support this for the reason mentioned above, but we
184 * also don't want it to stop on null bytes. Ruby actually allows null bytes
185 * within strings, comments, regular expressions, etc. So we need to be able to
186 * skip past them.
187 *
188 * Finally, we want to support encodings wherein the charset could contain
189 * characters that are trailing bytes of multi-byte characters. For example, in
190 * Shift_JIS, the backslash character can be a trailing byte. In that case we
191 * need to take a slower path and iterate one multi-byte character at a time.
192 */
193 const uint8_tcxx . . $ uint8_t# *
194 pm_strpbrkcxx . . $ pm_strpbrk(dc8c01d73e842f11).(pm_parser_tcxx . . $ pm_parser_t# *parser, const uint8_tcxx . . $ uint8_t# *source, const uint8_tcxx . . $ uint8_t# *charset, ptrdiff_t length, boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! validate) {
195 if (length <= 0) {
196 return NULLcxx . . $ `<external>/15298297234472979974/stddef.h:419:9`!;
197 } else if (!parser->encoding_changedcxx . . $ pm_parser#encoding_changed.) {
198 return pm_strpbrk_utf8cxx . . $ pm_strpbrk_utf8(b327dff00a4ff2b2).(parser, source, charset, (size_t) length, validate);
199 } else if (parser->encodingcxx . . $ pm_parser#encoding. == PM_ENCODING_ASCII_8BIT_ENTRYcxx . . $ `include/prism/encoding.h:259:9`!) {
200 return pm_strpbrk_ascii_8bitcxx . . $ pm_strpbrk_ascii_8bit(b327dff00a4ff2b2).(parser, source, charset, (size_t) length, validate);
201 } else if (parser->encodingcxx . . $ pm_parser#encoding.->multibytecxx . . $ $anonymous_type_f1726b61bf0350fa_0#multibyte.) {
202 return pm_strpbrk_multi_bytecxx . . $ pm_strpbrk_multi_byte(b327dff00a4ff2b2).(parser, source, charset, (size_t) length, validate);
203 } else {
204 return pm_strpbrk_single_bytecxx . . $ pm_strpbrk_single_byte(b327dff00a4ff2b2).(parser, source, charset, (size_t) length, validate);
205 }
206 }