-
Notifications
You must be signed in to change notification settings - Fork 1
/
jcode.pl
executable file
·358 lines (334 loc) · 8.78 KB
/
jcode.pl
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package jcode;
;######################################################################
;#
;# jcode.pl: Japanese character code conversion library
;#
;# Copyright (c) 1995 Kazumasa Utashiro <utashiro@iij.ad.jp>
;# Internet Initiative Japan Inc.
;# Sanban-cho, Chiyoda-ku, Tokyo 102, Japan
;#
;# Copyright (c) 1992,1993,1994 Kazumasa Utashiro
;# Software Research Associates, Inc.
;# Original by srekcah@sra.co.jp, Feb 1992
;#
;# Redistribution for any purpose, without significant modification,
;# is granted as long as all copyright notices are retained. THIS
;# SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
;# IMPLIED WARRANTIES ARE DISCLAIMED.
;#
;; $rcsid = q$Id: jcode.pl,v 1.12.1.1 1996/09/08 14:42:16 utashiro Exp $;
;#
;######################################################################
;#
;# INTERFACE:
;#
;# &jcode'getcode(*line)
;# Return 'jis', 'sjis', 'euc' or undef according to
;# Japanese character code in $line. Return 'binary' if
;# the data has non-character code.
;#
;# &jcode'convert(*line, $ocode [, $icode])
;# Convert the line in any Japanese code to specified
;# code in second argument $ocode. $ocode is any of
;# 'jis', 'sjis' or 'euc'. Input code is recognized
;# automatically from the line itself when $icode is not
;# supplied. $icode also can be specified, but xxx2yyy
;# routine is more efficient when both codes are known.
;#
;# It returns a list of pointer of convert subroutine and
;# input code. It means that this routine returns the
;# input code of the line in scalar context.
;#
;# &jcode'xxx2yyy(*line)
;# Convert Japanese code from xxx to yyy. xxx and yyy
;# are one of "jis", "sjis" or "euc". These subroutines
;# return number of converted substrings. So return
;# value 0 means the line was not converted at all.
;#
;# &jcode'jis_inout($in, $out)
;# Set or inquire JIS start and end sequences. Default
;# is "ESC-$-B" and "ESC-(-B". If you supplied only one
;# character, "ESC-$" or "ESC-(" is added as a prefix
;# for each character respectively. Acutually "ESC-(-B"
;# is not a sequence to end JIS code but a sequence to
;# start ASCII code set. So `in' and `out' are somewhat
;# misleading.
;#
;# &jcode'get_inout($string)
;# Get JIS start and end sequences from $string.
;#
;# $jcode'convf{'xxx', 'yyy'}
;# The value of this associative array is pointer to the
;# subroutine jcode'xxx2yyy().
;#
;# &jcode'cache()
;# &jcode'nocache()
;# &jcode'flush()
;# Usually, converted character is cached in memory to
;# avoid same calculations have to be done many times.
;# To disable this caching, call &jcode'nocache(). It
;# can be revived by &jcode'cache() and cache is flushed
;# by calling &jcode'flush(). &cache() and &nocache()
;# functions return previous caching state.
;#
;# ---------------------------------------------------------------
;#
;# &jcode'tr(*line, $from, $to [, $option]);
;# &jcode'tr emulates tr operator for 2 byte code. This
;# funciton is under construction and doesn't have full
;# feature of tr. Range operator like a-z is not
;# supported. Only 'd' is interpreted as option.
;#
;# ---------------------------------------------------------------
;#
;# &jcode'init()
;# Initialize the variables used in other functions. You
;# don't have to call this when using jocde.pl by do or
;# require. Call it first if you embedded the jcode.pl
;# in your script.
;#
;######################################################################
;#
;# SAMPLES
;#
;# Convert any Kanji code to JIS and print each line with code name.
;#
;# while (<>) {
;# $code = &jcode'convert(*_, 'jis');
;# print $code, "\t", $_;
;# }
;#
;# Convert all lines to JIS according to the first recognized line.
;#
;# while (<>) {
;# print, next unless /[\033\200-\377]/;
;# (*f, $icode) = &jcode'convert(*_, 'jis');
;# print;
;# defined(&f) || next;
;# while (<>) { &f(*_); print; }
;# last;
;# }
;#
;# The safest way for converting to JIS.
;#
;# while (<>) {
;# ($matched, $code) = &jcode'getcode(*_);
;# print, next unless (@buf || $matched);
;# push(@readahead, $_);
;# next unless $code;
;# eval "&jcode'${code}2jis(*_), print while (\$_ = shift(\@buf));";
;# eval "&jcode'${code}2jis(*_), print while (\$_ = <>);";
;# last;
;# }
;#
;######################################################################
;#
;# Call initialize function if not called yet. This sounds strange
;# but this makes easy to embed the jcode.pl in the script. Call
;# &jcode'init at the beginning of the script in that case.
;#
&init unless defined $version;
;#
;# Initialize variables.
;#
sub init {
($version) = ($rcsid =~ /,v ([\d.]+)/);
$re_sjis_c = '[\201-\237\340-\374][\100-\176\200-\374]';
$re_sjis_s = "($re_sjis_c)+";
$re_euc_c = '[\241-\376][\241-\376]';
$re_euc_s = "($re_euc_c)+";
$re_jin = '\033\$[\@B]';
$re_jout = '\033\([BJ]';
$re_binary = '[\000-\006\177\377]';
&jis_inout("\033\$B", "\033(B");
$cache = 1;
for $from ('jis', 'sjis', 'euc') {
for $to ('jis', 'sjis', 'euc') {
eval "\$convf{$from, $to} = *${from}2${to};";
}
}
}
;#
;# Set JIS in and out final characters.
;#
sub jis_inout {
$jin = shift || $jin;
$jout = shift || $jout;
$jin = "\033\$".$jin if length($jin) == 1;
$jout = "\033\(".$jout if length($jout) == 1;
($jin, $jout);
}
;#
;# Get JIS in and out sequences from the string.
;#
sub get_inout {
local($jin, $jout);
$_[$[] =~ /$re_jin/o && ($jin = $&);
$_[$[] =~ /$re_jout/o && ($jout = $&);
($jin, $jout);
}
;#
;# Character code recognition
;#
sub getcode {
local(*_) = @_;
return undef unless /[\033\200-\377]/;
return 'jis' if /$re_jin|$re_jout/o;
return 'binary' if /$re_binary/o;
local($sjis, $euc);
$sjis += length($&) while /$re_sjis_s/go;
$euc += length($&) while /$re_euc_s/go;
(&max($sjis, $euc), ('euc', undef, 'sjis')[($sjis<=>$euc) + $[ + 1]);
}
sub max { $_[ $[ + ($_[$[] < $_[$[+1]) ]; }
;#
;# Convert any code to specified code
;#
sub convert {
local(*_, $ocode, $icode) = @_;
return (undef, undef) unless $icode = $icode || &getcode(*_);
return (undef, $icode) if $icode eq 'binary';
$ocode = 'jis' unless $ocode;
local(*convf) = $convf{$icode, $ocode};
do convf(*_);
(*convf, $icode);
}
;#
;# JIS to JIS
;#
sub jis2jis {
local(*_) = @_;
s/$re_jin/$jin/go;
s/$re_jout/$jout/go;
}
;#
;# SJIS to JIS
;#
sub sjis2jis {
local(*_) = @_;
s/$re_sjis_s/&_sjis2jis($&)/geo;
}
sub _sjis2jis {
local($_) = @_;
s/../$s2e{$&}||&s2e($&)/geo;
tr/\241-\376/\041-\176/;
$jin . $_ . $jout;
}
;#
;# EUC to JIS
;#
sub euc2jis {
local(*_) = @_;
s/$re_euc_s/&_euc2jis($&)/geo;
}
sub _euc2jis {
local($_) = @_;
tr/\241-\376/\041-\176/;
$jin . $_ . $jout;
}
;#
;# JIS to EUC
;#
sub jis2euc {
local(*_) = @_;
s/$re_jin([!-~]*)$re_jout/&_jis2euc($1)/geo;
}
sub _jis2euc {
local($_) = @_;
tr/\041-\176/\241-\376/;
$_;
}
;#
;# JIS to SJIS
;#
sub jis2sjis {
local(*_) = @_;
s/$re_jin([!-~]*)$re_jout/&_jis2sjis($1)/geo;
}
sub _jis2sjis {
local($_) = @_;
tr/\041-\176/\241-\376/;
s/../$e2s{$&}||&e2s($&)/ge;
$_;
}
;#
;# SJIS to EUC
;#
sub sjis2euc {
local(*_) = @_;
s/$re_sjis_c/$s2e{$&}||&s2e($&)/geo;
}
sub s2e {
local($c1, $c2) = unpack('CC', $code = shift);
if ($c2 >= 0x9f) {
$c1 = $c1 * 2 - ($c1 >= 0xe0 ? 0xe0 : 0x60);
$c2 += 2;
} else {
$c1 = $c1 * 2 - ($c1 >= 0xe0 ? 0xe1 : 0x61);
$c2 += 0x60 + ($c2 < 0x7f);
}
if ($cache) {
$s2e{$code} = pack('CC', $c1, $c2);
} else {
pack('CC', $c1, $c2);
}
}
;#
;# EUC to SJIS
;#
sub euc2sjis {
local(*_) = @_;
s/$re_euc_c/$e2s{$&}||&e2s($&)/geo;
}
sub e2s {
local($c1, $c2) = unpack('CC', $code = shift);
if ($c1 % 2) {
$c1 = ($c1>>1) + ($c1 < 0xdf ? 0x31 : 0x71);
$c2 -= 0x60 + ($c2 < 0xe0);
} else {
$c1 = ($c1>>1) + ($c1 < 0xdf ? 0x30 : 0x70);
$c2 -= 2;
}
if ($cache) {
$e2s{$code} = pack('CC', $c1, $c2);
} else {
pack('CC', $c1, $c2);
}
}
;#
;# SJIS to SJIS, EUC to EUC
;#
sub sjis2sjis { 0; }
sub euc2euc { 0; }
;#
;# Cache control functions
;#
sub cache {
($cache, $cache = 1)[$[];
}
sub nocache {
($cache, $cache = 0)[$[];
}
sub flushcache {
undef %e2s;
undef %s2e;
}
;#
;# TR function for 2-byte code
;#
sub tr {
local(*_, $from, $to, $opt) = @_;
local(@from, @to, %table);
local($wasjis, $count) = (0, 0);
&jis2euc(*_), $wasjis++ if $_ =~ /$re_jin/o;
&jis2euc(*from) if $from =~ /$re_jin/o;
&jis2euc(*to), $wasjis++ if $to =~ /$re_jin/o;
@from = $from =~ /[\200-\377].|./g;
@to = $to =~ /[\200-\377].|./g;
push(@to, ($opt =~ /d/ ? '' : $to[$#to]) x (@from - @to)) if @to < @from;
@table{@from} = @to;
s/[\200-\377].|./defined($table{$&}) && ++$count ? $table{$&} : $&/ge;
&euc2jis(*_) if $wasjis;
$count;
}
1;