lib/File/FormatIdentification/Regex.pm

filter: Statementstmt 96.8% 124 / 128 Branchbran 88.0% 44 / 50 Conditioncond 86.6% 13 / 15 MC/DCmcdc 80.0% 8 / 10 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 7 totaltotal 90.6% 204 / 225 CC 32 SCAR 23.8
CC
32
Cov
94%
peep_hole_optimizer
20.5
calc_quality
7.0
and_combine
4.0
CRAP 10.8
« lib/File/FormatIdentification/Pronom.pm ↑ Summary script/pronom2wxhexeditor.pl »
1 package File::FormatIdentification::Regex;
2  
3 2 our $VERSION = '0.06'; # VERSION
4  
5 # ABSTRACT helper module to combine and optimize regexes
6  
7 2 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use String::LCSS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Regexp::Assemble;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Regexp::Optimizer;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Exporter 'import'; # gives you Exporter's import() method directly
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 our @EXPORT =
17 qw(and_combine or_combine calc_quality simplify_two_or_combined_regex peep_hole_optimizer )
18 ; # symbols to export on request
19 2 our @EXPORT_OK = qw( hex_replace_from_bracket hex_replace_to_bracket );
20  
21  
22  
23  
24 sub and_combine (@) {
25 my @rx_groups = map {
26 5 ▶ my $rx = $_;
Subroutine and_combine: called CC=4 SCAR=13.9
Pod undocumented
27 11 my $rxfill = "";
28 11 my $ret = '';
29 11 ▶ if ( $rx =~ m#^\^$# ) { $ret = $rx; }
Branch: 2 branches
BranchTrueFalseTotal
if ($rx =~ /^\^$/) { } 1 10 11
elsif ($rx =~ /^\$$/) { } 1 9 10
30 1 elsif ( $rx =~ m#^\$$# ) { $ret = $rx; }
31 else {
32 9 ▶ if ( $rx =~ m#\$$# ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rx =~ /\$$/) 1 8 9
33 1 $rxfill = ".*";
34 }
35 9 $ret = "(?=$rxfill$rx)";
36 }
37 11 $ret;
38 } @_;
39 5 my $combined = join( "", @rx_groups );
40  
41 #my $rx = Regexp::Assemble->new;
42 #$rx->add( $combined );
43 #return $rx->as_string;
44 #my $o = Regexp::Optimizer->new;
45 #my $rcomb = qr/$combined/;
46 #return $o->as_string($rcomb);
47 5 return $combined;
48 }
49  
50 sub or_combine (@) {
51 2 ▶ my $ro = Regexp::Assemble->new;
Subroutine or_combine: called CC=2 SCAR=6.9
Pod undocumented
52 2 foreach my $rx (@_) {
53 5 $ro->add($rx);
54 }
55 2 return $ro->as_string;
56 }
57  
58 sub simplify_two_or_combined_regex($$) {
59 5757 ▶ my $rx1 = $_[0];
Subroutine simplify_two_or_combined_regex: called CC=3 SCAR=11.0
Pod undocumented
60 5757 my $rx2 = $_[1];
61 5757 my $rx = qr#\([A-Za-z0-9]*\)#;
62 5757 ▶ return "" if (($rx1 !~ m/$rx/) || ($rx2 !~ m/$rx/));
Branch: 1 branch
BranchTrueFalseTotal
unless $rx1 =~ /$rx/ and $rx2 =~ /$rx/ 2361 3396 5757
Condition100%3/3
$rx1 =~ /$rx/ and $rx2 =~ /$rx/
!ll&&!rl&&r
17915703396
MC/DC100%2/2
$rx1 =~ /$rx/ and $rx2 =~ /$rx/
$rx1 =~ /$rx/ $rx2 =~ /$rx/
Truth table100%3/3
A and B
A= $rx1 =~ /$rx/B= $rx2 =~ /$rx/
ABresult
0X0
100
111
63 # only left simplify supported yet
64 3396 return String::LCSS::lcss( $rx1, $rx2 );
65 }
66  
67 sub hex_replace_to_bracket {
68 1 ▶ my $regex = shift;
Subroutine hex_replace_to_bracket: called CC=1 SCAR=0.0
Pod undocumented
69 1 $regex =~ s#(?<=\\x)([0-9A-F]{2})#{$1}#g;
70 1 return $regex;
71 }
72  
73 sub hex_replace_from_bracket {
74 1 ▶ my $regex = shift;
Subroutine hex_replace_from_bracket: called CC=1 SCAR=0.0
Pod undocumented
75 1 $regex =~ s#(?<=\\x)\{([0-9A-F]{2})\}#$1#g;
76 1 return $regex;
77 }
78  
79 sub peep_hole_optimizer ($) {
80 5836 ▶ my $regex = $_[0]; # only works if special Regexes within File::FormatIdentification:: used
Subroutine peep_hole_optimizer: called CC=20 SCAR=30.2
Pod undocumented
81  
82 #$regex = hex_replace_to_bracket($regex);
83 5836 ▶ if ($regex =~ m/\\x[0-9]+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regex =~ /\\x[0-9]+/) 0 5836 5836
84 0 confess "regex '$regex' has invalid \\x sequences, use \\x{} instead!";
85 }
86 5836 my $oldregex = $regex;
87 ##### first optimize bracket-groups
88 5836 my $subrg =
89 qr#(?:[A-Za-z0-9])|(?:\\x\{[0-9A-F]{2}\})#; # matches: \x00-\xff or text
90 #my $subrg = qr#(?:\($subra\))#;
91 5836 my $subre = qr#(?:\($subrg(?:\|$subrg)+\))|(?:$subrg)#
92 ; # matches (…|…) or (…|…|…) ...
93 #$regex =~ s#\(\(($subra*)\)\)(?!\|)#(\1\)#g; # matches ((…))
94 5836 $regex =~ s#\(\(($subre+)\)\)#($1)#g;
95 5836 $regex =~ s#\(\((\([^)|]*\)(\|\([^)|]*\))+)\)\)#($1)#g;
96 ##### optimize common subsequences
97 ##### part1, combine bar|baz -> ba(r|z)
98 #say "BEFORE: regex=$regex";
99 5836 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($regex =~ /\(($subrg*)\)\|\(($subrg*)\)/ or $regex =~ /($subrg*)\|($subrg*)/) 1101 4755 5856
Condition100%3/3
$regex =~ /\(($subrg*)\)\|\(($subrg*)\)/ or $regex =~ /($subrg*)\|($subrg*)/
l!l&&r!l&&!r
8812204755
MC/DC100%2/2
$regex =~ /\(($subrg*)\)\|\(($subrg*)\)/ or $regex =~ /($subrg*)\|($subrg*)/
$regex =~ /\(($subrg*)\)\|\(($subrg*)\)/ $regex =~ /($subrg*)\|($subrg*)/
Truth table100%3/3
A or B
A= $regex =~ /\(($subrg*)\)\|\(($subrg*)\)/B= $regex =~ /($subrg*)\|($subrg*)/
ABresult
1X1
011
000
100 $regex =~ m#\(($subrg*)\)\|\(($subrg*)\)# ||
101 $regex =~ m#($subrg*)\|($subrg*)#
102 ) {
103 1101 my $rx1 = $1;
104 1101 my $rx2 = $2;
105  
106 #say "common subseq: $regex -> rx1=$rx1 rx2=$rx2";
107  
108 1101 my $common = String::LCSS::lcss( $rx1, $rx2 );
109 1101 ▶ if ( !defined $common || length($common) == 0 ) { last; }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $common or length $common == 0) 527 574 1101
Condition66%2/3
not defined $common or length $common == 0
l!l&&r!l&&!r
5270574
MC/DC50%1/2
not defined $common or length $common == 0
not defined $common length $common == 0
Truth table66%2/3
not(A) or B
A= not defined $commonB= length $common == 0
ABresult
1X1
011
000
110 574 ▶ if ( $common !~ m#^$subrg+$# ) { last; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($common =~ /^$subrg+$/) 464 110 574
111  
112 #say "!ok: $regex -> common=$common";
113  
114 # common prefix
115 110 ▶ if ( $rx1 =~ m#^(.*)$common$# && $rx2 =~ m#^(.*)$common$# ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rx1 =~ /^(.*)$common$/ and $rx2 =~ /^(.*)$common$/) { } 11 99 110
elsif ($rx1 =~ /^$common(.*)$/ and $rx2 =~ /^$common(.*)$/) { } 9 90 99
Condition100%3/3
$rx1 =~ /^(.*)$common$/ and $rx2 =~ /^(.*)$common$/
!ll&&!rl&&r
96311
MC/DC100%2/2
$rx1 =~ /^(.*)$common$/ and $rx2 =~ /^(.*)$common$/
$rx1 =~ /^(.*)$common$/ $rx2 =~ /^(.*)$common$/
Truth table100%3/3
A and B
A= $rx1 =~ /^(.*)$common$/B= $rx2 =~ /^(.*)$common$/
ABresult
0X0
100
111
116  
117 #say "suffix found";
118 11 $rx1 =~ m#^(.*)$common$#;
119 11 my $rx1_prefix = $1;
120 11 $rx2 =~ m#^(.*)$common$#;
121 11 my $rx2_prefix = $1;
122 11 my $subst = "($rx1_prefix|$rx2_prefix)$common";
123 11 ▶ if ( $regex =~ m#\(($subrg*)\)\|\(($subrg*)\)# ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($regex =~ /\(($subrg*)\)\|\(($subrg*)\)/) { } 9 2 11
elsif ($regex =~ /($subrg*)\|($subrg*)/) { } 2 0 2
124 9 $regex =~ s#\($subrg*\)\|\($subrg*\)#$subst#g;
125 }
126 elsif ( $regex =~ m#($subrg*)\|($subrg*)# ) {
127 2 $regex =~ s#$subrg*\|$subrg*#$subst#g;
128 }
129 }
130  
131 # common suffix
132 ▶ elsif ( $rx1 =~ m#^$common(.*)$# && $rx2 =~ m#^$common(.*)$# ) {
Condition66%2/3
$rx1 =~ /^$common(.*)$/ and $rx2 =~ /^$common(.*)$/
!ll&&!rl&&r
9009
MC/DC50%1/2
$rx1 =~ /^$common(.*)$/ and $rx2 =~ /^$common(.*)$/
$rx1 =~ /^$common(.*)$/ $rx2 =~ /^$common(.*)$/
Truth table66%2/3
A and B
A= $rx1 =~ /^$common(.*)$/B= $rx2 =~ /^$common(.*)$/
ABresult
0X0
100
111
133  
134 #say "prefix found";
135 9 $rx1 =~ m#^$common(.*)$#;
136 9 my $rx1_suffix = $1;
137 9 $rx2 =~ m#^$common(.*)$#;
138 9 my $rx2_suffix = $1;
139 9 my $subst = "$common($rx1_suffix|$rx2_suffix)";
140  
141 #say "subst=$subst";
142 9 ▶ if ( $regex =~ m#\(($subrg*)\)\|\(($subrg*)\)# ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($regex =~ /\(($subrg*)\)\|\(($subrg*)\)/) { } 7 2 9
elsif ($regex =~ /($subrg*)\|($subrg*)/) { } 2 0 2
143 7 $regex =~ s#\($subrg*\)\|\($subrg*\)#$subst#g;
144 }
145 elsif ( $regex =~ m#($subrg*)\|($subrg*)# ) {
146 2 $regex =~ s#$subrg*\|$subrg*#$subst#g;
147 }
148  
149 #say "regex=$regex";
150 }
151 else {
152 90 last;
153 }
154 }
155 ##### part2, combine barbara -> (bar){2}a
156 5836 ▶ while ( $regex =~ m#($subrg{3,}?)(\1+)(?!$subrg*\})# ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regex =~ /(${subrg}{3,}?)(\1+)(?!$subrg*\})/) 300 5572 5872
157 300 my $sub = $1;
158 300 ▶ if ( $sub =~ m#^($subrg)\1+$# ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub =~ /^($subrg)\1+$/) 264 36 300
159 264 last;
160 }
161 36 my $l1 = length($1);
162 36 my $l2 = length($2);
163 36 my $matches = 1 + ( $l2 / $l1 );
164  
165 #say "Found1 in regex='$regex' sub='$sub' with \$2=$2 l1=$l1 l2=$l2 matches=$matches";
166  
167 36 ▶ if ( $sub =~ m#^$subrg$# ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub =~ /^$subrg$/) { } 0 36 36
168 0 $regex =~ s#($subrg{3,}?)\1+(?!$subrg*\})#$sub\{$matches\}#;
169 }
170 else {
171 36 $regex =~ s#($subrg{3,}?)\1+(?!$subrg*\})#($sub)\{$matches\}#;
172 }
173 }
174 ##### part2, combine toooor -> to{4}r
175 5836 ▶ while ( $regex =~ m#($subrg)(\1{3,})(?!$subrg*\})# ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regex =~ /($subrg)(\1{3,})(?!$subrg*\})/) 581 5836 6417
176 581 my $sub = $1;
177 581 my $l1 = length($1);
178 581 my $l2 = length($2);
179 581 my $matches = 1 + ( $l2 / $l1 );
180  
181 #say "Found2 in regex='$regex' sub='$sub' with \$2=$2 l1=$l1 l2=$l2 matches=$matches";
182  
183 581 ▶ if ( $sub =~ m#^$subrg$# ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub =~ /^$subrg$/) { } 581 0 581
184 581 $regex =~ s#($subrg)\1{3,}(?!$subrg*\})#$sub\{$matches\}#;
185 }
186 else {
187 0 $regex =~ s#($subrg)\1{3,}(?!$subrg*\})#($sub)\{$matches\}#;
188 }
189 }
190 ##### part2, combine foooo -> fo{4}
191 #while ($regex =~ m#($subrg)\1{3,}(?!$subrg*\})#) {
192 # my $sub = $1;
193 # my $matches = $#+; $matches++;
194 # say "Found in regex='$regex' sub='$sub' with matches=$matches";
195 # $regex =~ s#($subrg)\1{3,}(?!$subrg*\}#$sub\{$matches\}#;
196 #}
197  
198 5836 return $regex;
199 }
200  
201 # calc regex quality, if more specific the quality is higher
202 sub calc_quality ($) {
203 4939 ▶ my $regex = shift;
Subroutine calc_quality: called CC=7 SCAR=19.5
Pod undocumented
204  
205 # replace all \xff with #
206 # replace all . with ( | | | | )
207 # replace all [abc] with (a|b|c)
208 # replace all [^abc] with (d|e|f|..|)
209 # then $len = count of # and $or = count of |
210 # divide it with $len / (1+$or)
211 4939 my $len = 0;
212 4939 my $alt = 0;
213 4939 ▶ while ( $regex =~ s/\\x[0-9a-f]{2}// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regex =~ s/\\x[0-9a-f]{2}//) 0 4939 4939
214 0 $len++;
215 }
216 4939 ▶ while ( $regex =~ s/\[\^(.*?)\]// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regex =~ s/\[\^(.*?)\]//) 67 4939 5006
217 67 $alt += ( 256 - length($1) );
218 67 $len++;
219 }
220 4939 ▶ while ( $regex =~ s/\[(.*?)\]// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regex =~ s/\[(.*?)\]//) 1060 4939 5999
221 1060 $alt += length($1);
222 1060 $len++;
223 }
224 4939 ▶ while ( $regex =~ s/\.// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regex =~ s/\.//) 7537 4939 12476
225 7537 $alt += 256;
226 7537 $len++;
227 }
228 4939 ▶ while ( $regex =~ s/[A-Za-z0-9 ]// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regex =~ s/[A-Za-z0-9 ]//) 189579 4939 194518
229 189579 $len++;
230 }
231 4939 my $tmp = $len / ( 1 + $alt );
232  
233 4939 ▶ my $quality = ( $tmp == 0 ) ? 0 : int( 1000 * log($tmp) ) / 1000;
Branch: 1 branch
BranchTrueFalseTotal
$tmp == 0 ? : 1 4938 4939
234  
235 #say "rest: $regex len=$len alt=$alt quality=$quality ($tmp)";
236 4939 return $quality;
237 }
238  
239 # see https://stackoverflow.com/questions/869809/combine-regexp#870506
240  
241 2 1;
242  
243 __END__
244  
245 =pod
246  
247 =encoding UTF-8
248  
249 =head1 NAME
250  
251 File::FormatIdentification::Regex
252  
253 =head1 VERSION
254  
255 version 0.06
256  
257 =head1 AUTHOR
258  
259 Andreas Romeyke <pause@andreas-romeyke.de>
260  
261 =head1 COPYRIGHT AND LICENSE
262  
263 This software is copyright (c) 2018 by Andreas Romeyke.
264  
265 This is free software; you can redistribute it and/or modify it under
266 the same terms as the Perl 5 programming language system itself.
267  
268 =cut
« lib/File/FormatIdentification/Pronom.pm ↑ Summary script/pronom2wxhexeditor.pl »