Commit | Line | Data |
c0d88b76 |
1 | package Encode::CN::HZ; |
2 | |
00a464f7 |
3 | use strict; |
00a464f7 |
4 | |
eb042f38 |
5 | use vars qw($VERSION); |
10c5ecbb |
6 | $VERSION = do { my @r = (q$Revision: 1.4 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; |
eb042f38 |
7 | |
67d7b5ef |
8 | use Encode (); |
10c5ecbb |
9 | |
10 | use base qw(Encode::Encoding); |
11 | __PACKAGE__->Define('hz'); |
c0d88b76 |
12 | |
0ab8f81e |
13 | # HZ is only escaped GB, so we implement it with the |
14 | # GB2312(raw) encoding here. Cf. RFCs 1842 & 1843. |
c0d88b76 |
15 | |
10c5ecbb |
16 | |
c0d88b76 |
17 | |
0ab8f81e |
18 | sub needs_lines { 1 } |
19 | |
20 | sub perlio_ok { |
0ab8f81e |
21 | return 0; # for the time being |
22 | } |
23 | |
c0d88b76 |
24 | sub decode |
25 | { |
26 | my ($obj,$str,$chk) = @_; |
67d7b5ef |
27 | my $gb = Encode::find_encoding('gb2312-raw'); |
c0d88b76 |
28 | |
00a464f7 |
29 | $str =~ s{~ # starting tilde |
30 | (?: |
31 | (~) # another tilde - escaped (set $1) |
32 | | # or |
33 | \n # \n - output nothing |
34 | | # or |
35 | \{ # opening brace of GB data |
36 | ( # set $2 to any number of... |
37 | (?: |
38 | [^~] # non-tilde GB character |
39 | | # or |
40 | ~(?!\}) # tilde not followed by a closing brace |
41 | )* |
42 | ) |
43 | ~\} # closing brace of GB data |
44 | | # XXX: invalid escape - maybe die on $chk? |
45 | ) |
46 | }{ |
47 | (defined $1) ? '~' # two tildes make one tilde |
48 | : |
49 | (defined $2) ? $gb->decode($2, $chk) # decode the characters |
50 | : |
67d7b5ef |
51 | '' # ~\n and invalid escape = '' |
00a464f7 |
52 | }egx; |
c0d88b76 |
53 | |
54 | return $str; |
55 | } |
56 | |
57 | sub encode |
58 | { |
59 | my ($obj,$str,$chk) = @_; |
00a464f7 |
60 | my ($out, $in_gb); |
67d7b5ef |
61 | my $gb = Encode::find_encoding('gb2312-raw'); |
c0d88b76 |
62 | |
63 | $str =~ s/~/~~/g; |
c0d88b76 |
64 | |
67d7b5ef |
65 | # XXX: Since CHECK and partial decoding has not been implemented yet, |
00a464f7 |
66 | # we'll use a very crude way to test for GB2312ness. |
67 | |
68 | for my $index (0 .. length($str) - 1) { |
69 | no warnings 'utf8'; |
70 | |
71 | my $char = substr($str, $index, 1); |
b2704119 |
72 | # try to encode this character |
73 | # with CHECK on so it stops at proper place. |
74 | # also note that the assignement was braced in eval |
75 | # -- dankogai |
76 | my $try; |
77 | eval{ $try = $gb->encode($char, 1) }; |
78 | |
67d7b5ef |
79 | if (defined($try)) { # is a GB character: |
00a464f7 |
80 | if ($in_gb) { |
67d7b5ef |
81 | $out .= $try; # in GB mode - just append it |
00a464f7 |
82 | } |
83 | else { |
67d7b5ef |
84 | $in_gb = 1; # enter GB mode, then append it |
85 | $out .= "~{$try"; |
00a464f7 |
86 | } |
67d7b5ef |
87 | } # not a GB character: |
00a464f7 |
88 | elsif ($in_gb) { |
67d7b5ef |
89 | $in_gb = 0; # leave GB mode, then append it |
90 | $out .= "~}$char"; |
00a464f7 |
91 | } |
92 | else { |
67d7b5ef |
93 | $out .= $char; # not in GB mode - just append it |
00a464f7 |
94 | } |
95 | } |
96 | |
67d7b5ef |
97 | $out .= '~}' if $in_gb; # add closing brace if needed |
00a464f7 |
98 | |
99 | return $out; |
c0d88b76 |
100 | } |
101 | |
102 | 1; |
103 | __END__ |
67d7b5ef |
104 | |
105 | |
106 | =head1 NAME |
107 | |
108 | Encode::CN::HZ -- internally used by Encode::CN |
109 | |
110 | =cut |