blib/lib/String/CaseProfile.pm

filter: Statementstmt 94.1% 161 / 171 Branchbran 90.8% 89 / 98 Conditioncond 63.6% 21 / 33 MC/DCmcdc 50.0% 11 / 22 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 87.6% 299 / 341 CC 62 SCAR 28.5
CC
62
Cov
90%
set_profile
31.5
copy_profile
14.7
get_profile
12.0
CRAP 17.2
↑ Summary
1 package String::CaseProfile;
2  
3 5 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 our @EXPORT_OK = qw(
11 get_profile
12 set_profile
13 copy_profile
14 );
15  
16 5 our %EXPORT_TAGS = ( 'all' => [ @EXPORT_OK ] );
17  
18 5 our $VERSION = '0.18';
19  
20  
21 5 my $word_re = qr{
22 \b(?:\p{Lu}{1,2}\.)+(?:\P{L}|$)
23 |
24 \b\p{Lu}{1,2}\/\p{Lu}{1,2}\b
25 |
26 (?:
27 \p{L}
28 |
29 (?<=\p{L})[-'\x92_&](?=\p{L})
30 |
31 (?<=[lL])\xB7(?=[lL])
32 |
33 \d
34 )+
35 }x;
36  
37  
38 5 my %types = (
39 '1st_uc' => 'f',
40 'all_uc' => 'u',
41 'all_lc' => 'l',
42 'other' => 'o',
43 );
44  
45  
46 54 ▶sub get_profile {
Subroutine get_profile: called CC=12 SCAR=24.9
Pod documented
47 54 my $string = shift;
48
49 54 my (@excluded, $strict);
50
51 54 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 5 49 54
52
53 5 ▶ if ($_[0]->{exclude}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"exclude"}) 3 2 5
54 3 @excluded = @{$_[0]->{exclude}};
55 }
56
57 5 $strict = $_[0]->{strict};
58
59 } else {
60
61 49 ▶ if ( defined $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 7 42 49
62 7 @excluded = @{ $_[0]} ;
63 }
64
65 }
66  
67 # read excluded words, if any
68 54 my %excluded;
69 54 ▶ if ( @excluded > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@excluded > 0) 10 44 54
70 10 $excluded{$_}++ foreach ( @excluded );
71 }
72
73 54 my @words = $string =~ /($word_re)/g;
74
75 54 my @word_types;
76 54 ▶ if ( @words == 1 && length $words[0] == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words == 1 and length $words[0] == 1) { } 4 50 54
Condition66%2/3
@words == 1 and length $words[0] == 1
!ll&&!rl&&r
5004
MC/DC50%1/2
@words == 1 and length $words[0] == 1
@words == 1 length $words[0] == 1
Truth table66%2/3
A and B
A= @words == 1B= length $words[0] == 1
ABresult
0X0
100
111
77
78 4 ▶ if ($words[0] =~ /^\p{Lu}$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($words[0] =~ /^\p{Lu}$/) { } 1 3 4
elsif ($words[0] =~ /^\p{Ll}$/) { } 1 2 3
79
80 1 push @word_types, 'all_uc';
81
82 } elsif ($words[0] =~ /^\p{Ll}$/) {
83
84 1 push @word_types, 'all_lc';
85
86 } else {
87
88 2 push @word_types, 'other';
89 }
90
91 } else {
92
93 @word_types = map {
94
95 50 ▶ _exclude($_, \%excluded)
Branch: 1 branch
BranchTrueFalseTotal
_exclude($_, \%excluded) ? : 10 169 179
96 ?
97 'excluded'
98 :
99 _word_type($_)
100
101 } @words;
102
103 }
104
105 54 my %profile;
106 54 ( $profile{fold}, $profile{string_type} ) = _string_type($strict, @word_types);
107
108 54 ▶ for (my $i = 0; $i <= $#words; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#words) 183 54 237
109 183 push @{$profile{words}}, {
110 word => $words[$i],
111 type => $word_types[$i],
112 }
113 }
114
115 54 $profile{report} = _create_report($string, \%profile);
116
117 54 return %profile;
118 }
119  
120  
121 179 ▶sub _exclude {
Subroutine _exclude: called CC=4 SCAR=13.9
122 179 my ($word, $excluded_href) = @_;
123
124 179 ▶ return 1 if $excluded_href->{$word};
Branch: 1 branch
BranchTrueFalseTotal
if $excluded_href->{$word} 8 171 179
125  
126 171 ▶ if ($word =~ /[-']/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ /[-']/) { } 5 166 171
127 5 my @pieces = split /[-']/, $word;
128 5 my @excluded = grep { $excluded_href->{$_} } @pieces;
129 5 ▶ if (@excluded) { return 1 } else { return 0 };
Branch: 1 branch
BranchTrueFalseTotal
if (@excluded) { } 2 3 5
130 } else {
131 166 return 0;
132 }
133 }
134  
135 54 ▶sub _create_report {
Subroutine _create_report: called CC=2 SCAR=6.9
136 54 my ($string, $prof_href) = @_;
137
138 54 my %prof = %{$prof_href};
139
140 54 my $report;
141 54 $report .= "String: $string\n";
142 54 $report .= "Type: $prof{string_type}\n";
143 54 $report .= "Pattern: $prof{fold}\n\n";
144 54 $report .= "Word Type\n--------------------------\n";
145 54 ▶ for ( my $i = 0; $i < scalar(@{$prof{words}}); $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$prof{"words"};}) 183 54 237
146 $report .= sprintf "%-20s%-20s\n", $prof{words}[$i]->{word},
147 183 $prof{words}[$i]->{type};
148 }
149
150 54 return $report;
151 }
152  
153  
154 25 ▶sub set_profile {
Subroutine set_profile: called CC=30 SCAR=34.5
Pod documented
155 25 my ($string, %ref_profile) = @_;
156  
157 25 my %string_profile = get_profile($string, $ref_profile{exclude});
158
159 25 my @words = map { $_->{word} } @{$string_profile{words}};
160 25 my @word_types = map { $_->{type} } @{$string_profile{words}};
161
162 25 my $force = $ref_profile{'force_change'};
163
164 # validate string_type
165 25 my ($legal, $ref_string_type);
166 25 ▶ if ($ref_profile{string_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_profile{"string_type"}) 19 6 25
167 19 $ref_string_type = $ref_profile{string_type};
168 19 ▶ if ($types{$ref_string_type} && $ref_string_type ne 'other') {
Branch: 2 branches
BranchTrueFalseTotal
if ($types{$ref_string_type} and $ref_string_type ne 'other') { } 16 3 19
elsif ($ref_string_type eq 'other') { } 2 1 3
Condition100%3/3
$types{$ref_string_type} and $ref_string_type ne "other"
!ll&&!rl&&r
1216
MC/DC100%2/2
$types{$ref_string_type} and $ref_string_type ne "other"
$types{$ref_string_type} $ref_string_type ne "other"
Truth table100%3/3
A and B
A= $types{$ref_string_type}B= $ref_string_type ne "other"
ABresult
0X0
100
111
169 16 $legal = 1;
170 } elsif ($ref_string_type eq 'other') {
171 2 return $string;
172 } else {
173 1 carp "Illegal value of string_type";
174 }
175 }
176
177 23 my @transformed;
178
179 23 ▶ if ($legal) {
Branch: 2 branches
BranchTrueFalseTotal
if ($legal) { } 16 7 23
elsif ($ref_profile{'custom'}) { } 6 1 7
180 16 ▶ if ($ref_string_type eq '1st_uc') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_string_type eq '1st_uc') { } 4 12 16
181 4 ▶ if ($word_types[0] eq 'excluded') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word_types[0] eq 'excluded') { } 0 4 4
182 0 $transformed[0] = $words[0];
183 } else {
184 4 $transformed[0] = _transform(
185 '1st_uc',
186 $words[0],
187 $word_types[0],
188 $force
189 );
190 }
191 4 ▶ for (my $i = 1; $i <= $#words; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#words) 13 4 17
192 13 ▶ if ($word_types[$i] eq 'excluded') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word_types[$i] eq 'excluded') { } 1 12 13
193 1 push @transformed, $words[$i];
194 } else {
195 12 push @transformed, _transform(
196 'all_lc',
197 $words[$i],
198 $word_types[$i],
199 $force
200 );
201 }
202 }
203 } else {
204 12 ▶ for (my $i = 0; $i <= $#words; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#words) 48 12 60
205 48 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($word_types[$i] eq 'excluded' and $ref_string_type ne 'all_uc') { } 2 46 48
Condition100%3/3
$word_types[$i] eq "excluded" and $ref_string_type ne "all_uc"
!ll&&!rl&&r
4512
MC/DC100%2/2
$word_types[$i] eq "excluded" and $ref_string_type ne "all_uc"
$word_types[$i] eq "excluded" $ref_string_type ne "all_uc"
Truth table100%3/3
A and B
A= $word_types[$i] eq "excluded"B= $ref_string_type ne "all_uc"
ABresult
0X0
100
111
206 $word_types[$i] eq 'excluded'
207 && $ref_string_type ne 'all_uc'
208 ) {
209 2 push @transformed, $words[$i];
210 } else {
211 46 push @transformed, _transform(
212 $ref_string_type,
213 $words[$i],
214 $word_types[$i],
215 $force
216 );
217 }
218 }
219 }
220
221 # custom profile
222 } elsif ($ref_profile{custom}) {
223
224 # validate default type
225 6 my ($type, $default_type);
226 6 ▶ if ($ref_profile{custom}->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_profile{"custom"}{"default"}) 4 2 6
227 4 $type = $ref_profile{custom}->{default};
228 4 ▶ if ($types{$type} && $types{$type} ne 'other') {
Branch: 1 branch
BranchTrueFalseTotal
if ($types{$type} and $types{$type} ne 'other') { } 3 1 4
Condition66%2/3
$types{$type} and $types{$type} ne "other"
!ll&&!rl&&r
103
MC/DC50%1/2
$types{$type} and $types{$type} ne "other"
$types{$type} $types{$type} ne "other"
Truth table66%2/3
A and B
A= $types{$type}B= $types{$type} ne "other"
ABresult
0X0
100
111
229 3 $default_type = $type;
230 } else {
231 1 carp "Illegal default value in custom profile";
232 }
233 }
234
235 6 ▶ for (my $i = 0; $i <= $#word_types; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#word_types) 22 6 28
236
237 22 my $in_index = $ref_profile{custom}->{index}->{$i};
238 22 my $trigger_type = $ref_profile{custom}->{$word_types[$i]};
239
240 22 ▶ if ($in_index) {
Branch: 3 branches
BranchTrueFalseTotal
if ($in_index) { } 3 19 22
elsif ($trigger_type) { } 5 14 19
elsif ($default_type) { } 7 7 14
241 3 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($word_types[$i] eq 'excluded' and $in_index ne 'all_uc') { } 1 2 3
elsif ($in_index ne $word_types[$i]) { } 2 0 2
Condition66%2/3
$word_types[$i] eq "excluded" and $in_index ne "all_uc"
!ll&&!rl&&r
201
MC/DC50%1/2
$word_types[$i] eq "excluded" and $in_index ne "all_uc"
$word_types[$i] eq "excluded" $in_index ne "all_uc"
Truth table66%2/3
A and B
A= $word_types[$i] eq "excluded"B= $in_index ne "all_uc"
ABresult
0X0
100
111
242 $word_types[$i] eq 'excluded'
243 && $in_index ne 'all_uc'
244 ) {
245 1 push @transformed, $words[$i];
246 } elsif ($in_index ne $word_types[$i]) {
247 2 push @transformed, _transform(
248 $in_index,
249 $words[$i],
250 $word_types[$i],
251 $force
252 );
253 } else {
254 0 push @transformed, $words[$i];
255 }
256 } elsif ($trigger_type) {
257 5 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($word_types[$i] eq 'excluded' and $ref_string_type ne 'all_uc') { } 0 5 5
Condition33%1/3
$word_types[$i] eq "excluded" and $ref_string_type ne "all_uc"
!ll&&!rl&&r
500
MC/DC0%0/2
$word_types[$i] eq "excluded" and $ref_string_type ne "all_uc"
$word_types[$i] eq "excluded" $ref_string_type ne "all_uc"
Truth table33%1/3
A and B
A= $word_types[$i] eq "excluded"B= $ref_string_type ne "all_uc"
ABresult
0X0
100
111
258 $word_types[$i] eq 'excluded'
259 && $ref_string_type ne 'all_uc'
260 ) {
261 0 push @transformed, $words[$i];
262 } else {
263 5 push @transformed, _transform(
264 $trigger_type,
265 $words[$i],
266 $word_types[$i],
267 $force
268 );
269 }
270  
271 } elsif ($default_type) { # use default type
272 7 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($word_types[$i] eq 'excluded' and $ref_string_type ne 'all_uc') { } 0 7 7
Condition33%1/3
$word_types[$i] eq "excluded" and $ref_string_type ne "all_uc"
!ll&&!rl&&r
700
MC/DC0%0/2
$word_types[$i] eq "excluded" and $ref_string_type ne "all_uc"
$word_types[$i] eq "excluded" $ref_string_type ne "all_uc"
Truth table33%1/3
A and B
A= $word_types[$i] eq "excluded"B= $ref_string_type ne "all_uc"
ABresult
0X0
100
111
273 $word_types[$i] eq 'excluded'
274 && $ref_string_type ne 'all_uc'
275 ) {
276 0 push @transformed, $words[$i];
277 } else {
278 7 push @transformed, _transform(
279 $default_type,
280 $words[$i],
281 $word_types[$i],
282 $force
283 );
284 }
285 } else {
286 7 push @transformed, $words[$i];
287 }
288 }
289 }
290
291 # transform string
292 23 ▶ if (@transformed) {
Branch: 1 branch
BranchTrueFalseTotal
if (@transformed) 22 1 23
293 22 ▶ for (my $i = 0; $i <= $#words; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#words) 87 22 109
294 87 $string =~ s/\b$words[$i]\b/$transformed[$i]/;
295 }
296 }
297  
298 23 return $string;
299 }
300  
301  
302 7 ▶sub copy_profile {
Subroutine copy_profile: called CC=8 SCAR=26.9
Pod documented
303 7 my %options = @_;
304
305 7 my $from = $options{from};
306 7 my $to = $options{to};
307 7 my $strict = $options{strict};
308 7 my $exclude = $options{exclude};
309
310 7 ▶ if ( $from && $to ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($from and $to) { } 7 0 7
elsif (not $from || $to) { } 0 0 0
elsif (not $from) { } 0 0 0
Condition33%1/3
$from and $to
!ll&&!rl&&r
007
MC/DC0%0/2
$from and $to
$from $to
Truth table33%1/3
A and B
A= $fromB= $to
ABresult
0X0
100
111
311
312 7 ▶ if ( $exclude || $strict ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exclude or $strict) { } 2 5 7
Condition100%3/3
$exclude or $strict
l!l&&r!l&&!r
115
MC/DC100%2/2
$exclude or $strict
$exclude $strict
Truth table100%3/3
A or B
A= $excludeB= $strict
ABresult
1X1
011
000
313
314 2 my %ref_profile = get_profile( $from, {
315 exclude => $exclude,
316 strict => $strict
317 }
318 );
319
320 2 $ref_profile{exclude} = $exclude;
321
322 2 return set_profile( $to, %ref_profile );
323
324 } else {
325
326 5 return set_profile($options{to}, get_profile($options{from}));
327
328 }
329
330 ▶ } elsif ( !$from && !$to ) {
Condition0%0/3
$from || $to
l!l&&r!l&&!r
000
MC/DC0%0/2
$from || $to
$from $to
Truth table0%0/3
A || B
A= $fromB= $to
ABresult
1X1
011
000
331
332 0 carp "Missing parameters\n";
333 0 return '';
334
335 } elsif ( !$from ) {
336
337 0 carp "Missing reference string\n";
338 0 return $to;
339
340 } else {
341
342 0 carp "Missing target string\n";
343 0 return '';
344
345 }
346 }
347  
348  
349 169 ▶sub _word_type {
Subroutine _word_type: called CC=5 SCAR=16.1
350 169 my ($word) = @_;
351
352 169 ▶ if ($word =~ /^[bcdfghjklmnpqrstvwxyz]$/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($word =~ /^[bcdfghjklmnpqrstvwxyz]$/i) { } 4 165 169
elsif ($word =~ /^\p{Lu}(?:\p{Ll}|[-'\x92\xB7])*$/) { } 23 142 165
elsif ($word =~ /^(?:\p{Ll}|[-'\x92\xB7])+$/) { } 116 26 142
elsif ($word =~ /^(?:\p{Lu}|[-'\x92\xB7])+$/) { } 18 8 26
353 4 return 'other';
354 } elsif ($word =~ /^\p{Lu}(?:\p{Ll}|[-'\x92\xB7])*$/) {
355 23 return '1st_uc';
356 } elsif ($word =~ /^(?:\p{Ll}|[-'\x92\xB7])+$/) {
357 116 return 'all_lc';
358 } elsif ($word =~ /^(?:\p{Lu}|[-'\x92\xB7])+$/) {
359 18 return 'all_uc';
360 } else {
361 8 return 'other';
362 }
363
364 }
365  
366  
367 54 ▶sub _string_type {
Subroutine _string_type: called CC=5 SCAR=16.1
368
369 54 my $strict = shift;
370 54 my @types = @_;
371
372 54 my $types_str = join "", map { $types{$_} } grep { $_ ne 'excluded' } @types;
373
374 # remove 'other' word types
375 54 my $clean_str = $types_str;
376 54 ▶ $clean_str =~ s/o//g unless $strict;
Branch: 1 branch
BranchTrueFalseTotal
unless $strict 52 2 54
377
378 54 my $string_type;
379
380 54 ▶ if ($clean_str =~ /^fl*$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($clean_str =~ /^fl*$/) { } 21 33 54
elsif ($clean_str =~ /^u+$/) { } 9 24 33
elsif ($clean_str =~ /^l+$/) { } 20 4 24
381 21 $string_type = '1st_uc';
382 } elsif ($clean_str =~ /^u+$/) {
383 9 $string_type = 'all_uc';
384 } elsif ($clean_str =~ /^l+$/) {
385 20 $string_type = 'all_lc';
386 } else {
387 4 $string_type = 'other';
388 }
389
390 54 return ($types_str, $string_type);
391 }
392  
393  
394 76 ▶sub _transform {
Subroutine _transform: called CC=3 SCAR=11.0
395 76 my ($type, $word, $word_type, $force) = @_;
396
397 76 ▶ return $word if ($word_type eq 'other' && !$force);
Branch: 1 branch
BranchTrueFalseTotal
if $word_type eq "other" and not $force 2 74 76
Condition100%3/3
$word_type eq "other" and not $force
!ll&&!rl&&r
7312
MC/DC100%2/2
$word_type eq "other" and not $force
$word_type eq "other" not $force
Truth table100%3/3
A and not(B)
A= $word_type eq "other"B= not $force
ABresult
0X0
100
111
398
399 74 my %dispatch = (
400 '1st_uc' => ucfirst(lc($word)),
401 'all_uc' => uc($word),
402 'all_lc' => lc($word),
403 'other' => $word,
404 );
405
406 74 $dispatch{$type};
407 }
408  
409  
410 5 1;
411 __END__
↑ Summary