blib/lib/Regexp/Assemble.pm

filter: Statementstmt 99.5% 1160 / 1165 Branchbran 98.0% 831 / 848 Conditioncond 99.4% 188 / 189 MC/DCmcdc 97.2% 104 / 107 Subroutinesub 100% 98 / 98 Podpod 100% 49 / 49 totaltotal 98.9% 2430 / 2456 CC 513 SCAR 30.3
CC
513
Cov
99%
_fastlex
66.2
_insert_path
38.0
_lex
36.0
CRAP 20.7
↑ Summary
1 # Regexp::Assemple.pm
2 #
3 # Copyright (c) 2004-2011 David Landgren
4 # All rights reserved
5  
6 package Regexp::Assemble;
7  
8 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 ▶use constant DEBUG_ADD => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use constant DEBUG_TAIL => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use constant DEBUG_LEX => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use constant DEBUG_TIME => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 11 ▶use vars qw/$have_Storable $Current_Lexer $Default_Lexer $Single_Char $Always_Fail/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # The following patterns were generated with examples/naive.
19  
20 11 $Default_Lexer = qr/(?![[(\\]).(?:[*+?]\??|\{\d+(?:,\d*)?\}\??)?|\\(?:[bABCEGLQUXZ]|[lu].|(?:[^\w]|[aefnrtdDwWsS]|c.|0\d{2}|x(?:[\da-fA-F]{2}|{[\da-fA-F]{4}})|N\{\w+\}|[Pp](?:\{\w+\}|.))(?:[*+?]\??|\{\d+(?:,\d*)?\}\??)?)|\[.*?(?<!\\)\](?:[*+?]\??|\{\d+(?:,\d*)?\}\??)?|\(.*?(?<!\\)\)(?:[*+?]\??|\{\d+(?:,\d*)?\}\??)?/; # ]) restore equilibrium
21  
22 11 $Single_Char = qr/^(?:\\(?:[aefnrtdDwWsS]|c.|[^\w\/{|}-]|0\d{2}|x(?:[\da-fA-F]{2}|{[\da-fA-F]{4}}))|[^\$^])$/;
23  
24 # The pattern to return when nothing has been added (and thus not match anything)
25  
26 11 $Always_Fail = "^\\b\0";
27  
28 11 our $VERSION = '0.38';
29  
30 # ------------------------------------------------
31  
32 2197 ▶sub new {
Subroutine new: called CC=18 SCAR=28.9
Pod documented
33 2197 my $class = shift;
34 2197 my %args = @_;
35  
36 2197 my $anc;
37 2197 for $anc (qw(word line string)) {
38 6591 ▶ if (exists $args{"anchor_$anc"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"anchor_$anc"}) 135 6456 6591
39 135 my $val = delete $args{"anchor_$anc"};
40 135 for my $anchor ("anchor_${anc}_begin", "anchor_${anc}_end") {
41 270 ▶ $args{$anchor} = $val unless exists $args{$anchor};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{$anchor} 269 1 270
42 }
43 }
44 }
45  
46 # anchor_string_absolute sets anchor_string_begin and anchor_string_end_absolute
47 2197 ▶ if (exists $args{anchor_string_absolute}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"anchor_string_absolute"}) 3 2194 2197
48 3 my $val = delete $args{anchor_string_absolute};
49 3 for my $anchor (qw(anchor_string_begin anchor_string_end_absolute)) {
50 6 ▶ $args{$anchor} = $val unless exists $args{$anchor};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{$anchor} 5 1 6
51 }
52 }
53  
54 2197 ▶ exists $args{$_} or $args{$_} = 0 for qw(
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$_} or $args{$_} = 0 315 30443 30758
55 anchor_word_begin
56 anchor_word_end
57 anchor_line_begin
58 anchor_line_end
59 anchor_string_begin
60 anchor_string_end
61 anchor_string_end_absolute
62 debug
63 dup_warn
64 indent
65 lookahead
66 mutable
67 track
68 unroll_plus
69 );
70  
71 2197 ▶ exists $args{$_} or $args{$_} = 1 for qw(
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$_} or $args{$_} = 1 960 5631 6591
72 fold_meta_pairs
73 reduce
74 chomp
75 );
76  
77 2197 @args{qw(re str path)} = (undef, undef, []);
78  
79 2197 ▶ $args{flags} ||= delete $args{modifiers} || '';
Condition100%5/5
$args{"flags"} ||= delete $args{"modifiers"} || ""
l!l&&r!l&&!r
712189
delete $args{'modifiers'} || ''
l!l
12189
MC/DC100%2/2
$args{"flags"} ||= delete $args{"modifiers"} || ""
$args{"flags"} delete $args{'modifiers'}
Truth table100%3/3
A ||= B
A= $args{"flags"}B= delete $args{'modifiers'}
ABresult
1X1
011
000
80 2197 ▶ $args{lex} = $Current_Lexer if defined $Current_Lexer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Regexp::Assemble::Current_Lexer 62 2135 2197
81  
82 2197 my $self = bless \%args, $class;
83  
84 2197 ▶ if ($self->_debug(DEBUG_TIME)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_debug(8)) 1 2196 2197
85 1 $self->_init_time_func();
86 1 $self->{_begin_time} = $self->{_time_func}->();
87 }
88 $self->{input_record_separator} = delete $self->{rs}
89 2197 ▶ if exists $self->{rs};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"rs"} 2 2195 2197
90 2197 ▶ exists $self->{file} and $self->add_file($self->{file});
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"file"} and $self->add_file($self->{"file"}) 3 2194 2197
91  
92 2196 return $self;
93 }
94  
95 9 ▶sub _init_time_func {
Subroutine _init_time_func: called CC=4 SCAR=13.9
96 9 my $self = shift;
97 9 ▶ return if exists $self->{_time_func};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_time_func"} 3 6 9
98  
99 # attempt to improve accuracy
100 6 ▶ if (!defined($self->{_use_time_hires})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_use_time_hires"}) 5 1 6
101 5 eval {require Time::HiRes};
102 5 $self->{_use_time_hires} = $@;
103 }
104 $self->{_time_func} = length($self->{_use_time_hires}) > 0
105 4 ▶ ? sub { time }
Subroutine __ANON__: called CC=1 SCAR=0.0
106 6 ▶ : \&Time::HiRes::time
Branch: 1 branch
BranchTrueFalseTotal
length $self->{'_use_time_hires'} > 0 ? : 1 5 6
107 ;
108 }
109  
110 55 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
111 55 my $self = shift;
112 55 my $clone;
113 55 my @attr = grep {$_ ne 'path'} keys %$self;
114 55 @{$clone}{@attr} = @{$self}{@attr};
115 55 $clone->{path} = _path_clone($self->_path);
116 55 bless $clone, ref($self);
117 }
118  
119 884 ▶sub _fastlex {
Subroutine _fastlex: called CC=66 SCAR=41.9
120 884 my $self = shift;
121 884 my $record = shift;
122 884 my $len = 0;
123 884 my @path = ();
124 884 my $case = '';
125 884 my $qm = '';
126  
127 884 my $debug = $self->{debug} & DEBUG_LEX;
128 884 my $unroll_plus = $self->{unroll_plus};
129  
130 884 my $token;
131 my $qualifier;
132 884 ▶ $debug and print "# _lex <$record>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# _lex <$record>\n" 23 861 884
133 884 my $modifier = q{(?:[*+?]\\??|\\{(?:\\d+(?:,\d*)?|,\d+)\\}\\??)?};
134 884 my $class_matcher = qr/\[(?:\[:[a-z]+:\]|\\?.)*?\]/;
135 884 my $paren_matcher = qr/\(.*?(?<!\\)\)$modifier/;
136 884 my $misc_matcher = qr/(?:(c)(.)|(0)(\d{2}))($modifier)/;
137 884 my $regular_matcher = qr/([^\\[(])($modifier)/;
138 884 my $qm_matcher = qr/(\\?.)/;
139  
140 884 my $matcher = $regular_matcher;
141 {
142 884 ▶ if ($record =~ /\G$matcher/gc) {
Branch: 4 branches
BranchTrueFalseTotal
if ($record =~ /\G$matcher/cg) { } 3344 2616 5960
elsif ($record =~ /\G\\/cg) { } 1680 936 2616
elsif ($record =~ /\G($class_matcher)($modifier)/cg) { } 39 897 936
elsif ($record =~ /\G($paren_matcher)/cg) { } 13 884 897
143 # neither a \\ nor [ nor ( followed by a modifer
144 3344 ▶ if ($1 eq '\\E') {
Branch: 2 branches
BranchTrueFalseTotal
if ($1 eq '\\E') { } 12 3332 3344
elsif ($qm and $1 eq '\\L' || $1 eq '\\U') { } 5 6564 6569
145 12 ▶ $debug and print "# E\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# E\n" 3 9 12
146 12 $case = $qm = '';
147 12 $matcher = $regular_matcher;
148 12 redo;
149 }
150 ▶ elsif ($qm and ($1 eq '\\L' or $1 eq '\\U')) {
Condition100%6/6
$qm and $1 eq "\\L" || $1 eq "\\U"
!ll&&!rl&&r
3237905
$1 eq "\\L" or $1 eq "\\U"
l!l&&r!l&&!r
3290
MC/DC100%3/3
$qm and $1 eq "\\L" || $1 eq "\\U"
$qm $1 eq "\\L" $1 eq "\\U"
Truth table100%4/4
A and B or C
A= $qmB= $1 eq "\\L"C= $1 eq "\\U"
ABCresult
0XX0
1000
11X1
1011
151 5 ▶ $debug and print "# ignore \\L, \\U\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# ignore \\L, \\U\n" 2 3 5
152 5 redo;
153 }
154 3327 $token = $1;
155 3327 ▶ $qualifier = defined $2 ? $2 : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 3237 90 3327
156 3327 ▶ $debug and print "# token <$token> <$qualifier>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# token <$token> <$qualifier>\n" 43 3284 3327
157 3327 ▶ if ($qm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qm) { } 90 3237 3327
158 90 $token = quotemeta($token);
159 90 $token =~ s/^\\([^\w$()*+.?@\[\\\]^|{}\/])$/$1/;
160 }
161 else {
162 3237 $token =~ s{\A([][{}*+?@\\/])\Z}{\\$1};
163 }
164 3327 ▶ if ($unroll_plus and $qualifier =~ s/\A\+(\?)?\Z/*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unroll_plus and $qualifier =~ s/\A\+(\?)?\Z/*/) { } 22 3305 3327
Condition100%3/3
$unroll_plus and $qualifier =~ s/\A\+(\?)?\Z/*/
!ll&&!rl&&r
32594622
MC/DC100%2/2
$unroll_plus and $qualifier =~ s/\A\+(\?)?\Z/*/
$unroll_plus $qualifier =~ s/\A\+(\?)?\Z/*/
Truth table100%3/3
A and B
A= $unroll_plusB= $qualifier =~ s/\A\+(\?)?\Z/*/
ABresult
0X0
100
111
165 22 ▶ $1 and $qualifier .= $1;
Branch: 1 branch
BranchTrueFalseTotal
$1 and $qualifier .= $1 6 16 22
166 22 ▶ $debug and print " unroll <$token><$token><$qualifier>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print " unroll <$token><$token><$qualifier>\n" 8 14 22
167 22 ▶ $case and $token = $case eq 'L' ? lc($token) : uc($token);
Branch: 2 branches
BranchTrueFalseTotal
$case and $token = $case eq "L" ? lc $token : uc $token 4 18 22
$case eq 'L' ? : 2 2 4
168 22 push @path, $token, "$token$qualifier";
169 }
170 else {
171 3305 ▶ $debug and print " clean <$token>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print " clean <$token>\n" 35 3270 3305
172 3305 ▶ push @path,
Branch: 2 branches
BranchTrueFalseTotal
$case eq 'L' ? : 17 3288 3305
$case eq 'U' ? : 19 3269 3288
173 $case eq 'L' ? lc($token).$qualifier
174 : $case eq 'U' ? uc($token).$qualifier
175 : $token.$qualifier
176 ;
177 }
178 3327 redo;
179 }
180  
181 elsif ($record =~ /\G\\/gc) {
182 1680 ▶ $debug and print "# backslash\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# backslash\n" 27 1653 1680
183 # backslash
184 1680 ▶ if ($record =~ /\G([sdwSDW])($modifier)/gc) {
Branch: 8 branches
BranchTrueFalseTotal
if ($record =~ /\G([sdwSDW])($modifier)/cg) { } 443 1237 1680
elsif ($record =~ /\Gx([\da-fA-F]{2})($modifier)/cg) { } 9 1228 1237
elsif ($record =~ /\GQ/cg) { } 26 1202 1228
elsif ($record =~ /\G([LU])/cg) { } 15 1187 1202
elsif ($record =~ /\GE/cg) { } 6 1181 1187
elsif ($record =~ /\G([lu])(.)/cg) { } 6 1175 1181
elsif (my(@arg) = grep({defined $_;} $record =~ /\G$misc_matcher/cg)) { } 6 1169 1175
elsif ($record =~ /\G(.)/cg) { } 1165 4 1169
185 443 ($token, $qualifier) = ($1, $2);
186 443 ▶ $debug and print "# meta <$token> <$qualifier>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# meta <$token> <$qualifier>\n" 4 439 443
187 443 ▶ push @path, ($unroll_plus and $qualifier =~ s/\A\+(\?)?\Z/*/)
Branch: 2 branches
BranchTrueFalseTotal
$unroll_plus && $qualifier =~ s/\A\+(\?)?\Z/*/ ? : 3 440 443
defined $1 ? : 1 2 3
Condition100%3/3
$unroll_plus and $qualifier =~ s/\A\+(\?)?\Z/*/
!ll&&!rl&&r
43733
MC/DC100%2/2
$unroll_plus and $qualifier =~ s/\A\+(\?)?\Z/*/
$unroll_plus $qualifier =~ s/\A\+(\?)?\Z/*/
Truth table100%3/3
A and B
A= $unroll_plusB= $qualifier =~ s/\A\+(\?)?\Z/*/
ABresult
0X0
100
111
188 ? ("\\$token", "\\$token$qualifier" . (defined $1 ? $1 : ''))
189 : "\\$token$qualifier";
190 }
191 elsif ($record =~ /\Gx([\da-fA-F]{2})($modifier)/gc) {
192 9 ▶ $debug and print "# x $1\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# x $1\n" 4 5 9
193 9 $token = quotemeta(chr(hex($1)));
194 9 $qualifier = $2;
195 9 ▶ $debug and print "# cooked <$token>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# cooked <$token>\n" 4 5 9
196 9 $token =~ s/^\\([^\w$()*+.?\[\\\]^|{\/])$/$1/; # } balance
197 9 ▶ $debug and print "# giving <$token>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# giving <$token>\n" 4 5 9
198 9 ▶ push @path, ($unroll_plus and $qualifier =~ s/\A\+(\?)?\Z/*/)
Branch: 2 branches
BranchTrueFalseTotal
$unroll_plus && $qualifier =~ s/\A\+(\?)?\Z/*/ ? : 3 6 9
defined $1 ? : 1 2 3
Condition100%3/3
$unroll_plus and $qualifier =~ s/\A\+(\?)?\Z/*/
!ll&&!rl&&r
333
MC/DC100%2/2
$unroll_plus and $qualifier =~ s/\A\+(\?)?\Z/*/
$unroll_plus $qualifier =~ s/\A\+(\?)?\Z/*/
Truth table100%3/3
A and B
A= $unroll_plusB= $qualifier =~ s/\A\+(\?)?\Z/*/
ABresult
0X0
100
111
199 ? ($token, "$token$qualifier" . (defined $1 ? $1 : ''))
200 : "$token$qualifier";
201 }
202 elsif ($record =~ /\GQ/gc) {
203 26 ▶ $debug and print "# Q\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# Q\n" 4 22 26
204 26 $qm = 1;
205 26 $matcher = $qm_matcher;
206 }
207 elsif ($record =~ /\G([LU])/gc) {
208 15 ▶ $debug and print "# case $1\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# case $1\n" 6 9 15
209 15 $case = $1;
210 }
211 elsif ($record =~ /\GE/gc) {
212 6 ▶ $debug and print "# E\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# E\n" 2 4 6
213 6 $case = $qm = '';
214 6 $matcher = $regular_matcher;
215 }
216 elsif ($record =~ /\G([lu])(.)/gc) {
217 6 ▶ $debug and print "# case $1 to <$2>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# case $1 to <$2>\n" 2 4 6
218 6 ▶ push @path, $1 eq 'l' ? lc($2) : uc($2);
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'l' ? : 3 3 6
219 }
220 30 elsif (my @arg = grep {defined} $record =~ /\G$misc_matcher/gc) {
221 6 ▶ if ($] < 5.007) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.007") 0 6 6
222 0 my $len = 0;
223 0 $len += length($_) for @arg;
224 0 ▶ $debug and print "# pos ", pos($record), " fixup add $len\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# pos ", pos $record, " fixup add $len\n" 0 0 0
225 0 pos($record) = pos($record) + $len;
226 }
227 6 my $directive = shift @arg;
228 6 ▶ if ($directive eq 'c') {
Branch: 1 branch
BranchTrueFalseTotal
if ($directive eq 'c') { } 3 3 6
229 3 ▶ $debug and print "# ctrl <@arg>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# ctrl <@arg>\n" 1 2 3
230 3 push @path, "\\c" . uc(shift @arg);
231 }
232 else { # elsif ($directive eq '0') {
233 3 ▶ $debug and print "# octal <@arg>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# octal <@arg>\n" 1 2 3
234 3 my $ascii = oct(shift @arg);
235 3 ▶ push @path, ($ascii < 32)
Branch: 1 branch
BranchTrueFalseTotal
$ascii < 32 ? : 2 1 3
236 ? "\\c" . chr($ascii+64)
237 : chr($ascii)
238 ;
239 }
240 6 $path[-1] .= join( '', @arg ); # if @arg;
241 6 redo;
242 }
243 elsif ($record =~ /\G(.)/gc) {
244 1165 $token = $1;
245 1165 $token =~ s{[AZabefnrtz\[\]{}()\\\$*+.?@|/^]}{\\$token};
246 1165 ▶ $debug and print "# meta <$token>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# meta <$token>\n" 2 1163 1165
247 1165 push @path, $token;
248 }
249 else {
250 4 ▶ $debug and print "# ignore char at ", pos($record), " of <$record>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# ignore char at ", pos $record, " of <$record>\n" 1 3 4
251 }
252 1674 redo;
253 }
254  
255 elsif ($record =~ /\G($class_matcher)($modifier)/gc) {
256 # [class] followed by a modifer
257 39 my $class = $1;
258 39 ▶ my $qualifier = defined $2 ? $2 : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 39 0 39
259 39 ▶ $debug and print "# class begin <$class> <$qualifier>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# class begin <$class> <$qualifier>\n" 4 35 39
260 39 ▶ if ($class =~ /\A\[\\?(.)]\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /\A\[\\?(.)]\Z/) 12 27 39
261 12 $class = quotemeta $1;
262 12 $class =~ s{\A\\([!@%])\Z}{$1};
263 12 ▶ $debug and print "# class unwrap $class\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# class unwrap $class\n" 1 11 12
264 }
265 39 ▶ $debug and print "# class end <$class> <$qualifier>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# class end <$class> <$qualifier>\n" 4 35 39
266 39 ▶ push @path, ($unroll_plus and $qualifier =~ s/\A\+(\?)?\Z/*/)
Branch: 2 branches
BranchTrueFalseTotal
$unroll_plus && $qualifier =~ s/\A\+(\?)?\Z/*/ ? : 3 36 39
defined $1 ? : 1 2 3
Condition100%3/3
$unroll_plus and $qualifier =~ s/\A\+(\?)?\Z/*/
!ll&&!rl&&r
3243
MC/DC100%2/2
$unroll_plus and $qualifier =~ s/\A\+(\?)?\Z/*/
$unroll_plus $qualifier =~ s/\A\+(\?)?\Z/*/
Truth table100%3/3
A and B
A= $unroll_plusB= $qualifier =~ s/\A\+(\?)?\Z/*/
ABresult
0X0
100
111
267 ? ($class, "$class$qualifier" . (defined $1 ? $1 : ''))
268 : "$class$qualifier";
269 39 redo;
270 }
271  
272 elsif ($record =~ /\G($paren_matcher)/gc) {
273 13 ▶ $debug and print "# paren <$1>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# paren <$1>\n" 1 12 13
274 # (paren) followed by a modifer
275 13 push @path, $1;
276 13 redo;
277 }
278  
279 }
280 884 return \@path;
281 }
282  
283 211 ▶sub _lex {
Subroutine _lex: called CC=36 SCAR=35.8
284 211 my $self = shift;
285 211 my $record = shift;
286 211 my $len = 0;
287 211 my @path = ();
288 211 my $case = '';
289 211 my $qm = '';
290 my $re = defined $self->{lex} ? $self->{lex}
291 211 ▶ : defined $Current_Lexer ? $Current_Lexer
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'lex'} ? : 23 188 211
defined $Regexp::Assemble::Current_Lexer ? : 0 188 188
292 : $Default_Lexer;
293 211 my $debug = $self->{debug} & DEBUG_LEX;
294 211 ▶ $debug and print "# _lex <$record>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# _lex <$record>\n" 17 194 211
295 211 my ($token, $next_token, $diff, $token_len);
296 211 ▶ while( $record =~ /($re)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record =~ /($re)/g) 480 211 691
297 480 $token = $1;
298 480 $token_len = length($token);
299 480 ▶ $debug and print "# lexed <$token> len=$token_len\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# lexed <$token> len=$token_len\n" 81 399 480
300 480 ▶ if( pos($record) - $len > $token_len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos($record) - $len > $token_len) 15 465 480
301 15 $next_token = $token;
302 15 $token = substr( $record, $len, $diff = pos($record) - $len - $token_len );
303 15 ▶ $debug and print "# recover <", substr( $record, $len, $diff ), "> as <$token>, save <$next_token>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# recover <", substr($record, $len, $diff), "> as <$token>, save <$next_token>\n" 8 7 15
304 15 $len += $diff;
305 }
306 480 $len += $token_len;
307 TOKEN: {
308 480 ▶ if( substr( $token, 0, 1 ) eq '\\' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($token, 0, 1) eq '\\') { } 226 269 495
309 226 ▶ if( $token =~ /^\\([ELQU])$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token =~ /^\\([ELQU])$/) { } 51 175 226
elsif ($token =~ /^\\([lu])(.)$/) { } 3 172 175
elsif ($token =~ /^\\x([\da-fA-F]{2})$/) { } 41 131 172
310 51 ▶ if( $1 eq 'E' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($1 eq 'E') { } 12 39 51
elsif ($1 eq 'Q') { } 19 20 39
311 $qm and $re = defined $self->{lex} ? $self->{lex}
312 12 ▶ : defined $Current_Lexer ? $Current_Lexer
Branch: 3 branches
BranchTrueFalseTotal
$qm and $re = defined $self->{"lex"} ? $self->{"lex"} : (defined $Regexp::Assemble::Current_Lexer ? $Regexp::Assemble::Current_Lexer : $Regexp::Assemble::Default_Lexer) 6 6 12
defined $self->{'lex'} ? : 1 5 6
defined $Regexp::Assemble::Current_Lexer ? : 0 5 5
313 : $Default_Lexer;
314 12 $case = $qm = '';
315 }
316 elsif( $1 eq 'Q' ) {
317 19 $qm = $1;
318 # switch to a more precise lexer to quotemeta individual characters
319 19 $re = qr/\\?./;
320 }
321 else {
322 20 $case = $1;
323 }
324 51 ▶ $debug and print "# state change qm=<$qm> case=<$case>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# state change qm=<$qm> case=<$case>\n" 16 35 51
325 51 goto NEXT_TOKEN;
326 }
327 elsif( $token =~ /^\\([lu])(.)$/ ) {
328 3 ▶ $debug and print "# apply case=<$1> to <$2>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# apply case=<$1> to <$2>\n" 1 2 3
329 3 ▶ push @path, $1 eq 'l' ? lc($2) : uc($2);
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'l' ? : 1 2 3
330 3 goto NEXT_TOKEN;
331 }
332 elsif( $token =~ /^\\x([\da-fA-F]{2})$/ ) {
333 41 $token = quotemeta(chr(hex($1)));
334 41 ▶ $debug and print "# cooked <$token>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# cooked <$token>\n" 1 40 41
335 41 $token =~ s/^\\([^\w$()*+.?@\[\\\]^|{\/])$/$1/; # } balance
336 41 ▶ $debug and print "# giving <$token>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# giving <$token>\n" 1 40 41
337 }
338 else {
339 131 $token =~ s/^\\([^\w$()*+.?@\[\\\]^|{\/])$/$1/; # } balance
340 131 ▶ $debug and print "# backslashed <$token>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# backslashed <$token>\n" 4 127 131
341 }
342 }
343 else {
344 269 ▶ $case and $token = $case eq 'U' ? uc($token) : lc($token);
Branch: 2 branches
BranchTrueFalseTotal
$case and $token = $case eq "U" ? uc $token : lc $token 34 235 269
$case eq 'U' ? : 21 13 34
345 269 ▶ $qm and $token = quotemeta($token);
Branch: 1 branch
BranchTrueFalseTotal
$qm and $token = quotemeta $token 49 220 269
346 269 ▶ $token = '\\/' if $token eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $token eq "/" 2 267 269
347 }
348 # undo quotemeta's brute-force escapades
349 441 ▶ $qm and $token =~ s/^\\([^\w$()*+.?@\[\\\]^|{}\/])$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
$qm and $token =~ s<^\\([^\w$()*+.?\@\[\\\]^|{}/])$>[$1] 50 391 441
350 441 ▶ $debug and print "# <$token> case=<$case> qm=<$qm>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# <$token> case=<$case> qm=<$qm>\n" 72 369 441
351 441 push @path, $token;
352  
353 NEXT_TOKEN:
354 495 ▶ if( defined $next_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next_token) 15 480 495
355 15 ▶ $debug and print "# redo <$next_token>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# redo <$next_token>\n" 8 7 15
356 15 $token = $next_token;
357 15 $next_token = undef;
358 15 redo TOKEN;
359 }
360 }
361 }
362 211 ▶ if( $len < length($record) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < length $record) 7 204 211
363 # NB: the remainder only arises in the case of degenerate lexer,
364 # and if \Q is operative, the lexer will have been switched to
365 # /\\?./, which means there can never be a remainder, so we
366 # don't have to bother about quotemeta. In other words:
367 # $qm will never be true in this block.
368 7 my $remain = substr($record,$len);
369 7 ▶ $case and $remain = $case eq 'U' ? uc($remain) : lc($remain);
Branch: 2 branches
BranchTrueFalseTotal
$case and $remain = $case eq "U" ? uc $remain : lc $remain 2 5 7
$case eq 'U' ? : 1 1 2
370 7 ▶ $debug and print "# add remaining <$remain> case=<$case> qm=<$qm>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# add remaining <$remain> case=<$case> qm=<$qm>\n" 6 1 7
371 7 push @path, $remain;
372 }
373 211 ▶ $debug and print "# _lex out <@path>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# _lex out <@path>\n" 17 194 211
374 211 return \@path;
375 }
376  
377 1069 ▶sub add {
Subroutine add: called CC=10 SCAR=23.0
Pod documented
378 1069 my $self = shift;
379 1069 my $record;
380 1069 my $debug = $self->{debug} & DEBUG_LEX;
381 1069 ▶ while( defined( $record = shift @_ )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($record = shift @_)) 2279 1069 3348
382 2279 ▶ CORE::chomp($record) if $self->{chomp};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"chomp"} 1511 768 2279
383 2279 ▶ next if $self->{pre_filter} and not $self->{pre_filter}->($record);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pre_filter"} and not $self->{"pre_filter"}->($record) 1 2278 2279
Condition100%3/3
$self->{"pre_filter"} and not $self->{"pre_filter"}->($record)
!ll&&!rl&&r
227711
MC/DC100%2/2
$self->{"pre_filter"} and not $self->{"pre_filter"}->($record)
$self->{"pre_filter"} not $self->{"pre_filter"}->($record)
Truth table100%3/3
A and not(B)
A= $self->{"pre_filter"}B= not $self->{"pre_filter"}->($record)
ABresult
0X0
100
111
384 2278 ▶ $debug and print "# add <$record>\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# add <$record>\n" 32 2246 2278
385 2278 $self->{stats_raw} += length $record;
386 my $list = $record =~ /[+*?(\\\[{]/ # }]) restore equilibrium
387 2278 ▶ ? $self->{lex} ? $self->_lex($record) : $self->_fastlex($record)
Branch: 2 branches
BranchTrueFalseTotal
$record =~ /[+*?(\\\[{]/ ? : 897 1381 2278
$self->{'lex'} ? : 23 874 897
388 : [split //, $record]
389 ;
390 2278 ▶ next if $self->{filter} and not $self->{filter}->(@$list);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"filter"} and not $self->{"filter"}->(@$list) 1 2277 2278
Condition100%3/3
$self->{"filter"} and not $self->{"filter"}->(@$list)
!ll&&!rl&&r
227611
MC/DC100%2/2
$self->{"filter"} and not $self->{"filter"}->(@$list)
$self->{"filter"} not $self->{"filter"}->(@$list)
Truth table100%3/3
A and not(B)
A= $self->{"filter"}B= not $self->{"filter"}->(@$list)
ABresult
0X0
100
111
391 2277 $self->_insertr( $list );
392 }
393 1069 return $self;
394 }
395  
396 13 ▶sub add_file {
Subroutine add_file: called CC=10 SCAR=23.0
Pod documented
397 13 my $self = shift;
398 13 my $rs;
399 my @file;
400 13 ▶ if (ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 6 7 13
401 6 my $arg = shift;
402 $rs = $arg->{rs}
403 || $arg->{input_record_separator}
404 || $self->{input_record_separator}
405 6 ▶ || $/;
Condition100%9/9
$arg->{'rs'} || $arg->{'input_record_separator'} || $self->{'input_record_separator'} || $/
l!l&&r!l&&!r
411
$arg->{"rs"} or $arg->{"input_record_separator"} or $self->{"input_record_separator"}
l!l&&r!l&&!r
312
$arg->{"rs"} or $arg->{"input_record_separator"}
l!l&&r!l&&!r
213
MC/DC100%4/4
$arg->{'rs'} || $arg->{'input_record_separator'} || $self->{'input_record_separator'} || $/
$arg->{"rs"} $arg->{"input_record_separator"} $self->{"input_record_separator"} $/
Truth table100%5/5
A or B or C || D
A= $arg->{"rs"}B= $arg->{"input_record_separator"}C= $self->{"input_record_separator"}D= $/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
406 @file = ref($arg->{file}) eq 'ARRAY'
407 4 ? @{$arg->{file}}
408 6 ▶ : $arg->{file};
Branch: 1 branch
BranchTrueFalseTotal
ref $arg->{'file'} eq 'ARRAY' ? : 4 2 6
409 }
410 else {
411 7 ▶ $rs = $self->{input_record_separator} || $/;
Condition100%3/3
$self->{'input_record_separator'} || $/
l!l&&r!l&&!r
241
MC/DC100%2/2
$self->{'input_record_separator'} || $/
$self->{'input_record_separator'} $/
Truth table100%3/3
A || B
A= $self->{'input_record_separator'}B= $/
ABresult
1X1
011
000
412 7 @file = @_;
413 }
414 13 local $/ = $rs;
415 13 my $file;
416 13 for $file (@file) {
417 15 ▶ open my $fh, '<', $file or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or do { require Carp; Carp::croak("cannot open $file for input: $!") } 14 1 15
418 1 require Carp;
419 1 Carp::croak("cannot open $file for input: $!");
420 };
421 14 ▶ while (defined (my $rec = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $rec = readline $fh)) 55 14 69
422 55 $self->add($rec);
423 }
424 14 close $fh;
425 }
426 12 return $self;
427 }
428  
429 3398 ▶sub insert {
Subroutine insert: called CC=3 SCAR=11.0
Pod documented
430 3398 my $self = shift;
431 3398 ▶ return if $self->{filter} and not $self->{filter}->(@_);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"filter"} and not $self->{"filter"}->(@_) 1 3397 3398
Condition100%3/3
$self->{"filter"} and not $self->{"filter"}->(@_)
!ll&&!rl&&r
339611
MC/DC100%2/2
$self->{"filter"} and not $self->{"filter"}->(@_)
$self->{"filter"} not $self->{"filter"}->(@_)
Truth table100%3/3
A and not(B)
A= $self->{"filter"}B= not $self->{"filter"}->(@_)
ABresult
0X0
100
111
432 3397 $self->_insertr( [@_] );
433 3397 return $self;
434 }
435  
436 7084 ▶sub _insertr {
Subroutine _insertr: called CC=8 SCAR=20.8
437 7084 my $self = shift;
438 7084 ▶ my $dup = $self->{stats_dup} || 0;
Condition100%2/2
$self->{'stats_dup'} || 0
l!l
1186966
MC/DC100%1/1
$self->{'stats_dup'} || 0
$self->{'stats_dup'}
Truth table100%2/2
A
A= $self->{'stats_dup'}
Aresult
11
00
439 7084 $self->{path} = $self->_insert_path( $self->_path, $self->_debug(DEBUG_ADD), $_[0] );
440 7084 ▶ if( not defined $self->{stats_dup} or $dup == $self->{stats_dup} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->{'stats_dup'} or $dup == $self->{'stats_dup'}) { } 7073 11 7084
elsif ($self->{'dup_warn'}) { } 4 7 11
Condition100%3/3
not defined $self->{"stats_dup"} or $dup == $self->{"stats_dup"}
l!l&&r!l&&!r
695911411
MC/DC100%2/2
not defined $self->{"stats_dup"} or $dup == $self->{"stats_dup"}
not defined $self->{"stats_dup"} $dup == $self->{"stats_dup"}
Truth table100%3/3
not(A) or B
A= not defined $self->{"stats_dup"}B= $dup == $self->{"stats_dup"}
ABresult
1X1
011
000
441 7073 ++$self->{stats_add};
442 7073 ▶ $self->{stats_cooked} += defined($_) ? length($_) : 0 for @{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 30410 2 30412
443 }
444 elsif( $self->{dup_warn} ) {
445 4 ▶ if( ref $self->{dup_warn} eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'dup_warn'} eq 'CODE') { } 2 2 4
446 2 $self->{dup_warn}->($self, $_[0]);
447 }
448 else {
449 2 my $pattern = join( '', @{$_[0]} );
450 2 require Carp;
451 2 Carp::carp("duplicate pattern added: /$pattern/");
452 }
453 }
454 7084 $self->{str} = $self->{re} = undef;
455 }
456  
457 2 ▶sub lexstr {
Subroutine lexstr: called CC=1 SCAR=0.0
Pod documented
458 2 return shift->_lex(shift);
459 }
460  
461 3 ▶sub pre_filter {
Subroutine pre_filter: called CC=3 SCAR=11.0
Pod documented
462 3 my $self = shift;
463 3 my $pre_filter = shift;
464 3 ▶ if( defined $pre_filter and ref($pre_filter) ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pre_filter and ref $pre_filter ne "CODE") 1 2 3
Condition100%3/3
defined $pre_filter and ref $pre_filter ne "CODE"
!ll&&!rl&&r
111
MC/DC100%2/2
defined $pre_filter and ref $pre_filter ne "CODE"
defined $pre_filter ref $pre_filter ne "CODE"
Truth table100%3/3
A and B
A= defined $pre_filterB= ref $pre_filter ne "CODE"
ABresult
0X0
100
111
465 1 require Carp;
466 1 Carp::croak("pre_filter method not passed a coderef");
467 }
468 2 $self->{pre_filter} = $pre_filter;
469 2 return $self;
470 }
471  
472  
473 4 ▶sub filter {
Subroutine filter: called CC=3 SCAR=11.0
Pod documented
474 4 my $self = shift;
475 4 my $filter = shift;
476 4 ▶ if( defined $filter and ref($filter) ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter and ref $filter ne "CODE") 1 3 4
Condition100%3/3
defined $filter and ref $filter ne "CODE"
!ll&&!rl&&r
211
MC/DC100%2/2
defined $filter and ref $filter ne "CODE"
defined $filter ref $filter ne "CODE"
Truth table100%3/3
A and B
A= defined $filterB= ref $filter ne "CODE"
ABresult
0X0
100
111
477 1 require Carp;
478 1 Carp::croak("filter method not passed a coderef");
479 }
480 3 $self->{filter} = $filter;
481 3 return $self;
482 }
483  
484 800 ▶sub as_string {
Subroutine as_string: called CC=19 SCAR=29.4
Pod documented
485 800 my $self = shift;
486 800 ▶ if( not defined $self->{str} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"str"}) 798 2 800
487 798 ▶ if( $self->{track} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'track'}) { } 8 790 798
488 8 $self->{m} = undef;
489 8 $self->{mcount} = 0;
490 8 $self->{mlist} = [];
491 8 $self->{str} = _re_path_track($self, $self->_path, '', '');
492 }
493 else {
494 790 ▶ $self->_reduce unless ($self->{mutable} or not $self->{reduce});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"mutable"} or not $self->{"reduce"} 764 26 790
Condition100%3/3
$self->{"mutable"} or not $self->{"reduce"}
l!l&&r!l&&!r
818764
MC/DC100%2/2
$self->{"mutable"} or not $self->{"reduce"}
$self->{"mutable"} not $self->{"reduce"}
Truth table100%3/3
A or not(B)
A= $self->{"mutable"}B= not $self->{"reduce"}
ABresult
1X1
011
000
495 790 my $arg = {@_};
496 $arg->{indent} = $self->{indent}
497 790 ▶ if not exists $arg->{indent} and $self->{indent} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if not exists $arg->{"indent"} and $self->{"indent"} > 0 3 787 790
Condition100%3/3
not exists $arg->{"indent"} and $self->{"indent"} > 0
!ll&&!rl&&r
407473
MC/DC100%2/2
not exists $arg->{"indent"} and $self->{"indent"} > 0
not exists $arg->{"indent"} $self->{"indent"} > 0
Truth table100%3/3
not(A) and B
A= not exists $arg->{"indent"}B= $self->{"indent"} > 0
ABresult
0X0
100
111
498 790 ▶ if( exists $arg->{indent} and $arg->{indent} > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $arg->{'indent'} and $arg->{'indent'} > 0) { } 42 748 790
elsif ($self->{'lookahead'}) { } 35 713 748
Condition100%3/3
exists $arg->{"indent"} and $arg->{"indent"} > 0
!ll&&!rl&&r
747142
MC/DC100%2/2
exists $arg->{"indent"} and $arg->{"indent"} > 0
exists $arg->{"indent"} $arg->{"indent"} > 0
Truth table100%3/3
A and B
A= exists $arg->{"indent"}B= $arg->{"indent"} > 0
ABresult
0X0
100
111
499 42 $arg->{depth} = 0;
500 42 $self->{str} = _re_path_pretty($self, $self->_path, $arg);
501 }
502 elsif( $self->{lookahead} ) {
503 35 $self->{str} = _re_path_lookahead($self, $self->_path);
504 }
505 else {
506 713 $self->{str} = _re_path($self, $self->_path);
507 }
508 }
509 798 ▶ if (not length $self->{str}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $self->{'str'}) { } 9 789 798
510 # explicitly fail to match anything if no pattern was generated
511 9 $self->{str} = $Always_Fail;
512 }
513 else {
514 my $begin =
515 $self->{anchor_word_begin} ? '\\b'
516 : $self->{anchor_line_begin} ? '^'
517 789 ▶ : $self->{anchor_string_begin} ? '\A'
Branch: 3 branches
BranchTrueFalseTotal
$self->{'anchor_word_begin'} ? : 9 780 789
$self->{'anchor_line_begin'} ? : 3 777 780
$self->{'anchor_string_begin'} ? : 406 371 777
518 : ''
519 ;
520 my $end =
521 $self->{anchor_word_end} ? '\\b'
522 : $self->{anchor_line_end} ? '$'
523 : $self->{anchor_string_end} ? '\Z'
524 789 ▶ : $self->{anchor_string_end_absolute} ? '\z'
Branch: 4 branches
BranchTrueFalseTotal
$self->{'anchor_word_end'} ? : 3 786 789
$self->{'anchor_line_end'} ? : 3 783 786
$self->{'anchor_string_end'} ? : 403 380 783
$self->{'anchor_string_end_absolute'} ? : 6 374 380
525 : ''
526 ;
527 789 $self->{str} = "$begin$self->{str}$end";
528 }
529 798 ▶ $self->{path} = [] unless $self->{mutable};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"mutable"} 790 8 798
530 }
531 800 return $self->{str};
532 }
533  
534 122 ▶sub re {
Subroutine re: called CC=2 SCAR=6.9
Pod documented
535 122 my $self = shift;
536 122 ▶ $self->_build_re($self->as_string(@_)) unless defined $self->{re};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"re"} 121 1 122
537 122 return $self->{re};
538 }
539  
540 11 ▶use overload '""' => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=6.9
541 2130 my $self = shift;
542 2130 ▶ return $self->{re} if $self->{re};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"re"} 1641 489 2130
543 489 $self->_build_re($self->as_string());
544 489 return $self->{re};
545 11 };
546  
547 618 ▶sub _build_re {
Subroutine _build_re: called CC=4 SCAR=13.9
548 618 my $self = shift;
549 618 my $str = shift;
550 618 ▶ if( $self->{track} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'track'}) { } 8 610 618
551 11 ▶ use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
552 $self->{re} = length $self->{flags}
553 8 ▶ ? qr/(?$self->{flags}:$str)/
Branch: 1 branch
BranchTrueFalseTotal
length $self->{'flags'} ? : 1 7 8
554 : qr/$str/
555 ;
556 }
557 else {
558 # how could I not repeat myself?
559 $self->{re} = length $self->{flags}
560 610 ▶ ? qr/(?$self->{flags}:$str)/
Branch: 1 branch
BranchTrueFalseTotal
length $self->{'flags'} ? : 21 589 610
561 : qr/$str/
562 ;
563 }
564 }
565  
566 29 ▶sub match {
Subroutine match: called CC=9 SCAR=22.0
Pod documented
567 29 my $self = shift;
568 29 my $target = shift;
569 29 ▶ $self->_build_re($self->as_string(@_)) unless defined $self->{re};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"re"} 8 21 29
570 29 $self->{m} = undef;
571 29 $self->{mvar} = [];
572 29 ▶ if( not $target =~ /$self->{re}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($target =~ /$self->{"re"}/) 8 21 29
573 8 $self->{mbegin} = [];
574 8 $self->{mend} = [];
575 8 return undef;
576 }
577 21 ▶ $self->{m} = $^R if $] >= 5.009005;
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.009005" 21 0 21
578 21 $self->{mbegin} = _path_copy([@-]);
579 21 $self->{mend} = _path_copy([@+]);
580 21 my $n = 0;
581 21 ▶ for( my $n = 0; $n < @-; ++$n ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @-) 43 21 64
582 43 ▶ push @{$self->{mvar}}, substr($target, $-[$n], $+[$n] - $-[$n])
Branch: 1 branch
BranchTrueFalseTotal
if defined $-[$n] and defined $+[$n] 33 10 43
Condition66%2/3
defined $-[$n] and defined $+[$n]
!ll&&!rl&&r
10033
MC/DC50%1/2
defined $-[$n] and defined $+[$n]
defined $-[$n] defined $+[$n]
Truth table66%2/3
A and B
A= defined $-[$n]B= defined $+[$n]
ABresult
0X0
100
111
583 if defined $-[$n] and defined $+[$n];
584 }
585 21 ▶ if( $self->{track} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'track'}) { } 20 1 21
586 20 ▶ return defined $self->{m} ? $self->{mlist}[$self->{m}] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'m'} ? : 20 0 20
587 }
588 else {
589 1 return 1;
590 }
591 }
592  
593 4 ▶sub source {
Subroutine source: called CC=4 SCAR=13.9
Pod documented
594 4 my $self = shift;
595 4 ▶ return unless $self->{track};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"track"} 1 3 4
596 3 ▶ defined($_[0]) and return $self->{mlist}[$_[0]];
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and return $self->{"mlist"}[$_[0]] 1 2 3
597 2 ▶ return unless defined $self->{m};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"m"} 1 1 2
598 1 return $self->{mlist}[$self->{m}];
599 }
600  
601 3 ▶sub mbegin {
Subroutine mbegin: called CC=2 SCAR=6.9
Pod documented
602 3 my $self = shift;
603 3 ▶ return exists $self->{mbegin} ? $self->{mbegin} : [];
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'mbegin'} ? : 2 1 3
604 }
605  
606 3 ▶sub mend {
Subroutine mend: called CC=2 SCAR=6.9
Pod documented
607 3 my $self = shift;
608 3 ▶ return exists $self->{mend} ? $self->{mend} : [];
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'mend'} ? : 2 1 3
609 }
610  
611 19 ▶sub mvar {
Subroutine mvar: called CC=3 SCAR=11.0
Pod documented
612 19 my $self = shift;
613 19 ▶ return undef unless exists $self->{mvar};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"mvar"} 1 18 19
614 18 ▶ return defined($_[0]) ? $self->{mvar}[$_[0]] : $self->{mvar};
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 17 1 18
615 }
616  
617 5 ▶sub capture {
Subroutine capture: called CC=2 SCAR=6.9
Pod documented
618 5 my $self = shift;
619 5 ▶ if( $self->{mvar} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mvar"}) 4 1 5
620 4 my @capture = @{$self->{mvar}};
621 4 shift @capture;
622 4 return @capture;
623 }
624 1 return ();
625 }
626  
627 9 ▶sub matched {
Subroutine matched: called CC=2 SCAR=6.9
Pod documented
628 9 my $self = shift;
629 9 ▶ return defined $self->{m} ? $self->{mlist}[$self->{m}] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'m'} ? : 8 1 9
630 }
631  
632 2 ▶sub stats_add {
Subroutine stats_add: called CC=2 SCAR=6.9
Pod documented
633 2 my $self = shift;
634 2 ▶ return $self->{stats_add} || 0;
Condition100%2/2
$self->{'stats_add'} || 0
l!l
11
MC/DC100%1/1
$self->{'stats_add'} || 0
$self->{'stats_add'}
Truth table100%2/2
A
A= $self->{'stats_add'}
Aresult
11
00
635 }
636  
637 2 ▶sub stats_dup {
Subroutine stats_dup: called CC=2 SCAR=6.9
Pod documented
638 2 my $self = shift;
639 2 ▶ return $self->{stats_dup} || 0;
Condition100%2/2
$self->{'stats_dup'} || 0
l!l
11
MC/DC100%1/1
$self->{'stats_dup'} || 0
$self->{'stats_dup'}
Truth table100%2/2
A
A= $self->{'stats_dup'}
Aresult
11
00
640 }
641  
642 2 ▶sub stats_raw {
Subroutine stats_raw: called CC=2 SCAR=6.9
Pod documented
643 2 my $self = shift;
644 2 ▶ return $self->{stats_raw} || 0;
Condition100%2/2
$self->{'stats_raw'} || 0
l!l
11
MC/DC100%1/1
$self->{'stats_raw'} || 0
$self->{'stats_raw'}
Truth table100%2/2
A
A= $self->{'stats_raw'}
Aresult
11
00
645 }
646  
647 2 ▶sub stats_cooked {
Subroutine stats_cooked: called CC=2 SCAR=6.9
Pod documented
648 2 my $self = shift;
649 2 ▶ return $self->{stats_cooked} || 0;
Condition100%2/2
$self->{'stats_cooked'} || 0
l!l
11
MC/DC100%1/1
$self->{'stats_cooked'} || 0
$self->{'stats_cooked'}
Truth table100%2/2
A
A= $self->{'stats_cooked'}
Aresult
11
00
650 }
651  
652 6 ▶sub stats_length {
Subroutine stats_length: called CC=3 SCAR=11.0
Pod documented
653 6 my $self = shift;
654 6 ▶ return (defined $self->{str} and $self->{str} ne $Always_Fail) ? length $self->{str} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'str'} && $self->{'str'} ne $Regexp::Assemble::Always_Fail ? : 2 4 6
Condition100%3/3
defined $self->{"str"} and $self->{"str"} ne $Regexp::Assemble::Always_Fail
!ll&&!rl&&r
312
MC/DC100%2/2
defined $self->{"str"} and $self->{"str"} ne $Regexp::Assemble::Always_Fail
defined $self->{"str"} $self->{"str"} ne $Regexp::Assemble::Always_Fail
Truth table100%3/3
A and B
A= defined $self->{"str"}B= $self->{"str"} ne $Regexp::Assemble::Always_Fail
ABresult
0X0
100
111
655 }
656  
657 5 ▶sub dup_warn {
Subroutine dup_warn: called CC=2 SCAR=6.9
Pod documented
658 5 my $self = shift;
659 5 ▶ $self->{dup_warn} = defined($_[0]) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 4 1 5
660 5 return $self;
661 }
662  
663 5 ▶sub anchor_word_begin {
Subroutine anchor_word_begin: called CC=2 SCAR=6.9
Pod documented
664 5 my $self = shift;
665 5 ▶ $self->{anchor_word_begin} = defined($_[0]) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 2 3 5
666 5 return $self;
667 }
668  
669 4 ▶sub anchor_word_end {
Subroutine anchor_word_end: called CC=2 SCAR=6.9
Pod documented
670 4 my $self = shift;
671 4 ▶ $self->{anchor_word_end} = defined($_[0]) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 2 2 4
672 4 return $self;
673 }
674  
675 2 ▶sub anchor_word {
Subroutine anchor_word: called CC=1 SCAR=0.0
Pod documented
676 2 my $self = shift;
677 2 my $state = shift;
678 2 $self->anchor_word_begin($state)->anchor_word_end($state);
679 2 return $self;
680 }
681  
682 4 ▶sub anchor_line_begin {
Subroutine anchor_line_begin: called CC=2 SCAR=6.9
Pod documented
683 4 my $self = shift;
684 4 ▶ $self->{anchor_line_begin} = defined($_[0]) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 1 3 4
685 4 return $self;
686 }
687  
688 2 ▶sub anchor_line_end {
Subroutine anchor_line_end: called CC=2 SCAR=6.9
Pod documented
689 2 my $self = shift;
690 2 ▶ $self->{anchor_line_end} = defined($_[0]) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 1 1 2
691 2 return $self;
692 }
693  
694 2 ▶sub anchor_line {
Subroutine anchor_line: called CC=1 SCAR=0.0
Pod documented
695 2 my $self = shift;
696 2 my $state = shift;
697 2 $self->anchor_line_begin($state)->anchor_line_end($state);
698 2 return $self;
699 }
700  
701 277 ▶sub anchor_string_begin {
Subroutine anchor_string_begin: called CC=2 SCAR=6.9
Pod documented
702 277 my $self = shift;
703 277 ▶ $self->{anchor_string_begin} = defined($_[0]) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 276 1 277
704 277 return $self;
705 }
706  
707 276 ▶sub anchor_string_end {
Subroutine anchor_string_end: called CC=2 SCAR=6.9
Pod documented
708 276 my $self = shift;
709 276 ▶ $self->{anchor_string_end} = defined($_[0]) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 275 1 276
710 276 return $self;
711 }
712  
713 3 ▶sub anchor_string_end_absolute {
Subroutine anchor_string_end_absolute: called CC=2 SCAR=6.9
Pod documented
714 3 my $self = shift;
715 3 ▶ $self->{anchor_string_end_absolute} = defined($_[0]) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 2 1 3
716 3 return $self;
717 }
718  
719 274 ▶sub anchor_string {
Subroutine anchor_string: called CC=2 SCAR=6.9
Pod documented
720 274 my $self = shift;
721 274 ▶ my $state = defined($_[0]) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 1 273 274
722 274 $self->anchor_string_begin($state)->anchor_string_end($state);
723 274 return $self;
724 }
725  
726 2 ▶sub anchor_string_absolute {
Subroutine anchor_string_absolute: called CC=2 SCAR=6.9
Pod documented
727 2 my $self = shift;
728 2 ▶ my $state = defined($_[0]) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 1 1 2
729 2 $self->anchor_string_begin($state)->anchor_string_end_absolute($state);
730 2 return $self;
731 }
732  
733 605 ▶sub debug {
Subroutine debug: called CC=3 SCAR=11.0
Pod documented
734 605 my $self = shift;
735 605 ▶ $self->{debug} = defined($_[0]) ? $_[0] : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 604 1 605
736 605 ▶ if ($self->_debug(DEBUG_TIME)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_debug(8)) 4 601 605
737 # hmm, debugging time was switched on after instantiation
738 4 $self->_init_time_func;
739 4 $self->{_begin_time} = $self->{_time_func}->();
740 }
741 605 return $self;
742 }
743  
744 9 ▶sub dump {
Subroutine dump: called CC=1 SCAR=0.0
Pod documented
745 9 return _dump($_[0]->_path);
746 }
747  
748 22 ▶sub chomp {
Subroutine chomp: called CC=2 SCAR=6.9
Pod documented
749 22 my $self = shift;
750 22 ▶ $self->{chomp} = defined($_[0]) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 18 4 22
751 22 return $self;
752 }
753  
754 5 ▶sub fold_meta_pairs {
Subroutine fold_meta_pairs: called CC=2 SCAR=6.9
Pod documented
755 5 my $self = shift;
756 5 ▶ $self->{fold_meta_pairs} = defined($_[0]) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 4 1 5
757 5 return $self;
758 }
759  
760 4 ▶sub indent {
Subroutine indent: called CC=2 SCAR=6.9
Pod documented
761 4 my $self = shift;
762 4 ▶ $self->{indent} = defined($_[0]) ? $_[0] : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 3 1 4
763 4 return $self;
764 }
765  
766 22 ▶sub lookahead {
Subroutine lookahead: called CC=2 SCAR=6.9
Pod documented
767 22 my $self = shift;
768 22 ▶ $self->{lookahead} = defined($_[0]) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 21 1 22
769 22 return $self;
770 }
771  
772 24 ▶sub flags {
Subroutine flags: called CC=2 SCAR=6.9
Pod documented
773 24 my $self = shift;
774 24 ▶ $self->{flags} = defined($_[0]) ? $_[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 22 2 24
775 24 return $self;
776 }
777  
778 4 ▶sub modifiers {
Subroutine modifiers: called CC=1 SCAR=0.0
Pod documented
779 4 my $self = shift;
780 4 return $self->flags(@_);
781 }
782  
783 5 ▶sub track {
Subroutine track: called CC=2 SCAR=6.9
Pod documented
784 5 my $self = shift;
785 5 ▶ $self->{track} = defined($_[0]) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 4 1 5
786 5 return $self;
787 }
788  
789 2 ▶sub unroll_plus {
Subroutine unroll_plus: called CC=2 SCAR=6.9
Pod documented
790 2 my $self = shift;
791 2 ▶ $self->{unroll_plus} = defined($_[0]) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 1 1 2
792 2 return $self;
793 }
794  
795 1 ▶sub lex {
Subroutine lex: called CC=1 SCAR=0.0
Pod documented
796 1 my $self = shift;
797 1 $self->{lex} = qr($_[0]);
798 1 return $self;
799 }
800  
801 19 ▶sub reduce {
Subroutine reduce: called CC=2 SCAR=6.9
Pod documented
802 19 my $self = shift;
803 19 ▶ $self->{reduce} = defined($_[0]) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 18 1 19
804 19 return $self;
805 }
806  
807 5 ▶sub mutable {
Subroutine mutable: called CC=2 SCAR=6.9
Pod documented
808 5 my $self = shift;
809 5 ▶ $self->{mutable} = defined($_[0]) ? $_[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 4 1 5
810 5 return $self;
811 }
812  
813 19 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
814 # reinitialise the internal state of the object
815 19 my $self = shift;
816 19 $self->{path} = [];
817 19 $self->{re} = undef;
818 19 $self->{str} = undef;
819 19 return $self;
820 }
821  
822 4 ▶sub Default_Lexer {
Subroutine Default_Lexer: called CC=4 SCAR=13.9
Pod documented
823 4 ▶ if( $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) 3 1 4
824 3 ▶ if( my $refname = ref($_[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $refname = ref $_[0]) 1 2 3
825 1 require Carp;
826 1 Carp::croak("Cannot pass a $refname to Default_Lexer");
827 }
828 2 $Current_Lexer = $_[0];
829 }
830 3 ▶ return defined $Current_Lexer ? $Current_Lexer : $Default_Lexer;
Branch: 1 branch
BranchTrueFalseTotal
defined $Regexp::Assemble::Current_Lexer ? : 2 1 3
831 }
832  
833 # --- no user serviceable parts below ---
834  
835 # -- debug helpers
836  
837 12316 ▶sub _debug {
Subroutine _debug: called CC=2 SCAR=6.9
838 12316 my $self = shift;
839 12316 ▶ return $self->{debug} & shift() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'debug'} & shift() ? : 250 12066 12316
840 }
841  
842 # -- helpers
843  
844 10027 ▶sub _path {
Subroutine _path: called CC=1 SCAR=0.0
845 # access the path
846 10027 return $_[0]->{path};
847 }
848  
849 # -- the heart of the matter
850  
851 11 $have_Storable = do {
852 11 eval {
853 11 require Storable;
854 11 import Storable 'dclone';
855 };
856 11 ▶ $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 11 11
857 };
858  
859 55 ▶sub _path_clone {
Subroutine _path_clone: called CC=2 SCAR=6.9
860 55 ▶ $have_Storable ? dclone($_[0]) : _path_copy($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
$Regexp::Assemble::have_Storable ? : 50 5 55
861 }
862  
863 80 ▶sub _path_copy {
Subroutine _path_copy: called CC=4 SCAR=13.9
864 80 my $path = shift;
865 80 my $new = [];
866 80 ▶ for( my $p = 0; $p < @$path; ++$p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < @$path) 201 80 281
867 201 ▶ if( ref($path->[$p]) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $path->[$p] eq 'HASH') { } 9 192 201
elsif (ref $path->[$p] eq 'ARRAY') { } 3 189 192
868 9 push @$new, _node_copy($path->[$p]);
869 }
870 elsif( ref($path->[$p]) eq 'ARRAY' ) {
871 3 push @$new, _path_copy($path->[$p]);
872 }
873 else {
874 189 push @$new, $path->[$p];
875 }
876 }
877 80 return $new;
878 }
879  
880 11 ▶sub _node_copy {
Subroutine _node_copy: called CC=3 SCAR=11.0
881 11 my $node = shift;
882 11 my $new = {};
883 11 ▶ while( my( $k, $v ) = each %$node ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$node) 27 11 38
884 27 ▶ $new->{$k} = defined($v)
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 26 1 27
885 ? _path_copy($v)
886 : undef
887 ;
888 }
889 11 return $new;
890 }
891  
892 7158 ▶sub _insert_path {
Subroutine _insert_path: called CC=38 SCAR=36.4
893 7158 my $self = shift;
894 7158 my $list = shift;
895 7158 my $debug = shift;
896 7158 my @in = @{shift()}; # create a new copy
897 7158 ▶ if( @$list == 0 ) { # special case the first time
Branch: 1 branch
BranchTrueFalseTotal
if (@$list == 0) 2033 5125 7158
898 2033 ▶ if( @in == 0 or (@in == 1 and (not defined $in[0] or $in[0] eq ''))) {
Branch: 1 branch
BranchTrueFalseTotal
if (@in == 0 or @in == 1 and !defined($in[0]) || $in[0] eq '') { } 33 2000 2033
Condition100%9/9
@in == 0 or @in == 1 and !defined($in[0]) || $in[0] eq ""
l!l&&r!l&&!r
3302000
@in == 1 and !defined($in[0]) || $in[0] eq ""
!ll&&!rl&&r
134465630
not defined $in[0] or $in[0] eq ""
l!l&&r!l&&!r
228656
MC/DC100%4/4
@in == 0 or @in == 1 and !defined($in[0]) || $in[0] eq ""
@in == 0 @in == 1 not defined $in[0] $in[0] eq ""
Truth table100%5/5
A or B and not(C) or D
A= @in == 0B= @in == 1C= not defined $in[0]D= $in[0] eq ""
ABCDresult
1XXX1
011X1
01011
00XX0
01000
899 33 return [{'' => undef}];
900 }
901 else {
902 2000 return \@in;
903 }
904 }
905 5125 ▶ $debug and print "# _insert_path @{[_dump(\@in)]} into @{[_dump($list)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# _insert_path @{[_dump(\@in)];} into @{[_dump($list)];}\n" 151 4974 5125
906 5125 my $path = $list;
907 5125 my $offset = 0;
908 5125 my $token;
909 5125 ▶ if( not @in ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@in) 2 5123 5125
910 2 ▶ if( ref($list->[0]) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $list->[0] ne 'HASH') { } 1 1 2
911 1 return [ { '' => undef, $list->[0] => $list } ];
912 }
913 else {
914 1 $list->[0]{''} = undef;
915 1 return $list;
916 }
917 }
918 5123 ▶ while( defined( $token = shift @in )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($token = shift @in)) 15116 0 15116
919 17747 ▶ if( ref($token) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $token eq "HASH") 282 17465 17747
920 282 ▶ $debug and print "# p0=", _dump($path), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# p0=", _dump($path), "\n" 26 256 282
921 282 $path = $self->_insert_node( $path, $offset, $token, $debug, @in );
922 282 ▶ $debug and print "# p1=", _dump($path), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# p1=", _dump($path), "\n" 26 256 282
923 282 last;
924 }
925 17465 ▶ if( ref($path->[$offset]) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path->[$offset] eq "HASH") 3714 13751 17465
926 3714 ▶ $debug and print "# at (off=$offset len=@{[scalar @$path]}) ", _dump($path->[$offset]), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# at (off=$offset len=@{[scalar @$path];}) ", _dump($path->[$offset]), "\n" 54 3660 3714
927 3714 my $node = $path->[$offset];
928 3714 ▶ if( exists( $node->{$token} )) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{$token}) { } 2632 1082 3714
929 2632 ▶ if ($offset < $#$path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $path->$#*) { } 1 2631 2632
930 my $new = {
931 $token => [$token, @in],
932 1 _re_path($self, [$node]) => [@{$path}[$offset..$#$path]],
933 };
934 1 splice @$path, $offset, @$path-$offset, $new;
935 1 last;
936 }
937 else {
938 2631 ▶ $debug and print "# descend key=$token @{[_dump($node->{$token})]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# descend key=$token @{[_dump($node->{$token})];}\n" 31 2600 2631
939 2631 $path = $node->{$token};
940 2631 $offset = 0;
941 2631 redo;
942 }
943 }
944 else {
945 1082 ▶ $debug and print "# add path ($token:@{[_dump(\@in)]}) into @{[_dump($path)]} at off=$offset to end=@{[scalar $#$path]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# add path (${token}:@{[_dump(\@in)];}) into @{[_dump($path)];} at off=$offset to end=@{[scalar $path->$#*];}\n" 23 1059 1082
946 1082 ▶ if( $offset == $#$path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset == $path->$#*) { } 1075 7 1082
947 1075 $node->{$token} = [ $token, @in ];
948 }
949 else {
950 my $new = {
951 _node_key($token) => [ $token, @in ],
952 7 _node_key($node) => [@{$path}[$offset..$#{$path}]],
953 };
954 7 splice( @$path, $offset, @$path - $offset, $new );
955 7 ▶ $debug and print "# fused node=@{[_dump($new)]} path=@{[_dump($path)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# fused node=@{[_dump($new)];} path=@{[_dump($path)];}\n" 1 6 7
956 }
957 1082 last;
958 }
959 }
960  
961 13751 ▶ if( $debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 306 13445 13751
962 306 my $msg = '';
963 306 my $n;
964 306 ▶ for( $n = 0; $n < @$path; ++$n ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @$path) 1093 306 1399
965 1093 ▶ $msg .= ' ' if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 787 306 1093
966 my $atom = ref($path->[$n]) eq 'HASH'
967 1093 ▶ ? '{'.join( ' ', keys(%{$path->[$n]})).'}'
Branch: 1 branch
BranchTrueFalseTotal
ref $path->[$n] eq 'HASH' ? : 81 1012 1093
968 : $path->[$n]
969 ;
970 1093 ▶ $msg .= $n == $offset ? "<$atom>" : $atom;
Branch: 1 branch
BranchTrueFalseTotal
$n == $offset ? : 285 808 1093
971 }
972 306 print "# at path ($msg)\n";
973 }
974  
975 13751 ▶ if( $offset >= @$path ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($offset >= @$path) { } 732 13019 13751
elsif ($token ne $path->[$offset]) { } 2103 10916 13019
elsif (not @in) { } 923 9993 10916
976 732 push @$path, { $token => [ $token, @in ], '' => undef };
977 732 ▶ $debug and print "# added remaining @{[_dump($path)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# added remaining @{[_dump($path)];}\n" 21 711 732
978 732 last;
979 }
980 elsif( $token ne $path->[$offset] ) {
981 2103 ▶ $debug and print "# token $token not present\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# token $token not present\n" 79 2024 2103
982 splice @$path, $offset, @$path-$offset, {
983 length $token
984 ? ( _node_key($token) => [$token, @in])
985 : ( '' => undef )
986 ,
987 2103 ▶ $path->[$offset] => [@{$path}[$offset..$#{$path}]],
Branch: 1 branch
BranchTrueFalseTotal
length $token ? : 1996 107 2103
988 };
989 2103 ▶ $debug and print "# path=@{[_dump($path)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# path=@{[_dump($path)];}\n" 79 2024 2103
990 2103 last;
991 }
992 elsif( not @in ) {
993 923 ▶ $debug and print "# last token to add\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# last token to add\n" 2 921 923
994 923 ▶ if( defined( $path->[$offset+1] )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$path[$offset + 1]) { } 912 11 923
995 912 ++$offset;
996 912 ▶ if( ref($path->[$offset]) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path->[$offset] eq 'HASH') { } 118 794 912
997 118 ▶ $debug and print "# add sentinel to node\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# add sentinel to node\n" 1 117 118
998 118 $path->[$offset]{''} = undef;
999 }
1000 else {
1001 794 ▶ $debug and print "# convert <$path->[$offset]> to node for sentinel\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# convert <$path->[$offset]> to node for sentinel\n" 1 793 794
1002 splice @$path, $offset, @$path-$offset, {
1003 '' => undef,
1004 794 $path->[$offset] => [ @{$path}[$offset..$#{$path}] ],
1005 };
1006 }
1007 }
1008 else {
1009 # already seen this pattern
1010 11 ++$self->{stats_dup};
1011 }
1012 923 last;
1013 }
1014 # if we get here then @_ still contains a token
1015 9993 ++$offset;
1016 }
1017 5123 $list;
1018 }
1019  
1020 282 ▶sub _insert_node {
Subroutine _insert_node: called CC=32 SCAR=34.7
1021 282 my $self = shift;
1022 282 my $path = shift;
1023 282 my $offset = shift;
1024 282 my $token = shift;
1025 282 my $debug = shift;
1026 282 my $path_end = [@{$path}[$offset..$#{$path}]];
1027 # NB: $path->[$offset] and $[path_end->[0] are equivalent
1028 282 my $token_key = _re_path($self, [$token]);
1029 282 ▶ $debug and print "# insert node(@{[_dump($token)]}:@{[_dump(\@_)]}) (key=$token_key)",
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# insert node(@{[_dump($token)];}:@{[_dump(\@_)];}) (key=$token_key)", " at path=@{[_dump($path_end)];}\n" 26 256 282
1030 26 " at path=@{[_dump($path_end)]}\n";
1031 282 ▶ if( ref($path_end->[0]) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path_end->[0] eq 'HASH') { } 195 87 282
1032 195 ▶ if( exists($path_end->[0]{$token_key}) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $path_end->[0]{$token_key}) { } 25 170 195
elsif (not _node_eq($path_end->[0], $token)) { } 70 100 170
1033 25 ▶ if( @$path_end > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$path_end > 1) { } 2 23 25
1034 2 my $path_key = _re_path($self, [$path_end->[0]]);
1035 2 my $new = {
1036 $path_key => [ @$path_end ],
1037 $token_key => [ $token, @_ ],
1038 };
1039 2 ▶ $debug and print "# +bifurcate new=@{[_dump($new)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# +bifurcate new=@{[_dump($new)];}\n" 1 1 2
1040 2 splice( @$path, $offset, @$path_end, $new );
1041 }
1042 else {
1043 23 my $old_path = $path_end->[0]{$token_key};
1044 23 my $new_path = [];
1045 23 ▶ while( @$old_path and _node_eq( $old_path->[0], $token )) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$old_path and _node_eq($old_path->[0], $token)) 30 23 53
Condition100%3/3
@$old_path and _node_eq($old_path->[0], $token)
!ll&&!rl&&r
71630
MC/DC100%2/2
@$old_path and _node_eq($old_path->[0], $token)
@$old_path _node_eq($old_path->[0], $token)
Truth table100%3/3
A and B
A= @$old_pathB= _node_eq($old_path->[0], $token)
ABresult
0X0
100
111
1046 30 ▶ $debug and print "# identical nodes in sub_path ",
Branch: 2 branches
BranchTrueFalseTotal
$debug and print "# identical nodes in sub_path ", ref $token ? _dump($token) : $token, "\n" 7 23 30
ref $token ? : 6 1 7
1047 ref($token) ? _dump($token) : $token, "\n";
1048 30 push @$new_path, shift(@$old_path);
1049 30 $token = shift @_;
1050 }
1051 23 ▶ if( @$new_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$new_path) 23 0 23
1052 23 my $new;
1053 23 my $token_key = $token;
1054 23 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 6 17 23
1055 6 $new = {
1056 _re_path($self, $old_path) => $old_path,
1057 $token_key => [$token, @_],
1058 };
1059 6 ▶ $debug and print "# insert_node(bifurc) n=@{[_dump([$new])]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# insert_node(bifurc) n=@{[_dump([$new])];}\n" 1 5 6
1060 }
1061 else {
1062 17 ▶ $debug and print "# insert $token into old path @{[_dump($old_path)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# insert $token into old path @{[_dump($old_path)];}\n" 5 12 17
1063 17 ▶ if( @$old_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$old_path) { } 11 6 17
1064 11 $new = ($self->_insert_path( $old_path, $debug, [$token] ))->[0];
1065 }
1066 else {
1067 6 $new = { '' => undef, $token => [$token] };
1068 }
1069 }
1070 23 push @$new_path, $new;
1071 }
1072 23 $path_end->[0]{$token_key} = $new_path;
1073 23 ▶ $debug and print "# +_insert_node result=@{[_dump($path_end)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# +_insert_node result=@{[_dump($path_end)];}\n" 6 17 23
1074 23 splice( @$path, $offset, @$path_end, @$path_end );
1075 }
1076 }
1077 elsif( not _node_eq( $path_end->[0], $token )) {
1078 70 ▶ if( @$path_end > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$path_end > 1) { } 11 59 70
1079 11 my $path_key = _re_path($self, [$path_end->[0]]);
1080 11 my $new = {
1081 $path_key => [ @$path_end ],
1082 $token_key => [ $token, @_ ],
1083 };
1084 11 ▶ $debug and print "# path->node1 at $path_key/$token_key @{[_dump($new)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# path->node1 at $path_key/$token_key @{[_dump($new)];}\n" 1 10 11
1085 11 splice( @$path, $offset, @$path_end, $new );
1086 }
1087 else {
1088 59 ▶ $debug and print "# next in path is node, trivial insert at $token_key\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# next in path is node, trivial insert at $token_key\n" 2 57 59
1089 59 $path_end->[0]{$token_key} = [$token, @_];
1090 59 splice( @$path, $offset, @$path_end, @$path_end );
1091 }
1092 }
1093 else {
1094 100 ▶ while( @$path_end and _node_eq( $path_end->[0], $token )) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$path_end and _node_eq($path_end->[0], $token)) 131 100 231
Condition100%3/3
@$path_end and _node_eq($path_end->[0], $token)
!ll&&!rl&&r
4357131
MC/DC100%2/2
@$path_end and _node_eq($path_end->[0], $token)
@$path_end _node_eq($path_end->[0], $token)
Truth table100%3/3
A and B
A= @$path_endB= _node_eq($path_end->[0], $token)
ABresult
0X0
100
111
1095 131 ▶ $debug and print "# identical nodes @{[_dump([$token])]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# identical nodes @{[_dump([$token])];}\n" 9 122 131
1096 131 shift @$path_end;
1097 131 $token = shift @_;
1098 131 ++$offset;
1099 }
1100 100 ▶ if( @$path_end ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$path_end) { } 57 43 100
1101 57 ▶ $debug and print "# insert at $offset $token:@{[_dump(\@_)]} into @{[_dump($path_end)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# insert at $offset ${token}:@{[_dump(\@_)];} into @{[_dump($path_end)];}\n" 4 53 57
1102 57 $path_end = $self->_insert_path( $path_end, $debug, [$token, @_] );
1103 57 ▶ $debug and print "# got off=$offset s=@{[scalar @_]} path_add=@{[_dump($path_end)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# got off=$offset s=@{[scalar @_];} path_add=@{[_dump($path_end)];}\n" 4 53 57
1104 57 splice( @$path, $offset, @$path - $offset, @$path_end );
1105 57 ▶ $debug and print "# got final=@{[_dump($path)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# got final=@{[_dump($path)];}\n" 4 53 57
1106 }
1107 else {
1108 43 $token_key = _node_key($token);
1109 43 my $new = {
1110 '' => undef,
1111 $token_key => [ $token, @_ ],
1112 };
1113 43 ▶ $debug and print "# convert opt @{[_dump($new)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# convert opt @{[_dump($new)];}\n" 3 40 43
1114 43 push @$path, $new;
1115 }
1116 }
1117 }
1118 else {
1119 87 ▶ if( @$path_end ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$path_end) { } 74 13 87
1120 74 my $new = {
1121 $path_end->[0] => [ @$path_end ],
1122 $token_key => [ $token, @_ ],
1123 };
1124 74 ▶ $debug and print "# atom->node @{[_dump($new)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# atom->node @{[_dump($new)];}\n" 5 69 74
1125 74 splice( @$path, $offset, @$path_end, $new );
1126 74 ▶ $debug and print "# out=@{[_dump($path)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# out=@{[_dump($path)];}\n" 5 69 74
1127 }
1128 else {
1129 13 ▶ $debug and print "# add opt @{[_dump([$token,@_])]} via $token_key\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# add opt @{[_dump([$token, @_])];} via $token_key\n" 4 9 13
1130 13 push @$path, {
1131 '' => undef,
1132 $token_key => [ $token, @_ ],
1133 };
1134 }
1135 }
1136 282 $path;
1137 }
1138  
1139 810 ▶sub _reduce {
Subroutine _reduce: called CC=8 SCAR=20.8
1140 810 my $self = shift;
1141 810 my $context = { debug => $self->_debug(DEBUG_TAIL), depth => 0 };
1142  
1143 810 ▶ if ($self->_debug(DEBUG_TIME)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_debug(8)) 4 806 810
1144 4 $self->_init_time_func;
1145 4 my $now = $self->{_time_func}->();
1146 4 ▶ if (exists $self->{_begin_time}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_begin_time'}) { } 3 1 4
1147 3 printf "# load=%0.6f\n", $now - $self->{_begin_time};
1148 }
1149 else {
1150 1 printf "# load-epoch=%0.6f\n", $now;
1151 }
1152 4 $self->{_begin_time} = $self->{_time_func}->();
1153 }
1154  
1155 810 my ($head, $tail) = _reduce_path( $self->_path, $context );
1156 810 ▶ $context->{debug} and print "# final head=", _dump($head), ' tail=', _dump($tail), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$context->{"debug"} and print "# final head=", _dump($head), " tail=", _dump($tail), "\n" 20 790 810
1157 810 ▶ if( !@$head ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$head) { } 651 159 810
1158 651 $self->{path} = $tail;
1159 }
1160 else {
1161 $self->{path} = [
1162 159 @{_unrev_path( $tail, $context )},
1163 159 @{_unrev_path( $head, $context )},
1164 ];
1165 }
1166  
1167 810 ▶ if ($self->_debug(DEBUG_TIME)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_debug(8)) 4 806 810
1168 4 my $now = $self->{_time_func}->();
1169 4 ▶ if (exists $self->{_begin_time}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_begin_time'}) { } 4 0 4
1170 4 printf "# reduce=%0.6f\n", $now - $self->{_begin_time};
1171 }
1172 else {
1173 0 printf "# reduce-epoch=%0.6f\n", $now;
1174 }
1175 4 $self->{_begin_time} = $self->{_time_func}->();
1176 }
1177  
1178 810 ▶ $context->{debug} and print "# final path=", _dump($self->{path}), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$context->{"debug"} and print "# final path=", _dump($self->{"path"}), "\n" 20 790 810
1179 810 return $self;
1180 }
1181  
1182 1710 ▶sub _remove_optional {
Subroutine _remove_optional: called CC=2 SCAR=6.9
1183 1710 ▶ if( exists $_[0]->{''} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[0]{""}) 350 1360 1710
1184 350 delete $_[0]->{''};
1185 350 return 1;
1186 }
1187 1360 return 0;
1188 }
1189  
1190 810 ▶sub _reduce_path {
Subroutine _reduce_path: called CC=22 SCAR=30.9
1191 810 my ($path, $ctx) = @_;
1192 810 my $indent = ' ' x $ctx->{depth};
1193 810 my $debug = $ctx->{debug};
1194 810 ▶ $debug and print "#$indent _reduce_path $ctx->{depth} ", _dump($path), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print qq[#$indent _reduce_path $ctx->{"depth"} ], _dump($path), "\n" 20 790 810
1195 810 my $new;
1196 810 my $head = [];
1197 810 my $tail = [];
1198 810 ▶ while( defined( my $p = pop @$path )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $p = pop @$path)) 1159 810 1969
1199 1159 ▶ if( ref($p) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq 'HASH') { } 514 645 1159
1200 514 my ($node_head, $node_tail) = _reduce_node($p, _descend($ctx) );
1201 514 ▶ $debug and print "#$indent| head=", _dump($node_head), " tail=", _dump($node_tail), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "#$indent| head=", _dump($node_head), " tail=", _dump($node_tail), "\n" 20 494 514
1202 514 ▶ push @$head, @$node_head if scalar @$node_head;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$node_head 159 355 514
1203 514 ▶ push @$tail, ref($node_tail) eq 'HASH' ? $node_tail : @$node_tail;
Branch: 1 branch
BranchTrueFalseTotal
ref $node_tail eq 'HASH' ? : 487 27 514
1204 }
1205 else {
1206 645 ▶ if( @$head ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$head) { } 125 520 645
1207 125 ▶ $debug and print "#$indent| push $p leaves @{[_dump($path)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "#$indent| push $p leaves @{[_dump($path)];}\n" 7 118 125
1208 125 push @$tail, $p;
1209 }
1210 else {
1211 520 ▶ $debug and print "#$indent| unshift $p\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "#$indent| unshift $p\n" 4 516 520
1212 520 unshift @$tail, $p;
1213 }
1214 }
1215 }
1216 20 ▶ $debug and print "#$indent| tail nr=@{[scalar @$tail]} t0=", ref($tail->[0]),
Branch: 1 branch
BranchTrueFalseTotal
ref $tail->[0] eq 'HASH' ? : 18 2 20
1217 810 ▶ (ref($tail->[0]) eq 'HASH' ? " n=" . scalar(keys %{$tail->[0]}) : '' ),
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "#$indent| tail nr=@{[scalar @$tail];} t0=", ref $tail->[0], ref $tail->[0] eq "HASH" ? ' n=' . scalar(keys %{$$tail[0];}) : "", "\n" 20 790 810
1218 "\n";
1219 810 ▶ if( @$tail > 1
Branch: 1 branch
BranchTrueFalseTotal
if (@$tail > 1 and ref $tail->[0] eq "HASH" and keys %{$$tail[0];} == 2) 72 738 810
Condition100%6/6
@$tail > 1 and ref $tail->[0] eq "HASH" and keys %{$$tail[0];} == 2
!ll&&!rl&&r
7132572
@$tail > 1 and ref $tail->[0] eq "HASH"
!ll&&!rl&&r
60311097
MC/DC100%3/3
@$tail > 1 and ref $tail->[0] eq "HASH" and keys %{$$tail[0];} == 2
@$tail > 1 ref $tail->[0] eq "HASH" keys %{$$tail[0];} == 2
Truth table100%4/4
A and B and C
A= @$tail > 1B= ref $tail->[0] eq "HASH"C= keys %{$$tail[0];} == 2
ABCresult
0XX0
10X0
1100
1111
1220 and ref($tail->[0]) eq 'HASH'
1221 97 and keys %{$tail->[0]} == 2
1222 ) {
1223 72 my $opt;
1224 my $fixed;
1225 72 ▶ while( my ($key, $path) = each %{$tail->[0]} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$tail[0];}) 144 72 216
1226 144 ▶ $debug and print "#$indent| scan k=$key p=@{[_dump($path)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "#$indent| scan k=$key p=@{[_dump($path)];}\n" 14 130 144
1227 144 ▶ next unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 21 123 144
1228 123 ▶ if (@$path == 1 and ref($path->[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@$path == 1 and ref $path->[0] eq 'HASH') { } 8 115 123
Condition100%3/3
@$path == 1 and ref $path->[0] eq "HASH"
!ll&&!rl&&r
66498
MC/DC100%2/2
@$path == 1 and ref $path->[0] eq "HASH"
@$path == 1 ref $path->[0] eq "HASH"
Truth table100%3/3
A and B
A= @$path == 1B= ref $path->[0] eq "HASH"
ABresult
0X0
100
111
1229 8 $opt = $path->[0];
1230 }
1231 else {
1232 115 $fixed = $path;
1233 }
1234 }
1235 72 ▶ if( exists $tail->[0]{''} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tail->[0]{""}) 21 51 72
1236 21 my $path = [@{$tail}[1..$#{$tail}]];
1237 21 $tail = $tail->[0];
1238 21 ($head, $tail, $path) = _slide_tail( $head, $tail, $path, _descend($ctx) );
1239 21 $tail = [$tail, @$path];
1240 }
1241 }
1242 810 ▶ $debug and print "#$indent _reduce_path $ctx->{depth} out head=", _dump($head), ' tail=', _dump($tail), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print qq[#$indent _reduce_path $ctx->{"depth"} out head=], _dump($head), " tail=", _dump($tail), "\n" 20 790 810
1243 810 return ($head, $tail);
1244 }
1245  
1246 1080 ▶sub _reduce_node {
Subroutine _reduce_node: called CC=11 SCAR=24.0
1247 1080 my ($node, $ctx) = @_;
1248 1080 my $indent = ' ' x $ctx->{depth};
1249 1080 my $debug = $ctx->{debug};
1250 1080 my $optional = _remove_optional($node);
1251 1080 ▶ $debug and print "#$indent _reduce_node $ctx->{depth} in @{[_dump($node)]} opt=$optional\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print qq[#$indent _reduce_node $ctx->{"depth"} in @{[_dump($node)];} opt=$optional\n] 57 1023 1080
1252 1080 ▶ if( $optional and scalar keys %$node == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optional and scalar keys %$node == 1) 70 1010 1080
Condition100%3/3
$optional and scalar keys %$node == 1
!ll&&!rl&&r
9595170
MC/DC100%2/2
$optional and scalar keys %$node == 1
$optional scalar keys %$node == 1
Truth table100%3/3
A and B
A= $optionalB= scalar keys %$node == 1
ABresult
0X0
100
111
1253 70 my $path = (values %$node)[0];
1254 70 ▶ if( not grep { ref($_) eq 'HASH' } @$path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {ref $_ eq "HASH";} @$path) 61 9 70
1255 # if we have removed an optional, and there is only one path
1256 # left then there is nothing left to compare. Because of the
1257 # optional it cannot participate in any further reductions.
1258 # (unless we test for equality among sub-trees).
1259 61 my $result = {
1260 '' => undef,
1261 $path->[0] => $path
1262 };
1263 61 ▶ $debug and print "#$indent| fast fail @{[_dump($result)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "#$indent| fast fail @{[_dump($result)];}\n" 2 59 61
1264 61 return [], $result;
1265 }
1266 }
1267  
1268 1019 my( $fail, $reduce ) = _scan_node( $node, _descend($ctx) );
1269  
1270 1019 ▶ $debug and print "#$indent|_scan_node done opt=$optional reduce=@{[_dump($reduce)]} fail=@{[_dump($fail)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "#$indent|_scan_node done opt=$optional reduce=@{[_dump($reduce)];} fail=@{[_dump($fail)];}\n" 55 964 1019
1271  
1272 # We now perform tail reduction on each of the nodes in the reduce
1273 # hash. If we have only one key, we know we will have a successful
1274 # reduction (since everything that was inserted into the node based
1275 # on the value of the last token of each path all mapped to the same
1276 # value).
1277  
1278 1019 ▶ if( @$fail == 0 and keys %$reduce == 1 and not $optional) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$fail == 0 and keys %$reduce == 1 and not $optional) 506 513 1019
Condition100%6/6
@$fail == 0 and keys %$reduce == 1 and not $optional
!ll&&!rl&&r
5058506
@$fail == 0 and keys %$reduce == 1
!ll&&!rl&&r
140365514
MC/DC100%3/3
@$fail == 0 and keys %$reduce == 1 and not $optional
@$fail == 0 keys %$reduce == 1 not $optional
Truth table100%4/4
A and B and not(C)
A= @$fail == 0B= keys %$reduce == 1C= not $optional
ABCresult
0XX0
10X0
1100
1111
1279 # every path shares a common path
1280 506 my $path = (values %$reduce)[0];
1281 506 my ($common, $tail) = _do_reduce( $path, _descend($ctx) );
1282 506 ▶ $debug and print "#$indent|_reduce_node $ctx->{depth} common=@{[_dump($common)]} tail=", _dump($tail), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print qq[#$indent|_reduce_node $ctx->{"depth"} common=@{[_dump($common)];} tail=], _dump($tail), "\n" 50 456 506
1283 506 return( $common, $tail );
1284 }
1285  
1286 # this node resulted in a list of paths, game over
1287 513 $ctx->{indent} = $indent;
1288 513 return _reduce_fail( $reduce, $fail, $optional, _descend($ctx) );
1289 }
1290  
1291 513 ▶sub _reduce_fail {
Subroutine _reduce_fail: called CC=11 SCAR=24.0
1292 513 my( $reduce, $fail, $optional, $ctx ) = @_;
1293 513 my( $debug, $depth, $indent ) = @{$ctx}{qw(debug depth indent)};
1294 513 my %result;
1295 513 ▶ $result{''} = undef if $optional;
Branch: 1 branch
BranchTrueFalseTotal
if $optional 60 453 513
1296 513 my $p;
1297 513 for $p (keys %$reduce) {
1298 1181 my $path = $reduce->{$p};
1299 1181 ▶ if( scalar @$path == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$path == 1) { } 1114 67 1181
1300 1114 $path = $path->[0];
1301 1114 ▶ $debug and print "#$indent| -simple opt=$optional unrev @{[_dump($path)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "#$indent| -simple opt=$optional unrev @{[_dump($path)];}\n" 7 1107 1114
1302 1114 $path = _unrev_path($path, _descend($ctx) );
1303 1114 $result{_node_key($path->[0])} = $path;
1304 }
1305 else {
1306 67 ▶ $debug and print "#$indent| _do_reduce(@{[_dump($path)]})\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "#$indent| _do_reduce(@{[_dump($path)];})\n" 1 66 67
1307 67 my ($common, $tail) = _do_reduce( $path, _descend($ctx) );
1308 $path = [
1309 (
1310 ref($tail) eq 'HASH'
1311 ? _unrev_node($tail, _descend($ctx) )
1312 : _unrev_path($tail, _descend($ctx) )
1313 ),
1314 67 ▶ @{_unrev_path($common, _descend($ctx) )}
Branch: 1 branch
BranchTrueFalseTotal
ref $tail eq 'HASH' ? : 56 11 67
1315 ];
1316 67 ▶ $debug and print "#$indent| +reduced @{[_dump($path)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "#$indent| +reduced @{[_dump($path)];}\n" 1 66 67
1317 67 $result{_node_key($path->[0])} = $path;
1318 }
1319 }
1320 513 my $f;
1321 513 for $f( @$fail ) {
1322 219 ▶ $debug and print "#$indent| +fail @{[_dump($f)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "#$indent| +fail @{[_dump($f)];}\n" 3 216 219
1323 219 $result{$f->[0]} = $f;
1324 }
1325 513 ▶ $debug and print "#$indent _reduce_fail $depth fail=@{[_dump(\%result)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "#$indent _reduce_fail $depth fail=@{[_dump(\%result)];}\n" 5 508 513
1326 513 return ( [], \%result );
1327 }
1328  
1329 1019 ▶sub _scan_node {
Subroutine _scan_node: called CC=20 SCAR=30.0
1330 1019 my( $node, $ctx ) = @_;
1331 1019 my $indent = ' ' x $ctx->{depth};
1332 1019 my $debug = $ctx->{debug};
1333  
1334 # For all the paths in the node, reverse them. If the first token
1335 # of the path is a scalar, push it onto an array in a hash keyed by
1336 # the value of the scalar.
1337 #
1338 # If it is a node, call _reduce_node on this node beforehand. If we
1339 # get back a common head, all of the paths in the subnode shared a
1340 # common tail. We then store the common part and the remaining node
1341 # of paths (which is where the paths diverged from the end and install
1342 # this into the same hash. At this point both the common and the tail
1343 # are in reverse order, just as simple scalar paths are.
1344 #
1345 # On the other hand, if there were no common path returned then all
1346 # the paths of the sub-node diverge at the end character. In this
1347 # case the tail cannot participate in any further reductions and will
1348 # appear in forward order.
1349 #
1350 # certainly the hurgliest function in the whole file :(
1351  
1352 # $debug = 1 if $depth >= 8;
1353 1019 my @fail;
1354 my %reduce;
1355  
1356 1019 my $n;
1357 1019 for $n(
1358 2743 map { substr($_, index($_, '#')+1) }
1359 sort
1360 map {
1361 join( '|' =>
1362 9029 scalar(grep {ref($_) eq 'HASH'} @{$node->{$_}}),
1363 _node_offset($node->{$_}),
1364 2743 scalar @{$node->{$_}},
1365 )
1366 . "#$_"
1367 }
1368 keys %$node ) {
1369 2743 my( $end, @path ) = reverse @{$node->{$n}};
1370 2743 ▶ if( ref($end) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $end ne 'HASH') { } 2137 606 2743
1371 2137 ▶ $debug and print "# $indent|_scan_node push reduce ($end:@{[_dump(\@path)]})\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# $indent|_scan_node push reduce (${end}:@{[_dump(\@path)];})\n" 87 2050 2137
1372 2137 push @{$reduce{$end}}, [ $end, @path ];
1373 }
1374 else {
1375 606 ▶ $debug and print "# $indent|_scan_node head=", _dump(\@path), ' tail=', _dump($end), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# $indent|_scan_node head=", _dump(\@path), " tail=", _dump($end), "\n" 38 568 606
1376 606 my $new_path;
1377 # deal with sing, singing => s(?:ing)?ing
1378 606 ▶ if( keys %$end == 2 and exists $end->{''} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$end == 2 and exists $end->{""}) 94 512 606
Condition100%3/3
keys %$end == 2 and exists $end->{""}
!ll&&!rl&&r
12538794
MC/DC100%2/2
keys %$end == 2 and exists $end->{""}
keys %$end == 2 exists $end->{""}
Truth table100%3/3
A and B
A= keys %$end == 2B= exists $end->{""}
ABresult
0X0
100
111
1379 94 my ($key, $opt_path) = each %$end;
1380 94 ▶ ($key, $opt_path) = each %$end if $key eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "" 38 56 94
1381 94 $opt_path = [reverse @{$opt_path}];
1382 94 ▶ $debug and print "# $indent| check=", _dump($opt_path), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# $indent| check=", _dump($opt_path), "\n" 4 90 94
1383 94 my $end = { '' => undef, $opt_path->[0] => [@$opt_path] };
1384 94 my $head = [];
1385 94 my $path = [@path];
1386 94 ($head, my $slide, $path) = _slide_tail( $head, $end, $path, $ctx );
1387 94 ▶ if( @$head ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$head) 40 54 94
1388 40 $new_path = [ @$head, $slide, @$path ];
1389 }
1390 }
1391 606 ▶ if( $new_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_path) { } 40 566 606
1392 40 ▶ $debug and print "# $indent|_scan_node slid=", _dump($new_path), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# $indent|_scan_node slid=", _dump($new_path), "\n" 1 39 40
1393 40 push @{$reduce{$new_path->[0]}}, $new_path;
1394 }
1395 else {
1396 566 my( $common, $tail ) = _reduce_node( $end, _descend($ctx) );
1397 566 ▶ if( not @$common ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$common) { } 219 347 566
1398 219 ▶ $debug and print "# $indent| +failed $n\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# $indent| +failed $n\n" 3 216 219
1399 219 push @fail, [reverse(@path), $tail];
1400 }
1401 else {
1402 347 my $path = [@path];
1403 347 ▶ $debug and print "# $indent|_scan_node ++recovered common=@{[_dump($common)]} tail=",
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# $indent|_scan_node ++recovered common=@{[_dump($common)];} tail=", _dump($tail), " path=@{[_dump($path)];}\n" 34 313 347
1404 34 _dump($tail), " path=@{[_dump($path)]}\n";
1405 347 ▶ if( ref($tail) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tail eq "HASH" and keys %$tail == 2) 287 60 347
Condition100%3/3
ref $tail eq "HASH" and keys %$tail == 2
!ll&&!rl&&r
3426287
MC/DC100%2/2
ref $tail eq "HASH" and keys %$tail == 2
ref $tail eq "HASH" keys %$tail == 2
Truth table100%3/3
A and B
A= ref $tail eq "HASH"B= keys %$tail == 2
ABresult
0X0
100
111
1406 and keys %$tail == 2
1407 ) {
1408 287 ▶ if( exists $tail->{''} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tail->{""}) 121 166 287
1409 121 ($common, $tail, $path) = _slide_tail( $common, $tail, $path, $ctx );
1410 }
1411 }
1412 347 ▶ push @{$reduce{$common->[0]}}, [
Branch: 1 branch
BranchTrueFalseTotal
ref $tail eq 'HASH' ? : 313 34 347
1413 @$common,
1414 (ref($tail) eq 'HASH' ? $tail : @$tail ),
1415 @$path
1416 ];
1417 }
1418 }
1419 }
1420 }
1421 1019 ▶ $debug and print
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# $indent|_scan_node counts: reduce=@{[scalar keys %reduce];} fail=@{[scalar @fail];}\n" 55 964 1019
1422 55 "# $indent|_scan_node counts: reduce=@{[scalar keys %reduce]} fail=@{[scalar @fail]}\n";
1423 1019 return( \@fail, \%reduce );
1424 }
1425  
1426 573 ▶sub _do_reduce {
Subroutine _do_reduce: called CC=8 SCAR=20.8
1427 573 my ($path, $ctx) = @_;
1428 573 my $indent = ' ' x $ctx->{depth};
1429 573 my $debug = $ctx->{debug};
1430 573 my $ra = Regexp::Assemble->new(chomp=>0);
1431 573 $ra->debug($debug);
1432 573 ▶ $debug and print "# $indent| do @{[_dump($path)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# $indent| do @{[_dump($path)];}\n" 51 522 573
1433 573 $ra->_insertr( $_ ) for
1434 # When nodes come into the picture, we have to be careful
1435 # about how we insert the paths into the assembly.
1436 # Paths with nodes first, then closest node to front
1437 # then shortest path. Merely because if we can control
1438 # order in which paths containing nodes get inserted,
1439 # then we can make a couple of assumptions that simplify
1440 # the code in _insert_node.
1441 sort {
1442 6000 scalar(grep {ref($_) eq 'HASH'} @$a)
1443 1113 ▶ <=> scalar(grep {ref($_) eq 'HASH'} @$b)
Condition100%6/6
scalar grep({ref $_ eq 'HASH';} @$a) <=> scalar grep({ref $_ eq 'HASH';} @$b) || _node_offset($b) <=> _node_offset($a) || scalar @$a <=> scalar @$b
l!l&&r!l&&!r
302455356
scalar grep({ref $_ eq "HASH";} @$a) <=> scalar grep({ref $_ eq "HASH";} @$b) or _node_offset($b) <=> _node_offset($a)
l!l&&r!l&&!r
27626811
MC/DC100%3/3
scalar grep({ref $_ eq 'HASH';} @$a) <=> scalar grep({ref $_ eq 'HASH';} @$b) || _node_offset($b) <=> _node_offset($a) || scalar @$a <=> scalar @$b
scalar grep({ref $_ eq "HASH";} @$a) <=> scalar grep({ref $_ eq "HASH";} @$b) _node_offset($b) <=> _node_offset($a) scalar @$a <=> scalar @$b
Truth table100%4/4
A or B || C
A= scalar grep({ref $_ eq "HASH";} @$a) <=> scalar grep({ref $_ eq "HASH";} @$b)B= _node_offset($b) <=> _node_offset($a)C= scalar @$a <=> scalar @$b
ABCresult
1XX1
01X1
0011
0000
1444 ||
1445 _node_offset($b) <=> _node_offset($a)
1446 ||
1447 scalar @$a <=> scalar @$b
1448 }
1449 @$path
1450 ;
1451 573 $path = $ra->_path;
1452 573 my $common = [];
1453 573 ▶ push @$common, shift @$path while( ref($path->[0]) ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
ref $path->[0] ne "HASH" and do { push @$common, shift @$path } 1351 573 1924
1454 573 ▶ my $tail = scalar( @$path ) > 1 ? [@$path] : $path->[0];
Branch: 1 branch
BranchTrueFalseTotal
scalar @$path > 1 ? : 72 501 573
1455 573 ▶ $debug and print "# $indent| _do_reduce common=@{[_dump($common)]} tail=@{[_dump($tail)]}\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# $indent| _do_reduce common=@{[_dump($common)];} tail=@{[_dump($tail)];}\n" 51 522 573
1456 573 return ($common, $tail);
1457 }
1458  
1459 4422 ▶sub _node_offset {
Subroutine _node_offset: called CC=3 SCAR=11.0
1460 # return the offset that the first node is found, or -ve
1461 # optimised for speed
1462 4422 my $nr = @{$_[0]};
1463 4422 my $atom = -1;
1464 4422 ▶ ref($_[0]->[$atom]) eq 'HASH' and return $atom while ++$atom < $nr;
Branch: 2 branches
BranchTrueFalseTotal
++$atom < $nr and do { ref $_[0][$atom] eq "HASH" and return $atom } 18140 3458 21598
ref $_[0][$atom] eq "HASH" and return $atom 964 17176 18140
1465 3458 return -1;
1466 }
1467  
1468 240 ▶sub _slide_tail {
Subroutine _slide_tail: called CC=9 SCAR=22.0
1469 240 my $head = shift;
1470 240 my $tail = shift;
1471 240 my $path = shift;
1472 240 my $ctx = shift;
1473 240 my $indent = ' ' x $ctx->{depth};
1474 240 my $debug = $ctx->{debug};
1475 240 ▶ $debug and print "# $indent| slide in h=", _dump($head),
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# $indent| slide in h=", _dump($head), " t=", _dump($tail), " p=", _dump($path), "\n" 19 221 240
1476 ' t=', _dump($tail), ' p=', _dump($path), "\n";
1477 240 my $slide_path = (each %$tail)[-1];
1478 240 ▶ $slide_path = (each %$tail)[-1] unless defined $slide_path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slide_path 120 120 240
1479 240 ▶ $debug and print "# $indent| slide potential ", _dump($slide_path), " over ", _dump($path), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# $indent| slide potential ", _dump($slide_path), " over ", _dump($path), "\n" 19 221 240
1480 240 ▶ while( defined $path->[0] and $path->[0] eq $slide_path->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path->[0] and $path->[0] eq $slide_path->[0]) 154 240 394
Condition100%3/3
defined $path->[0] and $path->[0] eq $slide_path->[0]
!ll&&!rl&&r
31209154
MC/DC100%2/2
defined $path->[0] and $path->[0] eq $slide_path->[0]
defined $path->[0] $path->[0] eq $slide_path->[0]
Truth table100%3/3
A and B
A= defined $path->[0]B= $path->[0] eq $slide_path->[0]
ABresult
0X0
100
111
1481 154 ▶ $debug and print "# $indent| slide=tail=$slide_path->[0]\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# $indent| slide=tail=$slide_path->[0]\n" 9 145 154
1482 154 my $slide = shift @$path;
1483 154 shift @$slide_path;
1484 154 push @$slide_path, $slide;
1485 154 push @$head, $slide;
1486 }
1487 240 ▶ $debug and print "# $indent| slide path ", _dump($slide_path), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# $indent| slide path ", _dump($slide_path), "\n" 19 221 240
1488 240 my $slide_node = {
1489 '' => undef,
1490 _node_key($slide_path->[0]) => $slide_path,
1491 };
1492 240 ▶ $debug and print "# $indent| slide out h=", _dump($head),
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# $indent| slide out h=", _dump($head), " s=", _dump($slide_node), " p=", _dump($path), "\n" 19 221 240
1493 ' s=', _dump($slide_node), ' p=', _dump($path), "\n";
1494 240 return ($head, $slide_node, $path);
1495 }
1496  
1497 2690 ▶sub _unrev_path {
Subroutine _unrev_path: called CC=9 SCAR=22.0
1498 2690 my ($path, $ctx) = @_;
1499 2690 my $indent = ' ' x $ctx->{depth};
1500 2690 my $debug = $ctx->{debug};
1501 2690 my $new;
1502 2690 ▶ if( not grep { ref($_) } @$path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {ref $_;} @$path) 2196 494 2690
1503 2196 ▶ $debug and print "# ${indent}_unrev path fast ", _dump($path);
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# ${indent}_unrev path fast ", _dump($path) 79 2117 2196
1504 2196 $new = [reverse @$path];
1505 2196 ▶ $debug and print "# -> ", _dump($new), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# -> ", _dump($new), "\n" 79 2117 2196
1506 2196 return $new;
1507 }
1508 494 ▶ $debug and print "# ${indent}unrev path in ", _dump($path), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# ${indent}unrev path in ", _dump($path), "\n" 37 457 494
1509 494 ▶ while( defined( my $p = pop @$path )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $p = pop @$path)) 1483 494 1977
1510 1483 ▶ push @$new,
Branch: 2 branches
BranchTrueFalseTotal
ref $p eq 'HASH' ? : 568 915 1483
ref $p eq 'ARRAY' ? : 1 914 915
1511 ref($p) eq 'HASH' ? _unrev_node($p, _descend($ctx) )
1512 : ref($p) eq 'ARRAY' ? _unrev_path($p, _descend($ctx) )
1513 : $p
1514 ;
1515 }
1516 494 ▶ $debug and print "# ${indent}unrev path out ", _dump($new), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# ${indent}unrev path out ", _dump($new), "\n" 37 457 494
1517 494 return $new;
1518 }
1519  
1520 630 ▶sub _unrev_node {
Subroutine _unrev_node: called CC=5 SCAR=16.1
1521 630 my ($node, $ctx ) = @_;
1522 630 my $indent = ' ' x $ctx->{depth};
1523 630 my $debug = $ctx->{debug};
1524 630 my $optional = _remove_optional($node);
1525 630 ▶ $debug and print "# ${indent}unrev node in ", _dump($node), " opt=$optional\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# ${indent}unrev node in ", _dump($node), " opt=$optional\n" 46 584 630
1526 630 my $new;
1527 630 ▶ $new->{''} = undef if $optional;
Branch: 1 branch
BranchTrueFalseTotal
if $optional 229 401 630
1528 630 my $n;
1529 630 for $n( keys %$node ) {
1530 1167 my $path = _unrev_path($node->{$n}, _descend($ctx) );
1531 1167 $new->{_node_key($path->[0])} = $path;
1532 }
1533 630 ▶ $debug and print "# ${indent}unrev node out ", _dump($new), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "# ${indent}unrev node out ", _dump($new), "\n" 46 584 630
1534 630 return $new;
1535 }
1536  
1537 4657 ▶sub _node_key {
Subroutine _node_key: called CC=7 SCAR=19.5
1538 4657 my $node = shift;
1539 4657 ▶ return _node_key($node->[0]) if ref($node) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $node eq "ARRAY" 11 4646 4657
1540 4646 ▶ return $node unless ref($node) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node eq "HASH" 4441 205 4646
1541 205 my $key = '';
1542 205 my $k;
1543 205 for $k( keys %$node ) {
1544 452 ▶ next if $k eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "" 78 374 452
1545 374 ▶ $key = $k if $key eq '' or $key gt $k;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "" or $key gt $k 280 94 374
Condition100%3/3
$key eq "" or $key gt $k
l!l&&r!l&&!r
2057594
MC/DC100%2/2
$key eq "" or $key gt $k
$key eq "" $key gt $k
Truth table100%3/3
A or B
A= $key eq ""B= $key gt $k
ABresult
1X1
011
000
1546 }
1547 205 return $key;
1548 }
1549  
1550 6190 ▶sub _descend {
Subroutine _descend: called CC=1 SCAR=0.0
1551 # Take a context object, and increase the depth by one.
1552 # By creating a fresh hash each time, we don't have to
1553 # bother adding make-work code to decrease the depth
1554 # when we return from what we called.
1555 6190 my $ctx = shift;
1556 6190 return {%$ctx, depth => $ctx->{depth}+1};
1557 }
1558  
1559 #####################################################################
1560  
1561 650 ▶sub _make_class {
Subroutine _make_class: called CC=23 SCAR=31.4
1562 650 my $self = shift;
1563 650 my %set = map { ($_,1) } @_;
1564 650 ▶ delete $set{'\\d'} if exists $set{'\\w'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $set{"\\w"} 9 641 650
1565 650 ▶ delete $set{'\\D'} if exists $set{'\\W'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $set{"\\W"} 8 642 650
1566 return '.' if exists $set{'.'}
1567 or ($self->{fold_meta_pairs} and (
1568 (exists $set{'\\d'} and exists $set{'\\D'})
1569 or (exists $set{'\\s'} and exists $set{'\\S'})
1570 650 ▶ or (exists $set{'\\w'} and exists $set{'\\W'})
Branch: 1 branch
BranchTrueFalseTotal
if exists $set{"."} or $self->{"fold_meta_pairs"} and exists $set{"\\d"} && exists $set{"\\D"} || exists $set{"\\s"} && exists $set{"\\S"} || exists $set{"\\w"} && exists $set{"\\W"} 18 632 650
Condition100%21/21
exists $set{"."} or $self->{"fold_meta_pairs"} and exists $set{"\\d"} && exists $set{"\\D"} || exists $set{"\\s"} && exists $set{"\\S"} || exists $set{"\\w"} && exists $set{"\\W"}
l!l&&r!l&&!r
513632
$self->{"fold_meta_pairs"} and exists $set{"\\d"} && exists $set{"\\D"} || exists $set{"\\s"} && exists $set{"\\S"} || exists $set{"\\w"} && exists $set{"\\W"}
!ll&&!rl&&r
14249013
exists $set{"\\d"} and exists $set{"\\D"} or exists $set{"\\s"} and exists $set{"\\S"} or exists $set{"\\w"} and exists $set{"\\W"}
l!l&&r!l&&!r
94490
exists $set{"\\d"} and exists $set{"\\D"} or exists $set{"\\s"} and exists $set{"\\S"}
l!l&&r!l&&!r
54494
exists $set{"\\d"} and exists $set{"\\D"}
!ll&&!rl&&r
48995
exists $set{"\\s"} and exists $set{"\\S"}
!ll&&!rl&&r
49224
exists $set{"\\w"} and exists $set{"\\W"}
!ll&&!rl&&r
48734
MC/DC100%8/8
exists $set{"."} or $self->{"fold_meta_pairs"} and exists $set{"\\d"} && exists $set{"\\D"} || exists $set{"\\s"} && exists $set{"\\S"} || exists $set{"\\w"} && exists $set{"\\W"}
exists $set{"."} $self->{"fold_meta_pairs"} exists $set{"\\d"} exists $set{"\\D"} exists $set{"\\s"} exists $set{"\\S"} exists $set{"\\w"} exists $set{"\\W"}
Truth table58%10/17
A or B and C and D or E and F or G and H
A= exists $set{"."}B= $self->{"fold_meta_pairs"}C= exists $set{"\\d"}D= exists $set{"\\D"}E= exists $set{"\\s"}F= exists $set{"\\S"}G= exists $set{"\\w"}H= exists $set{"\\W"}
ABCDEFGHresult
1XXXXXXX1
0111XXXX1
010X11XX1
011011XX1
010X0X111
010X10111
01100X111
011010111
00XXXXXX0
010X0X0X0
010X0X100
010X100X0
010X10100
01100X0X0
01100X100
0110100X0
011010100
1571 ))
1572 ;
1573 632 for my $meta( q/\\d/, q/\\D/, q/\\s/, q/\\S/, q/\\w/, q/\\W/ ) {
1574 3792 ▶ if( exists $set{$meta} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $set{$meta}) 28 3764 3792
1575 28 my $re = qr/$meta/;
1576 28 my @delete;
1577 28 ▶ $_ =~ /^$re$/ and push @delete, $_ for keys %set;
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^$re$/ and push @delete, $_ 21 50 71
1578 28 ▶ delete @set{@delete} if @delete;
Branch: 1 branch
BranchTrueFalseTotal
if @delete 10 18 28
1579 }
1580 }
1581 632 ▶ return (keys %set)[0] if keys %set == 1;
Branch: 1 branch
BranchTrueFalseTotal
if keys %set == 1 7 625 632
1582 625 for my $meta( '.', '+', '*', '?', '(', ')', '^', '@', '$', '[', '/', ) {
1583 6875 ▶ exists $set{"\\$meta"} and $set{$meta} = delete $set{"\\$meta"};
Branch: 1 branch
BranchTrueFalseTotal
exists $set{"\\$meta"} and $set{$meta} = delete $set{"\\$meta"} 43 6832 6875
1584 }
1585 625 ▶ my $dash = exists $set{'-'} ? do { delete($set{'-'}), '-' } : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $set{'-'} ? : 20 605 625
1586 625 ▶ my $caret = exists $set{'^'} ? do { delete($set{'^'}), '^' } : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $set{'^'} ? : 7 618 625
1587 625 my $class = join( '' => sort keys %set );
1588 625 ▶ $class =~ s/0123456789/\\d/ and $class eq '\\d' and return $class;
Branch: 1 branch
BranchTrueFalseTotal
$class =~ s/0123456789/\\d/ and $class eq "\\d" and return $class 3 622 625
Condition100%3/3
$class =~ s/0123456789/\\d/ and $class eq "\\d"
!ll&&!rl&&r
62023
MC/DC100%2/2
$class =~ s/0123456789/\\d/ and $class eq "\\d"
$class =~ s/0123456789/\\d/ $class eq "\\d"
Truth table100%3/3
A and B
A= $class =~ s/0123456789/\\d/B= $class eq "\\d"
ABresult
0X0
100
111
1589 622 return "[$dash$class$caret]";
1590 }
1591  
1592 1015 ▶sub _re_sort {
Subroutine _re_sort: called CC=2 SCAR=6.9
1593 1015 ▶ return length $b <=> length $a || $a cmp $b
Condition100%3/3
length $b <=> length $a || $a cmp $b
l!l&&r!l&&!r
8072071
MC/DC100%2/2
length $b <=> length $a || $a cmp $b
length $b <=> length $a $a cmp $b
Truth table100%3/3
A || B
A= length $b <=> length $aB= $a cmp $b
ABresult
1X1
011
000
1594 }
1595  
1596 140 ▶sub _combine {
Subroutine _combine: called CC=5 SCAR=16.1
1597 140 my $self = shift;
1598 140 my $type = shift;
1599 # print "c in = @{[_dump(\@_)]}\n";
1600 # my $combine =
1601 return '('
1602 . $type
1603 140 . do {
1604 140 my( @short, @long );
1605 140 ▶ push @{ /^$Single_Char$/ ? \@short : \@long}, $_ for @_;
Branch: 1 branch
BranchTrueFalseTotal
/^$Regexp::Assemble::Single_Char$/ ? : 246 131 377
1606 140 ▶ if( @short == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@short == 1) { } 31 109 140
elsif (@short > 1) { } 77 32 109
1607 31 @long = sort _re_sort @long, @short;
1608 }
1609 elsif( @short > 1 ) {
1610 # yucky but true
1611 77 my @combine = (_make_class($self, @short), sort _re_sort @long);
1612 77 @long = @combine;
1613 }
1614 else {
1615 32 @long = sort _re_sort @long;
1616 }
1617 140 join( '|', @long );
1618 }
1619 . ')';
1620 # print "combine <$combine>\n";
1621 # $combine;
1622 }
1623  
1624 1738 ▶sub _combine_new {
Subroutine _combine_new: called CC=8 SCAR=20.8
1625 1738 my $self = shift;
1626 1738 my( @short, @long );
1627 1738 ▶ push @{ /^$Single_Char$/ ? \@short : \@long}, $_ for @_;
Branch: 1 branch
BranchTrueFalseTotal
/^$Regexp::Assemble::Single_Char$/ ? : 1843 1401 3244
1628 1738 ▶ if( @short == 1 and @long == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@short == 1 and @long == 0) { } 365 1373 1738
elsif (@short > 1 and @short == @_) { } 494 879 1373
Condition100%3/3
@short == 1 and @long == 0
!ll&&!rl&&r
1201172365
MC/DC100%2/2
@short == 1 and @long == 0
@short == 1 @long == 0
Truth table100%3/3
A and B
A= @short == 1B= @long == 0
ABresult
0X0
100
111
1629 365 return $short[0];
1630 }
1631 ▶ elsif( @short > 1 and @short == @_ ) {
Condition100%3/3
@short > 1 and @short == @_
!ll&&!rl&&r
86514494
MC/DC100%2/2
@short > 1 and @short == @_
@short > 1 @short == @_
Truth table100%3/3
A and B
A= @short > 1B= @short == @_
ABresult
0X0
100
111
1632 494 return _make_class($self, @short);
1633 }
1634 else {
1635 879 ▶ return '(?:'
Branch: 1 branch
BranchTrueFalseTotal
@short > 1 ? : 14 865 879
1636 . join( '|' =>
1637 @short > 1
1638 ? ( _make_class($self, @short), sort _re_sort @long)
1639 : ( (sort _re_sort( @long )), @short )
1640 )
1641 . ')';
1642 }
1643 }
1644  
1645 4772 ▶sub _re_path {
Subroutine _re_path: called CC=14 SCAR=26.4
1646 4772 my $self = shift;
1647 # in shorter assemblies, _re_path() is the second hottest
1648 # routine. after insert(), so make it fast.
1649  
1650 4772 ▶ if ($self->{unroll_plus}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"unroll_plus"}) 72 4700 4772
1651 # but we can't easily make this blockless
1652 72 my @arr = @{$_[0]};
1653 72 my $str = '';
1654 72 my $skip = 0;
1655 72 for my $i (0..$#arr) {
1656 127 ▶ if (ref($arr[$i]) eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $arr[$i] eq 'ARRAY') { } 1 126 127
elsif (ref $arr[$i] eq 'HASH') { } 28 98 126
elsif ($i < $#arr and $arr[$i + 1] =~ /\A$arr[$i]\*(\??)\Z/) { } 7 91 98
elsif ($skip) { } 7 84 91
1657 1 $str .= _re_path($self, $arr[$i]);
1658 }
1659 elsif (ref($arr[$i]) eq 'HASH') {
1660 $str .= exists $arr[$i]->{''}
1661 ? _combine_new( $self,
1662 7 map { _re_path( $self, $arr[$i]->{$_} ) } grep { $_ ne '' } keys %{$arr[$i]}
1663 ) . '?'
1664 28 ▶ : _combine_new($self, map { _re_path( $self, $arr[$i]->{$_} ) } keys %{$arr[$i]})
Branch: 1 branch
BranchTrueFalseTotal
exists $arr[$i]{''} ? : 7 21 28
1665 ;
1666 }
1667 ▶ elsif ($i < $#arr and $arr[$i+1] =~ /\A$arr[$i]\*(\??)\Z/) {
Condition100%3/3
$i < $#arr and $arr[$i + 1] =~ /\A$arr[$i]\*(\??)\Z/
!ll&&!rl&&r
47447
MC/DC100%2/2
$i < $#arr and $arr[$i + 1] =~ /\A$arr[$i]\*(\??)\Z/
$i < $#arr $arr[$i + 1] =~ /\A$arr[$i]\*(\??)\Z/
Truth table100%3/3
A and B
A= $i < $#arrB= $arr[$i + 1] =~ /\A$arr[$i]\*(\??)\Z/
ABresult
0X0
100
111
1668 7 ▶ $str .= "$arr[$i]+" . (defined $1 ? $1 : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 7 0 7
1669 7 ++$skip;
1670 }
1671 elsif ($skip) {
1672 7 $skip = 0;
1673 }
1674 else {
1675 84 $str .= $arr[$i];
1676 }
1677 }
1678 72 return $str;
1679 }
1680  
1681 4700 ▶ return join( '', @_ ) unless grep { length ref $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {length ref $_;} @_ 0 4700 4700
1682 4700 my $p;
1683 return join '', map {
1684 ref($_) eq '' ? $_
1685 9506 ▶ : ref($_) eq 'HASH' ? do {
Branch: 2 branches
BranchTrueFalseTotal
ref $_ eq '' ? : 7791 1715 9506
ref $_ eq 'HASH' ? : 1710 5 1715
1686 # In the case of a node, see whether there's a '' which
1687 # indicates that the whole thing is optional and thus
1688 # requires a trailing ?
1689 # Unroll the two different paths to avoid the needless
1690 # grep when it isn't necessary.
1691 1710 $p = $_;
1692 exists $_->{''}
1693 ? _combine_new( $self,
1694 801 map { _re_path( $self, $p->{$_} ) } grep { $_ ne '' } keys %$_
1695 ) . '?'
1696 1710 ▶ : _combine_new($self, map { _re_path( $self, $p->{$_} ) } keys %$_ )
Branch: 1 branch
BranchTrueFalseTotal
exists $_->{''} ? : 707 1003 1710
1697 }
1698 : _re_path($self, $_) # ref($_) eq 'ARRAY'
1699 4700 } @{$_[0]}
1700 }
1701  
1702 132 ▶sub _lookahead {
Subroutine _lookahead: called CC=11 SCAR=24.0
1703 132 my $in = shift;
1704 132 my %head;
1705 my $path;
1706 132 for $path( keys %$in ) {
1707 328 ▶ next unless defined $in->{$path};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $in->{$path} 61 267 328
1708 # print "look $path: ", ref($in->{$path}[0]), ".\n";
1709 267 ▶ if( ref($in->{$path}[0]) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $in->{$path}[0] eq 'HASH') { } 15 252 267
elsif (ref $in->{$path}[0] eq 'ARRAY') { } 2 250 252
1710 15 my $next = 0;
1711 15 ▶ while( ref($in->{$path}[$next]) eq 'HASH' and @{$in->{$path}} > $next + 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in->{$path}[$next] eq "HASH" and @{$in->{$path};} > $next + 1) 11 15 26
Condition100%3/3
ref $in->{$path}[$next] eq "HASH" and @{$in->{$path};} > $next + 1
!ll&&!rl&&r
10511
MC/DC100%2/2
ref $in->{$path}[$next] eq "HASH" and @{$in->{$path};} > $next + 1
ref $in->{$path}[$next] eq "HASH" @{$in->{$path};} > $next + 1
Truth table100%3/3
A and B
A= ref $in->{$path}[$next] eq "HASH"B= @{$in->{$path};} > $next + 1
ABresult
0X0
100
111
1712 11 ▶ if( exists $in->{$path}[$next]{''} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $in->{$path}[$next]{""}) 5 6 11
1713 5 ++$head{$in->{$path}[$next+1]};
1714 }
1715 11 ++$next;
1716 }
1717 15 my $inner = _lookahead( $in->{$path}[0] );
1718 15 @head{ keys %$inner } = (values %$inner);
1719 }
1720 elsif( ref($in->{$path}[0]) eq 'ARRAY' ) {
1721 2 my $subpath = $in->{$path}[0];
1722 2 ▶ for( my $sp = 0; $sp < @$subpath; ++$sp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sp < @$subpath) 3 0 3
1723 3 ▶ if( ref($subpath->[$sp]) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subpath->[$sp] eq 'HASH') { } 2 1 3
1724 2 my $follow = _lookahead( $subpath->[$sp] );
1725 2 @head{ keys %$follow } = (values %$follow);
1726 2 ▶ last unless exists $subpath->[$sp]{''};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $subpath->[$sp]{""} 1 1 2
1727 }
1728 else {
1729 1 ++$head{$subpath->[$sp]};
1730 1 last;
1731 }
1732 }
1733 }
1734 else {
1735 250 ++$head{ $in->{$path}[0] };
1736 }
1737 }
1738 # print "_lookahead ", _dump($in), '==>', _dump([keys %head]), "\n";
1739 132 return \%head;
1740 }
1741  
1742 265 ▶sub _re_path_lookahead {
Subroutine _re_path_lookahead: called CC=15 SCAR=27.1
1743 265 my $self = shift;
1744 265 my $in = shift;
1745 # print "_re_path_la in ", _dump($in), "\n";
1746 265 my $out = '';
1747 265 ▶ for( my $p = 0; $p < @$in; ++$p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < @$in) 573 265 838
1748 573 ▶ if( ref($in->[$p]) eq '' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $in->[$p] eq '') { } 462 111 573
elsif (ref $in->[$p] eq 'ARRAY') { } 2 109 111
1749 462 $out .= $in->[$p];
1750 462 next;
1751 }
1752 elsif( ref($in->[$p]) eq 'ARRAY' ) {
1753 2 $out .= _re_path_lookahead($self, $in->[$p]);
1754 2 next;
1755 }
1756 # print "$p ", _dump($in->[$p]), "\n";
1757 my $path = [
1758 228 map { _re_path_lookahead($self, $in->[$p]{$_} ) }
1759 275 grep { $_ ne '' }
1760 109 keys %{$in->[$p]}
1761 ];
1762 109 my $ahead = _lookahead($in->[$p]);
1763 109 my $more = 0;
1764 109 ▶ if( exists $in->[$p]{''} and $p + 1 < @$in ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $in->[$p]{""} and $p + 1 < @$in) 12 97 109
Condition100%3/3
exists $in->[$p]{""} and $p + 1 < @$in
!ll&&!rl&&r
623512
MC/DC100%2/2
exists $in->[$p]{""} and $p + 1 < @$in
exists $in->[$p]{""} $p + 1 < @$in
Truth table100%3/3
A and B
A= exists $in->[$p]{""}B= $p + 1 < @$in
ABresult
0X0
100
111
1765 12 my $next = 1;
1766 12 ▶ while( $p + $next < @$in ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p + $next < @$in) 14 0 14
1767 14 ▶ if( ref( $in->[$p+$next] ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$in[$p + $next] eq 'HASH') { } 2 12 14
1768 2 my $follow = _lookahead( $in->[$p+$next] );
1769 2 @{$ahead}{ keys %$follow } = (values %$follow);
1770 }
1771 else {
1772 12 ++$ahead->{$in->[$p+$next]};
1773 12 last;
1774 }
1775 2 ++$next;
1776 }
1777 12 $more = 1;
1778 }
1779 109 my $nr_one = grep { /^$Single_Char$/ } @$path;
1780 109 my $nr = @$path;
1781 109 ▶ if( $nr_one > 1 and $nr_one == $nr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nr_one > 1 and $nr_one == $nr) { } 18 91 109
Condition100%3/3
$nr_one > 1 and $nr_one == $nr
!ll&&!rl&&r
88318
MC/DC100%2/2
$nr_one > 1 and $nr_one == $nr
$nr_one > 1 $nr_one == $nr
Truth table100%3/3
A and B
A= $nr_one > 1B= $nr_one == $nr
ABresult
0X0
100
111
1782 18 $out .= _make_class($self, @$path);
1783 18 ▶ $out .= '?' if exists $in->[$p]{''};
Branch: 1 branch
BranchTrueFalseTotal
if exists $in->[$p]{""} 3 15 18
1784 }
1785 else {
1786 my $zwla = keys(%$ahead) > 1
1787 91 ▶ ? _combine($self, '?=', grep { s/\+$//; $_ } keys %$ahead )
Branch: 1 branch
BranchTrueFalseTotal
keys %$ahead > 1 ? : 71 20 91
1788 : '';
1789 91 ▶ my $patt = $nr > 1 ? _combine($self, '?:', @$path ) : $path->[0];
Branch: 1 branch
BranchTrueFalseTotal
$nr > 1 ? : 64 27 91
1790 # print "have nr=$nr n1=$nr_one n=", _dump($in->[$p]), ' a=', _dump([keys %$ahead]), " zwla=$zwla patt=$patt @{[_dump($path)]}\n";
1791 91 ▶ if( exists $in->[$p]{''} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $in->[$p]{''}) { } 44 47 91
1792 44 ▶ $out .= $more ? "$zwla(?:$patt)?" : "(?:$zwla$patt)?";
Branch: 1 branch
BranchTrueFalseTotal
$more ? : 11 33 44
1793 }
1794 else {
1795 47 $out .= "$zwla$patt";
1796 }
1797 }
1798 }
1799 265 return $out;
1800 }
1801  
1802 33 ▶sub _re_path_track {
Subroutine _re_path_track: called CC=9 SCAR=22.0
1803 33 my $self = shift;
1804 33 my $in = shift;
1805 33 my $normal = shift;
1806 33 my $augmented = shift;
1807 33 my $o;
1808 33 my $simple = '';
1809 33 my $augment = '';
1810 33 ▶ for( my $n = 0; $n < @$in; ++$n ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @$in) 114 33 147
1811 114 ▶ if( ref($in->[$n]) eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in->[$n] eq '') { } 104 10 114
1812 104 $o = $in->[$n];
1813 104 $simple .= $o;
1814 104 $augment .= $o;
1815 104 ▶ if( (
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @$in - 1 and ref $$in[$n + 1] eq "HASH" and exists $$in[$n + 1]{""} or $n == @$in - 1) 24 80 104
Condition100%9/9
$n < @$in - 1 and ref $$in[$n + 1] eq "HASH" and exists $$in[$n + 1]{""} or $n == @$in - 1
l!l&&r!l&&!r
12380
$n < @$in - 1 and ref $$in[$n + 1] eq "HASH" and exists $$in[$n + 1]{""}
!ll&&!rl&&r
9581
$n < @$in - 1 and ref $$in[$n + 1] eq "HASH"
!ll&&!rl&&r
23729
MC/DC50%2/4
$n < @$in - 1 and ref $$in[$n + 1] eq "HASH" and exists $$in[$n + 1]{""} or $n == @$in - 1
$n < @$in - 1 ref $$in[$n + 1] eq "HASH" exists $$in[$n + 1]{""} $n == @$in - 1
Truth table57%4/7
A and B and C or D
A= $n < @$in - 1B= ref $$in[$n + 1] eq "HASH"C= exists $$in[$n + 1]{""}D= $n == @$in - 1
ABCDresult
111X1
0XX11
10X11
11011
0XX00
10X00
11000
1816 $n < @$in - 1
1817 and ref($in->[$n+1]) eq 'HASH' and exists $in->[$n+1]{''}
1818 )
1819 or $n == @$in - 1
1820 ) {
1821 24 push @{$self->{mlist}}, $normal . $simple ;
1822 24 ▶ $augment .= $] < 5.009005
Branch: 1 branch
BranchTrueFalseTotal
$] < 5.009005 ? : 0 24 24
1823 ? "(?{\$self->{m}=$self->{mcount}})"
1824 : "(?{$self->{mcount}})"
1825 ;
1826 24 ++$self->{mcount};
1827 }
1828 }
1829 else {
1830 my $path = [
1831 25 map { $self->_re_path_track( $in->[$n]{$_}, $normal.$simple , $augmented.$augment ) }
1832 26 grep { $_ ne '' }
1833 10 keys %{$in->[$n]}
1834 ];
1835 10 $o = '(?:' . join( '|' => sort _re_sort @$path ) . ')';
1836 10 ▶ $o .= '?' if exists $in->[$n]{''};
Branch: 1 branch
BranchTrueFalseTotal
if exists $in->[$n]{""} 1 9 10
1837 10 $simple .= $o;
1838 10 $augment .= $o;
1839 }
1840 }
1841 33 return $augment;
1842 }
1843  
1844 411 ▶sub _re_path_pretty {
Subroutine _re_path_pretty: called CC=15 SCAR=27.1
1845 411 my $self = shift;
1846 411 my $in = shift;
1847 411 my $arg = shift;
1848 411 my $pre = ' ' x (($arg->{depth}+0) * $arg->{indent});
1849 411 my $indent = ' ' x (($arg->{depth}+1) * $arg->{indent});
1850 411 my $out = '';
1851 411 $arg->{depth}++;
1852 411 my $prev_was_paren = 0;
1853 411 ▶ for( my $p = 0; $p < @$in; ++$p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < @$in) 1084 411 1495
1854 1084 ▶ if( ref($in->[$p]) eq '' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $in->[$p] eq '') { } 910 174 1084
elsif (ref $in->[$p] eq 'ARRAY') { } 3 171 174
1855 910 ▶ $out .= "\n$pre" if $prev_was_paren;
Branch: 1 branch
BranchTrueFalseTotal
if $prev_was_paren 39 871 910
1856 910 $out .= $in->[$p];
1857 910 $prev_was_paren = 0;
1858 }
1859 elsif( ref($in->[$p]) eq 'ARRAY' ) {
1860 3 $out .= _re_path($self, $in->[$p]);
1861 }
1862 else {
1863 my $path = [
1864 369 map { _re_path_pretty($self, $in->[$p]{$_}, $arg ) }
1865 419 grep { $_ ne '' }
1866 171 keys %{$in->[$p]}
1867 ];
1868 171 my $nr = @$path;
1869 171 my( @short, @long );
1870 171 ▶ push @{/^$Single_Char$/ ? \@short : \@long}, $_ for @$path;
Branch: 1 branch
BranchTrueFalseTotal
/^$Regexp::Assemble::Single_Char$/ ? : 102 267 369
1871 171 ▶ if( @short == $nr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@short == $nr) { } 37 134 171
1872 37 ▶ $out .= $nr == 1 ? $path->[0] : _make_class($self, @short);
Branch: 1 branch
BranchTrueFalseTotal
$nr == 1 ? : 10 27 37
1873 37 ▶ $out .= '?' if exists $in->[$p]{''};
Branch: 1 branch
BranchTrueFalseTotal
if exists $in->[$p]{""} 13 24 37
1874 }
1875 else {
1876 134 ▶ $out .= "\n" if length $out;
Branch: 1 branch
BranchTrueFalseTotal
if length $out 98 36 134
1877 134 ▶ $out .= $pre if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 98 36 134
1878 134 $out .= "(?:\n$indent";
1879 134 ▶ if( @short < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@short < 2) { } 133 1 134
1880 133 my $r = 0;
1881 $out .= join( "\n$indent|" => map {
1882 133 ▶ $r++ and $_ =~ s/^\(\?:/\n$indent(?:/;
Branch: 1 branch
BranchTrueFalseTotal
$r++ and $_ =~ s/^\(\?:/\n$indent(?:/ 165 133 298
1883 298 $_
1884 }
1885 sort _re_sort @$path
1886 );
1887 }
1888 else {
1889 1 $out .= join( "\n$indent|" => ( (sort _re_sort @long), _make_class($self, @short) ));
1890 }
1891 134 $out .= "\n$pre)";
1892 134 ▶ if( exists $in->[$p]{''} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $in->[$p]{''}) { } 37 97 134
1893 37 $out .= "\n$pre?";
1894 37 $prev_was_paren = 0;
1895 }
1896 else {
1897 97 $prev_was_paren = 1;
1898 }
1899 }
1900 }
1901 }
1902 411 $arg->{depth}--;
1903 411 return $out;
1904 }
1905  
1906 425 ▶sub _node_eq {
Subroutine _node_eq: called CC=9 SCAR=22.0
1907 425 ▶ return 0 if not defined $_[0] or not defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] and defined $_[1] 3 422 425
Condition100%3/3
defined $_[0] and defined $_[1]
!ll&&!rl&&r
21422
MC/DC100%2/2
defined $_[0] and defined $_[1]
defined $_[0] defined $_[1]
Truth table100%3/3
A and B
A= defined $_[0]B= defined $_[1]
ABresult
0X0
100
111
1908 422 ▶ return 0 if ref $_[0] ne ref $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] ne ref $_[1] 41 381 422
1909 # Now that we have determined that the reference of each
1910 # argument are the same, we only have to test the first
1911 # one, which gives us a nice micro-optimisation.
1912 381 ▶ if( ref($_[0]) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 305 76 381
elsif (ref $_[0] eq 'ARRAY') { } 9 67 76
1913 305 keys %{$_[0]} == keys %{$_[1]}
1914 and
1915 # does this short-circuit to avoid _re_path() cost more than it saves?
1916 305 ▶ join( '|' => sort keys %{$_[0]}) eq join( '|' => sort keys %{$_[1]})
Branch: 1 branch
BranchTrueFalseTotal
keys %{$_[0];} == keys %{$_[1];} and join("|", sort(keys %{$_[0];})) eq join("|", sort(keys %{$_[1];})) and _re_path(undef, [$_[0]]) eq _re_path(undef, [$_[1]]) 244 61 305
Condition100%3/3
keys %{$_[0];} == keys %{$_[1];} and join("|", sort(keys %{$_[0];})) eq join("|", sort(keys %{$_[1];}))
!ll&&!rl&&r
3328244
MC/DC100%2/2
keys %{$_[0];} == keys %{$_[1];} and join("|", sort(keys %{$_[0];})) eq join("|", sort(keys %{$_[1];}))
keys %{$_[0];} == keys %{$_[1];} join("|", sort(keys %{$_[0];})) eq join("|", sort(keys %{$_[1];}))
Truth table100%3/3
A and B
A= keys %{$_[0];} == keys %{$_[1];}B= join("|", sort(keys %{$_[0];})) eq join("|", sort(keys %{$_[1];}))
ABresult
0X0
100
111
1917 and
1918 _re_path(undef, [$_[0]] ) eq _re_path(undef, [$_[1]] );
1919 }
1920 elsif( ref($_[0]) eq 'ARRAY' ) {
1921 9 ▶ scalar @{$_[0]} == scalar @{$_[1]}
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$_[0];} == scalar @{$_[1];} and _re_path(undef, $_[0]) eq _re_path(undef, $_[1]) 8 1 9
1922 and
1923 _re_path(undef, $_[0]) eq _re_path(undef, $_[1]);
1924 }
1925 else {
1926 67 $_[0] eq $_[1];
1927 }
1928 }
1929  
1930 7 ▶sub _pretty_dump {
Subroutine _pretty_dump: called CC=1 SCAR=0.0
1931 7 return sprintf "\\x%02x", ord(shift);
1932 }
1933  
1934 5579 ▶sub _dump {
Subroutine _dump: called CC=9 SCAR=22.0
1935 5579 my $path = shift;
1936 5579 ▶ return _dump_node($path) if ref($path) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $path eq "HASH" 522 5057 5579
1937 5057 my $dump = '[';
1938 5057 my $d;
1939 5057 my $nr = 0;
1940 5057 for $d( @$path ) {
1941 11036 ▶ $dump .= ' ' if $nr++;
Branch: 1 branch
BranchTrueFalseTotal
if $nr++ 6090 4946 11036
1942 11036 ▶ if( ref($d) eq 'HASH' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $d eq 'HASH') { } 1340 9696 11036
elsif (ref $d eq 'ARRAY') { } 242 9454 9696
elsif (defined $d) { } 9453 1 9454
1943 1340 $dump .= _dump_node($d);
1944 }
1945 elsif( ref($d) eq 'ARRAY' ) {
1946 242 $dump .= _dump($d);
1947 }
1948 elsif( defined $d ) {
1949 # D::C indicates the second test is redundant
1950 # $dump .= ( $d =~ /\s/ or not length $d )
1951 9453 ▶ $dump .= (
Branch: 2 branches
BranchTrueFalseTotal
$d =~ /\s/ ? : 3 9450 9453
$d =~ /^[\x00-\x1f]$/ ? : 5 9445 9450
1952 $d =~ /\s/ ? qq{'$d'} :
1953 $d =~ /^[\x00-\x1f]$/ ? _pretty_dump($d) :
1954 $d
1955 );
1956 }
1957 else {
1958 1 $dump .= '*';
1959 }
1960 }
1961 5057 return $dump . ']';
1962 }
1963  
1964 1862 ▶sub _dump_node {
Subroutine _dump_node: called CC=5 SCAR=16.1
1965 1862 my $node = shift;
1966 1862 my $dump = '{';
1967 1862 my $nr = 0;
1968 1862 my $n;
1969 1862 for $n (sort keys %$node) {
1970 3899 ▶ $dump .= ' ' if $nr++;
Branch: 1 branch
BranchTrueFalseTotal
if $nr++ 2038 1861 3899
1971 # Devel::Cover shows this to test to be redundant
1972 # $dump .= ( $n eq '' and not defined $node->{$n} )
1973 $dump .= $n eq ''
1974 ? '*'
1975 : ($n =~ /^[\x00-\x1f]$/ ? _pretty_dump($n) : $n)
1976 3899 ▶ . "=>" . _dump($node->{$n})
Branch: 2 branches
BranchTrueFalseTotal
$n eq '' ? : 688 3211 3899
$n =~ /^[\x00-\x1f]$/ ? : 2 3209 3211
1977 ;
1978 }
1979 1862 return $dump . '}';
1980 }
1981  
1982 =pod
1983  
1984 =head1 NAME
1985  
1986 Regexp::Assemble - Assemble multiple Regular Expressions into a single RE
1987  
1988 =head1 SYNOPSIS
1989  
1990 use Regexp::Assemble;
1991  
1992 my $ra = Regexp::Assemble->new;
1993 $ra->add( 'ab+c' );
1994 $ra->add( 'ab+-' );
1995 $ra->add( 'a\w\d+' );
1996 $ra->add( 'a\d+' );
1997 print $ra->re; # prints a(?:\w?\d+|b+[-c])
1998  
1999 =head1 DESCRIPTION
2000  
2001 Regexp::Assemble takes an arbitrary number of regular expressions
2002 and assembles them into a single regular expression (or RE) that
2003 matches all that the individual REs match.
2004  
2005 As a result, instead of having a large list of expressions to loop
2006 over, a target string only needs to be tested against one expression.
2007 This is interesting when you have several thousand patterns to deal
2008 with. Serious effort is made to produce the smallest pattern possible.
2009  
2010 It is also possible to track the original patterns, so that you can
2011 determine which, among the source patterns that form the assembled
2012 pattern, was the one that caused the match to occur.
2013  
2014 You should realise that large numbers of alternations are processed
2015 in perl's regular expression engine in O(n) time, not O(1). If you
2016 are still having performance problems, you should look at using a
2017 trie. Note that Perl's own regular expression engine will implement
2018 trie optimisations in perl 5.10 (they are already available in
2019 perl 5.9.3 if you want to try them out). C<Regexp::Assemble> will
2020 do the right thing when it knows it's running on a trie'd perl.
2021 (At least in some version after this one).
2022  
2023 Some more examples of usage appear in the accompanying README. If
2024 that file is not easy to access locally, you can find it on a web
2025 repository such as
2026 L<http://search.cpan.org/dist/Regexp-Assemble/README> or
2027 L<http://cpan.uwinnipeg.ca/htdocs/Regexp-Assemble/README.html>.
2028  
2029 See also L</LIMITATIONS>.
2030  
2031 =head1 Methods
2032  
2033 =head2 add(LIST)
2034  
2035 Takes a string, breaks it apart into a set of tokens (respecting
2036 meta characters) and inserts the resulting list into the C<R::A>
2037 object. It uses a naive regular expression to lex the string
2038 that may be fooled complex expressions (specifically, it will
2039 fail to lex nested parenthetical expressions such as
2040 C<ab(cd(ef)?gh)ij> correctly). If this is the case, the end of
2041 the string will not be tokenised correctly and returned as one
2042 long string.
2043  
2044 On the one hand, this may indicate that the patterns you are
2045 trying to feed the C<R::A> object are too complex. Simpler
2046 patterns might allow the algorithm to work more effectively and
2047 perform more reductions in the resulting pattern.
2048  
2049 On the other hand, you can supply your own pattern to perform the
2050 lexing if you need. The test suite contains an example of a lexer
2051 pattern that will match one level of nested parentheses.
2052  
2053 Note that there is an internal optimisation that will bypass a
2054 much of the lexing process. If a string contains no C<\>
2055 (backslash), C<[> (open square bracket), C<(> (open paren),
2056 C<?> (question mark), C<+> (plus), C<*> (star) or C<{> (open
2057 curly), a character split will be performed directly.
2058  
2059 A list of strings may be supplied, thus you can pass it a file
2060 handle of a file opened for reading:
2061  
2062 $re->add( '\d+-\d+-\d+-\d+\.example\.com' );
2063 $re->add( <IN> );
2064  
2065 If the file is very large, it may be more efficient to use a
2066 C<while> loop, to read the file line-by-line:
2067  
2068 $re->add($_) while <IN>;
2069  
2070 The C<add> method will chomp the lines automatically. If you
2071 do not want this to occur (you want to keep the record
2072 separator), then disable C<chomp>ing.
2073  
2074 $re->chomp(0);
2075 $re->add($_) while <IN>;
2076  
2077 This method is chainable.
2078  
2079 =head2 add_file(FILENAME [...])
2080  
2081 Takes a list of file names. Each file is opened and read
2082 line by line. Each line is added to the assembly.
2083  
2084 $r->add_file( 'file.1', 'file.2' );
2085  
2086 If a file cannot be opened, the method will croak. If you cannot
2087 afford to let this happen then you should wrap the call in a C<eval>
2088 block.
2089  
2090 Chomping happens automatically unless you the C<chomp(0)> method
2091 to disable it. By default, input lines are read according to the
2092 value of the C<input_record_separator> attribute (if defined), and
2093 will otherwise fall back to the current setting of the system C<$/>
2094 variable. The record separator may also be specified on each
2095 call to C<add_file>. Internally, the routine C<local>ises the
2096 value of C<$/> to whatever is required, for the duration of the
2097 call.
2098  
2099 An alternate calling mechanism using a hash reference is
2100 available. The recognised keys are:
2101  
2102 =over 4
2103  
2104 =item file
2105  
2106 Reference to a list of file names, or the name of a single
2107 file.
2108  
2109 $r->add_file({file => ['file.1', 'file.2', 'file.3']});
2110 $r->add_file({file => 'file.n'});
2111  
2112 =item input_record_separator
2113  
2114 If present, indicates what constitutes a line
2115  
2116 $r->add_file({file => 'data.txt', input_record_separator => ':' });
2117  
2118 =item rs
2119  
2120 An alias for input_record_separator (mnemonic: same as the
2121 English variable names).
2122  
2123 =back
2124  
2125 $r->add_file( {
2126 file => [ 'pattern.txt', 'more.txt' ],
2127 input_record_separator => "\r\n",
2128 });
2129  
2130 =head2 clone()
2131  
2132 Clones the contents of a Regexp::Assemble object and creates a new
2133 object (in other words it performs a deep copy).
2134  
2135 If the Storable module is installed, its dclone method will be used,
2136 otherwise the cloning will be performed using a pure perl approach.
2137  
2138 You can use this method to take a snapshot of the patterns that have
2139 been added so far to an object, and generate an assembly from the
2140 clone. Additional patterns may to be added to the original object
2141 afterwards.
2142  
2143 my $re = $main->clone->re();
2144 $main->add( 'another-pattern-\\d+' );
2145  
2146 =head2 insert(LIST)
2147  
2148 Takes a list of tokens representing a regular expression and
2149 stores them in the object. Note: you should not pass it a bare
2150 regular expression, such as C<ab+c?d*e>. You must pass it as
2151 a list of tokens, I<e.g.> C<('a', 'b+', 'c?', 'd*', 'e')>.
2152  
2153 This method is chainable, I<e.g.>:
2154  
2155 my $ra = Regexp::Assemble->new
2156 ->insert( qw[ a b+ c? d* e ] )
2157 ->insert( qw[ a c+ d+ e* f ] );
2158  
2159 Lexing complex patterns with metacharacters and so on can consume
2160 a significant proportion of the overall time to build an assembly.
2161 If you have the information available in a tokenised form, calling
2162 C<insert> directly can be a big win.
2163  
2164 =head2 lexstr
2165  
2166 Use the C<lexstr> method if you are curious to see how a pattern
2167 gets tokenised. It takes a scalar on input, representing a pattern,
2168 and returns a reference to an array, containing the tokenised
2169 pattern. You can recover the original pattern by performing a
2170 C<join>:
2171  
2172 my @token = $re->lexstr($pattern);
2173 my $new_pattern = join( '', @token );
2174  
2175 If the original pattern contains unnecessary backslashes, or C<\x4b>
2176 escapes, or quotemeta escapes (C<\Q>...C<\E>) the resulting pattern
2177 may not be identical.
2178  
2179 Call C<lexstr> does not add the pattern to the object, it is merely
2180 for exploratory purposes. It will, however, update various statistical
2181 counters.
2182  
2183 =head2 pre_filter(CODE)
2184  
2185 Allows you to install a callback to check that the pattern being
2186 loaded contains valid input. It receives the pattern as a whole to
2187 be added, before it been tokenised by the lexer. It may to return
2188 0 or C<undef> to indicate that the pattern should not be added, any
2189 true value indicates that the contents are fine.
2190  
2191 A filter to strip out trailing comments (marked by #):
2192  
2193 $re->pre_filter( sub { $_[0] =~ s/\s*#.*$//; 1 } );
2194  
2195 A filter to ignore blank lines:
2196  
2197 $re->pre_filter( sub { length(shift) } );
2198  
2199 If you want to remove the filter, pass C<undef> as a parameter.
2200  
2201 $ra->pre_filter(undef);
2202  
2203 This method is chainable.
2204  
2205 =head2 filter(CODE)
2206  
2207 Allows you to install a callback to check that the pattern being
2208 loaded contains valid input. It receives a list on input, after it
2209 has been tokenised by the lexer. It may to return 0 or undef to
2210 indicate that the pattern should not be added, any true value
2211 indicates that the contents are fine.
2212  
2213 If you know that all patterns you expect to assemble contain
2214 a restricted set of of tokens (e.g. no spaces), you could do
2215 the following:
2216  
2217 $ra->filter(sub { not grep { / / } @_ });
2218  
2219 or
2220  
2221 sub only_spaces_and_digits {
2222 not grep { ![\d ] } @_
2223 }
2224 $ra->filter( \&only_spaces_and_digits );
2225  
2226 These two examples will silently ignore faulty patterns, If you
2227 want the user to be made aware of the problem you should raise an
2228 error (via C<warn> or C<die>), log an error message, whatever is
2229 best. If you want to remove a filter, pass C<undef> as a parameter.
2230  
2231 $ra->filter(undef);
2232  
2233 This method is chainable.
2234  
2235 =head2 as_string
2236  
2237 Assemble the expression and return it as a string. You may want to do
2238 this if you are writing the pattern to a file. The following arguments
2239 can be passed to control the aspect of the resulting pattern:
2240  
2241 B<indent>, the number of spaces used to indent nested grouping of
2242 a pattern. Use this to produce a pretty-printed pattern (for some
2243 definition of "pretty"). The resulting output is rather verbose. The
2244 reason is to ensure that the metacharacters C<(?:> and C<)> always
2245 occur on otherwise empty lines. This allows you grep the result for an
2246 even more synthetic view of the pattern:
2247  
2248 egrep -v '^ *[()]' <regexp.file>
2249  
2250 The result of the above is quite readable. Remember to backslash the
2251 spaces appearing in your own patterns if you wish to use an indented
2252 pattern in an C<m/.../x> construct. Indenting is ignored if tracking
2253 is enabled.
2254  
2255 The B<indent> argument takes precedence over the C<indent>
2256 method/attribute of the object.
2257  
2258 Calling this
2259 method will drain the internal data structure. Large numbers of patterns
2260 can eat a significant amount of memory, and this lets perl recover the
2261 memory used for other purposes.
2262  
2263 If you want to reduce the pattern I<and> continue to add new patterns,
2264 clone the object and reduce the clone, leaving the original object intact.
2265  
2266 =head2 re
2267  
2268 Assembles the pattern and return it as a compiled RE, using the
2269 C<qr//> operator.
2270  
2271 As with C<as_string>, calling this method will reset the internal data
2272 structures to free the memory used in assembling the RE.
2273  
2274 The B<indent> attribute, documented in the C<as_string> method, can be
2275 used here (it will be ignored if tracking is enabled).
2276  
2277 With method chaining, it is possible to produce a RE without having
2278 a temporary C<Regexp::Assemble> object lying around, I<e.g.>:
2279  
2280 my $re = Regexp::Assemble->new
2281 ->add( q[ab+cd+e] )
2282 ->add( q[ac\\d+e] )
2283 ->add( q[c\\d+e] )
2284 ->re;
2285  
2286 The C<$re> variable now contains a Regexp object that can be used
2287 directly:
2288  
2289 while( <> ) {
2290 /$re/ and print "Something in [$_] matched\n";
2291 )
2292  
2293 The C<re> method is called when the object is used in string context
2294 (hence, within an C<m//> operator), so by and large you do not even
2295 need to save the RE in a separate variable. The following will work
2296 as expected:
2297  
2298 my $re = Regexp::Assemble->new->add( qw[ fee fie foe fum ] );
2299 while( <IN> ) {
2300 if( /($re)/ ) {
2301 print "Here be giants: $1\n";
2302 }
2303 }
2304  
2305 This approach does not work with tracked patterns. The
2306 C<match> and C<matched> methods must be used instead, see below.
2307  
2308 =head2 match(SCALAR)
2309  
2310 The following information applies to Perl 5.8 and below. See
2311 the section that follows for information on Perl 5.10.
2312  
2313 If pattern tracking is in use, you must C<use re 'eval'> in order
2314 to make things work correctly. At a minimum, this will make your
2315 code look like this:
2316  
2317 my $did_match = do { use re 'eval'; $target =~ /$ra/ }
2318 if( $did_match ) {
2319 print "matched ", $ra->matched, "\n";
2320 }
2321  
2322 (The main reason is that the C<$^R> variable is currently broken
2323 and an ugly workaround that runs some Perl code during the match
2324 is required, in order to simulate what C<$^R> should be doing. See
2325 Perl bug #32840 for more information if you are curious. The README
2326 also contains more information). This bug has been fixed in 5.10.
2327  
2328 The important thing to note is that with C<use re 'eval'>, THERE
2329 ARE SECURITY IMPLICATIONS WHICH YOU IGNORE AT YOUR PERIL. The problem
2330 is this: if you do not have strict control over the patterns being
2331 fed to C<Regexp::Assemble> when tracking is enabled, and someone
2332 slips you a pattern such as C</^(?{system 'rm -rf /'})/> and you
2333 attempt to match a string against the resulting pattern, you will
2334 know Fear and Loathing.
2335  
2336 What is more, the C<$^R> workaround means that that tracking does
2337 not work if you perform a bare C</$re/> pattern match as shown
2338 above. You have to instead call the C<match> method, in order to
2339 supply the necessary context to take care of the tracking housekeeping
2340 details.
2341  
2342 if( defined( my $match = $ra->match($_)) ) {
2343 print " $_ matched by $match\n";
2344 }
2345  
2346 In the case of a successful match, the original matched pattern
2347 is returned directly. The matched pattern will also be available
2348 through the C<matched> method.
2349  
2350 (Except that the above is not true for 5.6.0: the C<match> method
2351 returns true or undef, and the C<matched> method always returns
2352 undef).
2353  
2354 If you are capturing parts of the pattern I<e.g.> C<foo(bar)rat>
2355 you will want to get at the captures. See the C<mbegin>, C<mend>,
2356 C<mvar> and C<capture> methods. If you are not using captures
2357 then you may safely ignore this section.
2358  
2359 In 5.10, since the bug concerning C<$^R> has been resolved, there
2360 is no need to use C<re 'eval'> and the assembled pattern does
2361 not require any Perl code to be executed during the match.
2362  
2363 =head2 new()
2364  
2365 Creates a new C<Regexp::Assemble> object. The following optional
2366 key/value parameters may be employed. All keys have a corresponding
2367 method that can be used to change the behaviour later on. As a
2368 general rule, especially if you're just starting out, you don't
2369 have to bother with any of these.
2370  
2371 B<anchor_*>, a family of optional attributes that allow anchors
2372 (C<^>, C<\b>, C<\Z>...) to be added to the resulting pattern.
2373  
2374 B<flags>, sets the C<imsx> flags to add to the assembled regular
2375 expression. Warning: no error checking is done, you should ensure
2376 that the flags you pass are understood by the version of Perl you
2377 are using. B<modifiers> exists as an alias, for users familiar
2378 with L<Regexp::List>.
2379  
2380 B<chomp>, controls whether the pattern should be chomped before being
2381 lexed. Handy if you are reading patterns from a file. By default,
2382 C<chomp>ing is performed (this behaviour changed as of version 0.24,
2383 prior versions did not chomp automatically).
2384 See also the C<file> attribute and the C<add_file> method.
2385  
2386 B<file>, slurp the contents of the specified file and add them
2387 to the assembly. Multiple files may be processed by using a list.
2388  
2389 my $r = Regexp::Assemble->new(file => 're.list');
2390  
2391 my $r = Regexp::Assemble->new(file => ['re.1', 're.2']);
2392  
2393 If you really don't want chomping to occur, you will have to set
2394 the C<chomp> attribute to 0 (zero). You may also want to look at
2395 the C<input_record_separator> attribute, as well.
2396  
2397 B<input_record_separator>, controls what constitutes a record
2398 separator when using the C<file> attribute or the C<add_file>
2399 method. May be abbreviated to B<rs>. See the C<$/> variable in
2400 L<perlvar>.
2401  
2402 B<lookahead>, controls whether the pattern should contain zero-width
2403 lookahead assertions (For instance: (?=[abc])(?:bob|alice|charles).
2404 This is not activated by default, because in many circumstances the
2405 cost of processing the assertion itself outweighs the benefit of
2406 its faculty for short-circuiting a match that will fail. This is
2407 sensitive to the probability of a match succeeding, so if you're
2408 worried about performance you'll have to benchmark a sample population
2409 of targets to see which way the benefits lie.
2410  
2411 B<track>, controls whether you want know which of the initial
2412 patterns was the one that matched. See the C<matched> method for
2413 more details. Note for version 5.8 of Perl and below, in this mode
2414 of operation YOU SHOULD BE AWARE OF THE SECURITY IMPLICATIONS that
2415 this entails. Perl 5.10 does not suffer from any such restriction.
2416  
2417 B<indent>, the number of spaces used to indent nested grouping of
2418 a pattern. Use this to produce a pretty-printed pattern. See the
2419 C<as_string> method for a more detailed explanation.
2420  
2421 B<pre_filter>, allows you to add a callback to enable sanity checks
2422 on the pattern being loaded. This callback is triggered before the
2423 pattern is split apart by the lexer. In other words, it operates
2424 on the entire pattern. If you are loading patterns from a file,
2425 this would be an appropriate place to remove comments.
2426  
2427 B<filter>, allows you to add a callback to enable sanity checks on
2428 the pattern being loaded. This callback is triggered after the
2429 pattern has been split apart by the lexer.
2430  
2431 B<unroll_plus>, controls whether to unroll, for example, C<x+> into
2432 C<x>, C<x*>, which may allow additional reductions in the
2433 resulting assembled pattern.
2434  
2435 B<reduce>, controls whether tail reduction occurs or not. If set,
2436 patterns like C<a(?:bc+d|ec+d)> will be reduced to C<a[be]c+d>.
2437 That is, the end of the pattern in each part of the b... and d...
2438 alternations is identical, and hence is hoisted out of the alternation
2439 and placed after it. On by default. Turn it off if you're really
2440 pressed for short assembly times.
2441  
2442 B<lex>, specifies the pattern used to lex the input lines into
2443 tokens. You could replace the default pattern by a more sophisticated
2444 version that matches arbitrarily nested parentheses, for example.
2445  
2446 B<debug>, controls whether copious amounts of output is produced
2447 during the loading stage or the reducing stage of assembly.
2448  
2449 my $ra = Regexp::Assemble->new;
2450 my $rb = Regexp::Assemble->new( chomp => 1, debug => 3 );
2451  
2452 B<mutable>, controls whether new patterns can be added to the object
2453 after the assembled pattern is generated. DEPRECATED.
2454  
2455 This method/attribute will be removed in a future release. It doesn't
2456 really serve any purpose, and may be more effectively replaced by
2457 cloning an existing C<Regexp::Assemble> object and spinning out a
2458 pattern from that instead.
2459  
2460 =head2 source()
2461  
2462 When using tracked mode, after a successful match is made, returns
2463 the original source pattern that caused the match. In Perl 5.10,
2464 the C<$^R> variable can be used to as an index to fetch the correct
2465 pattern from the object.
2466  
2467 If no successful match has been performed, or the object is not in
2468 tracked mode, this method returns C<undef>.
2469  
2470 my $r = Regexp::Assemble->new->track(1)->add(qw(foo? bar{2} [Rr]at));
2471  
2472 for my $w (qw(this food is rather barren)) {
2473 if ($w =~ /$r/) {
2474 print "$w matched by ", $r->source($^R), $/;
2475 }
2476 else {
2477 print "$w no match\n";
2478 }
2479 }
2480  
2481 =head2 mbegin()
2482  
2483 This method returns a copy of C<@-> at the moment of the
2484 last match. You should ordinarily not need to bother with
2485 this, C<mvar> should be able to supply all your needs.
2486  
2487 =head2 mend()
2488  
2489 This method returns a copy of C<@+> at the moment of the
2490 last match.
2491  
2492 =head2 mvar(NUMBER)
2493  
2494 The C<mvar> method returns the captures of the last match.
2495 C<mvar(1)> corresponds to $1, C<mvar(2)> to $2, and so on.
2496 C<mvar(0)> happens to return the target string matched,
2497 as a byproduct of walking down the C<@-> and C<@+> arrays
2498 after the match.
2499  
2500 If called without a parameter, C<mvar> will return a
2501 reference to an array containing all captures.
2502  
2503 =head2 capture
2504  
2505 The C<capture> method returns the the captures of the last
2506 match as an array. Unlink C<mvar>, this method does not
2507 include the matched string. It is equivalent to getting an
2508 array back that contains C<$1, $2, $3, ...>.
2509  
2510 If no captures were found in the match, an empty array is
2511 returned, rather than C<undef>. You are therefore guaranteed
2512 to be able to use C<< for my $c ($re->capture) { ... >>
2513 without have to check whether anything was captured.
2514  
2515 =head2 matched()
2516  
2517 If pattern tracking has been set, via the C<track> attribute,
2518 or through the C<track> method, this method will return the
2519 original pattern of the last successful match. Returns undef
2520 match has yet been performed, or tracking has not been enabled.
2521  
2522 See below in the NOTES section for additional subtleties of
2523 which you should be aware of when tracking patterns.
2524  
2525 Note that this method is not available in 5.6.0, due to
2526 limitations in the implementation of C<(?{...})> at the time.
2527  
2528 =head2 Statistics/Reporting routines
2529  
2530 =head2 stats_add
2531  
2532 Returns the number of patterns added to the assembly (whether
2533 by C<add> or C<insert>). Duplicate patterns are not included
2534 in this total.
2535  
2536 =head2 stats_dup
2537  
2538 Returns the number of duplicate patterns added to the assembly.
2539 If non-zero, this may be a sign that something is wrong with
2540 your data (or at the least, some needless redundancy). This may
2541 occur when you have two patterns (for instance, C<a\-b> and
2542 C<a-b>) which map to the same result.
2543  
2544 =head2 stats_raw()
2545  
2546 Returns the raw number of bytes in the patterns added to the
2547 assembly. This includes both original and duplicate patterns.
2548 For instance, adding the two patterns C<ab> and C<ab> will
2549 count as 4 bytes.
2550  
2551 =head2 stats_cooked()
2552  
2553 Return the true number of bytes added to the assembly. This
2554 will not include duplicate patterns. Furthermore, it may differ
2555 from the raw bytes due to quotemeta treatment. For instance,
2556 C<abc\,def> will count as 7 (not 8) bytes, because C<\,> will
2557 be stored as C<,>. Also, C<\Qa.b\E> is 7 bytes long, however,
2558 after the quotemeta directives are processed, C<a\.b> will be
2559 stored, for a total of 4 bytes.
2560  
2561 =head2 stats_length()
2562  
2563 Returns the length of the resulting assembled expression.
2564 Until C<as_string> or C<re> have been called, the length
2565 will be 0 (since the assembly will have not yet been
2566 performed). The length includes only the pattern, not the
2567 additional (C<(?-xism...>) fluff added by the compilation.
2568  
2569 =head2 dup_warn(NUMBER|CODEREF)
2570  
2571 Turns warnings about duplicate patterns on or off. By
2572 default, no warnings are emitted. If the method is
2573 called with no parameters, or a true parameter,
2574 the object will carp about patterns it has
2575 already seen. To turn off the warnings, use 0 as a
2576 parameter.
2577  
2578 $r->dup_warn();
2579  
2580 The method may also be passed a code block. In this case
2581 the code will be executed and it will receive a reference
2582 to the object in question, and the lexed pattern.
2583  
2584 $r->dup_warn(
2585 sub {
2586 my $self = shift;
2587 print $self->stats_add, " patterns added at line $.\n",
2588 join( '', @_ ), " added previously\n";
2589 }
2590 )
2591  
2592 =head2 Anchor routines
2593  
2594 Suppose you wish to assemble a series of patterns that all begin
2595 with C<^> and end with C<$> (anchor pattern to the beginning and
2596 end of line). Rather than add the anchors to each and every pattern
2597 (and possibly forget to do so when a new entry is added), you may
2598 specify the anchors in the object, and they will appear in the
2599 resulting pattern, and you no longer need to (or should) put them
2600 in your source patterns. For example, the two following snippets
2601 will produce identical patterns:
2602  
2603 $r->add(qw(^this ^that ^them))->as_string;
2604  
2605 $r->add(qw(this that them))->anchor_line_begin->as_string;
2606  
2607 # both techniques will produce ^th(?:at|em|is)
2608  
2609 All anchors are possible word (C<\b>) boundaries, line
2610 boundaries (C<^> and C<$>) and string boundaries (C<\A>
2611 and C<\Z> (or C<\z> if you absolutely need it)).
2612  
2613 The shortcut C<anchor_I<mumble>> implies both
2614 C<anchor_I<mumble>_begin> C<anchor_I<mumble>_end>
2615 is also available. If different anchors are specified
2616 the most specific anchor wins. For instance, if both
2617 C<anchor_word_begin> and C<anchor_line_begin> are
2618 specified, C<anchor_word_begin> takes precedence.
2619  
2620 All the anchor methods are chainable.
2621  
2622 =head2 anchor_word_begin
2623  
2624 The resulting pattern will be prefixed with a C<\b>
2625 word boundary assertion when the value is true. Set
2626 to 0 to disable.
2627  
2628 $r->add('pre')->anchor_word_begin->as_string;
2629 # produces '\bpre'
2630  
2631 =head2 anchor_word_end
2632  
2633 The resulting pattern will be suffixed with a C<\b>
2634 word boundary assertion when the value is true. Set
2635 to 0 to disable.
2636  
2637 $r->add(qw(ing tion))
2638 ->anchor_word_end
2639 ->as_string; # produces '(?:tion|ing)\b'
2640  
2641 =head2 anchor_word
2642  
2643 The resulting pattern will be have C<\b>
2644 word boundary assertions at the beginning and end
2645 of the pattern when the value is true. Set
2646 to 0 to disable.
2647  
2648 $r->add(qw(cat carrot)
2649 ->anchor_word(1)
2650 ->as_string; # produces '\bca(?:rro)t\b'
2651  
2652 =head2 anchor_line_begin
2653  
2654 The resulting pattern will be prefixed with a C<^>
2655 line boundary assertion when the value is true. Set
2656 to 0 to disable.
2657  
2658 $r->anchor_line_begin;
2659 # or
2660 $r->anchor_line_begin(1);
2661  
2662 =head2 anchor_line_end
2663  
2664 The resulting pattern will be suffixed with a C<$>
2665 line boundary assertion when the value is true. Set
2666 to 0 to disable.
2667  
2668 # turn it off
2669 $r->anchor_line_end(0);
2670  
2671 =head2 anchor_line
2672  
2673 The resulting pattern will be have the C<^> and C<$>
2674 line boundary assertions at the beginning and end
2675 of the pattern, respectively, when the value is true. Set
2676 to 0 to disable.
2677  
2678 $r->add(qw(cat carrot)
2679 ->anchor_line
2680 ->as_string; # produces '^ca(?:rro)t$'
2681  
2682 =head2 anchor_string_begin
2683  
2684 The resulting pattern will be prefixed with a C<\A>
2685 string boundary assertion when the value is true. Set
2686 to 0 to disable.
2687  
2688 $r->anchor_string_begin(1);
2689  
2690 =head2 anchor_string_end
2691  
2692 The resulting pattern will be suffixed with a C<\Z>
2693 string boundary assertion when the value is true. Set
2694 to 0 to disable.
2695  
2696 # disable the string boundary end anchor
2697 $r->anchor_string_end(0);
2698  
2699 =head2 anchor_string_end_absolute
2700  
2701 The resulting pattern will be suffixed with a C<\z>
2702 string boundary assertion when the value is true. Set
2703 to 0 to disable.
2704  
2705 # disable the string boundary absolute end anchor
2706 $r->anchor_string_end_absolute(0);
2707  
2708 If you don't understand the difference between
2709 C<\Z> and C<\z>, the former will probably do what
2710 you want.
2711  
2712 =head2 anchor_string
2713  
2714 The resulting pattern will be have the C<\A> and C<\Z>
2715 string boundary assertions at the beginning and end
2716 of the pattern, respectively, when the value is true. Set
2717 to 0 to disable.
2718  
2719 $r->add(qw(cat carrot)
2720 ->anchor_string
2721 ->as_string; # produces '\Aca(?:rro)t\Z'
2722  
2723 =head2 anchor_string_absolute
2724  
2725 The resulting pattern will be have the C<\A> and C<\z>
2726 string boundary assertions at the beginning and end
2727 of the pattern, respectively, when the value is true. Set
2728 to 0 to disable.
2729  
2730 $r->add(qw(cat carrot)
2731 ->anchor_string_absolute
2732 ->as_string; # produces '\Aca(?:rro)t\z'
2733  
2734 =head2 debug(NUMBER)
2735  
2736 Turns debugging on or off. Statements are printed
2737 to the currently selected file handle (STDOUT by default).
2738 If you are already using this handle, you will have to
2739 arrange to select an output handle to a file of your own
2740 choosing, before call the C<add>, C<as_string> or C<re>)
2741 functions, otherwise it will scribble all over your
2742 carefully formatted output.
2743  
2744 =over 4
2745  
2746 =item * 0
2747  
2748 Off. Turns off all debugging output.
2749  
2750 =item * 1
2751  
2752 Add. Trace the addition of patterns.
2753  
2754 =item * 2
2755  
2756 Reduce. Trace the process of reduction and assembly.
2757  
2758 =item * 4
2759  
2760 Lex. Trace the lexing of the input patterns into its constituent
2761 tokens.
2762  
2763 =item * 8
2764  
2765 Time. Print to STDOUT the time taken to load all the patterns. This is
2766 nothing more than the difference between the time the object was
2767 instantiated and the time reduction was initiated.
2768  
2769 # load=<num>
2770  
2771 Any lengthy computation performed in the client code will be reflected
2772 in this value. Another line will be printed after reduction is
2773 complete.
2774  
2775 # reduce=<num>
2776  
2777 The above output lines will be changed to C<load-epoch> and
2778 C<reduce-epoch> if the internal state of the object is corrupted
2779 and the initial timestamp is lost.
2780  
2781 The code attempts to load L<Time::HiRes> in order to report fractional
2782 seconds. If this is not successful, the elapsed time is displayed
2783 in whole seconds.
2784  
2785 =back
2786  
2787 Values can be added (or or'ed together) to trace everything
2788  
2789 $r->debug(7)->add( '\\d+abc' );
2790  
2791 Calling C<debug> with no arguments turns debugging off.
2792  
2793 =head2 dump()
2794  
2795 Produces a synthetic view of the internal data structure. How
2796 to interpret the results is left as an exercise to the reader.
2797  
2798 print $r->dump;
2799  
2800 =head2 chomp(0|1)
2801  
2802 Turns chomping on or off.
2803  
2804 IMPORTANT: As of version 0.24, chomping is now on by default as it
2805 makes C<add_file> Just Work. The only time you may run into trouble
2806 is with C<add("\\$/")>. So don't do that, or else explicitly turn
2807 off chomping.
2808  
2809 To avoid incorporating (spurious)
2810 record separators (such as "\n" on Unix) when reading from a file,
2811 C<add()> C<chomp>s its input. If you don't want this to happen,
2812 call C<chomp> with a false value.
2813  
2814 $re->chomp(0); # really want the record separators
2815 $re->add(<DATA>);
2816  
2817 =head2 fold_meta_pairs(NUMBER)
2818  
2819 Determines whether C<\s>, C<\S> and C<\w>, C<\W> and C<\d>, C<\D>
2820 are folded into a C<.> (dot). Folding happens by default (for
2821 reasons of backwards compatibility, even though it is wrong when
2822 the C</s> expression modifier is active).
2823  
2824 Call this method with a false value to prevent this behaviour (which
2825 is only a problem when dealing with C<\n> if the C</s> expression
2826 modifier is also set).
2827  
2828 $re->add( '\\w', '\\W' );
2829 my $clone = $re->clone;
2830  
2831 $clone->fold_meta_pairs(0);
2832 print $clone->as_string; # prints '.'
2833 print $re->as_string; # print '[\W\w]'
2834  
2835 =head2 indent(NUMBER)
2836  
2837 Sets the level of indent for pretty-printing nested groups
2838 within a pattern. See the C<as_string> method for more details.
2839 When called without a parameter, no indenting is performed.
2840  
2841 $re->indent( 4 );
2842 print $re->as_string;
2843  
2844 =head2 lookahead(0|1)
2845  
2846 Turns on zero-width lookahead assertions. This is usually
2847 beneficial when you expect that the pattern will usually fail.
2848 If you expect that the pattern will usually match you will
2849 probably be worse off.
2850  
2851 =head2 flags(STRING)
2852  
2853 Sets the flags that govern how the pattern behaves (for
2854 versions of Perl up to 5.9 or so, these are C<imsx>). By
2855 default no flags are enabled.
2856  
2857 =head2 modifiers(STRING)
2858  
2859 An alias of the C<flags> method, for users familiar with
2860 C<Regexp::List>.
2861  
2862 =head2 track(0|1)
2863  
2864 Turns tracking on or off. When this attribute is enabled,
2865 additional housekeeping information is inserted into the
2866 assembled expression using C<({...}> embedded code
2867 constructs. This provides the necessary information to
2868 determine which, of the original patterns added, was the
2869 one that caused the match.
2870  
2871 $re->track( 1 );
2872 if( $target =~ /$re/ ) {
2873 print "$target matched by ", $re->matched, "\n";
2874 }
2875  
2876 Note that when this functionality is enabled, no
2877 reduction is performed and no character classes are
2878 generated. In other words, C<brag|tag> is not
2879 reduced down to C<(?:br|t)ag> and C<dig|dim> is not
2880 reduced to C<di[gm]>.
2881  
2882 =head2 unroll_plus(0|1)
2883  
2884 Turns the unrolling of plus metacharacters on or off. When
2885 a pattern is broken up, C<a+> becomes C<a>, C<a*> (and
2886 C<b+?> becomes C<b>, C<b*?>. This may allow the freed C<a>
2887 to assemble with other patterns. Not enabled by default.
2888  
2889 =head2 lex(SCALAR)
2890  
2891 Change the pattern used to break a string apart into tokens.
2892 You can examine the C<eg/naive> script as a starting point.
2893  
2894 =head2 reduce(0|1)
2895  
2896 Turns pattern reduction on or off. A reduced pattern may
2897 be considerably shorter than an unreduced pattern. Consider
2898 C</sl(?:ip|op|ap)/> I<versus> C</sl[aio]p/>. An unreduced
2899 pattern will be very similar to those produced by
2900 C<Regexp::Optimizer>. Reduction is on by default. Turning
2901 it off speeds assembly (but assembly is pretty fast -- it's
2902 the breaking up of the initial patterns in the lexing stage
2903 that can consume a non-negligible amount of time).
2904  
2905 =head2 mutable(0|1)
2906  
2907 This method has been marked as DEPRECATED. It will be removed
2908 in a future release. See the C<clone> method for a technique
2909 to replace its functionality.
2910  
2911 =head2 reset()
2912  
2913 Empties out the patterns that have been C<add>ed or C<insert>-ed
2914 into the object. Does not modify the state of controller attributes
2915 such as C<debug>, C<lex>, C<reduce> and the like.
2916  
2917 =head2 Default_Lexer
2918  
2919 B<Warning:> the C<Default_Lexer> function is a class method, not
2920 an object method. It is a fatal error to call it as an object
2921 method.
2922  
2923 The C<Default_Lexer> method lets you replace the default pattern
2924 used for all subsequently created C<Regexp::Assemble> objects. It
2925 will not have any effect on existing objects. (It is also possible
2926 to override the lexer pattern used on a per-object basis).
2927  
2928 The parameter should be an ordinary scalar, not a compiled
2929 pattern. If the pattern fails to match all parts of the string,
2930 the missing parts will be returned as single chunks. Therefore
2931 the following pattern is legal (albeit rather cork-brained):
2932  
2933 Regexp::Assemble::Default_Lexer( '\\d' );
2934  
2935 The above pattern will split up input strings digit by digit, and
2936 all non-digit characters as single chunks.
2937  
2938 =head1 DIAGNOSTICS
2939  
2940 "Cannot pass a C<refname> to Default_Lexer"
2941  
2942 You tried to replace the default lexer pattern with an object
2943 instead of a scalar. Solution: You probably tried to call
2944 C<< $obj->Default_Lexer >>. Call the qualified class method instead
2945 C<Regexp::Assemble::Default_Lexer>.
2946  
2947 "filter method not passed a coderef"
2948  
2949 "pre_filter method not passed a coderef"
2950  
2951 A reference to a subroutine (anonymous or otherwise) was expected.
2952 Solution: read the documentation for the C<filter> method.
2953  
2954 "duplicate pattern added: /.../"
2955  
2956 The C<dup_warn> attribute is active, and a duplicate pattern was
2957 added (well duh!). Solution: clean your data.
2958  
2959 "cannot open [file] for input: [reason]"
2960  
2961 The C<add_file> method was unable to open the specified file for
2962 whatever reason. Solution: make sure the file exists and the script
2963 has the required privileges to read it.
2964  
2965 =head1 NOTES
2966  
2967 This module has been tested successfully with a range of versions
2968 of perl, from 5.005_03 to 5.9.3. Use of 5.6.0 is not recommended.
2969  
2970 The expressions produced by this module can be used with the PCRE
2971 library.
2972  
2973 Remember to "double up" your backslashes if the patterns are
2974 hard-coded as constants in your program. That is, you should
2975 literally C<add('a\\d+b')> rather than C<add('a\d+b')>. It
2976 usually will work either way, but it's good practice to do so.
2977  
2978 Where possible, supply the simplest tokens possible. Don't add
2979 C<X(?-\d+){2})Y> when C<X-\d+-\d+Y> will do. The reason is that
2980 if you also add C<X\d+Z> the resulting assembly changes
2981 dramatically: C<X(?:(?:-\d+){2}Y|-\d+Z)> I<versus>
2982 C<X-\d+(?:-\d+Y|Z)>. Since R::A doesn't perform enough analysis,
2983 it won't "unroll" the C<{2}> quantifier, and will fail to notice
2984 the divergence after the first C<-d\d+>.
2985  
2986 Furthermore, when the string 'X-123000P' is matched against the
2987 first assembly, the regexp engine will have to backtrack over each
2988 alternation (the one that ends in Y B<and> the one that ends in Z)
2989 before determining that there is no match. No such backtracking
2990 occurs in the second pattern: as soon as the engine encounters the
2991 'P' in the target string, neither of the alternations at that point
2992 (C<-\d+Y> or C<Z>) could succeed and so the match fails.
2993  
2994 C<Regexp::Assemble> does, however, know how to build character
2995 classes. Given C<a-b>, C<axb> and C<a\db>, it will assemble these
2996 into C<a[-\dx]b>. When C<-> (dash) appears as a candidate for a
2997 character class it will be the first character in the class. When
2998 C<^> (circumflex) appears as a candidate for a character class it
2999 will be the last character in the class.
3000  
3001 It also knows about meta-characters than can "absorb" regular
3002 characters. For instance, given C<X\d> and C<X5>, it knows that
3003 C<5> can be represented by C<\d> and so the assembly is just C<X\d>.
3004 The "absorbent" meta-characters it deals with are C<.>, C<\d>, C<\s>
3005 and C<\W> and their complements. It will replace C<\d>/C<\D>,
3006 C<\s>/C<\S> and C<\w>/C<\W> by C<.> (dot), and it will drop C<\d>
3007 if C<\w> is also present (as will C<\D> in the presence of C<\W>).
3008  
3009 C<Regexp::Assemble> deals correctly with C<quotemeta>'s propensity
3010 to backslash many characters that have no need to be. Backslashes on
3011 non-metacharacters will be removed. Similarly, in character classes,
3012 a number of characters lose their magic and so no longer need to be
3013 backslashed within a character class. Two common examples are C<.>
3014 (dot) and C<$>. Such characters will lose their backslash.
3015  
3016 At the same time, it will also process C<\Q...\E> sequences. When
3017 such a sequence is encountered, the inner section is extracted and
3018 C<quotemeta> is applied to the section. The resulting quoted text
3019 is then used in place of the original unquoted text, and the C<\Q>
3020 and C<\E> metacharacters are thrown away. Similar processing occurs
3021 with the C<\U...\E> and C<\L...\E> sequences. This may have surprising
3022 effects when using a dispatch table. In this case, you will need
3023 to know exactly what the module makes of your input. Use the C<lexstr>
3024 method to find out what's going on:
3025  
3026 $pattern = join( '', @{$re->lexstr($pattern)} );
3027  
3028 If all the digits 0..9 appear in a character class, C<Regexp::Assemble>
3029 will replace them by C<\d>. I'd do it for letters as well, but
3030 thinking about accented characters and other glyphs hurts my head.
3031  
3032 In an alternation, the longest paths are chosen first (for example,
3033 C<horse|bird|dog>). When two paths have the same length, the path
3034 with the most subpaths will appear first. This aims to put the
3035 "busiest" paths to the front of the alternation. For example, the
3036 list C<bad>, C<bit>, C<few>, C<fig> and C<fun> will produce the
3037 pattern C<(?:f(?:ew|ig|un)|b(?:ad|it))>. See F<eg/tld> for a
3038 real-world example of how alternations are sorted. Once you have
3039 looked at that, everything should be crystal clear.
3040  
3041 When tracking is in use, no reduction is performed. nor are
3042 character classes formed. The reason is that it is
3043 too difficult to determine the original pattern afterwards. Consider the
3044 two patterns C<pale> and C<palm>. These should be reduced to
3045 C<pal[em]>. The final character matches one of two possibilities.
3046 To resolve whether it matched an C<'e'> or C<'m'> would require
3047 keeping track of the fact that the pattern finished up in a character
3048 class, which would the require a whole lot more work to figure out
3049 which character of the class matched. Without character classes
3050 it becomes much easier. Instead, C<pal(?:e|m)> is produced, which
3051 lets us find out more simply where we ended up.
3052  
3053 Similarly, C<dogfood> and C<seafood> should form C<(?:dog|sea)food>.
3054 When the pattern is being assembled, the tracking decision needs
3055 to be made at the end of the grouping, but the tail of the pattern
3056 has not yet been visited. Deferring things to make this work correctly
3057 is a vast hassle. In this case, the pattern becomes merely
3058 C<(?:dogfood|seafood>. Tracked patterns will therefore be bulkier than
3059 simple patterns.
3060  
3061 There is an open bug on this issue:
3062  
3063 L<http://rt.perl.org/rt3/Ticket/Display.html?id=32840>
3064  
3065 If this bug is ever resolved, tracking would become much easier to
3066 deal with (none of the C<match> hassle would be required - you could
3067 just match like a regular RE and it would Just Work).
3068  
3069 =head1 SEE ALSO
3070  
3071 =over 4
3072  
3073 =item L<perlre>
3074  
3075 General information about Perl's regular expressions.
3076  
3077 =item L<re>
3078  
3079 Specific information about C<use re 'eval'>.
3080  
3081 =item Regex::PreSuf
3082  
3083 C<Regex::PreSuf> takes a string and chops it itself into tokens of
3084 length 1. Since it can't deal with tokens of more than one character,
3085 it can't deal with meta-characters and thus no regular expressions.
3086 Which is the main reason why I wrote this module.
3087  
3088 =item Regexp::Optimizer
3089  
3090 C<Regexp::Optimizer> produces regular expressions that are similar to
3091 those produced by R::A with reductions switched off. It's biggest
3092 drawback is that it is exponentially slower than Regexp::Assemble on
3093 very large sets of patterns.
3094  
3095 =item Regexp::Parser
3096  
3097 Fine grained analysis of regular expressions.
3098  
3099 =item Regexp::Trie
3100  
3101 Funnily enough, this was my working name for C<Regexp::Assemble>
3102 during its development. I changed the name because I thought it
3103 was too obscure. Anyway, C<Regexp::Trie> does much the same as
3104 C<Regexp::Optimizer> and C<Regexp::Assemble> except that it runs
3105 much faster (according to the author). It does not recognise
3106 meta characters (that is, 'a+b' is interpreted as 'a\+b').
3107  
3108 =item Text::Trie
3109  
3110 C<Text::Trie> is well worth investigating. Tries can outperform very
3111 bushy (read: many alternations) patterns.
3112  
3113 =item Tree::Trie
3114  
3115 C<Tree::Trie> is another module that builds tries. The algorithm that
3116 C<Regexp::Assemble> uses appears to be quite similar to the
3117 algorithm described therein, except that C<R::A> solves its
3118 end-marker problem without having to rewrite the leaves.
3119  
3120 =back
3121  
3122 =head1 See Also
3123  
3124 For alternatives to this module, consider one of:
3125  
3126 =over 4
3127  
3128 =item o L<Data::Munge>
3129  
3130 =item o L<OnSearch::Regex>
3131  
3132 =item o L<Regex::PreSuf>
3133  
3134 =back
3135  
3136 =head1 LIMITATIONS
3137  
3138 Some mildly complex cases are not handled well. See examples/failure.01.pl
3139 and L<https://rt.cpan.org/Public/Bug/Display.html?id=104897>.
3140  
3141 See also L<https://rt.cpan.org/Public/Bug/Display.html?id=106480> for a discussion
3142 of some of the issues arising with the use of a huge number of alterations. Thanx to
3143 Slaven Rezic for the details of trie 'v' non-trie operations within Perl which influence
3144 regexp handling of alternations.
3145  
3146 <Regexp::Assemble> does not attempt to find common substrings. For
3147 instance, it will not collapse C</cabababc/> down to C</c(?:ab){3}c/>.
3148 If there's a module out there that performs this sort of string
3149 analysis I'd like to know about it. But keep in mind that the
3150 algorithms that do this are very expensive: quadratic or worse.
3151  
3152 C<Regexp::Assemble> does not interpret meta-character modifiers.
3153 For instance, if the following two patterns are
3154 given: C<X\d> and C<X\d+>, it will not determine that C<\d> can be
3155 matched by C<\d+>. Instead, it will produce C<X(?:\d|\d+)>. Along
3156 a similar line of reasoning, it will not determine that C<Z> and
3157 C<Z\d+> is equivalent to C<Z\d*> (It will produce C<Z(?:\d+)?>
3158 instead).
3159  
3160 You cannot remove a pattern that has been added to an object. You'll
3161 just have to start over again. Adding a pattern is difficult enough,
3162 I'd need a solid argument to convince me to add a C<remove> method.
3163 If you need to do this you should read the documentation for the
3164 C<clone> method.
3165  
3166 C<Regexp::Assemble> does not (yet)? employ the C<(?E<gt>...)>
3167 construct.
3168  
3169 The module does not produce POSIX-style regular expressions. This
3170 would be quite easy to add, if there was a demand for it.
3171  
3172 =head1 BUGS
3173  
3174 Patterns that generate look-ahead assertions sometimes produce
3175 incorrect patterns in certain obscure corner cases. If you
3176 suspect that this is occurring in your pattern, disable
3177 lookaheads.
3178  
3179 Tracking doesn't really work at all with 5.6.0. It works better
3180 in subsequent 5.6 releases. For maximum reliability, the use of
3181 a 5.8 release is strongly recommended. Tracking barely works with
3182 5.005_04. Of note, using C<\d>-style meta-characters invariably
3183 causes panics. Tracking really comes into its own in Perl 5.10.
3184  
3185 If you feed C<Regexp::Assemble> patterns with nested parentheses,
3186 there is a chance that the resulting pattern will be uncompilable
3187 due to mismatched parentheses (not enough closing parentheses). This
3188 is normal, so long as the default lexer pattern is used. If you want
3189 to find out which pattern among a list of 3000 patterns are to blame
3190 (speaking from experience here), the F<eg/debugging> script offers
3191 a strategy for pinpointing the pattern at fault. While you may not
3192 be able to use the script directly, the general approach is easy to
3193 implement.
3194  
3195 The algorithm used to assemble the regular expressions makes extensive
3196 use of mutually-recursive functions (that is, A calls B, B calls
3197 A, ...) For deeply similar expressions, it may be possible to provoke
3198 "Deep recursion" warnings.
3199  
3200 The module has been tested extensively, and has an extensive test
3201 suite (that achieves close to 100% statement coverage), but you
3202 never know... A bug may manifest itself in two ways: creating a
3203 pattern that cannot be compiled, such as C<a\(bc)>, or a pattern
3204 that compiles correctly but that either matches things it shouldn't,
3205 or doesn't match things it should. It is assumed that Such problems
3206 will occur when the reduction algorithm encounters some sort of
3207 edge case. A temporary work-around is to disable reductions:
3208  
3209 my $pattern = $assembler->reduce(0)->re;
3210  
3211 A discussion about implementation details and where bugs might lurk
3212 appears in the README file. If this file is not available locally,
3213 you should be able to find a copy on the Web at your nearest CPAN
3214 mirror.
3215  
3216 Seriously, though, a number of people have been using this module to
3217 create expressions anywhere from 140Kb to 600Kb in size, and it seems to
3218 be working according to spec. Thus, I don't think there are any serious
3219 bugs remaining.
3220  
3221 If you are feeling brave, extensive debugging traces are available to
3222 figure out where assembly goes wrong.
3223  
3224 Please report all bugs at
3225 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Regexp-Assemble>
3226  
3227 Make sure you include the output from the following two commands:
3228  
3229 perl -MRegexp::Assemble -le 'print $Regexp::Assemble::VERSION'
3230 perl -V
3231  
3232 There is a mailing list for the discussion of C<Regexp::Assemble>.
3233 Subscription details are available at
3234 L<http://listes.mongueurs.net/mailman/listinfo/regexp-assemble>.
3235  
3236 =head1 ACKNOWLEDGEMENTS
3237  
3238 This module grew out of work I did building access maps for Postfix,
3239 a modern SMTP mail transfer agent. See L<http://www.postfix.org/>
3240 for more information. I used Perl to build large regular expressions
3241 for blocking dynamic/residential IP addresses to cut down on spam
3242 and viruses. Once I had the code running for this, it was easy to
3243 start adding stuff to block really blatant spam subject lines, bogus
3244 HELO strings, spammer mailer-ids and more...
3245  
3246 I presented the work at the French Perl Workshop in 2004, and the
3247 thing most people asked was whether the underlying mechanism for
3248 assembling the REs was available as a module. At that time it was
3249 nothing more that a twisty maze of scripts, all different. The
3250 interest shown indicated that a module was called for. I'd like to
3251 thank the people who showed interest. Hey, it's going to make I<my>
3252 messy scripts smaller, in any case.
3253  
3254 Thomas Drugeon was a valuable sounding board for trying out
3255 early ideas. Jean Forget and Philippe Blayo looked over an early
3256 version. H.Merijn Brandt stopped over in Paris one evening, and
3257 discussed things over a few beers.
3258  
3259 Nicholas Clark pointed out that while what this module does
3260 (?:c|sh)ould be done in perl's core, as per the 2004 TODO, he
3261 encouraged me to continue with the development of this module. In
3262 any event, this module allows one to gauge the difficulty of
3263 undertaking the endeavour in C. I'd rather gouge my eyes out with
3264 a blunt pencil.
3265  
3266 Paul Johnson settled the question as to whether this module should
3267 live in the Regex:: namespace, or Regexp:: namespace. If you're
3268 not convinced, try running the following one-liner:
3269  
3270 perl -le 'print ref qr//'
3271  
3272 Philippe Bruhat found a couple of corner cases where this module
3273 could produce incorrect results. Such feedback is invaluable,
3274 and only improves the module's quality.
3275  
3276 =head1 Machine-Readable Change Log
3277  
3278 The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>.
3279  
3280 =head1 AUTHOR
3281  
3282 David Landgren
3283  
3284 Copyright (C) 2004-2011. All rights reserved.
3285  
3286 http://www.landgren.net/perl/
3287  
3288 If you use this module, I'd love to hear about what you're using
3289 it for. If you want to be informed of updates, send me a note.
3290  
3291 Ron Savage is co-maint of the module, starting with V 0.36.
3292  
3293 =head1 Repository
3294  
3295 L<https://github.com/ronsavage/Regexp-Assemble.git>
3296  
3297 =head1 TODO
3298  
3299 1. Tree equivalencies. Currently, /contend/ /content/ /resend/ /resent/
3300 produces (?:conten[dt]|resend[dt]) but it is possible to produce
3301 (?:cont|res)en[dt] if one can spot the common tail nodes (and walk back
3302 the equivalent paths). Or be by me my => /[bm][ey]/ in the simplest case.
3303  
3304 To do this requires a certain amount of restructuring of the code.
3305 Currently, the algorithm uses a two-phase approach. In the first
3306 phase, the trie is traversed and reductions are performed. In the
3307 second phase, the reduced trie is traversed and the pattern is
3308 emitted.
3309  
3310 What has to occur is that the reduction and emission have to occur
3311 together. As a node is completed, it is replaced by its string
3312 representation. This then allows child nodes to be compared for
3313 equality with a simple 'eq'. Since there is only a single traversal,
3314 the overall generation time might drop, even though the context
3315 baggage required to delve through the tree will be more expensive
3316 to carry along (a hash rather than a couple of scalars).
3317  
3318 Actually, a simpler approach is to take on a secret sentinel
3319 atom at the end of every pattern, which gives the reduction
3320 algorithm sufficient traction to create a perfect trie.
3321  
3322 I'm rewriting the reduction code using this technique.
3323  
3324 2. Investigate how (?>foo) works. Can it be applied?
3325  
3326 5. How can a tracked pattern be serialised? (Add freeze and thaw methods).
3327  
3328 6. Store callbacks per tracked pattern.
3329  
3330 12. utf-8... hmmmm...
3331  
3332 14. Adding qr//'ed patterns. For example, consider
3333 $r->add ( qr/^abc/i )
3334 ->add( qr/^abd/ )
3335 ->add( qr/^ab e/x );
3336 this should admit abc abC aBc aBC abd abe as matches
3337  
3338 16. Allow a fast, unsafe tracking mode, that can be used if a(?bc)?
3339 can't happen. (Possibly carp if it does appear during traversal)?
3340  
3341 17. given a-\d+-\d+-\d+-\d+-b, produce a(?:-\d+){4}-b. Something
3342 along the lines of (.{4))(\1+) would let the regexp engine
3343 itself be brought to bear on the matter, which is a rather
3344 appealing idea. Consider
3345  
3346 while(/(?!\+)(\S{2,}?)(\1+)/g) { ... $1, $2 ... }
3347  
3348 as a starting point.
3349  
3350 19. The reduction code has become unbelievably baroque. Adding code
3351 to handle (sting,singing,sing) => s(?:(?:ing)?|t)ing was far
3352 too difficult. Adding more stuff just breaks existing behaviour.
3353 And fixing the ^abcd$ ... bug broke stuff all over again.
3354 Now that the corner cases are more clearly identified, a full
3355 rewrite of the reduction code is needed. And would admit the
3356 possibility of implementing items 1 and 17.
3357  
3358 20. Handle debug unrev with a separate bit
3359  
3360 23. Japhy's http://www.perlmonks.org/index.pl?node_id=90876 list2range
3361 regexp
3362  
3363 24. Lookahead assertions contain serious bugs (as shown by
3364 assembling powersets. Need to save more context during reduction,
3365 which in turn will simplify the preparation of the lookahead
3366 classes. See also 19.
3367  
3368 26. _lex() swamps the overall run-time. It stems from the decision
3369 to use a single regexp to pull apart any pattern. A suite of
3370 simpler regexp to pick of parens, char classes, quantifiers
3371 and bare tokens may be faster. (This has been implemented as
3372 _fastlex(), but it's only marginally faster. Perhaps split-by-
3373 char and lex a la C?
3374  
3375 27. We don't, as yet, unroll_plus a paren e.g. (abc)+?
3376  
3377 28. We don't reroll unrolled a a* to a+ in indented or tracked
3378 output
3379  
3380 29. Use (*MARK n) in blead for tracked patterns, and use (*FAIL) for
3381 the unmatchable pattern.
3382  
3383 =head1 LICENSE
3384  
3385 This library is free software; you can redistribute it and/or modify
3386 it under the same terms as Perl itself.
3387  
3388 =cut
3389  
3390 # Return a +ve value to tell Perl the module is ready to go.
3391  
3392 11 'The Lusty Decadent Delights of Imperial Pompeii';
↑ Summary