line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Unicode::Escape; |
2
|
|
|
|
|
|
|
|
3
|
11
|
|
|
11
|
|
566373
|
use warnings; |
|
11
|
|
|
|
|
29
|
|
|
11
|
|
|
|
|
449
|
|
4
|
11
|
|
|
11
|
|
63
|
use strict; |
|
11
|
|
|
|
|
26
|
|
|
11
|
|
|
|
|
754
|
|
5
|
11
|
|
|
11
|
|
70
|
use Carp; |
|
11
|
|
|
|
|
29
|
|
|
11
|
|
|
|
|
1154
|
|
6
|
|
|
|
|
|
|
|
7
|
11
|
|
|
11
|
|
17702
|
use Unicode::String; |
|
11
|
|
|
|
|
159087
|
|
|
11
|
|
|
|
|
847
|
|
8
|
11
|
|
|
11
|
|
154
|
use Exporter; |
|
11
|
|
|
|
|
23
|
|
|
11
|
|
|
|
|
392
|
|
9
|
11
|
|
|
11
|
|
23174
|
use Encode; |
|
11
|
|
|
|
|
264196
|
|
|
11
|
|
|
|
|
1316
|
|
10
|
|
|
|
|
|
|
|
11
|
11
|
|
|
11
|
|
115
|
use vars qw( $VERSION @ISA @EXPORT_OK ); |
|
11
|
|
|
|
|
26
|
|
|
11
|
|
|
|
|
7378
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
$VERSION = '0.0.2'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
@EXPORT_OK = qw( |
18
|
|
|
|
|
|
|
escape |
19
|
|
|
|
|
|
|
unescape |
20
|
|
|
|
|
|
|
); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub new { |
24
|
10
|
|
|
10
|
1
|
82
|
my ($class, $str, $enc) = @_; |
25
|
10
|
|
100
|
|
|
84
|
$enc ||= 'utf8'; |
26
|
10
|
|
|
|
|
53
|
Encode::from_to($str, $enc, 'utf8'); |
27
|
10
|
|
|
|
|
35435
|
return bless \$str, $class; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub escape { |
32
|
25
|
|
|
25
|
1
|
127
|
my ($self, $enc) = @_; |
33
|
25
|
|
100
|
|
|
131
|
$enc ||= 'utf8'; |
34
|
25
|
|
|
|
|
44
|
my $str; |
35
|
25
|
100
|
|
|
|
74
|
if(ref $self) { |
36
|
11
|
|
|
|
|
33
|
$str = $$self; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
else { |
39
|
14
|
|
|
|
|
105
|
Encode::from_to($self, $enc, 'utf8'); |
40
|
14
|
|
|
|
|
67956
|
$str = $self; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
25
|
|
|
|
|
158
|
my $us = Unicode::String->new($str); |
44
|
25
|
|
|
|
|
374
|
my $rslt = ''; |
45
|
25
|
|
|
|
|
99
|
while(my $uchar = $us->chop) { |
46
|
641
|
|
|
|
|
12581
|
my $utf8 = $uchar->utf8; |
47
|
641
|
100
|
|
|
|
7095
|
$rslt = (($utf8 =~ /[\x80-\xff]/) ? '\\u'.unpack('H4', $uchar->utf16be) : $utf8) . $rslt; |
48
|
|
|
|
|
|
|
} |
49
|
25
|
|
|
|
|
429
|
return $rslt; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub unescape { |
54
|
30
|
|
|
30
|
1
|
132
|
my ($self, $enc) = @_; |
55
|
30
|
|
100
|
|
|
139
|
$enc ||= 'utf8'; |
56
|
30
|
100
|
|
|
|
140
|
my $str = (ref $self) ? $$self : $self; |
57
|
|
|
|
|
|
|
|
58
|
30
|
|
|
|
|
1146
|
my @chars = split(//, $str); |
59
|
30
|
|
|
|
|
305
|
my $us = Unicode::String->new(); |
60
|
30
|
|
|
|
|
350
|
while(defined(my $char = shift(@chars))) { |
61
|
666
|
100
|
|
|
|
12348
|
if($char eq '\\') { |
62
|
162
|
100
|
|
|
|
344
|
if(($char = shift(@chars)) eq 'u') { |
63
|
150
|
|
|
|
|
216
|
my $i = 0; |
64
|
150
|
|
|
|
|
318
|
for(; $i < 4; $i++) { |
65
|
600
|
100
|
|
|
|
2175
|
unless($chars[$i] =~ /[0-9a-fA-F]/){ |
66
|
4
|
|
|
|
|
33
|
last; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
150
|
100
|
|
|
|
2912
|
if($i == 4) { |
70
|
146
|
|
|
|
|
540
|
my $hex = hex(join('', splice(@chars, 0, 4))); |
71
|
146
|
|
|
|
|
468
|
$us->append(Unicode::String::chr($hex)); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
else { |
74
|
4
|
|
|
|
|
21
|
$us->append('u'); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
else { |
78
|
12
|
|
|
|
|
49
|
$us->append('\\'.$char); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
else { |
82
|
504
|
|
|
|
|
1306
|
$us->append($char); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
30
|
|
|
|
|
873
|
my $result = $us->utf8; |
86
|
30
|
|
|
|
|
128
|
Encode::from_to($result, 'utf8', $enc); |
87
|
30
|
|
|
|
|
52966
|
return $result; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |
92
|
|
|
|
|
|
|
__END__ |