blib/lib/Linux/Event/WebSocket/_UTF8.pm

filter: Statementstmt 100% 37 / 37 Branchbran 81.2% 13 / 16 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 3 totaltotal 81.8% 63 / 77 CC 11 SCAR 11.9
CC
11
Cov
88%
encode
5.1
_byte_copy
4.8
_decode_copy
4.0
CRAP 3.3
« blib/lib/Linux/Event/WebSocket/Connection.pm ↑ Summary blib/lib/Linux/Event/WebSocket/_BQ.pm »
1 package Linux::Event::WebSocket::_UTF8;
2 11 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use utf8 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 62 ▶sub _byte_copy ($bytes) {
Subroutine _byte_copy: called CC=4 SCAR=15.6
10 62 ▶ croak 'UTF-8 input must be a defined scalar'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $bytes or ref $bytes 0 62 62
Condition33%1/3
not defined $bytes or ref $bytes
l!l&&r!l&&!r
0062
MC/DC0%0/2
not defined $bytes or ref $bytes
not defined $bytes ref $bytes
Truth table33%1/3
not(A) or B
A= not defined $bytesB= ref $bytes
ABresult
1X1
011
000
11 if !defined($bytes) || ref($bytes);
12  
13 62 my $copy = "$bytes";
14 62 ▶ croak 'UTF-8 input must contain bytes'
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($copy, 1) 0 62 62
15 if !utf8::downgrade($copy, 1);
16 62 return $copy;
17 }
18  
19 69 ▶sub _decode_copy ($copy) {
Subroutine _decode_copy: called CC=4 SCAR=13.9
20 69 my $byte_length = length $copy;
21 69 my $decoded = $copy;
22 69 ▶ croak 'invalid RFC 3629 UTF-8' if !utf8::decode($decoded);
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::decode($decoded) 8 61 69
23  
24 # A valid UTF-8 byte string can keep the original byte scalar when every
25 # code point was ASCII. Comparing decoded character count with input byte
26 # count is much cheaper here than scanning the entire payload with a regex.
27 61 ▶ return $copy if length($decoded) == $byte_length;
Branch: 1 branch
BranchTrueFalseTotal
if length $decoded == $byte_length 47 14 61
28  
29 # Perl accepts surrogate and out-of-range code points in its internal UTF-8
30 # representation, while RFC 3629 forbids them.
31 14 ▶ croak 'invalid RFC 3629 UTF-8'
Branch: 1 branch
BranchTrueFalseTotal
if $decoded =~ /[\x{d800}-\x{dfff}]|[^\x{0}-\x{10ffff}]/u 2 12 14
32 if $decoded =~ /[\x{d800}-\x{dfff}]|[^\x{0}-\x{10ffff}]/;
33  
34 12 return $decoded;
35 }
36  
37 62 ▶sub _decode_bytes ($bytes) {
Subroutine _decode_bytes: called CC=1 SCAR=0.0
38 62 return _decode_copy(_byte_copy($bytes));
39 }
40  
41 45 ▶sub validate_bytes ($class, $bytes) {
Subroutine validate_bytes: called CC=1 SCAR=0.0
Pod undocumented
42 45 _decode_bytes($bytes);
43 35 return 1;
44 }
45  
46 17 ▶sub decode ($class, $bytes) {
Subroutine decode: called CC=1 SCAR=0.0
Pod undocumented
47 17 return _decode_bytes($bytes);
48 }
49  
50 16 ▶sub encode ($class, $text) {
Subroutine encode: called CC=5 SCAR=16.4
Pod undocumented
51 16 ▶ croak 'UTF-8 text must be a defined scalar'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $text or ref $text 0 16 16
Condition33%1/3
not defined $text or ref $text
l!l&&r!l&&!r
0016
MC/DC0%0/2
not defined $text or ref $text
not defined $text ref $text
Truth table33%1/3
not(A) or B
A= not defined $textB= ref $text
ABresult
1X1
011
000
52 if !defined($text) || ref($text);
53  
54 16 my $copy = "$text";
55 16 ▶ if (!utf8::is_utf8($copy)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::is_utf8($copy)) 7 9 16
56 7 _decode_copy($copy);
57 7 return $copy;
58 }
59  
60 9 ▶ croak 'UTF-8 text contains a non-Unicode scalar value'
Branch: 1 branch
BranchTrueFalseTotal
if $copy =~ /[\x{d800}-\x{dfff}]|[^\x{0}-\x{10ffff}]/u 3 6 9
61 if $copy =~ /[\x{d800}-\x{dfff}]|[^\x{0}-\x{10ffff}]/;
62  
63 6 utf8::encode($copy);
64 6 return $copy;
65 }
66  
67 11 1;
« blib/lib/Linux/Event/WebSocket/Connection.pm ↑ Summary blib/lib/Linux/Event/WebSocket/_BQ.pm »