blib/lib/Regex/PreSuf.pm

filter: Statementstmt 90.4% 171 / 189 Branchbran 80.0% 80 / 100 Conditioncond 84.6% 33 / 39 MC/DCmcdc 73.9% 17 / 23 Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 4 totaltotal 84.8% 308 / 363 CC 79 SCAR 40.4
CC
79
Cov
87%
_presuf
75.4
presuf
6.0
prefix_length
6.0
CRAP 56.7
↑ Summary
1 package Regex::PreSuf;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 local $^W = 1;
5 3 ▶use vars qw($VERSION $DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 $VERSION = "1.17";
8  
9 3 $DEBUG = 0;
10  
11 =pod
12  
13 =head1 NAME
14  
15 Regex::PreSuf - create regular expressions from word lists
16  
17 =head1 SYNOPSIS
18  
19 use Regex::PreSuf;
20
21 my $re = presuf(qw(foobar fooxar foozap));
22  
23 # $re should be now 'foo(?:zap|[bx]ar)'
24  
25 =head1 DESCRIPTION
26  
27 The B<presuf()> subroutine builds regular expressions out of 'word
28 lists', lists of strings. The regular expression matches the same
29 words as the word list. These regular expressions normally run faster
30 than a simple-minded '|'-concatenation of the words.
31  
32 Examples:
33  
34 =over 4
35  
36 =item *
37  
38 'foobar fooxar' => 'foo[bx]ar'
39  
40 =item *
41  
42 'foobar foozap' => 'foo(?:bar|zap)'
43  
44 =item *
45  
46 'foobar fooar' => 'foob?ar'
47  
48 =back
49  
50 The downsides:
51  
52 =over 4
53  
54 =item *
55  
56 The original order of the words is not necessarily respected,
57 for example because the character class matches are collected
58 together, separate from the '|' alternations.
59  
60 =item *
61  
62 The module blithely ignores any specialness of any regular expression
63 metacharacters such as the C<.*?+{}[]^$>, they are just plain ordinary
64 boring characters.
65  
66 =back
67  
68 For the second downside there is an exception. The module has some
69 rudimentary grasp of how to use the 'any character' metacharacter.
70 If you call B<presuf()> like this:
71  
72 my $re = presuf({ anychar=>1 }, qw(foobar foo.ar fooxar));
73  
74 # $re should be now 'foo.ar'
75  
76 The module finds out the common prefixes and suffixes of the words and
77 then recursively looks at the remaining differences. However, by
78 default only common prefixes are used because for many languages
79 (natural or artificial) this seems to produce the fastest matchers.
80 To allow also for suffixes use
81  
82 my $re = presuf({ suffixes=>1 }, ...);
83  
84 To use B<only> suffixes use
85  
86 my $re = presuf({ prefixes=>0 }, ...);
87  
88 (this implicitly enables suffixes)
89  
90 =head2 Debugging
91  
92 In case you want to flood your session without debug messages
93 you can turn on debugging by saying
94  
95 Regex::PreSuf::debug(1);
96  
97 How to turn them off again is left as an exercise for the kind reader.
98  
99 =head1 COPYRIGHT
100  
101 Jarkko Hietaniemi
102  
103 This code is distributed under the same copyright terms as Perl itself.
104  
105 =cut
106  
107 3 ▶use vars qw(@ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
108 3 require Exporter;
109 3 @ISA = qw(Exporter);
110 3 @EXPORT = qw(presuf);
111  
112 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod undocumented
113 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
114 0 $DEBUG = shift;
115 } else {
116 0 return $DEBUG;
117 }
118 }
119  
120 39429 ▶sub prefix_length {
Subroutine prefix_length: called CC=6 SCAR=17.9
Pod undocumented
121 39429 my $n = 0;
122 39429 my %diff;
123  
124 39429 for(my $m = 0; ; $m++) {
125 67729 foreach (@_) {
126 554093 ▶ $diff{ @{$_} <= $m ? '' : $_->[$m] }++;
Branch: 1 branch
BranchTrueFalseTotal
@{$_;} <= $m ? : 1916 552177 554093
127 }
128 67729 ▶ last if keys %diff > 1;
Branch: 1 branch
BranchTrueFalseTotal
if keys %diff > 1 39426 28303 67729
129 28303 ▶ if (exists $diff{ '' } and $diff{ '' } == @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $diff{""} and $diff{""} == @_) 3 28300 28303
Condition66%2/3
exists $diff{""} and $diff{""} == @_
!ll&&!rl&&r
2830003
MC/DC50%1/2
exists $diff{""} and $diff{""} == @_
exists $diff{""} $diff{""} == @_
Truth table66%2/3
A and B
A= exists $diff{""}B= $diff{""} == @_
ABresult
0X0
100
111
130 3 %diff = ();
131 3 last;
132 }
133 28300 %diff = ();
134 28300 $n = $m+1;
135 }
136  
137 39429 return ($n, %diff);
138 }
139  
140 39429 ▶sub suffix_length {
Subroutine suffix_length: called CC=6 SCAR=17.9
Pod undocumented
141 39429 my $n = 0;
142 39429 my %diff;
143  
144 39429 for(my $m = 1; ; $m++) {
145 43809 foreach (@_){
146 402154 ▶ $diff{ @{$_} < $m ? '' : $_->[-$m] }++;
Branch: 1 branch
BranchTrueFalseTotal
@{$_;} < $m ? : 1228 400926 402154
147 }
148 43809 ▶ last if keys %diff > 1;
Branch: 1 branch
BranchTrueFalseTotal
if keys %diff > 1 39426 4383 43809
149 4383 ▶ if (exists $diff{ '' } and $diff{ '' } == @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $diff{""} and $diff{""} == @_) 3 4380 4383
Condition66%2/3
exists $diff{""} and $diff{""} == @_
!ll&&!rl&&r
438003
MC/DC50%1/2
exists $diff{""} and $diff{""} == @_
exists $diff{""} $diff{""} == @_
Truth table66%2/3
A and B
A= exists $diff{""}B= $diff{""} == @_
ABresult
0X0
100
111
150 3 %diff = ();
151 3 last;
152 }
153 4380 %diff = ();
154 4380 $n = $m;
155 }
156  
157 39429 return ($n, %diff);
158 }
159  
160 39426 ▶sub _presuf {
Subroutine _presuf: called CC=63 SCAR=43.2
161 39426 my $level = shift;
162 39426 ▶ my $INDENT = " " x $level if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Regex::PreSuf::DEBUG 0 39426 39426
163 39426 my $param = shift;
164
165 0 ▶ print "_presuf:$INDENT <- ", join(" ", map { join('', @$_) } @_), "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Regex::PreSuf::DEBUG 0 39426 39426
166 if $DEBUG;
167  
168 39426 ▶ return '' if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 0 39426 39426
169  
170 39426 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 9 39417 39426
171 9 my $presuf = join('', @{ $_[0] });
172 9 ▶ print "_presuf:$INDENT -> $presuf\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Regex::PreSuf::DEBUG 0 9 9
173 9 return $presuf;
174 }
175  
176 39417 my ($pre_n, %pre_d) = prefix_length @_;
177 39417 my ($suf_n, %suf_d) = suffix_length @_;
178  
179 39417 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Regex::PreSuf::DEBUG) 0 39417 39417
180 0 print "_presuf:$INDENT pre_n = $pre_n (",join(" ",%pre_d),")\n";
181 0 print "_presuf:$INDENT suf_n = $suf_n (",join(" ",%suf_d),")\n";
182 }
183  
184 my $prefixes = not exists $param->{ prefixes } ||
185 39417 ▶ $param->{ prefixes };
Condition66%2/3
exists $param->{'prefixes'} || $param->{'prefixes'}
l!l&&r!l&&!r
3039414
MC/DC50%1/2
exists $param->{'prefixes'} || $param->{'prefixes'}
exists $param->{'prefixes'} $param->{'prefixes'}
Truth table66%2/3
A || B
A= exists $param->{'prefixes'}B= $param->{'prefixes'}
ABresult
1X1
011
000
186 my $suffixes = $param->{ suffixes } ||
187 ( exists $param->{ prefixes } &&
188 39417 ▶ not $param->{ prefixes });
Condition83%5/6
$param->{'suffixes'} || exists $param->{'prefixes'} && !$param->{'prefixes'}
l!l&&r!l&&!r
7339407
exists $param->{"prefixes"} and not $param->{"prefixes"}
!ll&&!rl&&r
3940703
MC/DC66%2/3
$param->{'suffixes'} || exists $param->{'prefixes'} && !$param->{'prefixes'}
$param->{'suffixes'} exists $param->{"prefixes"} not $param->{"prefixes"}
Truth table75%3/4
A || B and not(C)
A= $param->{'suffixes'}B= exists $param->{"prefixes"}C= not $param->{"prefixes"}
ABCresult
1XX1
0111
00X0
0100
189  
190 39417 ▶ if ($prefixes and not $suffixes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefixes and not $suffixes) 39407 10 39417
Condition100%3/3
$prefixes and not $suffixes
!ll&&!rl&&r
3739407
MC/DC100%2/2
$prefixes and not $suffixes
$prefixes not $suffixes
Truth table100%3/3
A and not(B)
A= $prefixesB= not $suffixes
ABresult
0X0
100
111
191 # On qw(rattle rattlesnake) clear suffix.
192 39407 foreach (keys %pre_d) {
193 122335 ▶ if ($_ eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "") 1900 120435 122335
194 1900 $suf_n = 0;
195 1900 %suf_d = ();
196 1900 last;
197 }
198 }
199 }
200  
201 39417 ▶ if ($suffixes and not $prefixes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($suffixes and not $prefixes) 3 39414 39417
Condition100%3/3
$suffixes and not $prefixes
!ll&&!rl&&r
3940773
MC/DC100%2/2
$suffixes and not $prefixes
$suffixes not $prefixes
Truth table100%3/3
A and not(B)
A= $suffixesB= not $prefixes
ABresult
0X0
100
111
202 3 foreach (keys %suf_d) {
203 8 ▶ if ($_ eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "") 0 8 8
204 0 $pre_n = 0;
205 0 %pre_d = ();
206 0 last;
207 }
208 }
209 }
210  
211 39417 ▶ if ($pre_n or $suf_n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre_n or $suf_n) { } 19731 19686 39417
Condition100%3/3
$pre_n or $suf_n
l!l&&r!l&&!r
196537819686
MC/DC100%2/2
$pre_n or $suf_n
$pre_n $suf_n
Truth table100%3/3
A or B
A= $pre_nB= $suf_n
ABresult
1X1
011
000
212 19731 ▶ if ($pre_n == $suf_n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre_n == $suf_n) 1191 18540 19731
213 1191 my $eq_n = 1;
214 1191 my $eq_s = join('', @{ $_[0] });
215  
216 1191 foreach (@_[ 1 .. $#_ ]) {
217 1191 ▶ last if $eq_s ne join('', @{ $_ });
Branch: 1 branch
BranchTrueFalseTotal
if $eq_s ne join("", @{$_;}) 1191 0 1191
218 0 $eq_n++;
219 }
220  
221 1191 ▶ if ($eq_n == @_) { # All equal. How boring.
Branch: 1 branch
BranchTrueFalseTotal
if ($eq_n == @_) 0 1191 1191
222 0 ▶ print "_presuf:$INDENT -> $eq_s\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Regex::PreSuf::DEBUG 0 0 0
223 0 return $eq_s;
224 }
225 }
226  
227 19731 my $ps_n = $pre_n + $suf_n;
228 19731 my $overlap; # Guard against prefix and suffix overlapping.
229  
230 19731 foreach (@_) {
231 184627 ▶ if (@{ $_ } < $ps_n) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$_;} < $ps_n) 52 184575 184627
232 52 $overlap = 1;
233 52 last;
234 }
235 }
236  
237 # Remove prefixes and suffixes and recurse.
238  
239 my $pre_s = $pre_n ?
240 19731 ▶ join('', @{ $_[0] }[ 0 .. $pre_n - 1 ]) : '';
Branch: 1 branch
BranchTrueFalseTotal
$pre_n ? : 19653 78 19731
241 my $suf_s = $suf_n ?
242 19731 ▶ join('', @{ $_[0] }[ -$suf_n .. -1 ]) : '';
Branch: 1 branch
BranchTrueFalseTotal
$suf_n ? : 2554 17177 19731
243 19731 my @presuf;
244  
245 19731 ▶ if ($overlap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overlap) { } 52 19679 19731
246 52 ▶ if ($prefixes and not $suffixes) {
Branch: 2 branches
BranchTrueFalseTotal
if ($prefixes and not $suffixes) { } 51 1 52
elsif ($suffixes) { } 1 0 1
Condition66%2/3
$prefixes and not $suffixes
!ll&&!rl&&r
0151
MC/DC50%1/2
$prefixes and not $suffixes
$prefixes not $suffixes
Truth table66%2/3
A and not(B)
A= $prefixesB= not $suffixes
ABresult
0X0
100
111
247 51 $suf_s = '';
248 51 foreach (@_) {
249 push @presuf,
250 104 [ @{ $_ }[ $pre_n .. $#{ $_ } ] ];
251 }
252 } elsif ($suffixes) {
253 1 $pre_s = '';
254 1 foreach (@_) {
255 push @presuf,
256 2 [ @{ $_ }[ 0 .. $#{ $_ } - $suf_n ] ];
257 }
258 }
259 } else {
260 19679 foreach (@_) {
261 push @presuf,
262 184544 [ @{ $_ }[ $pre_n .. $#{ $_ } - $suf_n ] ];
263 }
264 }
265  
266 19731 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Regex::PreSuf::DEBUG) 0 19731 19731
267 0 print "_presuf:$INDENT pre_s = $pre_s\n";
268 0 print "_presuf:$INDENT suf_s = $suf_s\n";
269 print "_presuf:$INDENT presuf = ",
270 0 join(" ", map { join('', @$_) } @presuf), "\n";
271 }
272  
273 19731 my $presuf = $pre_s . _presuf($level + 1, $param, @presuf) . $suf_s;
274  
275 19731 ▶ print "_presuf:$INDENT -> $presuf\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Regex::PreSuf::DEBUG 0 19731 19731
276  
277 19731 return $presuf;
278 } else {
279 19686 my @len_n;
280 my @len_1;
281 19686 my $len_0 = 0;
282 19686 my (@alt_n, @alt_1);
283  
284 19686 foreach (@_) {
285 184595 my $len = @{$_};
286 184595 ▶ if ($len > 1) { push @len_n, $_ }
Branch: 2 branches
BranchTrueFalseTotal
if ($len > 1) { } 179739 4856 184595
elsif ($len == 1) { } 3727 1129 4856
287 3727 elsif ($len == 1) { push @len_1, $_ }
288 1129 else { $len_0++ } # $len == 0
289 }
290  
291 # NOTE: does not preserve the order of the words.
292  
293 19686 ▶ if (@len_n) { # Alternation.
Branch: 1 branch
BranchTrueFalseTotal
if (@len_n) 19333 353 19686
294 19333 ▶ if (@len_n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@len_n == 1) { } 2183 17150 19333
295 2183 @alt_n = join('', @{ $len_n[0] });
296 } else {
297 17150 my @pre_d = keys %pre_d;
298 17150 my @suf_d = keys %suf_d;
299  
300 17150 my (%len_m, @len_m);
301  
302 17150 ▶ if ($prefixes and $suffixes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefixes and $suffixes) 2 17148 17150
Condition100%3/3
$prefixes and $suffixes
!ll&&!rl&&r
2171462
MC/DC100%2/2
$prefixes and $suffixes
$prefixes $suffixes
Truth table100%3/3
A and B
A= $prefixesB= $suffixes
ABresult
0X0
100
111
303 2 ▶ if (@pre_d < @suf_d) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pre_d < @suf_d) { } 1 1 2
304 1 $suffixes = 0;
305 } else {
306 1 ▶ if (@pre_d == @suf_d) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pre_d == @suf_d) { } 1 0 1
307 1 ▶ if ( $param->{ suffixes } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{'suffixes'}) { } 1 0 1
308 1 $prefixes = 0;
309 } else {
310 0 $suffixes = 0;
311 }
312 } else {
313 0 $prefixes = 0;
314 }
315 }
316 }
317  
318 17150 ▶ if ($prefixes) {
Branch: 2 branches
BranchTrueFalseTotal
if ($prefixes) { } 17147 3 17150
elsif ($suffixes) { } 3 0 3
319 17147 foreach (@len_n) {
320 177545 push @{ $len_m{ $_->[ 0 ] } }, $_;
321 }
322 } elsif ($suffixes) {
323 3 foreach (@len_n) {
324 11 push @{ $len_m{ $_->[ -1 ] } }, $_;
325 }
326 }
327  
328 17150 foreach (sort keys %len_m) {
329 55498 ▶ if (@{ $len_m{ $_ } } > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$len_m{$_};} > 1) { } 19612 35886 55498
330 push @alt_n,
331 19612 _presuf($level + 1, $param, @{ $len_m{ $_ } });
332 } else {
333 35886 push @alt_n, join('', @{ $len_m{ $_ }->[0] });
334 }
335 }
336 }
337 }
338  
339 19686 ▶ if (@len_1) { # Character classes.
Branch: 1 branch
BranchTrueFalseTotal
if (@len_1) 3297 16389 19686
340 3297 ▶ if ($param->{ anychar } and
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{'anychar'} and exists $pre_d{'.'} || exists $suf_d{'.'} and grep {$_->[0] eq '.';} @len_1) { } 3 3294 3297
Condition88%8/9
$param->{"anychar"} and exists $pre_d{"."} || exists $suf_d{"."} and grep {$_->[0] eq ".";} @len_1
!ll&&!rl&&r
329313
$param->{"anychar"} and exists $pre_d{"."} || exists $suf_d{"."}
!ll&&!rl&&r
329034
exists $pre_d{"."} or exists $suf_d{"."}
l!l&&r!l&&!r
403
MC/DC75%3/4
$param->{"anychar"} and exists $pre_d{"."} || exists $suf_d{"."} and grep {$_->[0] eq ".";} @len_1
$param->{"anychar"} exists $pre_d{"."} exists $suf_d{"."} grep {$_->[0] eq ".";} @len_1
Truth table66%4/6
A and B or C and D
A= $param->{"anychar"}B= exists $pre_d{"."}C= exists $suf_d{"."}D= grep {$_->[0] eq ".";} @len_1
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
341 (exists $pre_d{ '.' } or exists $suf_d{ '.' }) and
342 7 grep { $_->[0] eq '.' } @len_1) {
343 3 push @alt_1, '.';
344 } else {
345 3294 ▶ if (@len_1 == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@len_1 == 1) { } 2891 403 3294
346 push @alt_1,
347 2891 join('', @{$len_1[0]});
348 } else {
349 403 my %uniq;
350 push @alt_1,
351 join('', '[', (sort
352 831 grep { ! $uniq{$_}++ }
353 403 map { join('', @$_) } @len_1), ']' );
354 }
355 }
356 }
357  
358 19686 my $alt = join('|', @alt_n, @alt_1);
359  
360 19686 ▶ $alt = '(?:' . $alt . ')' unless @alt_n == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @alt_n == 0 19333 353 19686
361  
362 19686 ▶ $alt .= '?' if $len_0;
Branch: 1 branch
BranchTrueFalseTotal
if $len_0 1129 18557 19686
363  
364 19686 ▶ print "_presuf:$INDENT -> $alt\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Regex::PreSuf::DEBUG 0 19686 19686
365  
366 19686 return $alt;
367 }
368 }
369  
370 83 ▶sub presuf {
Subroutine presuf: called CC=6 SCAR=17.9
Pod undocumented
371 83 ▶ my $param = ref $_[0] eq 'HASH' ? shift : { };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 6 77 83
372  
373 83 ▶ return '' if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 0 83 83
374  
375 83 my @args = map { quotemeta() } @_;
376  
377 # Undo quotemeta for anychars.
378 83 ▶ @args = map { s/\\\././g; $_ } @args if $param->{ anychar };
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"anychar"} 3 80 83
379  
380 83 s/\\(\s)/$1/g for @args;
381  
382 83 foreach (@args) {
383 42934 $_ = [ /(\\?.)/gs ];
384 }
385  
386 83 return _presuf(0, $param, @args);
387 }
388  
389 3 1;
↑ Summary