Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions matcher/src/exact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ impl Matcher {
return None;
}
}
Some(1) => {
Some(_) => {
// first letter is not at index 0: search the leading
// non-letter on the full last-valid-start window
(max_score, max_pos) = self.substring_match_ascii_with_prefilter(
haystack,
needle,
Expand All @@ -133,17 +135,6 @@ impl Matcher {
return None;
}
}
Some(len) => {
(max_score, max_pos) = self.substring_match_ascii_with_prefilter(
haystack,
needle,
1,
memmem::find_iter(&haystack[..haystack.len() - needle.len() + len], needle),
);
if max_score == 0 {
return None;
}
}
// in case we don't have any letter in the needle
// we can treat the search as case sensitive and use memmem directly which is way faster
None => (),
Expand Down Expand Up @@ -236,7 +227,7 @@ impl Matcher {
.checked_sub(1)
.map(|i| haystack[i].char_class(&self.config))
.unwrap_or(self.config.initial_char_class);
let end = haystack.len() - needle.len();
let end = haystack.len() - needle.len() + 1;
for (i, &c) in haystack[start..end].iter().enumerate() {
let (c, char_class) = c.char_class_and_normalize(&self.config);
if c != needle[0] {
Expand Down
75 changes: 75 additions & 0 deletions matcher/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,3 +769,78 @@ fn umlaut() {
.match_list(paths, &mut matcher);
assert_eq!(matches.len(), 2);
}

#[test]
fn test_substring_unicode_suffix() {
// Unicode haystack + ASCII needle: the last valid start used to be
// excluded (exclusive end was one short), so a suffix match was missed.
assert_matches(
&[Substring],
true,
false,
false,
false,
&[
("üfoo", "foo", &[1, 2, 3], 2 * BONUS_CONSECUTIVE),
("éabc", "abc", &[1, 2, 3], 2 * BONUS_CONSECUTIVE),
("你好foo", "foo", &[2, 3, 4], 2 * BONUS_CONSECUTIVE),
],
);
}

#[test]
fn test_substring_crlf_suffix() {
// `\r\n` forces the Unicode Utf32Str path even though every byte is ASCII.
let mut matcher = Matcher::new(Config::DEFAULT);
let matches = Pattern::parse("'bar", CaseMatching::Smart, Normalization::Smart)
.match_list(["foo\r\nbar", "foo\nbar", "bar"], &mut matcher);
let items: Vec<_> = matches.iter().map(|(item, _)| *item).collect();
assert!(
items.contains(&"foo\r\nbar"),
"CRLF suffix should match: {items:?}"
);
assert!(items.contains(&"foo\nbar"), "{items:?}");
assert!(items.contains(&"bar"), "{items:?}");
}

#[test]
fn test_substring_leading_digits_ignore_case() {
// Needle is already lowercase (Matcher ignore_case contract). The first
// letter sits at index 2, which used to shrink the memmem window so both
// the mixed-case and the all-lowercase haystacks were missed.
assert_matches(
&[Substring],
false,
false,
false,
false,
&[
(
"xx12FOOyy",
"12foo",
&[2, 3, 4, 5, 6],
BONUS_CAMEL123 * (BONUS_FIRST_CHAR_MULTIPLIER + 4),
),
(
"xx12fooyy",
"12foo",
&[2, 3, 4, 5, 6],
BONUS_CAMEL123 * (BONUS_FIRST_CHAR_MULTIPLIER + 4),
),
],
);

let mut matcher = Matcher::new(Config::DEFAULT);
let matches = Pattern::parse("'12foo", CaseMatching::Smart, Normalization::Smart)
.match_list(["xx12FOOyy", "xx12fooyy", "nope"], &mut matcher);
let items: Vec<_> = matches.iter().map(|(item, _)| *item).collect();
assert!(items.contains(&"xx12FOOyy"), "{items:?}");
assert!(items.contains(&"xx12fooyy"), "{items:?}");
assert!(!items.contains(&"nope"), "{items:?}");

let matches = Pattern::parse("'2024rep", CaseMatching::Smart, Normalization::Smart)
.match_list(["notes/2024REPORT.md", "notes/2024rep.md"], &mut matcher);
let items: Vec<_> = matches.iter().map(|(item, _)| *item).collect();
assert!(items.contains(&"notes/2024REPORT.md"), "{items:?}");
assert!(items.contains(&"notes/2024rep.md"), "{items:?}");
}