Debian Codegraph @

debian/rust-vendor/fancy-regex/tests/bytes/matching.rs

raw
1 use fancy_regex::{BytesMode, RegexBuilder};
2
3 #[test]
4 fn bytes_find_from_pos() {
5 let re = RegexBuilder::new(r"\d+")
6 .bytes_mode(BytesMode::Ascii)
7 .build()
8 .unwrap();
9 let mat = re.find_from_pos(b"abc 123", 4).unwrap().unwrap();
10 assert_eq!(mat.start(), 4);
11 assert_eq!(mat.end(), 7);
12 assert_eq!(mat.as_bytes(), b"123");
13 }
14
15 #[test]
16 fn bytes_find_from_pos_no_match() {
17 let re = RegexBuilder::new(r"\d+")
18 .bytes_mode(BytesMode::Ascii)
19 .build()
20 .unwrap();
21 let result = re.find_from_pos(b"abc 123", 7).unwrap();
22 assert!(result.is_none());
23 }
24
25 #[test]
26 fn bytes_non_utf8_input() {
27 let re = RegexBuilder::new(r"\d+")
28 .bytes_mode(BytesMode::Ascii)
29 .build()
30 .unwrap();
31 let input = b"\x80\x81\x82 123";
32 assert!(re.is_match(input).unwrap());
33
34 let mat = re.find_from_pos(input, 0).unwrap().unwrap();
35 assert_eq!(mat.as_bytes(), b"123");
36 }
37
38 #[test]
39 fn bytes_ascii_dot_matches_non_utf8() {
40 let re = RegexBuilder::new(r".+")
41 .bytes_mode(BytesMode::Ascii)
42 .build()
43 .unwrap();
44 assert!(re.is_match(b"\x80\x81\x82").unwrap());
45
46 let re = RegexBuilder::new(r".")
47 .bytes_mode(BytesMode::Ascii)
48 .build()
49 .unwrap();
50 assert!(re.is_match(b"\xff").unwrap());
51 assert!(re.is_match(b"\x80").unwrap());
52
53 let re = RegexBuilder::new(r".*")
54 .bytes_mode(BytesMode::Ascii)
55 .build()
56 .unwrap();
57 let mat = re.find_from_pos(b"\xff\xfe\xfd", 0).unwrap().unwrap();
58 assert_eq!(mat.as_bytes(), b"\xff\xfe\xfd");
59 }
60
61 #[test]
62 fn bytes_unicode_bytes_dot_does_not_match_raw_bytes() {
63 let re = RegexBuilder::new(r".")
64 .bytes_mode(BytesMode::UnicodeBytes)
65 .build()
66 .unwrap();
67 assert!(!re.is_match(b"\x80").unwrap());
68 assert!(re.is_match(b"A").unwrap());
69 }
70
71 #[test]
72 fn bytes_unicode_bytes_char_classes_still_unicode() {
73 let re = RegexBuilder::new(r"\w+")
74 .bytes_mode(BytesMode::UnicodeBytes)
75 .build()
76 .unwrap();
77 assert!(re.is_match("café".as_bytes()).unwrap());
78 }
79
80 #[test]
81 fn bytes_ascii_char_classes_are_ascii_only() {
82 let re = RegexBuilder::new(r"^\w+$")
83 .bytes_mode(BytesMode::Ascii)
84 .build()
85 .unwrap();
86 assert!(re.is_match(b"hello").unwrap());
87 assert!(!re.is_match("café".as_bytes()).unwrap());
88 }
89
90 #[test]
91 fn bytes_find_returns_matchbytes() {
92 let re = RegexBuilder::new(r"\d+")
93 .bytes_mode(BytesMode::Ascii)
94 .build()
95 .unwrap();
96 let mat = re.find(b"abc 123").unwrap().unwrap();
97 assert_eq!(mat.start(), 4);
98 assert_eq!(mat.end(), 7);
99 assert_eq!(mat.as_bytes(), b"123");
100 }
101
102 #[test]
103 fn bytes_find_no_match() {
104 let re = RegexBuilder::new(r"\d+")
105 .bytes_mode(BytesMode::Ascii)
106 .build()
107 .unwrap();
108 let result = re.find(b"abc").unwrap();
109 assert!(result.is_none());
110 }
111
112 #[test]
113 fn bytes_find_iter() {
114 let re = RegexBuilder::new(r"\d+")
115 .bytes_mode(BytesMode::Ascii)
116 .build()
117 .unwrap();
118 let mut matches = re.find_iter(b"a1 b23 c456");
119 let m1 = matches.next().unwrap().unwrap();
120 assert_eq!(m1.as_bytes(), b"1");
121 let m2 = matches.next().unwrap().unwrap();
122 assert_eq!(m2.as_bytes(), b"23");
123 let m3 = matches.next().unwrap().unwrap();
124 assert_eq!(m3.as_bytes(), b"456");
125 assert!(matches.next().is_none());
126 }
127
128 #[test]
129 fn bytes_find_iter_non_utf8() {
130 let re = RegexBuilder::new(r".+")
131 .bytes_mode(BytesMode::Ascii)
132 .build()
133 .unwrap();
134 let mut matches = re.find_iter(b"\x80\x81\x82");
135 let m = matches.next().unwrap().unwrap();
136 assert_eq!(m.as_bytes(), b"\x80\x81\x82");
137 assert!(matches.next().is_none());
138 }
139
140 #[test]
141 fn bytes_find_with_str_still_works() {
142 let re = RegexBuilder::new(r"\d+")
143 .bytes_mode(BytesMode::Ascii)
144 .build()
145 .unwrap();
146 let mat = re.find("abc 123").unwrap().unwrap();
147 assert_eq!(mat.as_str(), "123");
148 }
149
150 #[test]
151 fn bytes_find_fixed_array_input() {
152 let re = RegexBuilder::new(r"\d+")
153 .bytes_mode(BytesMode::Ascii)
154 .build()
155 .unwrap();
156 let input: &[u8; 7] = b"abc 456";
157 let m = re.find(input).unwrap().unwrap();
158 assert_eq!(m.as_bytes(), b"456");
159 assert_eq!(m.start(), 4);
160 assert_eq!(m.end(), 7);
161 }
162
163 #[test]
164 fn bytes_is_match_fixed_array_input() {
165 let re = RegexBuilder::new(r"\d+")
166 .bytes_mode(BytesMode::Ascii)
167 .build()
168 .unwrap();
169 let yes: &[u8; 7] = b"abc 123";
170 let no: &[u8; 3] = b"abc";
171 assert!(re.is_match(yes).unwrap());
172 assert!(!re.is_match(no).unwrap());
173 }
174
175 /// Case-insensitive backref in bytes mode.
176 ///
177 /// This covers the `text_bytes.is_ascii()` fix in the VM: previously the code
178 /// checked `s.is_ascii()` (the whole input), so a non-ASCII *prefix* before an
179 /// ASCII capture caused the ASCII fast-path to be skipped even when the
180 /// captured slice itself was pure ASCII.
181 #[test]
182 fn bytes_backrefs_casei() {
183 // ASCII: case-insensitive backref match
184 assert_match_bytes(r"(abc)(?i:\1)", b"abcABC");
185 // ASCII: case-insensitive backref no-match
186 assert_no_match_bytes(r"(abc)(?i:\1)", b"abcdef");
187
188 // Non-ASCII prefix before ASCII capture – exercises the is_ascii() fix
189 assert_match_bytes(r"(abc)(?i:\1)", "δabcABC".as_bytes());
190
191 // Unicode case folding (δ ↔ Δ) is NOT applied in BytesMode::Ascii because
192 // unicode=false is implied, so the backref uses ASCII-only case folding.
193 assert_no_match_bytes(r"(δ)(?i:\1)", "δΔ".as_bytes());
194 // Unicode: case-insensitive backref no-match
195 assert_no_match_bytes(r"(δ)(?i:\1)", "δσ".as_bytes());
196
197 // Non-UTF-8 bytes: 0xFF is not a valid UTF-8 byte, so (.) captures it as
198 // a single raw byte; no ASCII case-insensitive match possible for these bytes.
199 assert_no_match_bytes(r"(.)(?i:\1)", b"\xff\xfe\xfd\xfc\xff\xfe\xfd\xfb");
200 }
201
202 // --- Hard-mode tests (force VM via atomic groups) ---
203 // These tests use `(?>...)` to force the backtracking VM path,
204 // verifying that byte-level advancement works correctly with non-UTF-8 high bytes.
205
206 /// Atomic group forces hard mode. The dot should advance by 1 byte in Ascii mode,
207 /// even for bytes >= 0x80 that would be multi-byte UTF-8 lead bytes.
208 #[test]
209 fn bytes_hard_mode_dot_advances_one_byte() {
210 // (?>.) forces hard mode; the dot should match exactly 1 byte (0xC0),
211 // and then the literal `x` should match at position 1.
212 // 0xC0 is a 2-byte UTF-8 lead byte; in Unicode mode it would try to
213 // advance 2 bytes, but in Ascii mode it should advance only 1.
214 assert_match_bytes(r"(?>.)x", b"\xC0x");
215 // Same with 0xE0 (3-byte UTF-8 lead byte)
216 assert_match_bytes(r"(?>.)x", b"\xE0x");
217 // Same with 0xF0 (4-byte UTF-8 lead byte)
218 assert_match_bytes(r"(?>.)x", b"\xF0x");
219 }
220
221 /// Verify that the SplitUnanchored preamble (which uses `Any`) advances 1 byte
222 /// at a time in Ascii mode, allowing matches after high bytes.
223 #[test]
224 fn bytes_hard_mode_unanchored_skips_high_bytes() {
225 // The pattern is unanchored. The VM needs to skip over 0xFF, 0xFE, 0xFD
226 // (each 1 byte) to find the match at "ab".
227 // The atomic group forces hard mode.
228 assert_match_bytes(r"(?>ab)", b"\xFF\xFE\xFDab");
229 }
230
231 /// `AnyNoNL` in hard mode should advance 1 byte for high bytes.
232 #[test]
233 fn bytes_hard_mode_dot_no_newline() {
234 // (?s) is NOT set, so `.` means AnyNoNL. Atomic group forces VM.
235 // 0x80 is not a newline, so dot should match it (1 byte) and then match `y`.
236 assert_match_bytes(r"(?>.)y", b"\x80y");
237 // But newline should not be matched by dot (no (?s))
238 assert_no_match_bytes(r"^(?>.)y", b"\ny");
239 }
240
241 /// GoBack instruction should go back 1 byte in Ascii mode, even for high bytes.
242 /// We use a lookbehind with a dot (which is hard due to the lookbehind) to test
243 /// that GoBack(1) retreats exactly 1 byte, even when the byte at that position
244 /// is a high byte (>= 0x80) that would normally be a multi-byte UTF-8 lead byte.
245 #[test]
246 fn bytes_hard_mode_lookbehind_goback() {
247 // `(?<=.)x` is hard (lookbehind). GoBack(1) goes back 1 byte from `x`.
248 // In Ascii mode, the dot in the lookbehind then matches the single byte 0xC0.
249 // In Unicode mode this would fail because 0xC0 followed by 'x' is invalid UTF-8.
250 assert_match_bytes(r"(?<=.)x", b"\xC0x");
251 // Verify with a 2-char lookbehind: GoBack(2) should go back 2 bytes.
252 assert_match_bytes(r"(?<=..)x", b"\xE0\xF0x");
253 }
254
255 /// Multiple dots in an atomic group should each advance 1 byte.
256 #[test]
257 fn bytes_hard_mode_multiple_dots() {
258 // (?>...) should consume exactly 3 bytes, then `z` should match.
259 assert_match_bytes(r"(?>.{3})z", b"\xC0\xE0\xF0z");
260 // Verify the match doesn't happen if z isn't at position 3
261 assert_no_match_bytes(r"^(?>.{3})z", b"\xC0\xE0z\xF0");
262 }
263
264 // --- UnicodeBytes hard-mode tests ---
265 // These verify that UnicodeBytes mode still uses codepoint-level advancement
266 // in the VM (not byte-level), while accepting non-UTF-8 input.
267
268 /// In UnicodeBytes mode, the dot should advance by codepoint length, not 1 byte.
269 /// A valid multi-byte UTF-8 sequence followed by a literal should work correctly.
270 #[test]
271 fn bytes_unicode_bytes_hard_mode_dot_advances_codepoint() {
272 // é is 0xC3 0xA9 (2-byte UTF-8). In UnicodeBytes mode with atomic group (hard),
273 // the dot should consume both bytes as one codepoint, then match 'x'.
274 let re = RegexBuilder::new(r"(?>.)x")
275 .bytes_mode(BytesMode::UnicodeBytes)
276 .build()
277 .unwrap();
278 assert!(re.is_match("éx".as_bytes()).unwrap());
279 // Should NOT match if x is at byte offset 1 (middle of the codepoint)
280 assert!(!re.is_match(b"\xC3x").unwrap());
281 }
282
283 /// In UnicodeBytes mode, the unanchored preamble should skip by codepoint.
284 #[test]
285 fn bytes_unicode_bytes_hard_mode_unanchored_skips_codepoints() {
286 // The VM skips over "é" (2 bytes) as one codepoint to find the match.
287 let re = RegexBuilder::new(r"(?>ab)")
288 .bytes_mode(BytesMode::UnicodeBytes)
289 .build()
290 .unwrap();
291 assert!(re.is_match("éab".as_bytes()).unwrap());
292 }
293
294 /// In UnicodeBytes mode, GoBack should retreat by codepoint, not by byte.
295 #[test]
296 fn bytes_unicode_bytes_hard_mode_lookbehind_goback() {
297 // `(?<=.)x` is hard. GoBack(1) should go back 1 codepoint.
298 // "é" is 2 bytes, so GoBack(1) from 'x' at offset 2 should land at offset 0.
299 let re = RegexBuilder::new(r"(?<=.)x")
300 .bytes_mode(BytesMode::UnicodeBytes)
301 .build()
302 .unwrap();
303 assert!(re.is_match("éx".as_bytes()).unwrap());
304 }
305
306 /// UnicodeBytes mode: dot does NOT match isolated non-UTF-8 bytes (unlike Ascii mode).
307 #[test]
308 fn bytes_unicode_bytes_hard_mode_dot_rejects_invalid_utf8() {
309 // (?>.)x: the dot in UnicodeBytes mode only matches valid Unicode scalar values.
310 // 0xC0 alone is not valid UTF-8, so dot should not match it.
311 let re = RegexBuilder::new(r"^(?>.)x")
312 .bytes_mode(BytesMode::UnicodeBytes)
313 .build()
314 .unwrap();
315 assert!(!re.is_match(b"\xC0x").unwrap());
316 }
317
318 // --- Seek instruction with high bytes in Ascii mode ---
319
320 /// Seek pre-filter with backref pattern should advance byte-by-byte over high bytes.
321 /// The seek instruction's `advance_one` is used when computing `next_seek_start`.
322 #[test]
323 fn bytes_hard_mode_seek_with_high_bytes() {
324 // Pattern: (abc)\1 with seek enabled. The seek pre-filter derives a pattern
325 // for "abc" and uses it to skip ahead. We place high bytes before the match
326 // to verify the seek advances correctly over them.
327 let re = RegexBuilder::new(r"(abc)\1")
328 .bytes_mode(BytesMode::Ascii)
329 .seek(true)
330 .build()
331 .unwrap();
332 let input = b"\xFF\xFE\xFDabcabc";
333 let mat = re.find(input).unwrap().unwrap();
334 assert_eq!(mat.start(), 3);
335 assert_eq!(mat.end(), 9);
336 assert_eq!(mat.as_bytes(), b"abcabc");
337 }
338
339 /// Seek with a zero-width match scenario: the seek pre-filter matches at a position
340 /// but the main pattern fails, requiring backtrack and re-seek past a high byte.
341 #[test]
342 fn bytes_hard_mode_seek_advances_past_high_byte_on_backtrack() {
343 // Pattern: lookahead for "x" then backref. The seek pattern is "x".
344 // Input has \xFF before the real match. If the seek finds "x" at position 1
345 // (after \xFF) and the full pattern fails, the next seek start should be
346 // position 2 (1 byte past), not a multi-byte advance.
347 let re = RegexBuilder::new(r"(?=x)(x)\1")
348 .bytes_mode(BytesMode::Ascii)
349 .seek(true)
350 .build()
351 .unwrap();
352 let input = b"\xFFxxx";
353 let mat = re.find(input).unwrap().unwrap();
354 assert_eq!(mat.as_bytes(), b"xx");
355 }
356
357 // --- AbsentRepeater with high bytes in Ascii mode ---
358
359 /// The absent repeater `(?~...)` advances one position at a time when scanning.
360 /// In Ascii mode, this should be 1 byte even for high bytes.
361 #[test]
362 fn bytes_hard_mode_absent_repeater_with_high_bytes() {
363 // (`+)(?~\1)\1 pattern: capture backticks, then absent-repeat (no backticks),
364 // then match backticks again. Place high bytes in the "body" to verify
365 // the absent repeater scans byte-by-byte.
366 let re = RegexBuilder::new(r"(`+)(?~\1)\1")
367 .bytes_mode(BytesMode::Ascii)
368 .build()
369 .unwrap();
370 let input = b"`\xFF\xFE\xC0`";
371 assert!(re.is_match(input).unwrap());
372 let mat = re.find(input).unwrap().unwrap();
373 assert_eq!(mat.as_bytes(), b"`\xFF\xFE\xC0`");
374 }
375
376 /// Absent repeater with lookahead inner (hard) scanning over high bytes.
377 #[test]
378 fn bytes_hard_mode_absent_repeater_lookahead_with_high_bytes() {
379 // (?~(?=end))end: match everything until "end" appears.
380 // High bytes before "end" should be scanned one byte at a time.
381 let re = RegexBuilder::new(r"(?~(?=end))end")
382 .bytes_mode(BytesMode::Ascii)
383 .build()
384 .unwrap();
385 let input = b"\xFF\xC0\xE0end";
386 assert!(re.is_match(input).unwrap());
387 let mat = re.find(input).unwrap().unwrap();
388 assert_eq!(mat.as_bytes(), b"\xFF\xC0\xE0end");
389 }
390
391 // --- Explicit \R tests in Ascii bytes mode ---
392
393 /// \R should compile and work in Ascii bytes mode (non-Unicode newline set).
394 #[test]
395 fn bytes_hard_mode_general_newline_ascii() {
396 // \R is compiled using atomic groups internally, making it hard.
397 // In Ascii mode it should match \r\n, \n, \r, \x0B, \x0C but NOT
398 // the Unicode newlines U+0085, U+2028, U+2029.
399 assert_match_bytes(r"\R", b"\r\n");
400 assert_match_bytes(r"\R", b"\n");
401 assert_match_bytes(r"\R", b"\r");
402 assert_match_bytes(r"\R", b"\x0B");
403 assert_match_bytes(r"\R", b"\x0C");
404
405 // Unicode newlines should NOT match in Ascii mode.
406 // U+0085 = 0xC2 0x85, U+2028 = 0xE2 0x80 0xA8, U+2029 = 0xE2 0x80 0xA9
407 assert_no_match_bytes(r"^\R$", "\u{0085}".as_bytes());
408 assert_no_match_bytes(r"^\R$", "\u{2028}".as_bytes());
409 assert_no_match_bytes(r"^\R$", "\u{2029}".as_bytes());
410 }
411
412 /// \R should not backtrack from \r\n to \r in Ascii bytes mode.
413 #[test]
414 fn bytes_hard_mode_general_newline_atomic() {
415 // \R\n should NOT match "\r\n" because \R atomically consumes \r\n,
416 // leaving nothing for the \n.
417 assert_no_match_bytes(r"\R\n", b"\r\n");
418 // But \R\R should match \r\n followed by \n
419 assert_match_bytes(r"\R\R", b"\r\n\n");
420 }
421
422 /// \R with high bytes: the unanchored search should skip high bytes to find newlines.
423 #[test]
424 fn bytes_hard_mode_general_newline_after_high_bytes() {
425 assert_match_bytes(r"\R", b"\xFF\xFE\n");
426 assert_match_bytes(r"a\Rb", b"\xFFa\nb");
427 }
428
429 #[cfg_attr(feature = "track_caller", track_caller)]
430 fn assert_match_bytes(re: &str, text: &[u8]) {
431 let result = match_bytes(re, text);
432 assert!(result, "Expected regex '{}' to match bytes {:?}", re, text);
433 }
434
435 #[cfg_attr(feature = "track_caller", track_caller)]
436 fn assert_no_match_bytes(re: &str, text: &[u8]) {
437 let result = match_bytes(re, text);
438 assert!(
439 !result,
440 "Expected regex '{}' to not match bytes {:?}",
441 re, text
442 );
443 }
444
445 fn match_bytes(re: &str, text: &[u8]) -> bool {
446 let regex = RegexBuilder::new(re)
447 .bytes_mode(BytesMode::Ascii)
448 .build()
449 .unwrap();
450 let result = regex.is_match(text);
451 assert!(
452 result.is_ok(),
453 "Expected match to succeed, but was {:?}",
454 result
455 );
456 result.unwrap()
457 }