MHonArc/Char.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 113 CC 10 SCAR 42.4
CC
10
Cov
0%
map_conv
110.0
_euc_jp_conv
2.0
_shiftjis_conv
2.0
CRAP 69.5
1 ##---------------------------------------------------------------------------##
2 ## File:
3 ## $Id: Char.pm,v 1.4 2010/12/31 20:34:00 ehood Exp $
4 ## Author:
5 ## Earl Hood earl@earlhood.com
6 ## Description:
7 ## POD after __END__
8 ##---------------------------------------------------------------------------##
9 ## Copyright (C) 1997-2002 Earl Hood, earl@earlhood.com
10 ##
11 ## This program is free software; you can redistribute it and/or modify
12 ## it under the terms of the GNU General Public License as published by
13 ## the Free Software Foundation; either version 2 of the License, or
14 ## (at your option) any later version.
15 ##
16 ## This program is distributed in the hope that it will be useful,
17 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ## GNU General Public License for more details.
20 ##
21 ## You should have received a copy of the GNU General Public License
22 ## along with this program; if not, write to the Free Software
23 ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 ## 02111-1307, USA
25 ##---------------------------------------------------------------------------##
26  
27 package MHonArc::Char;
28  
29 our $VERSION = '2.6.24';
30  
31 ###############################################################################
32 ## Routines
33 ###############################################################################
34  
35 ##---------------------------------------------------------------------------##
36 ## map_conv converts a string encoded by $charset to a string
37 ## defined by a given mapping table.
38 ##
39 sub map_conv {
40 my $data_r = shift; # Reference to text
41 my $charset = shift; # encoding (should be in lowercase
42 my $char_maps = shift; # MHonArc::CharMaps instance
43 my @maps = shift || (); # Additional maps to use
44  
45 # Pre-processing checks
46 if ($charset eq 'iso-2022-jp') {
47 # iso-2022-jp, convert to euc-jp first
48 require MHonArc::Char::JP;
49 MHonArc::Char::JP::jp_2022_to_euc($data_r);
50 $charset = 'euc-jp';
51  
52 } elsif ($charset eq 'iso-2022-kr') {
53 # if iso-2022-kr, convert to euc-kr first
54 require MHonArc::Char::KR;
55 MHonArc::Char::KR::kr_2022_to_euc($data_r);
56 $charset = 'cp949';
57 }
58  
59 # Get mapping
60 unshift(@maps, $char_maps->get_map($charset));
61  
62 # Convert text
63 if ($charset eq 'euc-jp') {
64 # Japanese
65 _euc_jp_conv($data_r, \@maps);
66 return $$data_r;
67 }
68 if ($charset eq 'cp932') {
69 # Japanese ShiftJIS
70 _shiftjis_conv($data_r, \@maps);
71 return $$data_r;
72 }
73 if ($charset eq 'cp949') {
74 # Korean
75 _euc_kr_conv($data_r, \@maps);
76 return $$data_r;
77 }
78 if ( $charset eq 'cp950'
79 || $charset eq 'cp936'
80 || $charset eq 'gb2312'
81 || $charset eq 'big5-eten'
82 || $charset eq 'big5-hkscs') {
83 # Chinese
84 _chinese_conv($data_r, \@maps);
85 return $$data_r;
86 }
87  
88 # Single byte charset
89 # Bug #14747: Performance and memory improvement for single byte
90 # charsets. A regex is dynamically built, specific
91 # to the map(s) provided, so replacement is done
92 # directly by regex engine.
93 # Patch provided by Andrew Shirrayev.
94 my ($map, $char, $code, %summap, $summap);
95 for ($code = 0x00; $code <= 0xFF; $code++) {
96 foreach $map (@maps) {
97 $char = $map->{chr($code)};
98 last if defined($char);
99 }
100 unless (defined($char)) {
101 next if ($code <= 0x7F);
102 $char = '?';
103 }
104 $summap{chr($code)} = $char;
105 $summap .= chr($code);
106 }
107 # DO NOT use /o here since we need to recompile each time.
108 $$data_r =~ s/([$summap])/$summap{$1}/ge;
109 $$data_r;
110 }
111  
112 sub _euc_jp_conv {
113 my $data_r = shift;
114 my $maps = shift;
115 my ($map, $char);
116  
117 $$data_r =~ s{
118 ([\x00-\x7E]|
119 [\x8E][\xA1-\xDF]|
120 [\xA1-\xFE][\xA1-\xFE]|
121 \x8F[\xA2-\xFE][\xA1-\xFE])
122 }{
123 foreach $map (@$maps) {
124 $char = $map->{$1};
125 last if defined($char);
126 }
127 $char = (length($1) > 1 ? '?' : $1) unless defined($char);
128 $char;
129 }gxe;
130 }
131  
132 sub _shiftjis_conv {
133 my $data_r = shift;
134 my $maps = shift;
135 my ($map, $char);
136  
137 $$data_r =~ s{
138 ([\x00-\x7E]|
139 [\xA1-\xDF]|
140 [\x81-\x9F\xE0-\xEF][\x40-\x7E\x80-\xFC])
141 }{
142 foreach $map (@$maps) {
143 $char = $map->{$1};
144 last if defined($char);
145 }
146 $char = (length($1) > 1 ? '?' : $1) unless defined($char);
147 $char;
148 }gxe;
149 }
150  
151 sub _euc_kr_conv {
152 my $data_r = shift;
153 my $maps = shift;
154 my ($map, $char);
155  
156 $$data_r =~ s{
157 ([\x00-\x80]|
158 [\x81-\xFE][\xA1-\xFE])
159 }{
160 foreach $map (@$maps) {
161 $char = $map->{$1};
162 last if defined($char);
163 }
164 $char = (length($1) > 1 ? '?' : $1) unless defined($char);
165 $char;
166 }gxe;
167 }
168  
169 sub _chinese_conv {
170 my $data_r = shift;
171 my $maps = shift;
172 my ($map, $char);
173  
174 $$data_r =~ s{
175 ([\x00-\x80]|
176 [\x81-\xFF][\x00-\xFF])
177 }{
178 foreach $map (@$maps) {
179 $char = $map->{$1};
180 last if defined($char);
181 }
182 $char = (length($1) > 1 ? '?' : $1) unless defined($char);
183 $char;
184 }gxe;
185 }
186  
187 ##---------------------------------------------------------------------------##
188 1;
189 __END__