| 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"
MC/DC100%1/1
$enc ||= "utf8"
$enc
Truth table100%2/2
A
A= $enc
| |||||||||||||
| 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"
MC/DC100%1/1
$enc ||= "utf8"
$enc
Truth table100%2/2
A
A= $enc
| |||||||||||||
| 34 | 25 | my $str; | |||||||||||
| 35 | 25 | ▶ | if(ref $self) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 46 | 641 | my $utf8 = $uchar->utf8; | |||||||||||
| 47 | 641 | ▶ | $rslt = (($utf8 =~ /[\x80-\xff]/) ? '\\u'.unpack('H4', $uchar->utf16be) : $utf8) . $rslt; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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"
MC/DC100%1/1
$enc ||= "utf8"
$enc
Truth table100%2/2
A
A= $enc
| |||||||||||||
| 56 | 30 | ▶ | my $str = (ref $self) ? $$self : $self; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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
| |||||||||||||
| 61 | 666 | ▶ | if($char eq '\\') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 162 | ▶ | if(($char = shift(@chars)) eq 'u') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | 150 | my $i = 0; | |||||||||||
| 64 | 150 | ▶ | for(; $i < 4; $i++) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 65 | 600 | ▶ | unless($chars[$i] =~ /[0-9a-fA-F]/){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | 4 | last; | |||||||||||
| 67 | } | ||||||||||||
| 68 | } | ||||||||||||
| 69 | 150 | ▶ | if($i == 4) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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__ | ||||||||||||