Sort on length of match

This commit is contained in:
2020-09-25 07:41:28 +02:00
parent 354350f734
commit 650ad87036

View File

@@ -1,3 +1,4 @@
use std::cmp::Reverse;
use std::str; use std::str;
use bstr::ByteSlice; use bstr::ByteSlice;
@@ -67,6 +68,8 @@ pub fn scan(input: impl AsRef<[u8]>) -> Vec<Match> {
index += 1; index += 1;
} }
result.sort_by_key(|mat| Reverse(mat.end - mat.start));
result result
} }
@@ -90,4 +93,23 @@ mod tests {
fn test_bug_001() { fn test_bug_001() {
assert_eq!(scan("ampersand"), vec![]); assert_eq!(scan("ampersand"), vec![]);
} }
#[test]
fn test_sort_by_length() {
assert_eq!(
scan("Ottoman Turkish"),
vec![
Match {
language: Language::Ota,
start: 0,
end: 15
},
Match {
language: Language::Tur,
start: 8,
end: 15
}
]
);
}
} }