lib/App/Greple/Pattern.pm

filter: Statementstmt 82.1% 78 / 95 Branchbran 53.8% 14 / 26 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 92.8% 26 / 28 Podpod 0.0% 0 / 21 totaltotal 68.7% 119 / 173 CC 15 SCAR 13.7
CC
15
Cov
76%
setup
10.1
cook_pattern
9.7
wstr
2.0
CRAP 4.0
« lib/App/Greple/perl.pm ↑ Summary lib/App/Greple.pm »
1 package App::Greple::Pattern;
2  
3 171 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 171 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 171 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 171 eval { warnings->unimport('experimental::regex_sets') };
7 171 eval { warnings->unimport('experimental::vlb') };
8 }
9 171 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 171 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 171 our @EXPORT = ();
13 171 our %EXPORT_TAGS = ();
14 171 our @EXPORT_OK = qw();
15  
16 171 ▶use Getopt::EX::Func qw(parse_func);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 ##
19 ## Flags
20 ##
21 171 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 171 FLAG_NONE => 0,
23 FLAG_NEGATIVE => 1,
24 FLAG_REQUIRED => 2,
25 FLAG_OPTIONAL => 4,
26 FLAG_REGEX => 8,
27 FLAG_IGNORECASE => 16,
28 FLAG_COOK => 32,
29 FLAG_OR => 64,
30 FLAG_LEXICAL => 128,
31 FLAG_FUNCTION => 256,
32 171 };
33 171 push @EXPORT, qw(
34 FLAG_NONE
35 FLAG_NEGATIVE
36 FLAG_REQUIRED
37 FLAG_OPTIONAL
38 FLAG_REGEX
39 FLAG_IGNORECASE
40 FLAG_COOK
41 FLAG_OR
42 FLAG_LEXICAL
43 FLAG_FUNCTION
44 );
45  
46 272 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
47 272 my $class = shift;
48 272 my $obj = bless {
49 STRING => undef,
50 COOKED => undef,
51 FLAG => FLAG_NONE,
52 REGEX => undef,
53 CATEGORY => undef,
54 FUNCTION => undef,
55 }, $class;
56  
57 272 ▶ $obj->setup(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 272 0 272
58  
59 270 $obj;
60 }
61  
62 272 ▶sub setup {
Subroutine setup: called CC=7 SCAR=23.2
Pod undocumented
63 272 my $obj = shift;
64 272 my $target = shift;
65 272 my %opt = @_;
66  
67 272 ▶ $obj->flag($opt{flag} // FLAG_NONE);
Condition50%1/2
$opt{"flag"} // 0
l!l
2720
MC/DC0%0/1
$opt{"flag"} // 0
$opt{"flag"}
Truth table50%1/2
A
A= $opt{"flag"}
Aresult
11
00
68  
69 272 ▶ if ($obj->is_function) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->is_function) { } 0 272 272
70 0 ▶ if ($target->can('call')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target->can('call')) { } 0 0 0
71 0 $obj->string('*FUNCTION');
72 0 $obj->cooked('*FUNCTION');
73 0 $obj->function($target);
74 } else {
75 0 $obj->string($target);
76 0 $obj->cooked($target);
77 0 $obj->function(parse_func({ PACKAGE => 'main' }, $target));
78 }
79 } else {
80 272 $obj->string($target);
81 272 ▶ $obj->cooked($obj->is_multiline
Branch: 1 branch
BranchTrueFalseTotal
$obj->is_multiline ? : 222 50 272
82 ? cook_pattern($target, flag => $obj->flag)
83 : $target);
84 $obj->regex(
85 272 do {
86 272 ▶ my $p = $obj->is_regex ? $obj->cooked : quotemeta($obj->cooked);
Branch: 1 branch
BranchTrueFalseTotal
$obj->is_regex ? : 272 0 272
87 272 ▶ $obj->is_ignorecase ? qr/$p/mi : qr/$p/m;
Branch: 1 branch
BranchTrueFalseTotal
$obj->is_ignorecase ? : 1 271 272
88 } );
89 }
90  
91 270 $obj;
92 }
93  
94 4985 ▶sub field : lvalue {
Subroutine field: called CC=2 SCAR=6.9
Pod undocumented
95 4985 my $obj = shift;
96 4985 my $name = shift;
97 4985 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1091 3894 4985
98 1091 $obj->{$name} = shift;
99 1091 $obj;
100 } else {
101 3894 $obj->{$name};
102 }
103 }
104  
105 3633 ▶sub flag : lvalue { shift->field ( FLAG => @_ ) }
Subroutine flag: called CC=1 SCAR=0.0
Pod undocumented
106 272 ▶sub string : lvalue { shift->field ( STRING => @_ ) }
Subroutine string: called CC=1 SCAR=0.0
Pod undocumented
107 584 ▶sub cooked : lvalue { shift->field ( COOKED => @_ ) }
Subroutine cooked: called CC=1 SCAR=0.0
Pod undocumented
108 495 ▶sub regex : lvalue { shift->field ( REGEX => @_ ) }
Subroutine regex: called CC=1 SCAR=0.0
Pod undocumented
109 0 ▶sub category : lvalue { shift->field ( CATEGORY => @_ ) }
Subroutine category: not called CC=1 SCAR=6.9
Pod undocumented
110 0 ▶sub function : lvalue { shift->field ( FUNCTION => @_ ) }
Subroutine function: not called CC=1 SCAR=6.9
Pod undocumented
111 1 ▶sub group_count : lvalue { shift->field ( GROUP_COUNT => @_ ) }
Subroutine group_count: called CC=1 SCAR=0.0
Pod undocumented
112  
113 668 ▶sub is_positive { !($_[0]->flag & FLAG_NEGATIVE) };
Subroutine is_positive: called CC=1 SCAR=0.0
Pod undocumented
114 427 ▶sub is_negative { $_[0]->flag & FLAG_NEGATIVE };
Subroutine is_negative: called CC=1 SCAR=0.0
Pod undocumented
115 455 ▶sub is_required { $_[0]->flag & FLAG_REQUIRED };
Subroutine is_required: called CC=1 SCAR=0.0
Pod undocumented
116 234 ▶sub is_optional { $_[0]->flag & FLAG_OPTIONAL };
Subroutine is_optional: called CC=1 SCAR=0.0
Pod undocumented
117 272 ▶sub is_regex { $_[0]->flag & FLAG_REGEX };
Subroutine is_regex: called CC=1 SCAR=0.0
Pod undocumented
118 272 ▶sub is_ignorecase { $_[0]->flag & FLAG_IGNORECASE };
Subroutine is_ignorecase: called CC=1 SCAR=0.0
Pod undocumented
119 272 ▶sub is_multiline { $_[0]->flag & FLAG_COOK };
Subroutine is_multiline: called CC=1 SCAR=0.0
Pod undocumented
120 529 ▶sub is_function { $_[0]->flag & FLAG_FUNCTION };
Subroutine is_function: called CC=1 SCAR=0.0
Pod undocumented
121  
122 171 ▶sub IsWide {
Subroutine IsWide: called CC=1 SCAR=0.0
Pod undocumented
123 171 return <<'END';
124 +utf8::East_Asian_Width=Wide
125 +utf8::East_Asian_Width=FullWidth
126 END
127 }
128  
129 171 my $wclass_re = qr{ \[ \p{IsWide}+ (?: \- \p{IsWide}+ )* \] }x;
130 171 my $wstr_re = qr{ (?: \p{IsWide} | $wclass_re )+ }x;
131  
132 44 ▶sub wstr {
Subroutine wstr: called CC=2 SCAR=7.1
Pod undocumented
133 44 local $_ = shift;
134 44 ▶ my @wchars = m{ \G ( $wclass_re | \X ) }gx or die;
Branch: 1 branch
BranchTrueFalseTotal
my(@wchars) = / \G ( $wclass_re | \X ) /gux or die 44 0 44
135 44 join '\\s*', @wchars;
136 }
137  
138 222 ▶sub cook_pattern {
Subroutine cook_pattern: called CC=6 SCAR=22.7
Pod undocumented
139 222 my $p = shift;
140 222 my %opt = @_;
141  
142 222 ▶ if ($p =~ s/^\\Q//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p =~ s/^\\Q//u) 0 222 222
143 0 return quotemeta($p);
144 }
145  
146 COOK:
147 {
148 222 $p =~ s{
149 (?<match>
150 (?<class>
151 \[[^\]]*\] [\?\*]? # character-class
152 )
153 |
154 (?<ahead>
155 \(\?[=!][^\)]*\) # look-ahead pattern
156 )
157 |
158 (?<behind>
159 \(\?\<[=!][^\)]*\) # look-behind pattern
160 )
161 |
162 (?<wstr> $wstr_re)
163 |
164 (?<else> [A-Z0-9_]+ | . )
165 )
166 }{
167 843 ▶ if (defined $+{ahead}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $+{'ahead'}) { } 0 843 843
elsif (defined $+{'wstr'}) { } 44 799 843
168 $+{match}
169 0 =~ s{\A \( \? [=!] \K
170 ( (?: $wstr_re | \| )+ )
171 }{
172 0 join '|', (map { wstr($_) } split /\|/, $1);
173 }erx;
174 } elsif (defined $+{wstr}) {
175 44 wstr($+{match});
176 } else {
177 799 $+{match};
178 }
179 }egx;
180  
181 # ( [
182 222 $p =~ s/\p{IsWide} \K (?= [\(\[] )/\\s*+/gx;
183  
184 # ) ]
185 222 $p =~ s{
186 (# look-behind ending wchar
187 \(\?<[=!][^\)]*\p{IsWide}\) (?! [|] | $ )
188 )
189 |
190 (# skip look-ahead/behind
191 \(\?<?[=!][^\)]*\)
192 )
193 |
194 (# whcar before ) or ]
195 \p{IsWide} [\)\]]+ [?]?+ (?! \\s\* | [|] | $ )
196 )
197 }{
198 0 ▶ if (defined $1) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $1) { } 0 0 0
elsif (defined $2) { } 0 0 0
199 0 $1 . "\\s*+";
200 } elsif (defined $2) {
201 0 $2;
202 } else {
203 0 $3 . "\\s*+";
204 }
205 }egx;
206  
207 # convert space not preceded by \ to \s+,
208 # removing \s* and \s*+ arround it
209 222 $p =~ s{
210 (?: \Q\s*\E \+?+ )*
211 (?: (?<!\\) [ ] )+
212 (?: \Q\s*\E \+?+ )*
213 }{\\s+}gx;
214 }
215  
216 222 $p;
217 }
218  
219 171 1;
« lib/App/Greple/perl.pm ↑ Summary lib/App/Greple.pm »