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