| 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
| |||||||||||||||||||||||||||||||||
| 30 | 1 | elsif ( $rx =~ m#^\$$# ) { $ret = $rx; } | |||||||||||||||||||||||||||||||
| 31 | else { | ||||||||||||||||||||||||||||||||
| 32 | 9 | ▶ | if ( $rx =~ m#\$$# ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$rx1 =~ /$rx/ and $rx2 =~ /$rx/
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/
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$regex =~ /\(($subrg*)\)\|\(($subrg*)\)/ or $regex =~ /($subrg*)\|($subrg*)/
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*)/
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
not defined $common or length $common == 0
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
| |||||||||||||||||||||||||||||||||
| 110 | 574 | ▶ | if ( $common !~ m#^$subrg+$# ) { last; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | #say "!ok: $regex -> common=$common"; | ||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | # common prefix | ||||||||||||||||||||||||||||||||
| 115 | 110 | ▶ | if ( $rx1 =~ m#^(.*)$common$# && $rx2 =~ m#^(.*)$common$# ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$rx1 =~ /^(.*)$common$/ and $rx2 =~ /^(.*)$common$/
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$/
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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(.*)$/
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(.*)$/
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 157 | 300 | my $sub = $1; | |||||||||||||||||||||||||||||||
| 158 | 300 | ▶ | if ( $sub =~ m#^($subrg)\1+$# ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 214 | 0 | $len++; | |||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | 4939 | ▶ | while ( $regex =~ s/\[\^(.*?)\]// ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 217 | 67 | $alt += ( 256 - length($1) ); | |||||||||||||||||||||||||||||||
| 218 | 67 | $len++; | |||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||
| 220 | 4939 | ▶ | while ( $regex =~ s/\[(.*?)\]// ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 221 | 1060 | $alt += length($1); | |||||||||||||||||||||||||||||||
| 222 | 1060 | $len++; | |||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||
| 224 | 4939 | ▶ | while ( $regex =~ s/\.// ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 225 | 7537 | $alt += 256; | |||||||||||||||||||||||||||||||
| 226 | 7537 | $len++; | |||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||
| 228 | 4939 | ▶ | while ( $regex =~ s/[A-Za-z0-9 ]// ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||