blib/lib/Unicode/CheckUTF8/PP.pm

filter: Statementstmt 93.1% 54 / 58 Branchbran 63.0% 29 / 46 Conditioncond 41.6% 5 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 73.8% 96 / 130 CC 29 SCAR 33.3
CC
29
Cov
76%
__ANON__
41.3
__ANON__
9.2
BEGIN
1.0
CRAP 28.0
↑ Summary
1 package Unicode::CheckUTF8::PP;
2 3 $Unicode::CheckUTF8::PP::VERSION = '0.002';
3 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 # ABSTRACT: Pure Perl implementation of Unicode::CheckUTF8
5  
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our @EXPORT = qw();
10 3 our @EXPORT_OK = qw(is_utf8);
11  
12 3 my @validSingleByte = (
13 0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
14 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
15 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
16 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
17 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
18 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
19 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
20 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0
21 );
22
23 3 my @trailingBytesForUTF8 = (
24 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
25 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
26 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
27 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
28 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
29 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
30 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
31 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
32 );
33  
34 my $_isLegalUTF8 = sub {
35 47096 ▶ my ($bytes, $length) = @_;
Subroutine __ANON__: called CC=21 SCAR=37.2
36  
37 47096 my $i = $length;
38 47096 ▶ if ( $length <= 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length <= 4) { } 47095 1 47096
39 47095 my $c;
40 47095 ▶ if ( $length == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length == 4) 1 47094 47095
41 1 $c = $bytes->[--$i];
42 1 ▶ return 0 unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 1 1
43 1 ▶ return 0 if $c < 0x80 || $c > 0xBF;
Branch: 1 branch
BranchTrueFalseTotal
if $c < 128 or $c > 191 0 1 1
Condition33%1/3
$c < 128 or $c > 191
l!l&&r!l&&!r
001
MC/DC0%0/2
$c < 128 or $c > 191
$c < 128 $c > 191
Truth table33%1/3
A or B
A= $c < 128B= $c > 191
ABresult
1X1
011
000
44 }
45
46 47095 ▶ if ( $length >= 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length >= 3) 502 46593 47095
47 502 $c = $bytes->[--$i];
48 502 ▶ return 0 unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 502 502
49 502 ▶ return 0 if $c < 0x80 || $c > 0xBF;
Branch: 1 branch
BranchTrueFalseTotal
if $c < 128 or $c > 191 0 502 502
Condition33%1/3
$c < 128 or $c > 191
l!l&&r!l&&!r
00502
MC/DC0%0/2
$c < 128 or $c > 191
$c < 128 $c > 191
Truth table33%1/3
A or B
A= $c < 128B= $c > 191
ABresult
1X1
011
000
50 }
51
52 47095 ▶ if ( $length >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length >= 2) 771 46324 47095
53 771 $c = $bytes->[--$i];
54 771 ▶ return 0 unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 1 770 771
55 770 ▶ return 0 if $c > 0xBF;
Branch: 1 branch
BranchTrueFalseTotal
if $c > 191 0 770 770
56
57 770 ▶ if ( $bytes->[$i] == 0xE0 ) { return 0 if $c < 0xA0; }
Branch: 4 branches
BranchTrueFalseTotal
if ($bytes->[$i] == 224) { } 0 770 770
elsif ($bytes->[$i] == 240) { } 0 770 770
elsif ($bytes->[$i] == 240) { } 0 770 770
if $c < 160 0 0 0
58 0 ▶ elsif ( $bytes->[$i] == 0xF0 ) { return 0 if $c < 0x90; }
Branch: 1 branch
BranchTrueFalseTotal
if $c < 144 0 0 0
59 0 ▶ elsif ( $bytes->[$i] == 0xF0 ) { return 0 if $c > 0x8F; }
Branch: 1 branch
BranchTrueFalseTotal
if $c > 143 0 0 0
60 770 ▶ else { return 0 if $c < 0x80; }
Branch: 1 branch
BranchTrueFalseTotal
if $c < 128 0 770 770
61 }
62
63 47094 ▶ if ( $length >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length >= 1) 47094 0 47094
64 47094 return $validSingleByte[ $bytes->[0] ];
65 }
66 }
67 else {
68 1 return 0;
69 }
70  
71 0 return 1;
72 3 };
73  
74 my $_isLegalUTF8String = sub {
75 176 ▶ my ($str) = @_;
Subroutine __ANON__: called CC=9 SCAR=22.2
76  
77 176 my @bytes = unpack 'U*', $str;
78 176 my $len = @bytes;
79 176 my $l = 0;
80
81 176 my $i = 0;
82 176 ▶ while ( $i < $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 47097 111 47208
83 47097 my $byte = $bytes[$i];
84 47097 my $length = $trailingBytesForUTF8[$byte] + 1;
85
86 # check for early termination of string
87 47097 foreach my $j ( 1 .. $length - 1 ) {
88 1279 ▶ return 0 unless defined $bytes[$j];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes[$j] 1 1278 1279
89 1278 ▶ return 0 if $bytes[$j] == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $bytes[$j] == 0 0 1278 1278
90 }
91
92 47096 ▶ return 0 unless
Branch: 1 branch
BranchTrueFalseTotal
unless &$_isLegalUTF8([@bytes[$i .. $i + $length - 1]], $length) 64 47032 47096
93 $_isLegalUTF8->([@bytes[$i..$i+$length-1]], $length);
94
95 47032 $l = $bytes[$i];
96 47032 $i += $length;
97 }
98  
99 111 ▶ return (@bytes && $l && $l == $bytes[-1]) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@bytes && $l && $l == $bytes[-1] ? : 109 2 111
Condition50%3/6
@bytes and $l and $l == $bytes[-1]
!ll&&!rl&&r
02109
@bytes and $l
!ll&&!rl&&r
00111
MC/DC33%1/3
@bytes and $l and $l == $bytes[-1]
@bytes $l $l == $bytes[-1]
Truth table50%2/4
A and B and C
A= @bytesB= $lC= $l == $bytes[-1]
ABCresult
0XX0
10X0
1100
1111
100 3 };
101  
102 sub is_utf8 {
103 176 ▶ return $_isLegalUTF8String->(@_);
Subroutine is_utf8: called CC=1 SCAR=0.0
Pod documented
104 }
105  
106 3 1;
107  
108 __END__
↑ Summary