line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Encode::Alias; |
2
|
40
|
|
|
40
|
|
412
|
use strict; |
|
40
|
|
|
|
|
126
|
|
|
40
|
|
|
|
|
1442
|
|
3
|
40
|
|
|
40
|
|
246
|
use warnings; |
|
40
|
|
|
|
|
101
|
|
|
40
|
|
|
|
|
3903
|
|
4
|
|
|
|
|
|
|
our $VERSION = do { my @r = ( q$Revision: 2.22 $ =~ /\d+/g ); sprintf "%d." . "%02d" x $#r, @r }; |
5
|
40
|
|
|
40
|
|
312
|
use constant DEBUG => !!$ENV{PERL_ENCODE_DEBUG}; |
|
40
|
|
|
|
|
117
|
|
|
40
|
|
|
|
|
2925
|
|
6
|
|
|
|
|
|
|
|
7
|
40
|
|
|
40
|
|
309
|
use Encode (); |
|
40
|
|
|
|
|
185
|
|
|
40
|
|
|
|
|
1536
|
|
8
|
|
|
|
|
|
|
|
9
|
40
|
|
|
40
|
|
267
|
use Exporter 'import'; |
|
40
|
|
|
|
|
96
|
|
|
40
|
|
|
|
|
93694
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# Public, encouraged API is exported by default |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our @EXPORT = |
14
|
|
|
|
|
|
|
qw ( |
15
|
|
|
|
|
|
|
define_alias |
16
|
|
|
|
|
|
|
find_alias |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our @Alias; # ordered matching list |
20
|
|
|
|
|
|
|
our %Alias; # cached known aliases |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub find_alias { |
23
|
3511
|
|
|
3511
|
0
|
7200
|
my $class = shift; |
24
|
3511
|
|
|
|
|
6753
|
my $find = shift; |
25
|
3511
|
100
|
|
|
|
9975
|
unless ( exists $Alias{$find} ) { |
26
|
1281
|
|
|
|
|
3288
|
$Alias{$find} = undef; # Recursion guard |
27
|
1281
|
|
|
|
|
3976
|
for ( my $i = 0 ; $i < @Alias ; $i += 2 ) { |
28
|
45188
|
|
|
|
|
82853
|
my $alias = $Alias[$i]; |
29
|
45188
|
|
|
|
|
81066
|
my $val = $Alias[ $i + 1 ]; |
30
|
45188
|
|
|
|
|
69829
|
my $new; |
31
|
45188
|
100
|
100
|
|
|
284795
|
if ( ref($alias) eq 'Regexp' && $find =~ $alias ) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
32
|
991
|
|
|
|
|
2103
|
DEBUG and warn "eval $val"; |
33
|
991
|
|
|
|
|
62270
|
$new = eval $val; |
34
|
991
|
|
|
|
|
3738
|
DEBUG and $@ and warn "$val, $@"; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
elsif ( ref($alias) eq 'CODE' ) { |
37
|
11
|
|
|
|
|
19
|
DEBUG and warn "$alias", "->", "($find)"; |
38
|
11
|
|
|
|
|
23
|
$new = $alias->($find); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
elsif ( lc($find) eq lc($alias) ) { |
41
|
31
|
|
|
|
|
74
|
$new = $val; |
42
|
|
|
|
|
|
|
} |
43
|
45188
|
100
|
|
|
|
158886
|
if ( defined($new) ) { |
44
|
1033
|
100
|
|
|
|
3228
|
next if $new eq $find; # avoid (direct) recursion on bugs |
45
|
888
|
|
|
|
|
1424
|
DEBUG and warn "$alias, $new"; |
46
|
888
|
50
|
|
|
|
3835
|
my $enc = |
47
|
|
|
|
|
|
|
( ref($new) ) ? $new : Encode::find_encoding($new); |
48
|
888
|
100
|
|
|
|
3227
|
if ($enc) { |
49
|
842
|
|
|
|
|
1896
|
$Alias{$find} = $enc; |
50
|
842
|
|
|
|
|
2120
|
last; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# case insensitive search when canonical is not in all lowercase |
56
|
|
|
|
|
|
|
# RT ticket #7835 |
57
|
1281
|
100
|
|
|
|
3693
|
unless ( $Alias{$find} ) { |
58
|
439
|
|
|
|
|
853
|
my $lcfind = lc($find); |
59
|
439
|
|
|
|
|
9967
|
for my $name ( keys %Encode::Encoding, keys %Encode::ExtModule ) |
60
|
|
|
|
|
|
|
{ |
61
|
95948
|
100
|
|
|
|
202053
|
$lcfind eq lc($name) or next; |
62
|
65
|
|
|
|
|
379
|
$Alias{$find} = Encode::find_encoding($name); |
63
|
65
|
|
|
|
|
235
|
DEBUG and warn "$find => $name"; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
3511
|
|
|
|
|
9051
|
if (DEBUG) { |
68
|
|
|
|
|
|
|
my $name; |
69
|
|
|
|
|
|
|
if ( my $e = $Alias{$find} ) { |
70
|
|
|
|
|
|
|
$name = $e->name; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
else { |
73
|
|
|
|
|
|
|
$name = ""; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
warn "find_alias($class, $find)->name = $name"; |
76
|
|
|
|
|
|
|
} |
77
|
3511
|
|
|
|
|
11108
|
return $Alias{$find}; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub define_alias { |
81
|
2286
|
|
|
2286
|
0
|
6853
|
while (@_) { |
82
|
2659
|
|
|
|
|
5450
|
my $alias = shift; |
83
|
2659
|
|
|
|
|
4811
|
my $name = shift; |
84
|
2659
|
50
|
|
|
|
9558
|
unshift( @Alias, $alias => $name ) # newer one has precedence |
85
|
|
|
|
|
|
|
if defined $alias; |
86
|
2659
|
100
|
|
|
|
7721
|
if ( ref($alias) ) { |
|
|
50
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# clear %Alias cache to allow overrides |
89
|
2015
|
|
|
|
|
5531
|
my @a = keys %Alias; |
90
|
2015
|
|
|
|
|
8249
|
for my $k (@a) { |
91
|
2246
|
100
|
100
|
|
|
14631
|
if ( ref($alias) eq 'Regexp' && $k =~ $alias ) { |
|
|
100
|
66
|
|
|
|
|
92
|
32
|
|
|
|
|
71
|
DEBUG and warn "delete \$Alias\{$k\}"; |
93
|
32
|
|
|
|
|
89
|
delete $Alias{$k}; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
elsif ( ref($alias) eq 'CODE' && $alias->($k) ) { |
96
|
359
|
|
|
|
|
2863
|
DEBUG and warn "delete \$Alias\{$k\}"; |
97
|
359
|
|
|
|
|
646
|
delete $Alias{$k}; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
elsif (defined $alias) { |
102
|
644
|
|
|
|
|
1143
|
DEBUG and warn "delete \$Alias\{$alias\}"; |
103
|
644
|
|
|
|
|
2597
|
delete $Alias{$alias}; |
104
|
|
|
|
|
|
|
} |
105
|
0
|
|
|
|
|
0
|
elsif (DEBUG) { |
106
|
|
|
|
|
|
|
require Carp; |
107
|
|
|
|
|
|
|
Carp::croak("undef \$alias"); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# Allow latin-1 style names as well |
113
|
|
|
|
|
|
|
# 0 1 2 3 4 5 6 7 8 9 10 |
114
|
|
|
|
|
|
|
our @Latin2iso = ( 0, 1, 2, 3, 4, 9, 10, 13, 14, 15, 16 ); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# Allow winlatin1 style names as well |
117
|
|
|
|
|
|
|
our %Winlatin2cp = ( |
118
|
|
|
|
|
|
|
'latin1' => 1252, |
119
|
|
|
|
|
|
|
'latin2' => 1250, |
120
|
|
|
|
|
|
|
'cyrillic' => 1251, |
121
|
|
|
|
|
|
|
'greek' => 1253, |
122
|
|
|
|
|
|
|
'turkish' => 1254, |
123
|
|
|
|
|
|
|
'hebrew' => 1255, |
124
|
|
|
|
|
|
|
'arabic' => 1256, |
125
|
|
|
|
|
|
|
'baltic' => 1257, |
126
|
|
|
|
|
|
|
'vietnamese' => 1258, |
127
|
|
|
|
|
|
|
); |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
init_aliases(); |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub undef_aliases { |
132
|
42
|
|
|
42
|
0
|
800
|
@Alias = (); |
133
|
42
|
|
|
|
|
198
|
%Alias = (); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub init_aliases { |
137
|
41
|
|
|
41
|
0
|
668
|
undef_aliases(); |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
# Try all-lower-case version should all else fails |
140
|
41
|
|
|
|
|
333
|
define_alias( qr/^(.*)$/ => '"\L$1"' ); |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
# UTF/UCS stuff |
143
|
41
|
|
|
|
|
232
|
define_alias( qr/^(unicode-1-1-)?UTF-?7$/i => '"UTF-7"' ); |
144
|
41
|
|
|
|
|
223
|
define_alias( qr/^UCS-?2-?LE$/i => '"UCS-2LE"' ); |
145
|
41
|
|
|
|
|
566
|
define_alias( |
146
|
|
|
|
|
|
|
qr/^UCS-?2-?(BE)?$/i => '"UCS-2BE"', |
147
|
|
|
|
|
|
|
qr/^UCS-?4-?(BE|LE|)?$/i => 'uc("UTF-32$1")', |
148
|
|
|
|
|
|
|
qr/^iso-10646-1$/i => '"UCS-2BE"' |
149
|
|
|
|
|
|
|
); |
150
|
41
|
|
|
|
|
314
|
define_alias( |
151
|
|
|
|
|
|
|
qr/^UTF-?(16|32)-?BE$/i => '"UTF-$1BE"', |
152
|
|
|
|
|
|
|
qr/^UTF-?(16|32)-?LE$/i => '"UTF-$1LE"', |
153
|
|
|
|
|
|
|
qr/^UTF-?(16|32)$/i => '"UTF-$1"', |
154
|
|
|
|
|
|
|
); |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
# ASCII |
157
|
41
|
|
|
|
|
226
|
define_alias( qr/^(?:US-?)ascii$/i => '"ascii"' ); |
158
|
41
|
|
|
|
|
163
|
define_alias( 'C' => 'ascii' ); |
159
|
41
|
|
|
|
|
227
|
define_alias( qr/\b(?:ISO[-_]?)?646(?:[-_]?US)?$/i => '"ascii"' ); |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
# Allow variants of iso-8859-1 etc. |
162
|
41
|
|
|
|
|
213
|
define_alias( qr/\biso[-_]?(\d+)[-_](\d+)$/i => '"iso-$1-$2"' ); |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
# At least HP-UX has these. |
165
|
41
|
|
|
|
|
225
|
define_alias( qr/\biso8859(\d+)$/i => '"iso-8859-$1"' ); |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# More HP stuff. |
168
|
41
|
|
|
|
|
223
|
define_alias( |
169
|
|
|
|
|
|
|
qr/\b(?:hp-)?(arabic|greek|hebrew|kana|roman|thai|turkish)8$/i => |
170
|
|
|
|
|
|
|
'"${1}8"' ); |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
# The Official name of ASCII. |
173
|
41
|
|
|
|
|
322
|
define_alias( qr/\bANSI[-_]?X3\.4[-_]?1968$/i => '"ascii"' ); |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
# This is a font issue, not an encoding issue. |
176
|
|
|
|
|
|
|
# (The currency symbol of the Latin 1 upper half |
177
|
|
|
|
|
|
|
# has been redefined as the euro symbol.) |
178
|
41
|
|
|
|
|
229
|
define_alias( qr/^(.+)\@euro$/i => '"$1"' ); |
179
|
|
|
|
|
|
|
|
180
|
41
|
|
|
|
|
238
|
define_alias( qr/\b(?:iso[-_]?)?latin[-_]?(\d+)$/i => |
181
|
|
|
|
|
|
|
'defined $Encode::Alias::Latin2iso[$1] ? "iso-8859-$Encode::Alias::Latin2iso[$1]" : undef' |
182
|
|
|
|
|
|
|
); |
183
|
|
|
|
|
|
|
|
184
|
41
|
|
|
|
|
357
|
define_alias( |
185
|
|
|
|
|
|
|
qr/\bwin(latin[12]|cyrillic|baltic|greek|turkish| |
186
|
|
|
|
|
|
|
hebrew|arabic|baltic|vietnamese)$/ix => |
187
|
|
|
|
|
|
|
'"cp" . $Encode::Alias::Winlatin2cp{lc($1)}' |
188
|
|
|
|
|
|
|
); |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
# Common names for non-latin preferred MIME names |
191
|
41
|
|
|
|
|
237
|
define_alias( |
192
|
|
|
|
|
|
|
'ascii' => 'US-ascii', |
193
|
|
|
|
|
|
|
'cyrillic' => 'iso-8859-5', |
194
|
|
|
|
|
|
|
'arabic' => 'iso-8859-6', |
195
|
|
|
|
|
|
|
'greek' => 'iso-8859-7', |
196
|
|
|
|
|
|
|
'hebrew' => 'iso-8859-8', |
197
|
|
|
|
|
|
|
'thai' => 'iso-8859-11', |
198
|
|
|
|
|
|
|
); |
199
|
|
|
|
|
|
|
# RT #20781 |
200
|
41
|
|
|
|
|
223
|
define_alias(qr/\btis-?620\b/i => '"iso-8859-11"'); |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# At least AIX has IBM-NNN (surprisingly...) instead of cpNNN. |
203
|
|
|
|
|
|
|
# And Microsoft has their own naming (again, surprisingly). |
204
|
|
|
|
|
|
|
# And windows-* is registered in IANA! |
205
|
41
|
|
|
|
|
290
|
define_alias( |
206
|
|
|
|
|
|
|
qr/\b(?:cp|ibm|ms|windows)[-_ ]?(\d{2,4})$/i => '"cp$1"' ); |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
# Sometimes seen with a leading zero. |
209
|
|
|
|
|
|
|
# define_alias( qr/\bcp037\b/i => '"cp37"'); |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
# Mac Mappings |
212
|
|
|
|
|
|
|
# predefined in *.ucm; unneeded |
213
|
|
|
|
|
|
|
# define_alias( qr/\bmacIcelandic$/i => '"macIceland"'); |
214
|
41
|
|
|
|
|
322
|
define_alias( qr/^(?:x[_-])?mac[_-](.*)$/i => '"mac$1"' ); |
215
|
|
|
|
|
|
|
# http://rt.cpan.org/Ticket/Display.html?id=36326 |
216
|
41
|
|
|
|
|
230
|
define_alias( qr/^macintosh$/i => '"MacRoman"' ); |
217
|
|
|
|
|
|
|
# https://rt.cpan.org/Ticket/Display.html?id=78125 |
218
|
41
|
|
|
|
|
214
|
define_alias( qr/^macce$/i => '"MacCentralEurRoman"' ); |
219
|
|
|
|
|
|
|
# Ououououou. gone. They are different! |
220
|
|
|
|
|
|
|
# define_alias( qr/\bmacRomanian$/i => '"macRumanian"'); |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
# Standardize on the dashed versions. |
223
|
41
|
|
|
|
|
247
|
define_alias( qr/\bkoi8[\s\-_]*([ru])$/i => '"koi8-$1"' ); |
224
|
|
|
|
|
|
|
|
225
|
41
|
50
|
|
|
|
248
|
unless ($Encode::ON_EBCDIC) { |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
# for Encode::CN |
228
|
41
|
|
|
|
|
309
|
define_alias( qr/\beuc.*cn$/i => '"euc-cn"' ); |
229
|
41
|
|
|
|
|
478
|
define_alias( qr/\bcn.*euc$/i => '"euc-cn"' ); |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
# define_alias( qr/\bGB[- ]?(\d+)$/i => '"euc-cn"' ) |
232
|
|
|
|
|
|
|
# CP936 doesn't have vendor-addon for GBK, so they're identical. |
233
|
41
|
|
|
|
|
262
|
define_alias( qr/^gbk$/i => '"cp936"' ); |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
# This fixes gb2312 vs. euc-cn confusion, practically |
236
|
41
|
|
|
|
|
367
|
define_alias( qr/\bGB[-_ ]?2312(?!-?raw)/i => '"euc-cn"' ); |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
# for Encode::JP |
239
|
41
|
|
|
|
|
214
|
define_alias( qr/\bjis$/i => '"7bit-jis"' ); |
240
|
41
|
|
|
|
|
233
|
define_alias( qr/\beuc.*jp$/i => '"euc-jp"' ); |
241
|
41
|
|
|
|
|
247
|
define_alias( qr/\bjp.*euc$/i => '"euc-jp"' ); |
242
|
41
|
|
|
|
|
238
|
define_alias( qr/\bujis$/i => '"euc-jp"' ); |
243
|
41
|
|
|
|
|
214
|
define_alias( qr/\bshift.*jis$/i => '"shiftjis"' ); |
244
|
41
|
|
|
|
|
257
|
define_alias( qr/\bsjis$/i => '"shiftjis"' ); |
245
|
41
|
|
|
|
|
247
|
define_alias( qr/\bwindows-31j$/i => '"cp932"' ); |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
# for Encode::KR |
248
|
41
|
|
|
|
|
233
|
define_alias( qr/\beuc.*kr$/i => '"euc-kr"' ); |
249
|
41
|
|
|
|
|
244
|
define_alias( qr/\bkr.*euc$/i => '"euc-kr"' ); |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
# This fixes ksc5601 vs. euc-kr confusion, practically |
252
|
41
|
|
|
|
|
283
|
define_alias( qr/(?:x-)?uhc$/i => '"cp949"' ); |
253
|
41
|
|
|
|
|
220
|
define_alias( qr/(?:x-)?windows-949$/i => '"cp949"' ); |
254
|
41
|
|
|
|
|
333
|
define_alias( qr/\bks_c_5601-1987$/i => '"cp949"' ); |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
# for Encode::TW |
257
|
41
|
|
|
|
|
697
|
define_alias( qr/\bbig-?5$/i => '"big5-eten"' ); |
258
|
41
|
|
|
|
|
225
|
define_alias( qr/\bbig5-?et(?:en)?$/i => '"big5-eten"' ); |
259
|
41
|
|
|
|
|
205
|
define_alias( qr/\btca[-_]?big5$/i => '"big5-eten"' ); |
260
|
41
|
|
|
|
|
245
|
define_alias( qr/\bbig5-?hk(?:scs)?$/i => '"big5-hkscs"' ); |
261
|
41
|
|
|
|
|
387
|
define_alias( qr/\bhk(?:scs)?[-_]?big5$/i => '"big5-hkscs"' ); |
262
|
|
|
|
|
|
|
} |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
# https://github.com/dankogai/p5-encode/issues/37 |
265
|
41
|
|
|
|
|
474
|
define_alias(qr/cp65000/i => '"UTF-7"'); |
266
|
41
|
|
|
|
|
244
|
define_alias(qr/cp65001/i => '"utf-8-strict"'); |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
# utf8 is blessed :) |
269
|
41
|
|
|
|
|
245
|
define_alias( qr/\bUTF-8$/i => '"utf-8-strict"' ); |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
# At last, Map white space and _ to '-' |
272
|
41
|
|
|
|
|
215
|
define_alias( qr/^(\S+)[\s_]+(.*)$/i => '"$1-$2"' ); |
273
|
|
|
|
|
|
|
} |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
1; |
276
|
|
|
|
|
|
|
__END__ |