Common/balanced.pm

filter: Statementstmt 100% 59 / 59 Branchbran 100% 10 / 10 Conditioncond 66.6% 6 / 9 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 92.2% 83 / 90 CC 10 SCAR 14.2
CC
10
Cov
96%
nested
6.0
__ANON__
5.1
BEGIN
1.0
CRAP 4.2
« Common/delimited.pm ↑ Summary Common/net.pm »
1 package Regexp::Common::balanced; {
2  
3 71 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 71 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 71 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 71 ▶no warnings 'syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 71 ▶use Regexp::Common qw /pattern clean no_defaults/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 71 our $VERSION = '2024080801';
12  
13 71 my %closer = ( '{'=>'}', '('=>')', '['=>']', '<'=>'>' );
14 71 my %cache;
15  
16 17 ▶sub nested {
Subroutine nested: called CC=6 SCAR=17.9
Pod undocumented
17 17 my ($start, $finish) = @_;
18  
19 17 ▶ return $cache {$start} {$finish} if exists $cache {$start} {$finish};
Branch: 1 branch
BranchTrueFalseTotal
if exists $cache{$start}{$finish} 5 12 17
20  
21 12 my @starts = map {s/\\(.)/$1/g; $_} grep {length}
22 $start =~ /([^|\\]+|\\.)+/gs;
23 12 my @finishes = map {s/\\(.)/$1/g; $_} grep {length}
24 $finish =~ /([^|\\]+|\\.)+/gs;
25  
26 12 push @finishes => ($finishes [-1]) x (@starts - @finishes);
27  
28 12 my @re;
29 12 local $" = "|";
30 12 foreach my $begin (@starts) {
31 15 my $end = shift @finishes;
32  
33 15 my $qb = quotemeta $begin;
34 15 my $qe = quotemeta $end;
35 15 my $fb = quotemeta substr $begin => 0, 1;
36 15 my $fe = quotemeta substr $end => 0, 1;
37  
38 15 my $tb = quotemeta substr $begin => 1;
39 15 my $te = quotemeta substr $end => 1;
40  
41 15 my $add;
42 15 ▶ if ($fb eq $fe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fb eq $fe) { } 1 14 15
43 1 push @re =>
44 qq /(?:$qb(?:(?>[^$fb]+)|$fb(?!$tb)(?!$te)|(?-1))*$qe)/;
45 }
46 else {
47 14 my @clauses = "(?>[^$fb$fe]+)";
48 14 ▶ push @clauses => "$fb(?!$tb)" if length $tb;
Branch: 1 branch
BranchTrueFalseTotal
if length $tb 1 13 14
49 14 ▶ push @clauses => "$fe(?!$te)" if length $te;
Branch: 1 branch
BranchTrueFalseTotal
if length $te 1 13 14
50 14 push @clauses => "(?-1)";
51 14 push @re => qq /(?:$qb(?:@clauses)*$qe)/;
52 }
53 }
54  
55 12 $cache {$start} {$finish} = qr /(@re)/;
56 }
57  
58  
59 pattern name => [qw /balanced -parens=() -begin= -end=/],
60 17 ▶ create => sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
61 17 my $flag = $_[1];
62 17 ▶ unless (defined $flag -> {-begin} && length $flag -> {-begin} &&
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $flag->{"-begin"} and length $flag->{"-begin"} and defined $flag->{"-end"} and length $flag->{"-end"}) 9 8 17
Condition66%6/9
defined $flag->{"-begin"} and length $flag->{"-begin"} and defined $flag->{"-end"} and length $flag->{"-end"}
!ll&&!rl&&r
908
defined $flag->{"-begin"} and length $flag->{"-begin"} and defined $flag->{"-end"}
!ll&&!rl&&r
908
defined $flag->{"-begin"} and length $flag->{"-begin"}
!ll&&!rl&&r
098
MC/DC25%1/4
defined $flag->{"-begin"} and length $flag->{"-begin"} and defined $flag->{"-end"} and length $flag->{"-end"}
defined $flag->{"-begin"} length $flag->{"-begin"} defined $flag->{"-end"} length $flag->{"-end"}
Truth table40%2/5
A and B and C and D
A= defined $flag->{"-begin"}B= length $flag->{"-begin"}C= defined $flag->{"-end"}D= length $flag->{"-end"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
63 defined $flag -> {-end} && length $flag -> {-end}) {
64 9 my @open = grep {index ($flag->{-parens}, $_) >= 0}
65 ('[','(','{','<');
66 9 my @close = map {$closer {$_}} @open;
67 9 $flag -> {-begin} = join "|" => @open;
68 9 $flag -> {-end} = join "|" => @close;
69 }
70 17 return nested @$flag {qw /-begin -end/};
71 },
72 71 ;
73  
74 }
75  
76 71 1;
77  
78 __END__
« Common/delimited.pm ↑ Summary Common/net.pm »