1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use {
super::NameMatch,
secular,
smallvec::{smallvec, SmallVec},
std::{
cmp::Reverse,
ops::Range,
},
};
type CandChars = SmallVec<[char; 32]>;
static SEPARATORS: &[char] = &[',', ';'];
// weights used in match score computing
const BONUS_MATCH: i32 = 50_000;
const BONUS_CANDIDATE_LENGTH: i32 = -1; // per char
pub fn norm_chars(s: &str) -> Box<[char]> {
secular::normalized_lower_lay_string(s)
.chars()
.collect::<Vec<char>>()
.into_boxed_slice()
}
/// a list of tokens we want to find, non overlapping
/// and in any order, in strings
#[derive(Debug, Clone, PartialEq)]
pub struct TokPattern {
toks: Vec<Box<[char]>>,
sum_len: usize,
}
// scoring basis ?
// - number of parts of the candidats (separated by / for example)
// that are touched by a tok ?
// - malus for adjacent ranges
// - bonus for ranges starting just after a separator
// - bonus for order ?
impl TokPattern {
pub fn new(pattern: &str) -> Self {
// we accept several separators. The first one
// we encounter among the possible ones is the
// separator of the whole. This allows using the
// other char: In ";ab,er", the comma isn't seen
// as a separator but as part of a tok
let sep = pattern.chars().find(|c| SEPARATORS.contains(c));
let mut toks: Vec<Box<[char]>> = if let Some(sep) = sep {
pattern.split(sep)
.filter(|s| !s.is_empty())
.map(norm_chars)
.collect()
} else {
if pattern.is_empty() {
Vec::new()
} else {
vec![norm_chars(pattern)]
}
};
// we sort the tokens from biggest to smallest
// because the current algorithm stops at the
// first match for any tok. Thus it would fail
// to find "abc,b" in "abcdb" if it looked first
// at the "b" token
toks.sort_by_key(|t| Reverse(t.len()));
let sum_len = toks.iter().map(|s| s.len()).sum();
Self {
toks,
sum_len,
}
}
/// an "empty" pattern is one which accepts everything because
/// it has no discriminant
pub fn is_empty(&self) -> bool {
self.sum_len == 0
}
/// return either None (no match) or a vec whose size is the number
/// of tokens
fn find_ranges(&self, candidate: &str) -> Option<Vec<Range<usize>>> {
if candidate.len() < self.sum_len || self.sum_len == 0 {
return None;
}
let mut cand_chars: CandChars = SmallVec::with_capacity(candidate.len());
cand_chars.extend(candidate.chars().map(secular::lower_lay_char));
// we first look for the first tok, it's simpler
let first_tok = &self.toks[0];
let l = first_tok.len();
let first_matching_range = (0..cand_chars.len()+1-l)
.map(|idx| idx..idx+l)
.find(|r| {
&cand_chars[r.start..r.end] == first_tok.as_ref()
});
// we initialize the vec only when the first tok is found
first_matching_range
.and_then(|first_matching_range| {
let mut matching_ranges = vec![first_matching_range];
for tok in self.toks.iter().skip(1) {
let l = tok.len();
let matching_range = (0..cand_chars.len()+1-l)
.map(|idx| idx..idx+l)
.filter(|r| {
&cand_chars[r.start..r.end] == tok.as_ref()
})
.find(|r| {
// check we're not intersecting a previous range
for pr in &matching_ranges {
if pr.contains(&r.start) || pr.contains(&(r.end-1)) {
return false;
}
}
true
});
if let Some(r) = matching_range {
matching_ranges.push(r);
} else {
return None;
}
}
Some(matching_ranges)
})
}
fn score_of_matching(&self, candidate: &str) -> i32 {
BONUS_MATCH + BONUS_CANDIDATE_LENGTH * candidate.len() as i32
}
/// note that it should not be called on empty patterns
pub fn find(&self, candidate: &str) -> Option<NameMatch> {
self.find_ranges(candidate)
.map(|matching_ranges| {
let mut pos = smallvec![0; self.sum_len];
let mut i = 0;
for r in matching_ranges {
for p in r {
pos[i] = p;
i += 1;
}
}
pos.sort_unstable();
let score = self.score_of_matching(candidate);
NameMatch { score, pos }
})
}
/// compute the score of the best match
/// Note that it should not be called on empty patterns
pub fn score_of(&self, candidate: &str) -> Option<i32> {
self.find_ranges(candidate)
.map(|_| self.score_of_matching(candidate))
}
}
#[cfg(test)]
mod tok_pattern_tests {
use {
super::*,
crate::pattern::Pos,
};
/// check position of the match of the pattern in name
fn check_pos(pattern: &str, name: &str, pos: &str) {
println!("checking pattern={:?} name={:?}", pattern, name);
let pat = TokPattern::new(pattern);
let match_pos = pat.find(name).unwrap().pos;
let target_pos: Pos = pos.chars()
.enumerate()
.filter(|(_, c)| *c=='^')
.map(|(i, _)| i)
.collect();
assert_eq!(match_pos, target_pos);
}
#[test]
fn check_match_pos() {
check_pos(
"m,",
"miaou",
"^ ",
);
check_pos(
"bat",
"cabat",
" ^^^",
);
check_pos(
";ba",
"babababaaa",
"^^ ",
);
check_pos(
"ba,ca",
"bababacaa",
"^^ ^^ ",
);
check_pos(
"sub,doc,2",
"/home/user/path2/subpath/Documents/",
" ^ ^^^ ^^^",
);
check_pos(
"ab,abc",
"0123/abc/ab/cdg",
" ^^^ ^^ ",
);
}
fn check_match(pattern: &str, name: &str, do_match: bool) {
assert_eq!(
TokPattern::new(pattern).find(name).is_some(),
do_match,
);
}
#[test]
fn test_separators() {
let a = TokPattern::new("ab;cd;ef");
let b = TokPattern::new("ab,cd,ef");
assert_eq!(a, b);
let a = TokPattern::new(",ab;cd;ef");
assert_eq!(a.toks.len(), 1);
assert_eq!(a.toks[0].len(), 8);
let a = TokPattern::new(";ab,cd,ef;");
assert_eq!(a.toks.len(), 1);
assert_eq!(a.toks[0].len(), 8);
}
#[test]
fn test_match() {
check_match("mia", "android/phonegap", false);
check_match("mi", "a", false);
check_match("mi", "π", false);
check_match("mi", "miaou/a", true);
}
#[test]
fn test_tok_repetitions() {
check_match("sub", "rasub", true);
check_match("sub,sub", "rasub", false);
check_match("sub,sub", "rasubandsub", true);
check_match("sub,sub,sub", "rasubandsub", false);
check_match("ghi,abc,def,ccc", "abccc/Defghi", false);
check_match("ghi,abc,def,ccc", "abcccc/Defghi", true);
}
}