lib/FP/RegexMatch.pm

filter: Statementstmt 63.6% 49 / 77 Branchbran 20.4% 9 / 44 Conditioncond 25.0% 1 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 77.7% 14 / 18 Podpod 0.0% 0 / 6 totaltotal 48.3% 73 / 151 CC 25 SCAR 39.5
CC
25
Cov
47%
all_matches
156.0
all_continuous_matches1
42.0
__ANON__
6.0
CRAP 52.0
« lib/PXML/Serialize.pm ↑ Summary lib/Chj/Package/OfPath.pm »
1 #
2 # Copyright (c) 2021 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FP::RegexMatch
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18  
19 =cut
20  
21 package FP::RegexMatch;
22 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 our @EXPORT = qw();
29 1 our @EXPORT_OK = qw(
30 all_matches
31 all_matches1
32 all_matches_whole
33 all_continuous_matches1
34 all_continuous_matches_whole
35 fullmatching
36 );
37 1 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
38  
39 1 ▶use Chj::TEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use FP::Show;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 # up to 9 captures in $re, bundled in arrays
43 0 ▶sub all_matches ($str, $re) {
Subroutine all_matches: not called CC=12 SCAR=50.5
Pod undocumented
44 0 my @res;
45 0 ▶ while ($str =~ m{$re}gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /$re/cg) 0 0 0
46 0 my @c;
47 0 ▶ push @c, $1 if defined $1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $1 0 0 0
48 0 ▶ push @c, $2 if defined $2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 0 0 0
49 0 ▶ push @c, $3 if defined $3;
Branch: 1 branch
BranchTrueFalseTotal
if defined $3 0 0 0
50 0 ▶ push @c, $4 if defined $4;
Branch: 1 branch
BranchTrueFalseTotal
if defined $4 0 0 0
51 0 ▶ push @c, $5 if defined $5;
Branch: 1 branch
BranchTrueFalseTotal
if defined $5 0 0 0
52 0 ▶ push @c, $6 if defined $6;
Branch: 1 branch
BranchTrueFalseTotal
if defined $6 0 0 0
53 0 ▶ push @c, $7 if defined $7;
Branch: 1 branch
BranchTrueFalseTotal
if defined $7 0 0 0
54 0 ▶ push @c, $8 if defined $8;
Branch: 1 branch
BranchTrueFalseTotal
if defined $8 0 0 0
55 0 ▶ push @c, $9 if defined $9;
Branch: 1 branch
BranchTrueFalseTotal
if defined $9 0 0 0
56 0 ▶ die "can't handle more than 9 captures" if defined $10;
Branch: 1 branch
BranchTrueFalseTotal
if defined $10 0 0 0
57 0 push @res, \@c;
58 }
59 \@res
60 0 }
61  
62 # only 1 capture in $re
63 1 ▶sub all_matches1 ($str, $re) {
Subroutine all_matches1: called CC=4 SCAR=14.0
Pod undocumented
64 1 my @res;
65 1 ▶ while ($str =~ m{$re}gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /$re/cg) 3 1 4
66 3 ▶ die "no capture" unless defined $1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $1 0 3 3
67 3 push @res, $1;
68 3 ▶ die "got more than 1 capture" if defined $2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 0 3 3
69 }
70 \@res
71 1 }
72  
73 # captures the whole string of all matches of $re
74 1 ▶sub all_matches_whole ($str, $re) {
Subroutine all_matches_whole: called CC=2 SCAR=6.9
Pod undocumented
75 1 my @res;
76 1 ▶ while ($str =~ m{($re)}gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /($re)/cg) 3 1 4
77 3 push @res, $1;
78 }
79 \@res
80 1 }
81  
82 # The list of (the single capture all matches of $re), requiring them
83 # to follow each other continuously from the current pos. Returns the
84 # pos of the remainder after.
85 0 ▶sub all_continuous_matches1 ($str, $re) {
Subroutine all_continuous_matches1: not called CC=6 SCAR=37.4
Pod undocumented
86 0 my @res;
87 0 ▶ my $pos = pos($str) // 0; # is it correct that undef pos means 0 ?
Condition0%0/2
pos $str // 0
l!l
00
MC/DC0%0/1
pos $str // 0
pos $str
Truth table0%0/2
A
A= pos $str
Aresult
11
00
88 0 ▶ while ($str =~ m{\G$re}gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G$re/cg) 0 0 0
89 0 $pos = pos($str);
90 0 ▶ die "no capture" unless defined $1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $1 0 0 0
91 0 push @res, $1;
92 0 ▶ die "got more than 1 capture" if defined $2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 0 0 0
93 }
94 0 ▶ wantarray ? (\@res, $pos) : \@res ## no critic
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
95 }
96  
97 # captures the whole string of all matches of $re, requiring them to
98 # follow each other continuously from the current pos. Returns the pos
99 # of the remainder after.
100 2 ▶sub all_continuous_matches_whole ($str, $re) {
Subroutine all_continuous_matches_whole: called CC=4 SCAR=14.0
Pod undocumented
101 2 my @res;
102 2 ▶ my $pos = pos($str) // 0; # is it correct that undef pos means 0 ?
Condition50%1/2
pos $str // 0
l!l
02
MC/DC0%0/1
pos $str // 0
pos $str
Truth table50%1/2
A
A= pos $str
Aresult
11
00
103 2 ▶ while ($str =~ m{\G($re)}gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G($re)/cg) 3 2 5
104 3 $pos = pos($str);
105 3 push @res, $1;
106 }
107 2 ▶ wantarray ? (\@res, $pos) : \@res ## no critic
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
108 }
109  
110 # ^ XX ach pos 0 auto since str is COPIED MAN KRST
111  
112 0 ▶sub fullmatching ($fn) {
Subroutine fullmatching: not called CC=1 SCAR=6.9
Pod undocumented
113 0 ▶ sub ($str, $re) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
114 0 my ($results, $restpos) = $fn->($str, $re);
115 0 my $rem = substr($str, $restpos);
116 0 ▶ $rem =~ /^\s*\z/ or die "non-matching remainder: " . show($rem);
Branch: 1 branch
BranchTrueFalseTotal
$rem =~ /^\s*\z/ or die 'non-matching remainder: ' . show($rem) 0 0 0
117 0 $results
118 }
119 0 }
120  
121 1 ▶TEST { all_matches1 "foo barO", qr/(o)/i } ['o', 'o', 'O'];
Subroutine __ANON__: called CC=1 SCAR=0.0
122 1 ▶TEST { all_matches_whole "foo barO", qr/o/i } ['o', 'o', 'O'];
Subroutine __ANON__: called CC=1 SCAR=0.0
123 1 ▶TEST { [all_continuous_matches_whole "oOo barO", qr/o/i] } [['o', 'O', 'o'], 3];
Subroutine __ANON__: called CC=1 SCAR=0.0
124 1 ▶TEST { [all_continuous_matches_whole "BoOo barO", qr/o/i] } [[], 0];
Subroutine __ANON__: called CC=1 SCAR=0.0
125  
126 1 1
« lib/PXML/Serialize.pm ↑ Summary lib/Chj/Package/OfPath.pm »