blib/lib/Zuzu/Module/String/Encode.pm

filter: Statementstmt 26.3% 24 / 91 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 69 MC/DCmcdc 0.0% 0 / 39 Subroutinesub 50.0% 7 / 14 Podpod 0.0% 0 / 1 totaltotal 10.7% 31 / 288 CC 63 SCAR 59.5
CC
63
Cov
10%
_decode
702.0
_encode
380.0
_canonical_encoding
240.0
CRAP 384.0
« blib/lib/Zuzu/Module/GUI/Objects.pm ↑ Summary blib/lib/Zuzu/Module/Proc.pm »
1 package Zuzu::Module::String::Encode;
2  
3 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '0.007002';
6  
7 2 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Zuzu::Util::NativeHelpers qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 native_function
12 2 );
13 2 ▶use Zuzu::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Zuzu::Value::BinaryString;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # std/string/encode: character-encoding conversions between String and
17 # BinaryString. UTF-16 and UTF-32 encode to big-endian without a BOM (the
18 # deterministic canonical form shared by all runtimes); decode honours a
19 # leading BOM and otherwise assumes big-endian.
20  
21 0 ▶sub _type_name {
Subroutine _type_name: not called CC=4 SCAR=30.0
22 0 my ( $value ) = @_;
23  
24 0 ▶ return 'Null' if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
25 0 ▶ return 'BinaryString'
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString") 0 0 0
Condition0%0/3
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
builtin::blessed($value) $value->isa("Zuzu::Value::BinaryString")
Truth table0%0/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::BinaryString")
ABresult
0X0
100
111
26 if blessed($value) and $value->isa('Zuzu::Value::BinaryString');
27 0 return 'String';
28 }
29  
30 0 ▶sub _die {
Subroutine _die: not called CC=1 SCAR=6.9
31 0 my ( $message ) = @_;
32  
33 0 die Zuzu::Error->new_runtime(
34 message => $message,
35 file => '<std/string/encode>',
36 line => 0,
37 );
38 }
39  
40 0 ▶sub _canonical_encoding {
Subroutine _canonical_encoding: not called CC=15 SCAR=54.8
41 0 my ( $name ) = @_;
42  
43 0 ▶ my $upper = uc( defined $name ? "$name" : 'UTF-8' );
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 0 0
44 0 $upper =~ s/\s+//g;
45 0 ▶ return 'utf8' if $upper eq 'UTF-8' or $upper eq 'UTF8';
Branch: 1 branch
BranchTrueFalseTotal
if $upper eq "UTF-8" or $upper eq "UTF8" 0 0 0
Condition0%0/3
$upper eq "UTF-8" or $upper eq "UTF8"
l!l&&r!l&&!r
000
MC/DC0%0/2
$upper eq "UTF-8" or $upper eq "UTF8"
$upper eq "UTF-8" $upper eq "UTF8"
Truth table0%0/3
A or B
A= $upper eq "UTF-8"B= $upper eq "UTF8"
ABresult
1X1
011
000
46 0 ▶ return 'utf16' if $upper eq 'UTF-16' or $upper eq 'UTF16' or $upper eq 'UTF-16BE';
Branch: 1 branch
BranchTrueFalseTotal
if $upper eq "UTF-16" or $upper eq "UTF16" or $upper eq "UTF-16BE" 0 0 0
Condition0%0/6
$upper eq "UTF-16" or $upper eq "UTF16" or $upper eq "UTF-16BE"
l!l&&r!l&&!r
000
$upper eq "UTF-16" or $upper eq "UTF16"
l!l&&r!l&&!r
000
MC/DC0%0/3
$upper eq "UTF-16" or $upper eq "UTF16" or $upper eq "UTF-16BE"
$upper eq "UTF-16" $upper eq "UTF16" $upper eq "UTF-16BE"
Truth table0%0/4
A or B or C
A= $upper eq "UTF-16"B= $upper eq "UTF16"C= $upper eq "UTF-16BE"
ABCresult
1XX1
01X1
0011
0000
47 0 ▶ return 'utf32' if $upper eq 'UTF-32' or $upper eq 'UTF32' or $upper eq 'UTF-32BE';
Branch: 1 branch
BranchTrueFalseTotal
if $upper eq "UTF-32" or $upper eq "UTF32" or $upper eq "UTF-32BE" 0 0 0
Condition0%0/6
$upper eq "UTF-32" or $upper eq "UTF32" or $upper eq "UTF-32BE"
l!l&&r!l&&!r
000
$upper eq "UTF-32" or $upper eq "UTF32"
l!l&&r!l&&!r
000
MC/DC0%0/3
$upper eq "UTF-32" or $upper eq "UTF32" or $upper eq "UTF-32BE"
$upper eq "UTF-32" $upper eq "UTF32" $upper eq "UTF-32BE"
Truth table0%0/4
A or B or C
A= $upper eq "UTF-32"B= $upper eq "UTF32"C= $upper eq "UTF-32BE"
ABCresult
1XX1
01X1
0011
0000
48 0 ▶ return 'latin1' if $upper eq 'ISO-8859-1' or $upper eq 'ISO8859-1'
Branch: 1 branch
BranchTrueFalseTotal
if $upper eq "ISO-8859-1" or $upper eq "ISO8859-1" or $upper eq "LATIN-1" or $upper eq "LATIN1" or $upper eq "LATIN" 0 0 0
Condition0%0/12
$upper eq "ISO-8859-1" or $upper eq "ISO8859-1" or $upper eq "LATIN-1" or $upper eq "LATIN1" or $upper eq "LATIN"
l!l&&r!l&&!r
000
$upper eq "ISO-8859-1" or $upper eq "ISO8859-1" or $upper eq "LATIN-1" or $upper eq "LATIN1"
l!l&&r!l&&!r
000
$upper eq "ISO-8859-1" or $upper eq "ISO8859-1" or $upper eq "LATIN-1"
l!l&&r!l&&!r
000
$upper eq "ISO-8859-1" or $upper eq "ISO8859-1"
l!l&&r!l&&!r
000
MC/DC0%0/5
$upper eq "ISO-8859-1" or $upper eq "ISO8859-1" or $upper eq "LATIN-1" or $upper eq "LATIN1" or $upper eq "LATIN"
$upper eq "ISO-8859-1" $upper eq "ISO8859-1" $upper eq "LATIN-1" $upper eq "LATIN1" $upper eq "LATIN"
Truth table0%0/6
A or B or C or D or E
A= $upper eq "ISO-8859-1"B= $upper eq "ISO8859-1"C= $upper eq "LATIN-1"D= $upper eq "LATIN1"E= $upper eq "LATIN"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
49 or $upper eq 'LATIN-1' or $upper eq 'LATIN1' or $upper eq 'LATIN';
50 0 return undef;
51 }
52  
53 0 ▶sub _encode {
Subroutine _encode: not called CC=19 SCAR=59.4
54 0 my ( $text, $encoding ) = @_;
55  
56 0 ▶ _die( 'TypeException: encode expects String, got ' . _type_name( $text ) )
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($text) or ref $text or not defined $text 0 0 0
Condition0%0/6
builtin::blessed($text) or ref $text or not defined $text
l!l&&r!l&&!r
000
builtin::blessed($text) or ref $text
l!l&&r!l&&!r
000
MC/DC0%0/3
builtin::blessed($text) or ref $text or not defined $text
builtin::blessed($text) ref $text not defined $text
Truth table0%0/4
A or B or not(C)
A= builtin::blessed($text)B= ref $textC= not defined $text
ABCresult
1XX1
01X1
0011
0000
57 if blessed($text) or ref($text) or not defined $text;
58  
59 0 my $canonical = _canonical_encoding( $encoding );
60 0 my $bytes;
61 0 ▶ if ( defined $canonical and $canonical eq 'utf8' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $canonical and $canonical eq 'utf8') { } 0 0 0
elsif (defined $canonical and $canonical eq 'utf16') { } 0 0 0
elsif (defined $canonical and $canonical eq 'utf32') { } 0 0 0
elsif (defined $canonical and $canonical eq 'latin1') { } 0 0 0
Condition0%0/3
defined $canonical and $canonical eq "utf8"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $canonical and $canonical eq "utf8"
defined $canonical $canonical eq "utf8"
Truth table0%0/3
A and B
A= defined $canonicalB= $canonical eq "utf8"
ABresult
0X0
100
111
62 0 $bytes = Encode::encode( 'UTF-8', "$text", Encode::FB_CROAK );
63 }
64 ▶ elsif ( defined $canonical and $canonical eq 'utf16' ) {
Condition0%0/3
defined $canonical and $canonical eq "utf16"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $canonical and $canonical eq "utf16"
defined $canonical $canonical eq "utf16"
Truth table0%0/3
A and B
A= defined $canonicalB= $canonical eq "utf16"
ABresult
0X0
100
111
65 0 $bytes = Encode::encode( 'UTF-16BE', "$text", Encode::FB_CROAK );
66 }
67 ▶ elsif ( defined $canonical and $canonical eq 'utf32' ) {
Condition0%0/3
defined $canonical and $canonical eq "utf32"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $canonical and $canonical eq "utf32"
defined $canonical $canonical eq "utf32"
Truth table0%0/3
A and B
A= defined $canonicalB= $canonical eq "utf32"
ABresult
0X0
100
111
68 0 $bytes = Encode::encode( 'UTF-32BE', "$text", Encode::FB_CROAK );
69 }
70 ▶ elsif ( defined $canonical and $canonical eq 'latin1' ) {
Condition0%0/3
defined $canonical and $canonical eq "latin1"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $canonical and $canonical eq "latin1"
defined $canonical $canonical eq "latin1"
Truth table0%0/3
A and B
A= defined $canonicalB= $canonical eq "latin1"
ABresult
0X0
100
111
71 0 for my $ch ( split //, "$text" ) {
72 0 ▶ _die( sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if ord $ch > 255 0 0 0
73 'Character U+%04X cannot be encoded as ISO-8859-1',
74 ord($ch),
75 ) ) if ord($ch) > 0xFF;
76 }
77 0 $bytes = Encode::encode( 'ISO-8859-1', "$text", Encode::FB_CROAK );
78 }
79 else {
80 # Practical extras: anything Encode knows about.
81 0 ▶ my $codec = Encode::find_encoding( defined $encoding ? "$encoding" : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $encoding ? : 0 0 0
82 0 ▶ _die( 'Unsupported encoding: ' . ( defined $encoding ? $encoding : '' ) )
Branch: 2 branches
BranchTrueFalseTotal
unless $codec 0 0 0
defined $encoding ? : 0 0 0
83 if not $codec;
84 0 $bytes = eval { $codec->encode( "$text", Encode::FB_CROAK ) };
85 0 ▶ _die( 'Cannot encode text as ' . $encoding . ': ' . ( $@ // 'unknown error' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 0 0
Condition0%0/2
$@ // "unknown error"
l!l
00
MC/DC0%0/1
$@ // "unknown error"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
86 if not defined $bytes;
87 }
88  
89 0 return Zuzu::Value::BinaryString->new( bytes => $bytes );
90 }
91  
92 0 ▶sub _decode {
Subroutine _decode: not called CC=26 SCAR=65.5
93 0 my ( $binary, $encoding ) = @_;
94  
95 0 ▶ _die( 'TypeException: decode expects BinaryString, got ' . _type_name( $binary ) )
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($binary) and $binary->isa("Zuzu::Value::BinaryString") 0 0 0
Condition0%0/3
builtin::blessed($binary) and $binary->isa("Zuzu::Value::BinaryString")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($binary) and $binary->isa("Zuzu::Value::BinaryString")
builtin::blessed($binary) $binary->isa("Zuzu::Value::BinaryString")
Truth table0%0/3
A and B
A= builtin::blessed($binary)B= $binary->isa("Zuzu::Value::BinaryString")
ABresult
0X0
100
111
96 if not( blessed($binary) and $binary->isa('Zuzu::Value::BinaryString') );
97  
98 0 ▶ my $bytes = $binary->bytes // '';
Condition0%0/2
$binary->bytes // ""
l!l
00
MC/DC0%0/1
$binary->bytes // ""
$binary->bytes
Truth table0%0/2
A
A= $binary->bytes
Aresult
11
00
99 0 my $canonical = _canonical_encoding( $encoding );
100 0 my $text;
101 0 ▶ if ( defined $canonical and $canonical eq 'utf8' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $canonical and $canonical eq 'utf8') { } 0 0 0
elsif (defined $canonical and $canonical eq 'utf16') { } 0 0 0
elsif (defined $canonical and $canonical eq 'utf32') { } 0 0 0
elsif (defined $canonical and $canonical eq 'latin1') { } 0 0 0
Condition0%0/3
defined $canonical and $canonical eq "utf8"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $canonical and $canonical eq "utf8"
defined $canonical $canonical eq "utf8"
Truth table0%0/3
A and B
A= defined $canonicalB= $canonical eq "utf8"
ABresult
0X0
100
111
102 0 $text = eval { Encode::decode( 'UTF-8', $bytes, Encode::FB_CROAK ) };
103 0 ▶ _die( 'Invalid UTF-8 in BinaryString' ) if not defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
104 }
105 ▶ elsif ( defined $canonical and $canonical eq 'utf16' ) {
Condition0%0/3
defined $canonical and $canonical eq "utf16"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $canonical and $canonical eq "utf16"
defined $canonical $canonical eq "utf16"
Truth table0%0/3
A and B
A= defined $canonicalB= $canonical eq "utf16"
ABresult
0X0
100
111
106 0 my $codec_name = 'UTF-16BE';
107 0 ▶ if ( $bytes =~ s/\A\xFE\xFF// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bytes =~ s/\A\xFE\xFF//) { } 0 0 0
elsif ($bytes =~ s/\A\xFF\xFE//) { } 0 0 0
108 0 $codec_name = 'UTF-16BE';
109 }
110 elsif ( $bytes =~ s/\A\xFF\xFE// ) {
111 0 $codec_name = 'UTF-16LE';
112 }
113 0 ▶ _die( 'UTF-16 input length must be a multiple of 2 bytes' )
Branch: 1 branch
BranchTrueFalseTotal
if length($bytes) % 2 0 0 0
114 if length($bytes) % 2;
115 0 $text = eval { Encode::decode( $codec_name, $bytes, Encode::FB_CROAK ) };
116 0 ▶ _die( 'Invalid UTF-16 in BinaryString' ) if not defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
117 }
118 ▶ elsif ( defined $canonical and $canonical eq 'utf32' ) {
Condition0%0/3
defined $canonical and $canonical eq "utf32"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $canonical and $canonical eq "utf32"
defined $canonical $canonical eq "utf32"
Truth table0%0/3
A and B
A= defined $canonicalB= $canonical eq "utf32"
ABresult
0X0
100
111
119 0 my $codec_name = 'UTF-32BE';
120 0 ▶ if ( $bytes =~ s/\A\x00\x00\xFE\xFF// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bytes =~ s/\A\x00\x00\xFE\xFF//) { } 0 0 0
elsif ($bytes =~ s/\A\xFF\xFE\x00\x00//) { } 0 0 0
121 0 $codec_name = 'UTF-32BE';
122 }
123 elsif ( $bytes =~ s/\A\xFF\xFE\x00\x00// ) {
124 0 $codec_name = 'UTF-32LE';
125 }
126 0 ▶ _die( 'UTF-32 input length must be a multiple of 4 bytes' )
Branch: 1 branch
BranchTrueFalseTotal
if length($bytes) % 4 0 0 0
127 if length($bytes) % 4;
128 0 $text = eval { Encode::decode( $codec_name, $bytes, Encode::FB_CROAK ) };
129 0 ▶ _die( 'Invalid UTF-32 in BinaryString' ) if not defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
130 }
131 ▶ elsif ( defined $canonical and $canonical eq 'latin1' ) {
Condition0%0/3
defined $canonical and $canonical eq "latin1"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $canonical and $canonical eq "latin1"
defined $canonical $canonical eq "latin1"
Truth table0%0/3
A and B
A= defined $canonicalB= $canonical eq "latin1"
ABresult
0X0
100
111
132 0 $text = Encode::decode( 'ISO-8859-1', $bytes, Encode::FB_CROAK );
133 }
134 else {
135 0 ▶ my $codec = Encode::find_encoding( defined $encoding ? "$encoding" : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $encoding ? : 0 0 0
136 0 ▶ _die( 'Unsupported encoding: ' . ( defined $encoding ? $encoding : '' ) )
Branch: 2 branches
BranchTrueFalseTotal
unless $codec 0 0 0
defined $encoding ? : 0 0 0
137 if not $codec;
138 0 $text = eval { $codec->decode( $bytes, Encode::FB_CROAK ) };
139 0 ▶ _die( 'Cannot decode bytes as ' . $encoding . ': ' . ( $@ // 'unknown error' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
Condition0%0/2
$@ // "unknown error"
l!l
00
MC/DC0%0/1
$@ // "unknown error"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
140 if not defined $text;
141 }
142  
143 0 return $text;
144 }
145  
146 2 ▶sub IMPORT {
Subroutine IMPORT: called CC=1 SCAR=2.6
Pod undocumented
147 2 my ( $class, $runtime ) = @_;
148  
149 my $encode_fn = native_function(
150 name => 'encode',
151 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
152 0 my ( $text, $encoding ) = @_;
153 0 ▶ return _encode( $text, defined $encoding ? $encoding : 'UTF-8' );
Branch: 1 branch
BranchTrueFalseTotal
defined $encoding ? : 0 0 0
154 },
155 2 );
156  
157 my $decode_fn = native_function(
158 name => 'decode',
159 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
160 0 my ( $binary, $encoding ) = @_;
161 0 ▶ return _decode( $binary, defined $encoding ? $encoding : 'UTF-8' );
Branch: 1 branch
BranchTrueFalseTotal
defined $encoding ? : 0 0 0
162 },
163 2 );
164  
165 return {
166 2 encode => $encode_fn,
167 decode => $decode_fn,
168 ENCODING_UTF8 => 'UTF-8',
169 ENCODING_UTF16 => 'UTF-16',
170 ENCODING_UTF32 => 'UTF-32',
171 ENCODING_LATIN => 'ISO-8859-1',
172 };
173 }
174  
175 2 1;
« blib/lib/Zuzu/Module/GUI/Objects.pm ↑ Summary blib/lib/Zuzu/Module/Proc.pm »