blib/lib/Regexp/CharClasses/Helper.pm

filter: Statementstmt 100% 34 / 34 Branchbran 92.8% 13 / 14 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 95.5% 64 / 67 CC 10 SCAR 13.8
CC
10
Cov
96%
_parse
6.1
fmt
5.0
BEGIN
1.0
CRAP 4.0
↑ Summary
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use v5.15.7;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package Regexp::CharClasses::Helper;
5  
6 # ABSTRACT: Helper for creating user defined character class strings
7 3 our $VERSION = '0.005'; # VERSION
8  
9 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use charnames ':full';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =pod
14  
15 =encoding utf8
16  
17 =head1 NAME
18  
19 Regexp::CharClasses::Helper - User defined character class strings by unicode name or literals
20  
21  
22 =head1 SYNOPSIS
23  
24 use Regexp::CharClasses::Helper;
25  
26 sub IsAorPlus {
27 return Regexp::CharClasses::Helper::fmt(
28 'A',
29 'LATIN SMALL LETTER A'
30 '+',
31 );
32 }
33 say 'matches' if 'A+' =~ /^\p{IsAorPlus}+$/;
34  
35 sub InCapitalsButNot42 {
36 return Regexp::CharClasses::Helper::fmt(
37 "+A\tZ", # from A till Z
38 "-\x42" # except \x42
39 );
40 }
41 say "doesn't" if 'ABC' =~ /\p{InCapitalsButNot42}+/;
42
43  
44 =head1 METHODS AND ARGUMENTS
45  
46 =over 4
47  
48 =cut
49  
50 1101 ▶sub _parse {
Subroutine _parse: called CC=6 SCAR=18.0
51 1101 ▶ my $in = $_[0] || $_;
Condition66%2/3
$_[0] || $_
l!l&&r!l&&!r
9940107
MC/DC50%1/2
$_[0] || $_
$_[0] $_
Truth table66%2/3
A || B
A= $_[0]B= $_
ABresult
1X1
011
000
52 1101 ▶ croak "Unknown charname '$in'" if $in =~ /^U\+/i;
Branch: 1 branch
BranchTrueFalseTotal
if $in =~ /^U\+/iu 1 1100 1101
53 1100 ▶ return $in if $in =~ /In|Is|::/;
Branch: 1 branch
BranchTrueFalseTotal
if $in =~ /In|Is|::/u 0 1100 1100
54 1100 ▶ my $code = length $in == 1 ? ord $in
Branch: 1 branch
BranchTrueFalseTotal
length $in == 1 ? : 971 129 1100
55 : charnames::vianame $in;
56 1100 ▶ croak "Unknown charname '$in'" unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 22 1078 1100
57 1078 return sprintf '%04x', $code;
58 }
59  
60 =item fmt()
61  
62 Takes in a list and turns it into the format specified by L<User-Defined Character Properties|http://perldoc.perl.org/perlunicode.html#User-Defined-Character-Properties>
63  
64 =cut
65  
66 580 ▶sub fmt {
Subroutine fmt: called CC=5 SCAR=16.1
Pod documented
67 return join '', map {
68 580 ▶ croak 'undef unexpected' unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 2 594 596
69 594 my ($prefix, @out) = ('', $_);
70 594 ▶ if (/^[-+!&]/ && !/^[-+!&]($|\t[^\t]|\t\t$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^[-+!&]/u and not /^[-+!&]($|\t[^\t]|\t\t$)/u) 452 142 594
Condition100%3/3
/^[-+!&]/u and not /^[-+!&]($|\t[^\t]|\t\t$)/u
!ll&&!rl&&r
12913452
MC/DC100%2/2
/^[-+!&]/u and not /^[-+!&]($|\t[^\t]|\t\t$)/u
/^[-+!&]/u not /^[-+!&]($|\t[^\t]|\t\t$)/u
Truth table100%3/3
A and not(B)
A= /^[-+!&]/uB= not /^[-+!&]($|\t[^\t]|\t\t$)/u
ABresult
0X0
100
111
71 452 $out[0] =~ s/^([-+!&])//;
72 452 $prefix = $1;
73 }
74 594 ▶ @out = ($1, $2) if $out[0] =~ /^(.+)\t(.+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $out[0] =~ /^(.+)\t(.+)$/u 510 84 594
75 594 $prefix . join("\t", map {_parse $_} @out) . "\n";
76 } @_;
77 }
78  
79 3 1;
80 __END__
↑ Summary