Data/Charset.pm

filter: Statementstmt 79.9% 335 / 419 Branchbran 73.3% 204 / 278 Conditioncond 57.7% 78 / 135 MC/DCmcdc 37.0% 30 / 81 Subroutinesub 75.8% 44 / 58 Podpod 100% 16 / 16 totaltotal 71.6% 707 / 987 CC 205 SCAR 31.5
CC
205
Cov
74%
new
104.3
_parse
45.2
generate
29.0
CRAP 23.3
« Data/Regex.pm ↑ Summary Data/Plugin/Text.pm »
1 package Mock::Data::Charset;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use Mock::Data::Util qw( _parse_context _escape_str );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 require Carp;
6 12 our @CARP_NOT= ('Mock::Data::Util');
7 12 require Mock::Data::Generator;
8 12 our @ISA= ( 'Mock::Data::Generator' );
9  
10 # ABSTRACT: Generator of strings from a set of characters
11 12 our $VERSION = '0.04'; # VERSION
12  
13  
14 12 our @generator_attrs= qw( str_len min_codepoint max_codepoint );
15  
16 52 ▶sub new {
Subroutine new: called CC=28 SCAR=46.5
Pod documented
17 52 my $class= shift;
18 52 my (%self, %parse);
19 # make the common case fast
20 52 ▶ if (@_ == 1 && !ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0]) 7 45 52
Condition100%3/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
25207
MC/DC100%2/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table100%3/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
21 7 qr/[$_[0]]/;
22 7 %self= ( notation => $_[0] );
23 7 ▶ if (ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 0 7 7
24 0 ▶ $self{generator_opts} ||= { %{ $class->{generator_opts} } };
Condition0%0/2
$self{"generator_opts"} ||= {%{$class->{"generator_opts"};}}
l!l
00
MC/DC0%0/1
$self{"generator_opts"} ||= {%{$class->{"generator_opts"};}}
$self{"generator_opts"}
Truth table0%0/2
A
A= $self{"generator_opts"}
Aresult
11
00
25 0 ▶ $self{max_codepoint} //= $class->{max_codepoint};
Condition0%0/3
$self{"max_codepoint"} //= $class->{"max_codepoint"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self{"max_codepoint"} //= $class->{"max_codepoint"}
$self{"max_codepoint"} $class->{"max_codepoint"}
Truth table0%0/3
A //= B
A= $self{"max_codepoint"}B= $class->{"max_codepoint"}
ABresult
1X1
011
000
26 0 $class= ref $class;
27 }
28 7 return bless \%self, $class;
29 }
30  
31 45 ▶ %self= @_ != 1? @_ : %{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
@_ != 1 ? : 25 20 45
32  
33 # Look for fields from the parser
34 45 ▶ $parse{classes}= delete $self{classes} if defined $self{classes};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self{"classes"} 6 39 45
35 45 ▶ $parse{codepoints}= delete $self{codepoints} if defined $self{codepoints};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self{"codepoints"} 2 43 45
36 45 ▶ $parse{codepoint_ranges}= delete $self{codepoint_ranges} if defined $self{codepoint_ranges};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self{"codepoint_ranges"} 0 45 45
37 45 ▶ $parse{negate}= delete $self{negate} if defined $self{negate};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self{"negate"} 0 45 45
38 45 ▶ if (defined $self{chars}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self{"chars"}) 3 42 45
39 3 push @{$parse{codepoints}}, map ord, @{$self{chars}};
40 3 delete $self{chars};
41 }
42 45 ▶ if (defined $self{ranges}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self{"ranges"}) 0 45 45
43 0 ▶ push @{$parse{codepoint_ranges}},
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
44 map +( ref $_? ( ord $_->[0], ord $_->[1] ) : ord ),
45 0 @{$self{ranges}};
46 0 delete $self{ranges};
47 }
48  
49 # If called on an object, carry over some settings
50 45 ▶ if (ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 0 45 45
51 0 ▶ if (!keys %parse && !defined $self{notation} && !$self{members} && !$self{member_invlist}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %parse or defined $self{"notation"} or $self{"members"} or $self{"member_invlist"}) 0 0 0
Condition0%0/9
keys %parse or defined $self{"notation"} or $self{"members"} or $self{"member_invlist"}
l!l&&r!l&&!r
000
keys %parse or defined $self{"notation"} or $self{"members"}
l!l&&r!l&&!r
000
keys %parse or defined $self{"notation"}
l!l&&r!l&&!r
000
MC/DC0%0/4
keys %parse or defined $self{"notation"} or $self{"members"} or $self{"member_invlist"}
keys %parse defined $self{"notation"} $self{"members"} $self{"member_invlist"}
Truth table0%0/5
A or B or C or D
A= keys %parseB= defined $self{"notation"}C= $self{"members"}D= $self{"member_invlist"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
52 @self{'_parse','notation','members','member_invlist'}=
53 0 @{$class}{'_parse','notation','members','member_invlist'};
54 }
55 0 ▶ $self{$_} //= $class->{$_} for @generator_attrs;
Condition0%0/3
$self{$_} //= $class->{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self{$_} //= $class->{$_}
$self{$_} $class->{$_}
Truth table0%0/3
A //= B
A= $self{$_}B= $class->{$_}
ABresult
1X1
011
000
56 0 $class= ref $class;
57 }
58  
59 45 ▶ if (defined $self{notation} && !keys %parse) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self{'notation'} and not keys %parse) { } 28 17 45
elsif (keys %parse) { } 11 6 17
Condition66%2/3
defined $self{"notation"} and not keys %parse
!ll&&!rl&&r
17028
MC/DC50%1/2
defined $self{"notation"} and not keys %parse
defined $self{"notation"} not keys %parse
Truth table66%2/3
A and not(B)
A= defined $self{"notation"}B= not keys %parse
ABresult
0X0
100
111
60 # want to trigger the syntax error exception now, not lazily later on
61 28 qr/[$self{notation}]/;
62 }
63 elsif (keys %parse) {
64 11 $self{_parse}= \%parse;
65 Carp::croak("Charset-building options (classes, chars, codepoints, ranges, codepoint_ranges, negate)"
66 ." cannot be combined with members, member_invlist or notation attributes")
67 11 ▶ if $self{members} or $self{member_invlist}; # allow notation to preserve original text
Branch: 1 branch
BranchTrueFalseTotal
if $self{"members"} or $self{"member_invlist"} 0 11 11
Condition33%1/3
$self{"members"} or $self{"member_invlist"}
l!l&&r!l&&!r
0011
MC/DC0%0/2
$self{"members"} or $self{"member_invlist"}
$self{"members"} $self{"member_invlist"}
Truth table33%1/3
A or B
A= $self{"members"}B= $self{"member_invlist"}
ABresult
1X1
011
000
68 }
69 else {
70 # At least one of members, member_invlist, notation, or _parse must be specified
71 Carp::croak("Require at least one of members, member_invlist, notation, or charset-building options")
72 6 ▶ unless $self{members} or $self{member_invlist};
Branch: 1 branch
BranchTrueFalseTotal
unless $self{"members"} or $self{"member_invlist"} 0 6 6
Condition33%1/3
$self{"members"} or $self{"member_invlist"}
l!l&&r!l&&!r
060
MC/DC0%0/2
$self{"members"} or $self{"member_invlist"}
$self{"members"} $self{"member_invlist"}
Truth table33%1/3
A or B
A= $self{"members"}B= $self{"member_invlist"}
ABresult
1X1
011
000
73 }
74
75 45 return bless \%self, $class;
76 }
77  
78 31 ▶sub _parse {
Subroutine _parse: called CC=9 SCAR=38.1
79 # If the '_parse' wasn't initialized, it can be derived from members or member_invlist or notation
80 31 ▶ $_[0]{_parse} || do {
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"_parse"} or do { my $self = shift(); if (defined $self->{"notation"}) { $self->{"_parse"} = $self->parse($self->{"notation"}); } elsif ($self->{"members"}) { $self->{"_parse"}{"codepoints"} = [map(ord($_), @{$$self{"members"};})]; } elsif (my $inv = $self->{"member_invlist"}) { my $i; for ($i = 0; $i < $inv->$#*; $i += 2) { if ($inv->[$i] + 1 == $$inv[$i + 1]) { push @{$$self{"_parse"}{"codepoints"};}, $inv->[$i]; } else { push @{$$self{"_parse"}{"codepoint_ranges"};}, $inv->[$i], $$inv[$i + 1] - 1; } ; } ; if ($i == $inv->$#*) { push @{$$self{"_parse"}{"codepoint_ranges"};}, $inv->[$i], $self->max_codepoint || 1114111; } ; } else { die "Unhandled lazy-build scenario"; } ; $self->{"_parse"} } 11 20 31
81 20 my $self= shift;
82 20 ▶ if (defined $self->{notation}) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $self->{'notation'}) { } 20 0 20
elsif ($self->{'members'}) { } 0 0 0
elsif (my $inv = $self->{'member_invlist'}) { } 0 0 0
83 20 $self->{_parse}= $self->parse($self->{notation});
84 }
85 elsif ($self->{members}) {
86 0 $self->{_parse}{codepoints}= [ map ord, @{$self->{members}} ];
87 }
88 elsif (my $inv= $self->{member_invlist}) {
89 0 my $i;
90 0 ▶ for ($i= 0; $i < $#$inv; $i+= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $inv->$#*) 0 0 0
91 0 ▶ if ($inv->[$i] + 1 == $inv->[$i+1]) { push @{$self->{_parse}{codepoints}}, $inv->[$i] }
Branch: 1 branch
BranchTrueFalseTotal
if ($inv->[$i] + 1 == $$inv[$i + 1]) { } 0 0 0
92 0 else { push @{$self->{_parse}{codepoint_ranges}}, $inv->[$i], $inv->[$i+1] - 1; }
93 }
94 0 ▶ if ($i == $#$inv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $inv->$#*) 0 0 0
95 0 ▶ push @{$self->{_parse}{codepoint_ranges}}, $inv->[$i], ($self->max_codepoint || 0x10FFFF);
Condition0%0/2
$self->max_codepoint || 1114111
l!l
00
MC/DC0%0/1
$self->max_codepoint || 1114111
$self->max_codepoint
Truth table0%0/2
A
A= $self->max_codepoint
Aresult
11
00
96 }
97 }
98 0 else { die "Unhandled lazy-build scenario" }
99 20 $self->{_parse};
100 };
101 }
102  
103  
104 16 ▶sub notation {
Subroutine notation: called CC=2 SCAR=7.2
Pod documented
105 16 ▶ $_[0]{notation} //= _deparse_charset($_[0]->_parse);
Condition66%2/3
$_[0]{"notation"} //= _deparse_charset($_[0]->_parse)
l!l&&r!l&&!r
1510
MC/DC0%0/2
$_[0]{"notation"} //= _deparse_charset($_[0]->_parse)
$_[0]{"notation"} _deparse_charset($_[0]->_parse)
Truth table66%2/3
A //= B
A= $_[0]{"notation"}B= _deparse_charset($_[0]->_parse)
ABresult
1X1
011
000
106 }
107  
108  
109 121 ▶sub min_codepoint {
Subroutine min_codepoint: called CC=2 SCAR=7.2
Pod documented
110 121 ▶ $_[0]{min_codepoint}= $_[1] if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 121 121
111 $_[0]{min_codepoint}
112 121 }
113 160 ▶sub max_codepoint {
Subroutine max_codepoint: called CC=1 SCAR=0.0
Pod documented
114 $_[0]{max_codepoint}
115 160 }
116  
117  
118 121 ▶sub str_len {
Subroutine str_len: called CC=2 SCAR=7.2
Pod documented
119 121 ▶ $_[0]{str_len}= $_[1] if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 121 121
120 121 $_[0]{str_len};
121 }
122  
123  
124 978 ▶sub count {
Subroutine count: called CC=2 SCAR=6.9
Pod documented
125 978 ▶ $_[0]{members}? scalar @{$_[0]{members}}
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'members'} ? : 83 895 978
126 : $_[0]->_invlist_index->[-1];
127 }
128  
129  
130 104 ▶sub members {
Subroutine members: called CC=2 SCAR=7.2
Pod documented
131 104 ▶ $_[0]{members} ||= $_[0]->_build_members;
Condition66%2/3
$_[0]{"members"} ||= $_[0]->_build_members
l!l&&r!l&&!r
83210
MC/DC0%0/2
$_[0]{"members"} ||= $_[0]->_build_members
$_[0]{"members"} $_[0]->_build_members
Truth table66%2/3
A ||= B
A= $_[0]{"members"}B= $_[0]->_build_members
ABresult
1X1
011
000
132 }
133  
134 21 ▶sub _build_members {
Subroutine _build_members: called CC=4 SCAR=14.1
135 21 my $self= shift;
136 21 my $invlist= $self->member_invlist;
137 21 my @members;
138 21 ▶ if (@$invlist > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$invlist > 1) 21 0 21
139 push @members, map chr, $invlist->[$_*2] .. ($invlist->[$_*2+1]-1)
140 21 for 0 .. (($#$invlist-1)>>1);
141 }
142 # an odd number of elements means the list ends with an "include-all"
143 21 ▶ push @members, map chr, $invlist->[-1] .. 0x10FFFF
Branch: 1 branch
BranchTrueFalseTotal
if 1 & @$invlist 0 21 21
144 if 1 & @$invlist;
145 21 return \@members;
146 }
147  
148 6 ▶sub Mock::Data::Charset::Util::expand_invlist {
Subroutine expand_invlist: called CC=4 SCAR=13.9
149 6 my $invlist= shift;
150 6 my @members;
151 6 ▶ if (@$invlist > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$invlist > 1) 5 1 6
152 push @members, $invlist->[$_*2] .. ($invlist->[$_*2+1]-1)
153 5 for 0 .. (($#$invlist-1)>>1);
154 }
155 # an odd number of elements means the list ends with an "include-all"
156 6 ▶ push @members, $invlist->[-1] .. 0x10FFFF
Branch: 1 branch
BranchTrueFalseTotal
if 1 & @$invlist 2 4 6
157 if 1 & @$invlist;
158 6 return \@members;
159 }
160  
161 # The index is private because there's not a good way to explain it to the user
162 2670 ▶sub _invlist_index {
Subroutine _invlist_index: called CC=2 SCAR=7.1
163 2670 my $self= shift;
164 2670 ▶ $self->{_invlist_index} ||= Mock::Data::Charset::Util::create_invlist_index($self->member_invlist);
Condition66%2/3
$self->{"_invlist_index"} ||= Mock::Data::Charset::Util::create_invlist_index($self->member_invlist)
l!l&&r!l&&!r
2645250
MC/DC0%0/2
$self->{"_invlist_index"} ||= Mock::Data::Charset::Util::create_invlist_index($self->member_invlist)
$self->{"_invlist_index"} Mock::Data::Charset::Util::create_invlist_index($self->member_invlist)
Truth table66%2/3
A ||= B
A= $self->{"_invlist_index"}B= Mock::Data::Charset::Util::create_invlist_index($self->member_invlist)
ABresult
1X1
011
000
165 }
166  
167 29 ▶sub Mock::Data::Charset::Util::create_invlist_index {
Subroutine create_invlist_index: called CC=3 SCAR=11.0
168 29 my $invlist= shift;
169 29 my $n_spans= (@$invlist + 1) >> 1;
170 29 my @index;
171 29 $#index= $n_spans-1;
172 29 my $total= 0;
173 $index[$_]= $total += $invlist->[$_*2+1] - $invlist->[$_*2]
174 29 for 0 .. (@$invlist >> 1)-1;
175 29 ▶ if (@$invlist & 1) { # In the case that the final range is infinite
Branch: 1 branch
BranchTrueFalseTotal
if (@$invlist & 1) 6 23 29
176 6 $index[$n_spans-1]= $total + 0x110000 - $invlist->[-1];
177 }
178 29 \@index;
179 }
180  
181  
182 1835 ▶sub member_invlist {
Subroutine member_invlist: called CC=3 SCAR=15.0
Pod documented
183 1835 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 1835 1835
184 0 $_[0]{member_invlist}= $_[1];
185 0 delete $_[0]{_invlist_index};
186 0 delete $_[0]{members};
187 0 delete $_[0]{notation};
188 }
189 1835 ▶ $_[0]{member_invlist} //= _build_member_invlist(@_);
Condition66%2/3
$_[0]{"member_invlist"} //= _build_member_invlist(@_)
l!l&&r!l&&!r
1796390
MC/DC0%0/2
$_[0]{"member_invlist"} //= _build_member_invlist(@_)
$_[0]{"member_invlist"} _build_member_invlist(@_)
Truth table66%2/3
A //= B
A= $_[0]{"member_invlist"}B= _build_member_invlist(@_)
ABresult
1X1
011
000
190 }
191  
192 39 ▶sub _build_member_invlist {
Subroutine _build_member_invlist: called CC=8 SCAR=20.9
193 39 my $self= shift;
194 39 my $max_codepoint= $self->max_codepoint;
195 # If the search space is small, and there is already a regex notation, it is probably faster
196 # to iterate and let perl do the work than to parse the charset.
197 39 my $invlist;
198 39 ▶ if (!defined $max_codepoint || $max_codepoint > 1000 || !defined $self->{notation}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $max_codepoint or $max_codepoint > 1000 or not defined $self->{"notation"}) 30 9 39
Condition83%5/6
not defined $max_codepoint or $max_codepoint > 1000 or not defined $self->{"notation"}
l!l&&r!l&&!r
2739
not defined $max_codepoint or $max_codepoint > 1000
l!l&&r!l&&!r
27012
MC/DC66%2/3
not defined $max_codepoint or $max_codepoint > 1000 or not defined $self->{"notation"}
not defined $max_codepoint $max_codepoint > 1000 not defined $self->{"notation"}
Truth table75%3/4
not(A) or B or not(C)
A= not defined $max_codepointB= $max_codepoint > 1000C= not defined $self->{"notation"}
ABCresult
1XX1
01X1
0011
0000
199 30 ▶ $max_codepoint ||= 0x10FFFF;
Condition100%2/2
$max_codepoint ||= 1114111
l!l
327
MC/DC100%1/1
$max_codepoint ||= 1114111
$max_codepoint
Truth table100%2/2
A
A= $max_codepoint
Aresult
11
00
200 30 $invlist= eval {
201 30 _parsed_charset_to_invlist($self->_parse, $max_codepoint);
202 }# or main::diag $@
203 }
204 39 ▶ $invlist ||= _charset_invlist_brute_force($self->notation, $max_codepoint);
Condition66%2/3
$invlist ||= _charset_invlist_brute_force($self->notation, $max_codepoint)
l!l&&r!l&&!r
3090
MC/DC0%0/2
$invlist ||= _charset_invlist_brute_force($self->notation, $max_codepoint)
$invlist _charset_invlist_brute_force($self->notation, $max_codepoint)
Truth table66%2/3
A ||= B
A= $invlistB= _charset_invlist_brute_force($self->notation, $max_codepoint)
ABresult
1X1
011
000
205 # If a user writes to the invlist, it will become out of sync with the Index,
206 # leading to confusing bugs.
207 39 ▶ if (Internals->can('SvREADONLY')) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Internals"->can("SvREADONLY")) 39 0 39
208 39 Internals::SvREADONLY($_,1) for @$invlist;
209 39 Internals::SvREADONLY(@$invlist,1);
210 }
211 39 return $invlist;
212 }
213  
214 # Lazy-built string of all basic-multilingual-plane characters
215 12 our $_ascii_chars;
216 12 our $_unicode_chars;
217 1 ▶sub _build_unicode_chars {
Subroutine _build_unicode_chars: called CC=6 SCAR=18.0
218 1 ▶ unless (defined $_unicode_chars) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Mock::Data::Charset::_unicode_chars) 1 0 1
219 # Construct ranges of valid characters separated by NUL.
220 # Older perls die when the regex engine encounters an invalid character
221 # but newer perls just treat the invalid character as "not a member",
222 # unless the set is a negation in which case non-characters *are* a member.
223 # This makes the assumption that if a non-char isn't a member then \0 won't
224 # be either.
225 1 $_unicode_chars= '';
226 1 $_unicode_chars .= chr($_) for 0 .. 0xD7FF;
227 1 $_unicode_chars .= "\0";
228 1 $_unicode_chars .= chr($_) for 0xFDF0 .. 0xFFFD;
229 1 for (1..16) {
230 16 $_unicode_chars .= "\0";
231 16 $_unicode_chars .= chr($_) for ($_<<16) .. (($_<<16)|0xFFFD);
232 }
233 }
234 1 \$_unicode_chars;
235 }
236  
237 9 ▶sub _charset_invlist_brute_force {
Subroutine _charset_invlist_brute_force: called CC=16 SCAR=28.3
238 9 my ($set, $max_codepoint)= @_;
239 9 ▶ my $inv= (ord $set == ord '^')? substr($set,1) : '^'.$set;
Branch: 1 branch
BranchTrueFalseTotal
ord $set == 94 ? : 1 8 9
240 9 my @invlist;
241
242 # optimize common case
243 9 ▶ if ($max_codepoint < 256) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_codepoint < 256) { } 8 1 9
244 # Find first character of every match and first character of every non-match
245 # and convert to codepoints.
246 8 ▶ @invlist= map +(defined $_? ord($_) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 31 3 34
Condition100%2/2
$Mock::Data::Charset::_ascii_chars //= join("", map(chr($_), 0..255))
l!l
71
MC/DC100%1/1
$Mock::Data::Charset::_ascii_chars //= join("", map(chr($_), 0..255))
$Mock::Data::Charset::_ascii_chars
Truth table100%2/2
A
A= $Mock::Data::Charset::_ascii_chars
Aresult
11
00
247 ($_ascii_chars //= join('', map chr($_), 0..255))
248 =~ / ( [$set] ) (?> [$set]* ) (?: \z | ( [$inv] ) (?> [$inv]* ) )/gx;
249 }
250 else {
251 1 ▶ _build_unicode_chars() unless defined $_unicode_chars;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Mock::Data::Charset::_unicode_chars 1 0 1
252 # This got complicated while trying to support perls that can't match against non-characters.
253 # The non-characters have been replaced by NULs, so need to capture the char before and after
254 # each transition in case one of them is a NUL.
255 1 ▶ my @endpoints=
Branch: 1 branch
BranchTrueFalseTotal
$max_codepoint < 1114111 ? : 1 0 1
256 ($max_codepoint < 0x10FFFF? substr($_unicode_chars,0,$max_codepoint+1) : $_unicode_chars)
257 =~ /( [$set] ) ( [$set] )* ( \z | [$inv] ) ( [$inv] )* /gx;
258 1 ▶ if (@endpoints) {
Branch: 1 branch
BranchTrueFalseTotal
if (@endpoints) 1 0 1
259 # List is a multiple of 4 elements: (first-member,last-member,first-non-member,last-non-member)
260 # We're not interested in the span of non-members at the end, so just remove those.
261 1 pop @endpoints; pop @endpoints;
262 # Iterate every transition of member/nonmember, and use the second character if present
263 # and isn't a NUL, else use the first character and add 1.
264 1 push @invlist, ord $endpoints[0];
265 1 ▶ for (my $i= 1; $i < @endpoints; $i+= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @endpoints) 19 1 20
266 19 ▶ if (defined $endpoints[$i+1] && ord $endpoints[$i+1]) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $endpoints[$i + 1] and ord $endpoints[$i + 1]) { } 18 1 19
elsif (defined $endpoints[$i]) { } 1 0 1
Condition66%2/3
defined $endpoints[$i + 1] and ord $endpoints[$i + 1]
!ll&&!rl&&r
1018
MC/DC50%1/2
defined $endpoints[$i + 1] and ord $endpoints[$i + 1]
defined $endpoints[$i + 1] ord $endpoints[$i + 1]
Truth table66%2/3
A and B
A= defined $endpoints[$i + 1]B= ord $endpoints[$i + 1]
ABresult
0X0
100
111
267 18 push @invlist, ord $endpoints[$i+1];
268 } elsif (defined $endpoints[$i]) {
269 1 push @invlist, 1 + ord $endpoints[$i];
270 } else {
271 0 push @invlist, 1 + $invlist[-1];
272 }
273 }
274 # substr is an estimate, because string skips characters, so remove any spurrous
275 # codepoints beyond the max
276 1 ▶ pop @invlist while @invlist && $invlist[-1] > $max_codepoint;
Branch: 1 branch
BranchTrueFalseTotal
@invlist and $invlist[-1] > $max_codepoint and do { pop @invlist } 1 1 2
Condition66%2/3
@invlist and $invlist[-1] > $max_codepoint
!ll&&!rl&&r
011
MC/DC50%1/2
@invlist and $invlist[-1] > $max_codepoint
@invlist $invlist[-1] > $max_codepoint
Truth table66%2/3
A and B
A= @invlistB= $invlist[-1] > $max_codepoint
ABresult
0X0
100
111
277 }
278 }
279 # If an "infinite" range would be returned, but the user set a maximum codepoint,
280 # list the max codepoint as the end of the invlist.
281 9 ▶ if ($max_codepoint < 0x10FFFF and 1 & @invlist) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_codepoint < 1114111 and 1 & @invlist) 4 5 9
Condition66%2/3
$max_codepoint < 1114111 and 1 & @invlist
!ll&&!rl&&r
054
MC/DC50%1/2
$max_codepoint < 1114111 and 1 & @invlist
$max_codepoint < 1114111 1 & @invlist
Truth table66%2/3
A and B
A= $max_codepoint < 1114111B= 1 & @invlist
ABresult
0X0
100
111
282 4 push @invlist, $max_codepoint+1;
283 }
284 9 return \@invlist;
285 }
286  
287 30 ▶sub _parsed_charset_to_invlist {
Subroutine _parsed_charset_to_invlist: called CC=14 SCAR=26.8
288 30 my ($parse, $max_codepoint)= @_;
289 30 my @invlists;
290 # convert the character list into an inversion list
291 30 ▶ if (defined (my $cp= $parse->{codepoints})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cp = $parse->{"codepoints"})) 5 25 30
292 5 my @chars= sort { $a <=> $b } @$cp;
293 5 my @invlist= (shift @chars);
294 5 push @invlist, $invlist[0] + 1;
295 5 ▶ for (my $i= 0; $i <= $#chars; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#chars) 5 5 10
296 # If the next char is adjacent, extend the span
297 5 ▶ if ($invlist[-1] == $chars[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invlist[-1] == $chars[$i]) { } 2 3 5
298 2 ++$invlist[-1];
299 } else {
300 3 push @invlist, $chars[$i], $chars[$i]+1;
301 }
302 }
303 5 push @invlists, \@invlist;
304 }
305 # Each range is an inversion list already
306 30 ▶ if (my $r= $parse->{codepoint_ranges}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $parse->{"codepoint_ranges"}) 10 20 30
307 10 ▶ for (my $i= 0; $i < (@$r >> 1); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$r >> 1) 11 10 21
308 11 my ($start, $limit)= ($r->[$i*2], $r->[$i*2+1]+1);
309 # Try to combine the range with the most recent inversion list, if possible,
310 11 ▶ if (@invlists && $invlists[-1][-1] < $start) {
Branch: 2 branches
BranchTrueFalseTotal
if (@invlists and $invlists[-1][-1] < $start) { } 1 10 11
elsif (@invlists and $invlists[-1][0] > $limit) { } 0 10 10
Condition66%2/3
@invlists and $invlists[-1][-1] < $start
!ll&&!rl&&r
1001
MC/DC50%1/2
@invlists and $invlists[-1][-1] < $start
@invlists $invlists[-1][-1] < $start
Truth table66%2/3
A and B
A= @invlistsB= $invlists[-1][-1] < $start
ABresult
0X0
100
111
311 1 push @{ $invlists[-1] }, $start, $limit;
312 ▶ } elsif (@invlists && $invlists[-1][0] > $limit) {
Condition33%1/3
@invlists and $invlists[-1][0] > $limit
!ll&&!rl&&r
1000
MC/DC0%0/2
@invlists and $invlists[-1][0] > $limit
@invlists $invlists[-1][0] > $limit
Truth table33%1/3
A and B
A= @invlistsB= $invlists[-1][0] > $limit
ABresult
0X0
100
111
313 0 unshift @{ $invlists[-1] }, $start, $limit;
314 } else {
315 # else just start a new inversion list
316 10 push @invlists, [ $start, $limit ]
317 }
318 }
319 }
320 # Convert each character class to an inversion list.
321 30 ▶ if ($parse->{classes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parse->{"classes"}) 15 15 30
322 push @invlists, _class_invlist($_)
323 15 for @{ $parse->{classes} };
324 }
325 30 my $invlist= Mock::Data::Charset::Util::merge_invlists(\@invlists, $max_codepoint);
326 # Perform negation of inversion list by either starting at char 0 or removing char 0
327 30 ▶ if ($parse->{negate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parse->{"negate"}) 1 29 30
328 1 ▶ if ($invlist->[0]) { unshift @$invlist, 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($invlist->[0]) { } 1 0 1
329 0 else { shift @$invlist; }
330 }
331 30 return $invlist;
332 }
333  
334  
335 12 our $_compile;
336 4 ▶sub compile {
Subroutine compile: called CC=1 SCAR=0.0
Pod documented
337 4 local $_compile= 1;
338 4 shift->generate(@_);
339 }
340 121 ▶sub generate {
Subroutine generate: called CC=24 SCAR=33.7
Pod documented
341 121 my ($self, $mock)= (shift, shift);
342 121 my ($len, $cp_min, $cp_max, $member_count)
343 = ($self->str_len, $self->min_codepoint, $self->max_codepoint, $self->count);
344 121 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 90 31 121
345 90 ▶ my %opts= ref $_[0] eq 'HASH'? %{ shift() } : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 87 3 90
346 90 ▶ $len= @_? shift : $opts{str_len} // $opts{len} // $opts{size}; # allow some aliases for length
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 88 2 90
Condition33%2/6
($opts{"str_len"} // $opts{"len"}) // $opts{"size"}
l!l&&r!l&&!r
200
$opts{"str_len"} // $opts{"len"}
l!l&&r!l&&!r
020
MC/DC0%0/3
($opts{"str_len"} // $opts{"len"}) // $opts{"size"}
$opts{"str_len"} $opts{"len"} $opts{"size"}
Truth table25%1/4
A // B // C
A= $opts{"str_len"}B= $opts{"len"}C= $opts{"size"}
ABCresult
1XX1
01X1
0011
0000
347 90 ▶ $cp_min= $opts{min_codepoint} // $cp_min;
Condition66%2/3
$opts{"min_codepoint"} // $cp_min
l!l&&r!l&&!r
6084
MC/DC50%1/2
$opts{"min_codepoint"} // $cp_min
$opts{"min_codepoint"} $cp_min
Truth table66%2/3
A // B
A= $opts{"min_codepoint"}B= $cp_min
ABresult
1X1
011
000
348 90 ▶ $cp_max= $opts{max_codepoint} // $cp_max;
Condition66%2/3
$opts{"max_codepoint"} // $cp_max
l!l&&r!l&&!r
27063
MC/DC50%1/2
$opts{"max_codepoint"} // $cp_max
$opts{"max_codepoint"} $cp_max
Truth table66%2/3
A // B
A= $opts{"max_codepoint"}B= $cp_max
ABresult
1X1
011
000
349 }
350 121 ▶ my ($memb_min, $memb_span)= !defined $cp_min && !defined $cp_max? (0,$member_count)
Branch: 1 branch
BranchTrueFalseTotal
!(defined $cp_min || defined $cp_max) ? : 94 27 121
Condition100%3/3
defined $cp_min || defined $cp_max
l!l&&r!l&&!r
62194
MC/DC100%2/2
defined $cp_min || defined $cp_max
defined $cp_min defined $cp_max
Truth table100%3/3
A || B
A= defined $cp_minB= defined $cp_max
ABresult
1X1
011
000
351 : $self->_codepoint_minmax_to_member_range($cp_min, $cp_max);
352  
353 # If compiling, $len will be a function, else it will be an integer
354 0 ▶ $len= !defined $len? ($_compile? sub { 1 } : 1 )
Subroutine __ANON__: not called CC=1 SCAR=6.9
355 0 ▶ : !ref $len? ($_compile? sub { $len } : $len)
Subroutine __ANON__: not called CC=1 SCAR=6.9
356 : ref $len eq 'ARRAY'? (
357 0 ▶ $_compile? sub { $len->[0] + int rand ($len->[1] - $len->[0]) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
358 121 ▶ : $len->[0] + int rand ($len->[1] - $len->[0])
Branch: 8 branches
BranchTrueFalseTotal
!defined($len) ? : 1 120 121
$Mock::Data::Charset::_compile ? : 0 1 1
!ref($len) ? : 88 32 120
$Mock::Data::Charset::_compile ? : 0 88 88
ref $len eq 'ARRAY' ? : 2 30 32
$Mock::Data::Charset::_compile ? : 0 2 2
ref $len eq 'CODE' ? : 30 0 30
$Mock::Data::Charset::_compile ? : 4 26 30
359 )
360 : ref $len eq 'CODE'? ($_compile? $len : $len->($mock))
361 : Carp::croak("Unknown str_len specification '$len'");
362  
363 # If member list is small-ish, use faster direct array access
364 121 ▶ if ($self->{members} || $member_count < 500) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'members'} or $member_count < 500) { } 97 24 121
Condition100%3/3
$self->{"members"} or $member_count < 500
l!l&&r!l&&!r
831424
MC/DC100%2/2
$self->{"members"} or $member_count < 500
$self->{"members"} $member_count < 500
Truth table100%3/3
A or B
A= $self->{"members"}B= $member_count < 500
ABresult
1X1
011
000
365 97 my $members= $self->members;
366 3 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
367 3 my $buf= '';
368 $buf .= $members->[$memb_min + int rand $memb_span]
369 3 for 1..$len->($_[0]);
370 3 return $buf;
371 97 ▶ } if $_compile;
Branch: 1 branch
BranchTrueFalseTotal
if $Mock::Data::Charset::_compile 4 93 97
372 93 my $buf= '';
373 $buf .= $members->[$memb_min + int rand $memb_span]
374 93 for 1..$len;
375 93 return $buf;
376 }
377 else {
378 24 my $invlist= $self->member_invlist;
379 24 my $index= $self->_invlist_index;
380 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
381 0 my $ret= '';
382 $ret .= chr _get_invlist_element($memb_min + int rand($memb_span), $invlist, $index)
383 0 for 1..$len->($_[0]);
384 24 ▶ } if $_compile;
Branch: 1 branch
BranchTrueFalseTotal
if $Mock::Data::Charset::_compile 0 24 24
385 24 my $buf= '';
386 $buf .= chr _get_invlist_element($memb_min + int rand($memb_span), $invlist, $index)
387 24 for 1..$len;
388 24 return $buf;
389 }
390 }
391  
392 27 ▶sub _codepoint_minmax_to_member_range {
Subroutine _codepoint_minmax_to_member_range: called CC=5 SCAR=16.3
393 27 my $self= shift;
394 27 my ($cp_min, $cp_max)= @_;
395 my $memb_min= !defined $cp_min? 0
396 27 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
!defined($cp_min) ? : 21 6 27
397 6 my ($at, $ins)= _find_invlist_element($cp_min, $self->member_invlist, $self->_invlist_index);
398 6 ▶ $at // $ins
Condition33%1/3
$at // $ins
l!l&&r!l&&!r
600
MC/DC0%0/2
$at // $ins
$at $ins
Truth table33%1/3
A // B
A= $atB= $ins
ABresult
1X1
011
000
399 };
400 my $memb_lim= !defined $cp_max? $self->count
401 27 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
!defined($cp_max) ? : 0 27 27
402 27 my ($at, $ins)= _find_invlist_element($cp_max, $self->member_invlist, $self->_invlist_index);
403 27 ▶ defined $at? $at + 1 : $ins;
Branch: 1 branch
BranchTrueFalseTotal
defined $at ? : 17 10 27
404 };
405 27 return ($memb_min, $memb_lim-$memb_min);
406 }
407  
408  
409 36 ▶sub parse {
Subroutine parse: called CC=4 SCAR=14.2
Pod documented
410 36 my ($self, $notation)= @_;
411 36 ▶ return { codepoints => [] } unless length $notation;
Branch: 1 branch
BranchTrueFalseTotal
unless length $notation 0 36 36
412 36 ▶ return { classes => ['All'] } if $notation eq '^';
Branch: 1 branch
BranchTrueFalseTotal
if $notation eq "^" 0 36 36
413 36 $notation .= ']';
414 # parse function needs $_ to be the input string
415 36 pos($notation)= 0;
416 36 return _parse_charset() for $notation;
417 }
418  
419 12 our $have_prop_invlist;
420 our %_parse_charset_backslash= (
421 a => ord "\a",
422 b => ord "\b",
423 0 ▶ c => sub { die "Unimplemented: \\c" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
424 1 ▶ d => sub { push @{$_[0]{classes}}, 'digit'; undef; },
Subroutine __ANON__: called CC=1 SCAR=0.0
425 0 ▶ D => sub { push @{$_[0]{classes}}, '^digit'; undef; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
426 e => ord "\e",
427 f => ord "\f",
428 0 ▶ h => sub { push @{$_[0]{classes}}, 'horizspace'; undef; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
429 0 ▶ H => sub { push @{$_[0]{classes}}, '^horizspace'; undef; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
430 n => ord "\n",
431 N => \&_parse_charset_namedchar,
432 o => \&_parse_charset_oct,
433 p => \&_parse_charset_classname,
434 4 ▶ P => sub { _parse_charset_classname(shift, 1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
435 r => ord "\r",
436 1 ▶ s => sub { push @{$_[0]{classes}}, 'space'; undef; },
Subroutine __ANON__: called CC=1 SCAR=0.0
437 0 ▶ S => sub { push @{$_[0]{classes}}, '^space'; undef; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
438 t => ord "\t",
439 0 ▶ v => sub { push @{$_[0]{classes}}, 'vertspace'; undef; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
440 0 ▶ V => sub { push @{$_[0]{classes}}, '^vertspace'; undef; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
441 6 ▶ w => sub { push @{$_[0]{classes}}, 'word'; undef; },
Subroutine __ANON__: called CC=1 SCAR=0.0
442 0 ▶ W => sub { push @{$_[0]{classes}}, '^word'; undef; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
443 12 x => \&_parse_charset_hex,
444 0 => \&_parse_charset_oct,
445 1 => \&_parse_charset_oct,
446 2 => \&_parse_charset_oct,
447 3 => \&_parse_charset_oct,
448 4 => \&_parse_charset_oct,
449 5 => \&_parse_charset_oct,
450 6 => \&_parse_charset_oct,
451 7 => \&_parse_charset_oct,
452 8 => \&_parse_charset_oct,
453 9 => \&_parse_charset_oct,
454 );
455 12 our %_class_invlist_cache= (
456 'Any' => [ 0 ],
457 '\\N' => [ 0, ord("\n"), 1+ord("\n") ],
458 );
459 26 ▶sub _class_invlist {
Subroutine _class_invlist: called CC=5 SCAR=16.2
460 26 my $class= shift;
461 26 ▶ if (ord $class == ord '^') {
Branch: 1 branch
BranchTrueFalseTotal
if (ord $class == 94) 5 21 26
462 5 return Mock::Data::Charset::Util::negate_invlist(
463 _class_invlist(substr($class,1))
464 );
465 }
466 21 ▶ return $_class_invlist_cache{$class} ||= do {
Condition66%2/3
$Mock::Data::Charset::_class_invlist_cache{$class} ||= do { $Mock::Data::Charset::have_prop_invlist = do { require Unicode::UCD; not !"Unicode::UCD"->can("prop_invlist") } unless defined $Mock::Data::Charset::have_prop_invlist; $Mock::Data::Charset::have_prop_invlist ? [Unicode::UCD::prop_invlist($class)] : _charset_invlist_brute_force("\\p{$class}", 1114111) }
l!l&&r!l&&!r
1290
MC/DC0%0/2
$Mock::Data::Charset::_class_invlist_cache{$class} ||= do { $Mock::Data::Charset::have_prop_invlist = do { require Unicode::UCD; not !"Unicode::UCD"->can("prop_invlist") } unless defined $Mock::Data::Charset::have_prop_invlist; $Mock::Data::Charset::have_prop_invlist ? [Unicode::UCD::prop_invlist($class)] : _charset_invlist_brute_force("\\p{$class}", 1114111) }
$Mock::Data::Charset::_class_invlist_cache{$class} do { $Mock::Data::Charset::have_prop_invlist = do { require Unicode::UCD; not !"Unicode::UCD"->can("prop_invlist") } unless defined $Mock::Data::Charset::have_prop_invlist; $Mock::Data::Charset::have_prop_invlist ? [Unicode::UCD::prop_invlist($class)] : _charset_invlist_brute_force("\\p{$class}", 1114111) }
Truth table66%2/3
A ||= B
A= $Mock::Data::Charset::_class_invlist_cache{$class}B= do { $Mock::Data::Charset::have_prop_invlist = do { require Unicode::UCD; not !"Unicode::UCD"->can("prop_invlist") } unless defined $Mock::Data::Charset::have_prop_invlist; $Mock::Data::Charset::have_prop_invlist ? [Unicode::UCD::prop_invlist($class)] : _charset_invlist_brute_force("\\p{$class}", 1114111) }
ABresult
1X1
011
000
467 9 ▶ $have_prop_invlist= do { require Unicode::UCD; !!Unicode::UCD->can('prop_invlist') }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Mock::Data::Charset::have_prop_invlist 3 6 9
468 unless defined $have_prop_invlist;
469 9 ▶ $have_prop_invlist? [ Unicode::UCD::prop_invlist($class) ]
Branch: 1 branch
BranchTrueFalseTotal
$Mock::Data::Charset::have_prop_invlist ? : 9 0 9
470 : _charset_invlist_brute_force("\\p{$class}", 0x10FFFF);
471 };
472 }
473 3 ▶sub _parse_charset_hex {
Subroutine _parse_charset_hex: called CC=3 SCAR=11.1
474 3 ▶ /\G( [0-9A-Fa-f]{2} | \{ ([0-9A-Fa-f]+) \} )/gcx
Branch: 1 branch
BranchTrueFalseTotal
/\G( [0-9A-Fa-f]{2} | \{ ([0-9A-Fa-f]+) \} )/cgx or die 'Invalid hex escape at ' . _parse_context() 3 0 3
475 or die "Invalid hex escape at "._parse_context;
476 3 ▶ return hex(defined $2? $2 : $1);
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 1 2 3
477 }
478 5 ▶sub _parse_charset_oct {
Subroutine _parse_charset_oct: called CC=3 SCAR=11.1
479 5 --pos; # The caller ate one of the characters we need to parse
480 5 ▶ /\G( [0-7]{3} | 0 | o\{ ([0-7]+) \} ) /gcx
Branch: 1 branch
BranchTrueFalseTotal
/\G( [0-7]{3} | 0 | o\{ ([0-7]+) \} ) /cgx or die 'Invalid octal escape at ' . _parse_context() 5 0 5
481 or die "Invalid octal escape at "._parse_context;
482 5 ▶ return oct(defined $2? $2 : $1);
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 1 4 5
483 }
484 2 ▶sub _parse_charset_namedchar {
Subroutine _parse_charset_namedchar: called CC=2 SCAR=10.1
485 2 require charnames;
486 2 ▶ /\G \{ ([^}]+) \} /gcx
Branch: 1 branch
BranchTrueFalseTotal
/\G \{ ([^}]+) \} /cgx and return charnames::vianame($1) 2 0 2
487 # or die "Invalid named char following \\N at '".substr($_,pos,10)."'";
488 and return charnames::vianame($1);
489 # Plain "\N" means every character except \n
490 0 push @{ $_[0]{classes} }, '\\N';
491 0 return;
492 }
493 7 ▶sub _parse_charset_classname {
Subroutine _parse_charset_classname: called CC=3 SCAR=11.0
494 7 my ($result, $negate)= @_;
495 7 ▶ /\G \{ ([^}]+) \} /gcx
Branch: 1 branch
BranchTrueFalseTotal
/\G \{ ([^}]+) \} /cgx or die 'Invalid class name following \\p at ' . _parse_context() 7 0 7
496 or die "Invalid class name following \\p at "._parse_context;
497 7 ▶ push @{$result->{classes}}, lc($negate? "^$1" : $1);
Branch: 1 branch
BranchTrueFalseTotal
$negate ? : 4 3 7
498 undef
499 7 }
500 41 ▶sub _parse_charset {
Subroutine _parse_charset: called CC=21 SCAR=31.2
501 41 my $flags= shift;
502 # argument is in $_, starting from pos($_)
503 41 my %parse;
504 my @range;
505 41 $parse{codepoints}= \my @chars;
506 41 ▶ $parse{negate}= 1 if /\G \^ /gcx;
Branch: 1 branch
BranchTrueFalseTotal
if /\G \^ /cgx 2 39 41
507 0 ▶ if (/\G]/gc) { push @chars, ord ']' }
Branch: 1 branch
BranchTrueFalseTotal
if (/\G]/cg) 0 41 41
508 41 while (1) {
509 133 my $cp; # literal codepoint to be added
510 # Check for special cases
511 133 ▶ if (/\G ( \\ | - | \[: | \] ) /gcx) {
Branch: 2 branches
BranchTrueFalseTotal
if (/\G ( \\ | - | \[: | \] ) /cgx) { } 88 45 133
elsif ($flags and ($flags->{'x'} || 0) >= 2 and /\G[ \t]/cg) { } 0 45 45
512 88 ▶ if ($1 eq '\\') {
Branch: 3 branches
BranchTrueFalseTotal
if ($1 eq '\\') { } 23 65 88
elsif ($1 eq '-') { } 19 46 65
elsif ($1 eq '[:') { } 5 41 46
513 23 ▶ /\G(.)/gc or die "Unexpected end of input";
Branch: 1 branch
BranchTrueFalseTotal
/\G(.)/cg or die "Unexpected end of input" 23 0 23
514 23 ▶ $cp= $_parse_charset_backslash{$1} || ord $1;
Condition33%1/3
$Mock::Data::Charset::_parse_charset_backslash{$1} || ord $1
l!l&&r!l&&!r
2300
MC/DC0%0/2
$Mock::Data::Charset::_parse_charset_backslash{$1} || ord $1
$Mock::Data::Charset::_parse_charset_backslash{$1} ord $1
Truth table33%1/3
A || B
A= $Mock::Data::Charset::_parse_charset_backslash{$1}B= ord $1
ABresult
1X1
011
000
515 23 ▶ $cp= $cp->(\%parse)
Branch: 1 branch
BranchTrueFalseTotal
if ref $cp 22 1 23
516 if ref $cp;
517 }
518 elsif ($1 eq '-') {
519 19 ▶ if (@range == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@range == 1) { } 18 1 19
520 18 push @range, ord '-';
521 18 next;
522 }
523 else {
524 1 $cp= ord '-';
525 }
526 }
527 elsif ($1 eq '[:') {
528 5 ▶ /\G ( [^:]+ ) :] /gcx
Branch: 1 branch
BranchTrueFalseTotal
/\G ( [^:]+ ) :] /cgx or die 'Invalid character class at ' . _parse_context() 5 0 5
529 or die "Invalid character class at "._parse_context;
530 5 push @{$parse{classes}}, $1;
531 }
532 else {
533 41 last; # $1 eq ']';
534 }
535 }
536 ▶ elsif ($flags && ($flags->{x}||0) >= 2 && /\G[ \t]/gc) {
Condition50%4/8
$flags and ($flags->{"x"} || 0) >= 2 and /\G[ \t]/cg
!ll&&!rl&&r
4500
$flags and ($flags->{"x"} || 0) >= 2
!ll&&!rl&&r
3960
$flags->{'x'} || 0
l!l
06
MC/DC0%0/3
$flags and ($flags->{"x"} || 0) >= 2 and /\G[ \t]/cg
$flags $flags->{'x'} /\G[ \t]/cg
Truth table50%2/4
A and B and C
A= $flagsB= $flags->{'x'}C= /\G[ \t]/cg
ABCresult
0XX0
10X0
1100
1111
537 0 next; # ignore space and tab under /xx
538 }
539 else {
540 45 ▶ /\G(.)/gc or die "Unexpected end of input";
Branch: 1 branch
BranchTrueFalseTotal
/\G(.)/cg or die "Unexpected end of input" 45 0 45
541 45 $cp= ord $1;
542 }
543 # If no single character was found, any range-in-progress needs converted to
544 # charcters
545 74 ▶ if (!defined $cp) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $cp) { } 20 54 74
elsif (@range == 1) { } 8 46 54
elsif (@range == 2) { } 17 29 46
546 20 push @chars, @range;
547 20 @range= ();
548 }
549 # At this point, $cp will contain the next ordinal of the character to include,
550 # but it might also be starting or finishing a range.
551 elsif (@range == 1) {
552 8 push @chars, $range[0];
553 8 $range[0]= $cp;
554 }
555 elsif (@range == 2) {
556 17 push @{$parse{codepoint_ranges}}, $range[0], $cp;
557 17 @range= ();
558 }
559 else {
560 29 push @range, $cp;
561 }
562 #printf "# pos %d cp %d range %s %s include %s\n", pos $_, $cp, $range[0] // '(null)', $range[1] // '(null)', join(',', @include);
563 }
564 41 push @chars, @range;
565 41 ▶ if (@chars) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chars) { } 13 28 41
566 13 @chars= sort { $a <=> $b } @chars;
567 } else {
568 28 delete $parse{codepoints};
569 }
570 41 return \%parse;
571 }
572  
573 3 ▶sub _ord_to_safe_regex_char {
Subroutine _ord_to_safe_regex_char: called CC=3 SCAR=16.0
574 3 ▶ return chr($_[0]) =~ /[\w]/? chr $_[0]
Branch: 2 branches
BranchTrueFalseTotal
chr($_[0]) =~ /[\w]/ ? : 3 0 3
$_[0] <= 255 ? : 0 0 0
575 : $_[0] <= 0xFF? sprintf('\x%02X',$_[0])
576 : sprintf('\x{%X}',$_[0])
577 }
578  
579 1 ▶sub _deparse_charset {
Subroutine _deparse_charset: called CC=9 SCAR=31.6
580 1 my $parse= shift;
581 1 my $str= '';
582 1 ▶ if (my $cp= $parse->{codepoints}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cp = $parse->{"codepoints"}) 1 0 1
583 $str .= _ord_to_safe_regex_char($_)
584 1 for @$cp;
585 }
586 1 ▶ if (my $r= $parse->{codepoint_ranges}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $parse->{"codepoint_ranges"}) 0 1 1
587 0 ▶ for (my $i= 0; $i < (@$r << 1); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$r << 1) 0 0 0
588 0 $str .= _ord_to_safe_regex_char($r->[$i*2]) . '-' . _ord_to_safe_regex_char($r->[$i*2+1]);
589 }
590 }
591 1 ▶ if (my $cl= $parse->{classes}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cl = $parse->{"classes"}) 0 1 1
592 # TODO: reverse conversions to \h \v etc.
593 0 for (@$cl) {
594 0 ▶ $str .= $_ eq '\N'? '\0-\x09\x0B-\x{10FFFF}'
Branch: 2 branches
BranchTrueFalseTotal
$_ eq '\\N' ? : 0 0 0
ord $_ == 94 ? : 0 0 0
595 : ord == ord '^'? '\P{'.substr($_,1).'}'
596 : '\p{'.$_.'}';
597 }
598 }
599 1 return $str;
600 }
601  
602  
603 857 ▶sub get_member {
Subroutine get_member: called CC=2 SCAR=6.9
Pod documented
604 857 ▶ $_[0]{members}? $_[0]{members}[$_[1]]
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'members'} ? : 1 856 857
605 : chr _get_invlist_element($_[1], $_[0]->member_invlist, $_[0]->_invlist_index);
606 }
607  
608 6 ▶sub get_member_codepoint {
Subroutine get_member_codepoint: called CC=2 SCAR=7.6
Pod documented
609 6 ▶ $_[0]{members}? ord $_[0]{members}[$_[1]]
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'members'} ? : 6 0 6
610 : _get_invlist_element($_[1], $_[0]->member_invlist, $_[0]->_invlist_index);
611 }
612  
613 1272 ▶sub _get_invlist_element {
Subroutine _get_invlist_element: called CC=8 SCAR=21.1
614 1272 my ($ofs, $invlist, $invlist_index)= @_;
615 1272 ▶ $ofs += @$invlist_index if $ofs < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ofs < 0 0 1272 1272
616 1272 ▶ return undef if $ofs >= $invlist_index->[-1] || $ofs < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ofs >= $invlist_index->[-1] or $ofs < 0 0 1272 1272
Condition33%1/3
$ofs >= $invlist_index->[-1] or $ofs < 0
l!l&&r!l&&!r
001272
MC/DC0%0/2
$ofs >= $invlist_index->[-1] or $ofs < 0
$ofs >= $invlist_index->[-1] $ofs < 0
Truth table33%1/3
A or B
A= $ofs >= $invlist_index->[-1]B= $ofs < 0
ABresult
1X1
011
000
617 # Binary Search to find the range that contains this numbered element
618 1272 my ($min, $max, $mid)= (0, $#$invlist_index);
619 1272 while (1) {
620 7272 $mid= ($min+$max) >> 1;
621 7272 ▶ if ($ofs >= $invlist_index->[$mid]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ofs >= $invlist_index->[$mid]) { } 3708 3564 7272
elsif ($mid > 0 and $ofs < $$invlist_index[$mid - 1]) { } 2292 1272 3564
622 3708 $min= $mid+1
623 }
624 ▶ elsif ($mid > 0 && $ofs < $invlist_index->[$mid-1]) {
Condition100%3/3
$mid > 0 and $ofs < $$invlist_index[$mid - 1]
!ll&&!rl&&r
7311992292
MC/DC100%2/2
$mid > 0 and $ofs < $$invlist_index[$mid - 1]
$mid > 0 $ofs < $$invlist_index[$mid - 1]
Truth table100%3/3
A and B
A= $mid > 0B= $ofs < $$invlist_index[$mid - 1]
ABresult
0X0
100
111
625 2292 $max= $mid-1
626 }
627 else {
628 1272 ▶ $ofs -= $invlist_index->[$mid-1] if $mid > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $mid > 0 1199 73 1272
629 1272 return $invlist->[$mid*2] + $ofs;
630 }
631 }
632 }
633  
634  
635 862 ▶sub find_member {
Subroutine find_member: called CC=1 SCAR=0.0
Pod documented
636 862 my ($self, $char)= @_;
637 862 return _find_invlist_element(ord $char, $self->member_invlist, $self->_invlist_index);
638 }
639  
640 895 ▶sub _find_invlist_element {
Subroutine _find_invlist_element: called CC=9 SCAR=22.0
641 895 my ($codepoint, $invlist, $index)= @_;
642 # Binary Search to find the range that contains this numbered element
643 895 my ($min, $max, $mid)= (0, $#$invlist);
644 895 while (1) {
645 6718 $mid= ($min+$max) >> 1;
646 6718 ▶ if ($mid > 0 && $codepoint < $invlist->[$mid]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mid > 0 and $codepoint < $invlist->[$mid]) { } 2621 4097 6718
elsif ($mid < $invlist->$#* and $codepoint >= $$invlist[$mid + 1]) { } 3202 895 4097
Condition100%3/3
$mid > 0 and $codepoint < $invlist->[$mid]
!ll&&!rl&&r
940882621
MC/DC100%2/2
$mid > 0 and $codepoint < $invlist->[$mid]
$mid > 0 $codepoint < $invlist->[$mid]
Truth table100%3/3
A and B
A= $mid > 0B= $codepoint < $invlist->[$mid]
ABresult
0X0
100
111
647 2621 $max= $mid-1
648 }
649 ▶ elsif ($mid < $#$invlist && $codepoint >= $invlist->[$mid+1]) {
Condition100%3/3
$mid < $invlist->$#* and $codepoint >= $$invlist[$mid + 1]
!ll&&!rl&&r
188773202
MC/DC100%2/2
$mid < $invlist->$#* and $codepoint >= $$invlist[$mid + 1]
$mid < $invlist->$#* $codepoint >= $$invlist[$mid + 1]
Truth table100%3/3
A and B
A= $mid < $invlist->$#*B= $codepoint >= $$invlist[$mid + 1]
ABresult
0X0
100
111
650 3202 $min= $mid+1;
651 }
652 else {
653 895 ▶ return (undef, 0) unless $codepoint >= $invlist->[$mid];
Branch: 1 branch
BranchTrueFalseTotal
unless $codepoint >= $invlist->[$mid] 1 894 895
654 894 ▶ return $codepoint - $invlist->[$mid] unless $mid > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $mid > 0 8 886 894
655 886 ▶ return $codepoint - $invlist->[$mid] + $index->[($mid >> 1) - 1] unless $mid & 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $mid & 1 873 13 886
656 # if $mid is an odd number, the range is excluded, and there is no match
657 13 ▶ return undef unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 2 11 13
658 11 return (undef, $index->[($mid-1)>>1]) # return insertion point as second val
659 }
660 }
661 }
662  
663  
664 0 ▶sub negate {
Subroutine negate: not called CC=1 SCAR=6.9
Pod documented
665 0 my $self= shift;
666 0 my $neg= Mock::Data::Charset::Util::negate_invlist($self->member_invlist, $self->max_codepoint);
667 0 return $self->new(member_invlist => $neg);
668 }
669 5 ▶sub Mock::Data::Charset::Util::negate_invlist {
Subroutine negate_invlist: called CC=5 SCAR=20.9
670 5 my ($invlist, $max_codepoint)= @_;
671 # Toggle first char of 0
672 0 ▶ $invlist= $invlist->[0]? [ 0, @$invlist ] : [ @{$invlist}[1..$#$invlist] ];
Branch: 1 branch
BranchTrueFalseTotal
$invlist->[0] ? : 5 0 5
673 # If max_codepoint is defined, and was the final char, remove the range starting at max_codepoint+1
674 5 ▶ if (@$invlist & 1 and defined $max_codepoint and $invlist->[-1] == $max_codepoint+1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$invlist & 1 and defined $max_codepoint and $invlist->[-1] == $max_codepoint + 1) 0 5 5
Condition33%2/6
@$invlist & 1 and defined $max_codepoint and $invlist->[-1] == $max_codepoint + 1
!ll&&!rl&&r
500
@$invlist & 1 and defined $max_codepoint
!ll&&!rl&&r
050
MC/DC0%0/3
@$invlist & 1 and defined $max_codepoint and $invlist->[-1] == $max_codepoint + 1
@$invlist & 1 defined $max_codepoint $invlist->[-1] == $max_codepoint + 1
Truth table25%1/4
A and B and C
A= @$invlist & 1B= defined $max_codepointC= $invlist->[-1] == $max_codepoint + 1
ABCresult
0XX0
10X0
1100
1111
675 0 pop @$invlist;
676 }
677 5 return $invlist;
678 }
679  
680  
681 0 ▶sub union {
Subroutine union: not called CC=3 SCAR=24.8
Pod documented
682 0 my $self= $_[0];
683 0 my @invlists= @_;
684 ref eq 'ARRAY' || ($_= $_->member_invlist)
685 0 ▶ for @invlists;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq "ARRAY" or $_ = $_->member_invlist 0 0 0
686 0 my $combined= Mock::Data::Charset::Util::merge_invlists(\@invlists, $self->max_codepoint);
687 0 return $self->new(member_invlist => $combined);
688 }
689  
690 #=head2 merge_invlists
691 #
692 # my $combined_invlist= $charset->merge_invlist( \@list2, \@list3, ... );
693 # my $combined_invlist= merge_invlist( \@list1, \@list2, ... );
694 #
695 #Merge one or more inversion lists into a superset of all of them.
696 #If called as a method, the L</member_invlist> is used as the first list.
697 #
698 #The return value is an inversion list, which can be wrapped in a Charset object by passing it
699 #as the C<member_invlist> attribute.
700 #
701 #The current L</max_codepoint> applies to the result. If called as a plain function, the
702 #C<max_codepoint> is assumed to be the Unicode maximum of C<0x10FFFF>.
703 #
704 #=cut
705  
706 38 ▶sub Mock::Data::Charset::Util::merge_invlists {
Subroutine merge_invlists: called CC=18 SCAR=29.0
707 38 my @invlists= @{shift()};
708 38 ▶ my $max_codepoint= shift // 0x10FFFF;
Condition100%2/2
shift() // 1114111
l!l
308
MC/DC100%1/1
shift() // 1114111
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
709  
710 38 ▶ return [] unless @invlists;
Branch: 1 branch
BranchTrueFalseTotal
unless @invlists 0 38 38
711 38 ▶ return [@{$invlists[0]}] unless @invlists > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @invlists > 1 24 14 38
712 14 my @combined= ();
713 # Repeatedly select the minimum range among the input lists and add it to the result
714 14 my @pos= (0)x@invlists;
715 14 ▶ while (@invlists) {
Branch: 1 branch
BranchTrueFalseTotal
if (@invlists) 4031 6 4037
716 4031 my ($min_ch, $min_i)= ($invlists[0][$pos[0]], 0);
717 # Find which inversion list contains the lowest range
718 4031 ▶ for (my $i= 1; $i < @invlists; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @invlists) 4022 4031 8053
719 4022 ▶ if ($invlists[$i][$pos[$i]] < $min_ch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invlists[$i][$pos[$i]] < $min_ch) 985 3037 4022
720 985 $min_ch= $invlists[$i][$pos[$i]];
721 985 $min_i= $i;
722 }
723 }
724 4031 ▶ last if $min_ch > $max_codepoint;
Branch: 1 branch
BranchTrueFalseTotal
if $min_ch > $max_codepoint 2 4029 4031
725 # Check for overlap of this new inclusion range with the previous
726 4029 ▶ if (@combined && $combined[-1] >= $min_ch) {
Branch: 1 branch
BranchTrueFalseTotal
if (@combined and $combined[-1] >= $min_ch) { } 3795 234 4029
Condition100%3/3
@combined and $combined[-1] >= $min_ch
!ll&&!rl&&r
142203795
MC/DC100%2/2
@combined and $combined[-1] >= $min_ch
@combined $combined[-1] >= $min_ch
Truth table100%3/3
A and B
A= @combinedB= $combined[-1] >= $min_ch
ABresult
0X0
100
111
727 # they overlap, so just replace the end-codepoint of the range
728 # if the new endpoint is larger
729 3795 my $new_end= $invlists[$min_i][$pos[$min_i]+1];
730 3795 ▶ $combined[-1]= $new_end if !defined $new_end || $combined[-1] < $new_end;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $new_end or $combined[-1] < $new_end 1569 2226 3795
Condition100%3/3
not defined $new_end or $combined[-1] < $new_end
l!l&&r!l&&!r
315662226
MC/DC100%2/2
not defined $new_end or $combined[-1] < $new_end
not defined $new_end $combined[-1] < $new_end
Truth table100%3/3
not(A) or B
A= not defined $new_endB= $combined[-1] < $new_end
ABresult
1X1
011
000
731 }
732 else {
733 # else, simply append the range
734 234 push @combined, @{$invlists[$min_i]}[$pos[$min_i] .. $pos[$min_i]+1];
735 }
736 # If the list is empty now, remove it from consideration
737 4029 ▶ if (($pos[$min_i] += 2) >= @{$invlists[$min_i]}) {
Branch: 2 branches
BranchTrueFalseTotal
if (($pos[$min_i] += 2) >= @{$invlists[$min_i];}) { } 21 4008 4029
elsif (@invlists == 1) { } 0 4008 4008
738 21 splice @invlists, $min_i, 1;
739 21 splice @pos, $min_i, 1;
740 # If the invlist ends with an infinite range now, we are done
741 21 ▶ if (!defined $combined[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $combined[-1]) 6 15 21
742 6 pop @combined;
743 6 last;
744 }
745 }
746 # If this is the only list remaining, append the rest and done
747 elsif (@invlists == 1) {
748 0 push @combined, @{$invlists[0]}[$pos[0] .. $#{$invlists[0]}];
749 0 last;
750 }
751 }
752 14 ▶ while ($combined[-1] > $max_codepoint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($combined[-1] > $max_codepoint) 1 14 15
753 1 pop @combined;
754 }
755 # If the list ends with inclusion, and the max_codepoint is less than unicode max,
756 # end the list with it.
757 14 ▶ if (1 & @combined and $max_codepoint < 0x10FFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 & @combined and $max_codepoint < 1114111) 1 13 14
Condition100%3/3
1 & @combined and $max_codepoint < 1114111
!ll&&!rl&&r
761
MC/DC100%2/2
1 & @combined and $max_codepoint < 1114111
1 & @combined $max_codepoint < 1114111
Truth table100%3/3
A and B
A= 1 & @combinedB= $max_codepoint < 1114111
ABresult
0X0
100
111
758 1 push @combined, $max_codepoint+1;
759 }
760 14 return \@combined;
761 }
762  
763 12 1;
764  
765 __END__
« Data/Regex.pm ↑ Summary Data/Plugin/Text.pm »