-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuzzy.pl
executable file
·343 lines (287 loc) · 7.09 KB
/
fuzzy.pl
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#! /usr/bin/perl
# $Header:$
use strict;
use warnings;
use File::Basename;
use FileHandle;
use Getopt::Long;
use IO::File;
use POSIX;
# Tests:
# acc
# acc.c
# bas.c
# Bas.c
#######################################################################
# Command line switches. #
#######################################################################
my %opts;
my %noext = (
o => 1,
cm => 1,
so => 1,
gif => 1,
png => 1,
bmp => 1,
);
sub main
{
Getopt::Long::Configure('require_order');
Getopt::Long::Configure('no_ignore_case');
usage() unless GetOptions(\%opts,
'debug',
'dir=s',
'f=s',
'files',
'help',
'src',
's=s',
);
usage(0) if $opts{help};
my %f;
if ($opts{dir}) {
foreach my $d (split(/,/, $opts{dir})) {
my $type = '';
$type = " -type f" if $opts{files};
my $fh = new FileHandle("find $d $type |");
while (<$fh>) {
chomp;
$f{$_} = 1;
}
}
} elsif ($opts{f}) {
my $fh = new FileHandle($opts{f});
die "Cannot read $opts{f} - $!" if !$fh;
while (<$fh>) {
chomp;
$f{$_} = 1;
}
} elsif ($opts{s}) {
$f{$opts{s}} = 1;
} else {
foreach my $p (split(":", $ENV{PATH})) {
$f{$_} = 1 foreach glob("$p/*");
}
}
foreach my $f (keys(%f)) {
my $e = basename($f);
next if $e !~ /^.*\.(.*)$/;
delete $f{$f} if $noext{$1};
}
my $pat = shift @ARGV;
usage() if !$pat;
my %h;
my %m;
my $dpat = $pat;
$dpat =~ s/(.)/$1*/g;
$dpat =~ s/\*$//;
# print "dpat=$dpat\n";
foreach my $f (sort(keys(%f))) {
my $b = basename($f);
my $r;
if ($b eq $pat) {
$h{$f} = 10;
$r = "X" x length($b);
} elsif (lc($b) eq lc($pat)) {
$h{$f} = 9;
$r = "X" x length($b);
} elsif ($r = re_match($b, $pat, bol => 1)) {
$h{$f} = 8;
} elsif ($r = re_match($b, $pat, bol => 1, ic => 1)) {
$h{$f} = 7;
} elsif ($r = re_match($b, $pat)) {
$h{$f} = 6;
} elsif ($r = re_match($b, $pat, ic => 1)) {
$h{$f} = 5;
} elsif ($r = re_match($b, $dpat, ic => 1)) {
$h{$f} = 4;
}
#print "r=$r\n";
$m{$f} = $r;
}
my %seen;
my @lst;
foreach my $h (sort { $h{$b} <=> $h{$a} } keys(%h)) {
push @lst, [$h{$h}, $h, $m{$h}];
}
display($_, $pat) foreach @lst;
}
sub display
{ my $parray = shift;
my $pat = shift;
my $mask = $parray->[2];
my $where = $parray->[0];
print "\033[37m$where - ";
print " mask=$mask -" if $opts{v};
my $p = $parray->[1];
print dirname($p) . "/" if $p =~ /\//;
my $b = basename($p);
if (length($mask) != length($b)) {
print "error mask mismatch:\n";
print " b=$b\n";
print " m=$mask\n";
return;
}
for (my $i = 0; $i < length($b); $i++) {
my $ch = substr($b, $i, 1);
if ((substr($mask, $i, 1) || '') eq 'X') {
print "\033[36m$ch\033[37m";
} else {
print $ch;
}
}
print "\n";
if ($opts{debug}) {
print " b=$b\n";
print " m=$mask\n";
}
}
sub re_match
{ my $b = shift;
my $pat = shift;
my %opts = @_;
#print "$_\n" foreach keys (%opts);
my @blst = split(/\./, $b);
#print "b=$b !", join("!", @blst), "!\n" if $b =~ /valgrind/;
my @plst = split(/\./, $pat);
my $i;
#print "$b - $pat - ", join(",", @blst), " ", join(",", @plst), "\n";
my $m = '';
for ($i = 0; $i < @blst; $i++) {
#print "i=$i plst=", scalar(@plst), "\n";
if ($i >= @plst) {
for (; $i < @blst; $i++) {
$m .= ".";
$m .= "." x length($blst[$i]);
}
return $m;
}
my $mat = re_match2($blst[$i], $plst[$i], \%opts);
#print " cmp $b1 $p1 = $mat\n" if $b =~ /^acc/;
#print "i=$i mat=$mat '$blst[$i]' '$plst[$i]'\n";
return "" if $mat eq '';
$m .= "." if $m;
$m .= $mat;
}
###############################################
# For a multi-part match, if we didnt #
# match, then dont match at all. #
###############################################
if ($i < @plst) {
return "";
}
#print "-> good\n";
return $m;
}
sub re_match2
{ my $b = shift;
my $pat = shift;
my $opts = shift;
my $str = '';
my $i = 0;
my $j = 0;
#print "match2: $b $pat\n";
for ($j = 0; $i < length($b) && $j < length($pat); ) {
my $b1 = substr($b, $i, 1);
my $p1 = substr($pat, $j, 1);
if ($opts->{ic}) {
$b1 = lc($b1);
$p1 = lc($p1);
}
if ($b1 eq $p1) {
$str .= 'X';
$i++, $j++;
next;
}
if ($p1 eq '*') {
$i++;
my $found = 0;
while ($i < length($b)) {
my $b1 = substr($b, $i, 1);
$b1 = lc($b1) if $opts->{ic};
if ($b1 eq $p1) {
$i++;
$found = 1;
last;
}
$i++;
$str .= " ";
}
last if !$found;
$j++;
next;
}
$i++;
$str .= "_";
}
if ($j < length($pat)) {
#print "not found\n";
return '';
}
while ($i++ < length($b)) {
$str .= "_";
}
#print "str=$str\n";
return '' if $opts->{bol} && substr($str, 0, 1) ne 'X';
return $str;
}
#######################################################################
# Print out command line usage. #
#######################################################################
sub usage
{ my $ret = shift;
my $msg = shift;
print $msg if $msg;
print <<EOF;
fuzzy.pl -- fuzzy filename matching
Usage: fuzzy.pl [switches] <search-term>
This is a simple tool to do fuzzy filename matching. It was designed
as a learning experience/POC, to do fuzzy matching, similar to what
many IDEs provide. "Cost based" ordering gets fiddly and tricky to debug.
The initial approach for this was to try various algorithms for
each item, and then order the results by "best". This code is designed
to search for filenames - a different set of tests would be
appropriate if we were doing spelling corrections. Whilst
this is useful for filenames, it can be used for code variable
names, since they have the same layout.
We need to provide list of candidate names to search from. This
can be provided in a variety of forms:
* If no switches are provided, then everything in \$PATH is chosen.
* We can provide "-f <filename>" to give a generated or curated list
of filenames
* We can use "-s <name>" to add a single entry, for debug purposes.
The results are listed in best-matching order, and color highlighting
to show the match.
We avoid native regular expressions - in reality, you do not
use these to specify files, and it overly complicates things.
Here is an example:
\$ fuzzy.pl xt
Without any switches, this would match, for instance:
- /usr/bin/xterm
- /usr/bin/xedit
...
The preference is exact case matching, at the start of the filename.
(Directory paths are ignored/stripped). "xterm" matches because
it starts with "xt". "xedit" matches because of the "x" and "t".
The search pattern is split on "." - so you could say:
\$ fuzzy.pl a.j
which might be used to match "argumentValidator.java". You can
omit the extension, or use a prefix/substring to select from
similar files but different extensions.
Switches:
-dir <path)
Scan (find \$dir -print) to generate a list of filenames to match
against.
-f <filename>
Specify a list of filenames to be matched against. E.g.
\$ find . -type f > files.lst
\$ fuzzy.pl -f files.lst abc
-s <name>
Add just <name> to the scan list - useful for debugging one-shot
scenarios.
-v Verbose - some extra debug.
EOF
exit(defined($ret) ? $ret : 1);
}
main();
0;