Escape/Unicode.pm

filter: Statementstmt 39.1% 38 / 97 Branchbran 11.7% 4 / 34 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 38.1% 8 / 21 Podpod 13.3% 2 / 15 totaltotal 29.3% 52 / 177 CC 20 SCAR 17.6
CC
20
Cov
31%
hex2chr
12.0
oct2chr
12.0
encode
6.0
CRAP 5.8
↑ Summary Escape.pm »
1 # Encoding of Unicode Escape Sequences (or Escaped Unicode)
2  
3 # $Id: Unicode.pm,v 1.13 2007-12-05 22:11:11+09 you Exp $
4  
5 package Encode::Escape::Unicode;
6  
7 1 our $VERSION = do { q$Revision: 1.13 $ =~ /\d+\.(\d+)/; sprintf "%.2f", $1 / 100 };
8  
9 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Encode::Encoding;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use base qw(Encode::Encoding);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 __PACKAGE__->Define(qw/unicode-escape unicode_escape/);
17  
18 1 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
19  
20 1 __PACKAGE__->enmode('default');
21 1 __PACKAGE__->demode('default');
22  
23 1 require Encode;
24 1 Encode->export_to_level(1, @_);
25 }
26  
27 1 our $enmode;
28 1 our $demode;
29 sub encoder($);
30 sub decoder($);
31  
32 #
33 # == encoder/decoder modes ==
34 #
35 1 our %encoder = (
36 undef => \&perl_encoder,
37 '' => \&perl_encoder,
38 default => \&perl_encoder,
39 perl => \&perl_encoder,
40 java => \&python_encoder,
41 python => \&python_encoder,
42 csharp => \&python_encoder,
43 );
44  
45 1 our %decoder = (
46 undef => \&perl_decoder,
47 '' => \&perl_decoder,
48 default => \&perl_decoder,
49 perl => \&perl_decoder,
50 java => \&python_decoder,
51 python => \&python_decoder,
52 csharp => \&python_decoder,
53 );
54  
55 #
56 # == encode/decode ==
57 #
58  
59 0 ▶sub encode($$;$) {
Subroutine encode: not called CC=2 SCAR=17.9
Pod documented
60 0 my ($obj, $str, $chk) = @_;
61 0 ▶ $_[1] = '' if $chk;
Branch: 1 branch
BranchTrueFalseTotal
if $chk 0 0 0
62 0 return encoder $str;
63 }
64  
65  
66 0 ▶sub decode($$;$) {
Subroutine decode: not called CC=2 SCAR=17.9
Pod documented
67 0 my ($obj, $str, $chk) = @_;
68 0 ▶ $_[1] = '' if $chk;
Branch: 1 branch
BranchTrueFalseTotal
if $chk 0 0 0
69 0 return decoder $str;
70 }
71  
72 #
73 # == enmode/demode ==
74 #
75  
76 1 ▶sub enmode ($$) {
Subroutine enmode: called CC=3 SCAR=12.7
Pod undocumented
77 1 my ($class, $mode) = @_;
78 1 ▶ $mode = 'undef' unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 0 1 1
79 1 ▶ unless (exists $encoder{$mode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Encode::Escape::Unicode::encoder{$mode}) 0 1 1
80 0 require Carp;
81 0 Carp::croak(
82 "Unknown enmode '$mode' for encoding '" . $class->name() . "'"
83 );
84 }
85 1 $enmode = $mode;
86 }
87  
88 1 ▶sub demode ($$) {
Subroutine demode: called CC=3 SCAR=12.7
Pod undocumented
89 1 my ($class, $mode) = @_;
90 1 ▶ $mode = 'undef' unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 0 1 1
91 1 ▶ unless (exists $decoder{$mode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Encode::Escape::Unicode::decoder{$mode}) 0 1 1
92 0 require Carp;
93 0 Carp::croak(
94 "Unknown demode '$mode' for encoding '" . $class->name() . "'"
95 );
96 }
97 1 $demode = $mode;
98 }
99  
100  
101 #
102 # = DATA AND SUBROUTINES FOR INTERNAL USE =
103 #
104  
105  
106 #
107 # == encoder/decoder ==
108 #
109  
110 0 ▶sub encoder($) {
Subroutine encoder: not called CC=2 SCAR=17.9
Pod undocumented
111 0 ▶ local $_ = ( defined $_[0] ? $_[0] : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
112 0 return $encoder{$enmode}->($_);
113 }
114 0 ▶sub decoder($) {
Subroutine decoder: not called CC=2 SCAR=17.9
Pod undocumented
115 0 ▶ local $_ = ( defined $_[0] ? $_[0] : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
116 0 return $decoder{$demode}->($_);
117 }
118  
119 #
120 # == enmode_encoder / demode_decoder ==
121 #
122  
123 # default (perl) escape sequences
124 #
125 0 ▶sub perl_encoder($) {
Subroutine perl_encoder: not called CC=2 SCAR=17.9
Pod undocumented
126  
127 0 ▶ local $_ = ( defined $_[0] ? $_[0] : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
128  
129 0 $_ = escape($_);
130 0 s/([\x00-\x1f\x{7f}-\x{ffff}])/"\\x\{".uc(chr2hex($1))."\}"/gse;
131  
132 0 return $_;
133 }
134  
135 0 ▶sub perl_decoder($) {
Subroutine perl_decoder: not called CC=2 SCAR=17.9
Pod undocumented
136  
137 0 ▶ local $_ = ( defined $_[0] ? $_[0] : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
138  
139 0 s/((?:\A|\G|[^\\]))\\x([\da-fA-F]{1,2})/$1.hex2chr($2)/gse;
140 0 s/((?:\A|\G|[^\\]))\\x\{([\da-fA-F]{1,4})\}/$1.hex2chr($2)/gse;
141  
142 0 return unescape($_);
143 }
144  
145 # python (or java, c#) escape sequences
146 #
147 0 ▶sub python_encoder($) {
Subroutine python_encoder: not called CC=2 SCAR=17.9
Pod undocumented
148  
149 0 ▶ local $_ = ( defined $_[0] ? $_[0] : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
150  
151 0 $_ = escape($_);
152 0 s/([\x00-\x1f\x{7f}-\x{ffff}])/'\u'.chr2hex($1)/gse;
153  
154 0 return $_;
155 }
156  
157 0 ▶sub python_decoder {
Subroutine python_decoder: not called CC=2 SCAR=17.9
Pod undocumented
158  
159 0 ▶ local $_ = ( defined $_[0] ? $_[0] : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
160  
161 0 s/((?:\A|\G|[^\\]))\\u([\da-fA-F]{4})/$1.hex2chr($2)/gse;
162  
163 0 return unescape($_);
164 }
165  
166 #
167 # == common data and subroutines ==
168 #
169  
170 1 my %ESCAPED = (
171 "\\" => '\\',
172 "\r" => 'r',
173 "\n" => 'n',
174 "\t" => 't',
175 "\a" => 'a',
176 "\b" => 'b',
177 "\e" => 'e',
178 "\f" => 'f',
179 "\"" => '"',
180 "\$" => '$',
181 "\@" => '@',
182 );
183  
184 1 my %UNESCAPED = ( reverse %ESCAPED );
185  
186 0 ▶sub escape ($) {
Subroutine escape: not called CC=2 SCAR=17.9
Pod undocumented
187 0 ▶ local $_ = ( defined $_[0] ? $_[0] : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
188 0 s/([\a\b\e\f\r\n\t\"\\\$\@])/\\$ESCAPED{$1}/sg;
189 0 return $_;
190 }
191  
192 0 ▶sub unescape ($) {
Subroutine unescape: not called CC=2 SCAR=17.9
Pod undocumented
193 0 ▶ local $_ = ( defined $_[0] ? $_[0] : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
194  
195 0 s/((?:\A|\G|[^\\]))\\([0-7]{1,3})/$1.oct2chr($2)/gse;
196  
197 0 s/((?:\A|\G|[^\\]))\\([^aAbBeEfFrRnNtT\\\"\$\@])/$1$2/g;
198  
199 0 s/((?:\A|\G|[^\\]))\\([aAbBeEfFrRnNtT\\\"\$\@])/$1.$UNESCAPED{lc($2)}/gse;
200  
201 0 return $_;
202 }
203  
204  
205  
206 0 ▶sub chr2hex {
Subroutine chr2hex: not called CC=2 SCAR=17.9
Pod undocumented
207 0 my($c) = @_;
208 0 ▶ if ( ord($c) < 65536 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord $c < 65536) { } 0 0 0
209 0 return sprintf("%04x", ord($c));
210 }
211 else {
212 0 require Carp;
213 0 Carp::croak (
214 "'unicode-escape' codec can't encode character: ordinal " . ord($c)
215 );
216 }
217 }
218  
219 0 ▶sub hex2chr {
Subroutine hex2chr: not called CC=3 SCAR=24.8
Pod undocumented
220 0 my($hex) = @_;
221 0 ▶ if ( hex($hex) >= 0 and hex($hex) < 65536) {
Branch: 1 branch
BranchTrueFalseTotal
if (hex $hex >= 0 and hex $hex < 65536) { } 0 0 0
Condition0%0/3
hex $hex >= 0 and hex $hex < 65536
!ll&&!rl&&r
000
MC/DC0%0/2
hex $hex >= 0 and hex $hex < 65536
hex $hex >= 0 hex $hex < 65536
Truth table0%0/3
A and B
A= hex $hex >= 0B= hex $hex < 65536
ABresult
0X0
100
111
222 0 return chr(hex($hex));
223 }
224 else {
225 0 require Carp;
226 0 Carp::croak(
227 "'unicode-escape' codec can't decode escape sequence: "
228 . "\\x$hex (ordinal " . hex($hex) . ")"
229 );
230 }
231 }
232  
233 0 ▶sub oct2chr {
Subroutine oct2chr: not called CC=3 SCAR=24.8
Pod undocumented
234 0 my($oct) = @_;
235 0 ▶ if ( oct($oct) >= 0 and oct($oct) < 256 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (oct $oct >= 0 and oct $oct < 256) { } 0 0 0
Condition0%0/3
oct $oct >= 0 and oct $oct < 256
!ll&&!rl&&r
000
MC/DC0%0/2
oct $oct >= 0 and oct $oct < 256
oct $oct >= 0 oct $oct < 256
Truth table0%0/3
A and B
A= oct $oct >= 0B= oct $oct < 256
ABresult
0X0
100
111
236 0 return chr(oct($oct));
237 }
238 else {
239 0 require Carp;
240 0 Carp::croak (
241 "'unicode-escape' codec can't decode escape sequence: "
242 . "\\$oct (ordinal " . oct($oct). ")"
243 );
244 }
245 }
246  
247 1 $\ = "\n";
248  
249  
250 1 1;
251 __END__
↑ Summary Escape.pm »