File Coverage

Encode/CN/Utility.pm
Criterion Covered Total %
statement 26 26 100.0
branch n/a
condition n/a
subroutine 10 10 100.0
pod 6 6 100.0
total 42 42 100.0


line stmt bran cond sub pod time code
1             package Encode::CN::Utility;
2            
3 1     1   790 use 5.006;
  1         3  
  1         38  
4 1     1   5 use Exporter;
  1         2  
  1         79  
5             @ISA = qw(Exporter);
6             @EXPORT = qw(
7             hz2gbk gbk2hz hz2utf8 hz2unicode utf82hz unicode2hz
8             gbk2utf8 gbk2unicode utf82gbk utf82unicode
9             unicode2gbk unicode2utf8
10             );
11            
12             $VERSION = '0.4';
13            
14 1     1   901 use Encode;
  1         11980  
  1         359  
15            
16             sub hz2gbk {
17 6     6 1 45 unpack "H*", shift;
18             }
19            
20             sub gbk2hz {
21 6     6 1 40 pack "H*", shift;
22             }
23            
24             sub hz2utf8 {
25 6     6 1 12310 unpack("H*", encode("utf8", decode("gbk", shift)));
26             }
27            
28             sub hz2unicode {
29 6     6 1 368 my $res = "";
30            
31 6         17 my $str = decode("gbk", shift);
32 6         178 my $len = length($str);
33 6         16 for(0..$len - 1) {
34 8         23 my $c = substr $str, $_, 1;
35 8         40 $res .= sprintf("%x", unpack("U*", $c));
36             }
37            
38 6         31 return $res;
39             }
40             sub utf82hz {
41 3     3 1 17 encode("gbk",decode("utf8", pack("H*", shift)));;
42             }
43            
44             sub unicode2hz {
45 3     3 1 539 encode("gbk", pack("U*", sprintf("%d", hex(shift))))
46             }
47            
48             sub AUTOLOAD {
49 6     6   843 my($fun) = ($AUTOLOAD =~ /.*:(\w+)$/);
50 6         19 my($f1, $f2) = split /2/, $fun;
51 6         8 &{"hz2". $f2}(&{$f1."2hz"}(shift));
  6         207  
  6         20  
52             }
53            
54             1;
55             __END__