fune/third_party/rust/regex/tests/searcher.rs
Mike Hommey c5503d7f3e Bug 1854045 - Update regex to 1.9.4. r=emilio,supply-chain-reviewers
The main side effect is the upgrade of aho-corasick to 1.1, which
contains a SIMD implementation for aarch64.

Differential Revision: https://phabricator.services.mozilla.com/D188673
2023-11-21 01:55:17 +00:00

93 lines
2.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

macro_rules! searcher {
($name:ident, $re:expr, $haystack:expr) => (
searcher!($name, $re, $haystack, vec vec![]);
);
($name:ident, $re:expr, $haystack:expr, $($steps:expr,)*) => (
searcher!($name, $re, $haystack, vec vec![$($steps),*]);
);
($name:ident, $re:expr, $haystack:expr, $($steps:expr),*) => (
searcher!($name, $re, $haystack, vec vec![$($steps),*]);
);
($name:ident, $re:expr, $haystack:expr, vec $expect_steps:expr) => (
#[test]
#[allow(unused_imports)]
fn $name() {
use std::str::pattern::{Pattern, Searcher};
use std::str::pattern::SearchStep::{Match, Reject, Done};
let re = regex::Regex::new($re).unwrap();
let mut se = re.into_searcher($haystack);
let mut got_steps = vec![];
loop {
match se.next() {
Done => break,
step => { got_steps.push(step); }
}
}
assert_eq!(got_steps, $expect_steps);
}
);
}
searcher!(searcher_empty_regex_empty_haystack, r"", "", Match(0, 0));
searcher!(
searcher_empty_regex,
r"",
"ab",
Match(0, 0),
Reject(0, 1),
Match(1, 1),
Reject(1, 2),
Match(2, 2)
);
searcher!(searcher_empty_haystack, r"\d", "");
searcher!(searcher_one_match, r"\d", "5", Match(0, 1));
searcher!(searcher_no_match, r"\d", "a", Reject(0, 1));
searcher!(
searcher_two_adjacent_matches,
r"\d",
"56",
Match(0, 1),
Match(1, 2)
);
searcher!(
searcher_two_non_adjacent_matches,
r"\d",
"5a6",
Match(0, 1),
Reject(1, 2),
Match(2, 3)
);
searcher!(searcher_reject_first, r"\d", "a6", Reject(0, 1), Match(1, 2));
searcher!(
searcher_one_zero_length_matches,
r"\d*",
"a1b2",
Match(0, 0), // ^
Reject(0, 1), // a
Match(1, 2), // a1
Reject(2, 3), // a1b
Match(3, 4), // a1b2
);
searcher!(
searcher_many_zero_length_matches,
r"\d*",
"a1bbb2",
Match(0, 0), // ^
Reject(0, 1), // a
Match(1, 2), // a1
Reject(2, 3), // a1b
Match(3, 3), // a1bb
Reject(3, 4), // a1bb
Match(4, 4), // a1bbb
Reject(4, 5), // a1bbb
Match(5, 6), // a1bbba
);
searcher!(
searcher_unicode,
r".+?",
"1Ⅱ2",
Match(0, 3),
Match(3, 4),
Match(4, 7),
Match(7, 8)
);