blib/lib/Unicode/Escape.pm

filter: Statementstmt 100% 63 / 63 Branchbran 100% 20 / 20 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 100% 105 / 105 CC 14 SCAR 16.3
CC
14
Cov
100%
unescape
9.0
escape
5.0
new
2.0
CRAP 5.1
↑ Summary
1 package Unicode::Escape;
2  
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use Unicode::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 ▶use vars qw( $VERSION @ISA @EXPORT_OK );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 11 $VERSION = '0.0.2';
14  
15 11 @ISA = qw(Exporter);
16  
17 11 @EXPORT_OK = qw(
18 escape
19 unescape
20 );
21  
22  
23 10 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
24 10 my ($class, $str, $enc) = @_;
25 10 ▶ $enc ||= 'utf8';
Condition100%2/2
$enc ||= "utf8"
l!l
55
MC/DC100%1/1
$enc ||= "utf8"
$enc
Truth table100%2/2
A
A= $enc
Aresult
11
00
26 10 Encode::from_to($str, $enc, 'utf8');
27 10 return bless \$str, $class;
28 }
29  
30  
31 25 ▶sub escape {
Subroutine escape: called CC=5 SCAR=16.1
Pod documented
32 25 my ($self, $enc) = @_;
33 25 ▶ $enc ||= 'utf8';
Condition100%2/2
$enc ||= "utf8"
l!l
1015
MC/DC100%1/1
$enc ||= "utf8"
$enc
Truth table100%2/2
A
A= $enc
Aresult
11
00
34 25 my $str;
35 25 ▶ if(ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 11 14 25
36 11 $str = $$self;
37 }
38 else {
39 14 Encode::from_to($self, $enc, 'utf8');
40 14 $str = $self;
41 }
42  
43 25 my $us = Unicode::String->new($str);
44 25 my $rslt = '';
45 25 ▶ while(my $uchar = $us->chop) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $uchar = $us->chop) 641 25 666
46 641 my $utf8 = $uchar->utf8;
47 641 ▶ $rslt = (($utf8 =~ /[\x80-\xff]/) ? '\\u'.unpack('H4', $uchar->utf16be) : $utf8) . $rslt;
Branch: 1 branch
BranchTrueFalseTotal
$utf8 =~ /[\x80-\xff]/ ? : 125 516 641
48 }
49 25 return $rslt;
50 }
51  
52  
53 30 ▶sub unescape {
Subroutine unescape: called CC=9 SCAR=22.0
Pod documented
54 30 my ($self, $enc) = @_;
55 30 ▶ $enc ||= 'utf8';
Condition100%2/2
$enc ||= "utf8"
l!l
237
MC/DC100%1/1
$enc ||= "utf8"
$enc
Truth table100%2/2
A
A= $enc
Aresult
11
00
56 30 ▶ my $str = (ref $self) ? $$self : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 14 16 30
57
58 30 my @chars = split(//, $str);
59 30 my $us = Unicode::String->new();
60 30 ▶ while(defined(my $char = shift(@chars))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $char = shift @chars)) 666 30 696
61 666 ▶ if($char eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq '\\') { } 162 504 666
62 162 ▶ if(($char = shift(@chars)) eq 'u') {
Branch: 1 branch
BranchTrueFalseTotal
if (($char = shift @chars) eq 'u') { } 150 12 162
63 150 my $i = 0;
64 150 ▶ for(; $i < 4; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 4) 600 146 746
65 600 ▶ unless($chars[$i] =~ /[0-9a-fA-F]/){
Branch: 1 branch
BranchTrueFalseTotal
unless ($chars[$i] =~ /[0-9a-fA-F]/) 4 596 600
66 4 last;
67 }
68 }
69 150 ▶ if($i == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 4) { } 146 4 150
70 146 my $hex = hex(join('', splice(@chars, 0, 4)));
71 146 $us->append(Unicode::String::chr($hex));
72 }
73 else {
74 4 $us->append('u');
75 }
76 }
77 else {
78 12 $us->append('\\'.$char);
79 }
80 }
81 else {
82 504 $us->append($char);
83 }
84 }
85 30 my $result = $us->utf8;
86 30 Encode::from_to($result, 'utf8', $enc);
87 30 return $result;
88 }
89  
90  
91 11 1;
92 __END__
↑ Summary