| 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
| |||||||||||||
| 46 | 0 | my @c; | |||||||||||
| 47 | 0 | ▶ | push @c, $1 if defined $1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | 0 | ▶ | push @c, $2 if defined $2; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | 0 | ▶ | push @c, $3 if defined $3; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | 0 | ▶ | push @c, $4 if defined $4; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | 0 | ▶ | push @c, $5 if defined $5; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | 0 | ▶ | push @c, $6 if defined $6; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | 0 | ▶ | push @c, $7 if defined $7; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | 0 | ▶ | push @c, $8 if defined $8; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 55 | 0 | ▶ | push @c, $9 if defined $9; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | 0 | ▶ | die "can't handle more than 9 captures" if defined $10; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 66 | 3 | ▶ | die "no capture" unless defined $1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | 3 | push @res, $1; | |||||||||||
| 68 | 3 | ▶ | die "got more than 1 capture" if defined $2; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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
MC/DC0%0/1
pos $str // 0
pos $str
Truth table0%0/2
A
A= pos $str
| |||||||||||||
| 88 | 0 | ▶ | while ($str =~ m{\G$re}gc) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | 0 | $pos = pos($str); | |||||||||||
| 90 | 0 | ▶ | die "no capture" unless defined $1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | 0 | push @res, $1; | |||||||||||
| 92 | 0 | ▶ | die "got more than 1 capture" if defined $2; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 93 | } | ||||||||||||
| 94 | 0 | ▶ | wantarray ? (\@res, $pos) : \@res ## no critic | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
MC/DC0%0/1
pos $str // 0
pos $str
Truth table50%1/2
A
A= pos $str
| |||||||||||||
| 103 | 2 | ▶ | while ($str =~ m{\G($re)}gc) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | 3 | $pos = pos($str); | |||||||||||
| 105 | 3 | push @res, $1; | |||||||||||
| 106 | } | ||||||||||||
| 107 | 2 | ▶ | wantarray ? (\@res, $pos) : \@res ## no critic | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 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 | |||||||||||