Repository navigation
Expand file tree
/
Copy pathjcode.pl
More file actions
executable file
·1328 lines (615 loc) · 19.2 KB
/
Copy pathjcode.pl
File metadata and controls
executable file
·1328 lines (615 loc) · 19.2 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package jcode;
;######################################################################
;#
;# jcode.pl: Perl library for Japanese character code conversion
;#
;# Copyright (c) 1995,1996,1997 Kazumasa Utashiro <utashiro@iij.ad.jp>
;# Internet Initiative Japan Inc.
;# 3-13 Kanda Nishiki-cho, Chiyoda-ku, Tokyo 101, Japan
;#
;# Copyright (c) 1992,1993,1994 Kazumasa Utashiro
;# Software Research Associates, Inc.
;#
;# Original version was developed under the name of srekcah@sra.co.jp
;# February 1992 and it was called kconv.pl at the beginning. This
;# address was a pen name for group of individuals and it is no longer
;# valid.
;#
;# Use and 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 2.6 1997/12/24 17:01:11 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.
;#
;# When evaluated in array context, it returns a list
;# contains two items. First value is the number of
;# characters which matched to the expected code, and
;# second value is the code name. It is useful if and
;# only if the number is not 0 and the code is undef, and
;# the case means it couldn't tell 'euc' or 'sjis'
;# because the evaluation score was exactly same. This
;# interface should be too tricky, though.
;#
;# Code detection between euc and sjis is very difficult
;# or sometimes impossible or even lead to wrong result
;# when it's include JIS X0201 KANA characters. So JIS
;# X0201 KANA is ignored for automatic code detection.
;#
;# &jcode'convert(*line, $ocode [, $icode [, $option]])
;# Convert the line in any Japanese code to the specified
;# code in the second argument $ocode. $ocode can be any
;# of "jis", "sjis" or "euc", or use "noconv" when you
;# don't want the code conversion. Input code is
;# recognized automatically from the line itself when
;# $icode is not supplied (JIS X0201 KANA is ignored.
;# See above). $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.
;#
;# See next paragraph for $option parameter.
;#
;# &jcode'xxx2yyy(*line [, $option])
;# Convert the Japanese code from xxx to yyy. String xxx
;# and yyy are any convination from "jis", "euc" or
;# "sjis". They return *approximate* number of converted
;# bytes. So return value 0 means the line was not
;# converted at all.
;#
;# Optional parameter $option is used to specify optional
;# conversion method. String "z" is for JIS X0201 KANA
;# to X0208 KANA, and "h" is for reverse.
;#
;# $jcode'convf{'xxx', 'yyy'}
;# The value of this associative array is pointer to the
;# subroutine jcode'xxx2yyy().
;#
;# &jcode'to($ocode, $line [, $icode [, $option]])
;# &jcode'jis($line [, $icode [, $option]])
;# &jcode'euc($line [, $icode [, $option]])
;# &jcode'sjis($line [, $icode [, $option]])
;# These functions are prepared for easy use of
;# call/return-by-value interface. You can use these
;# funcitons in s///e operation or any other place for
;# convenience.
;#
;# &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'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'h2z_xxx(*line);
;# JIS X0201 KANA (so-called Hankaku-KANA) to X0208 KANA
;# (Zenkaku-KANA) code conversion routine. String xxx is
;# any of "jis", "sjis" and "euc". From the difficulty
;# of recognizing code set from 1-byte KATAKANA string,
;# automatic code recognition is not supported.
;#
;# &jcode'z2h_xxx(*line);
;# X0208 to X0201 KANA code conversion routine. String
;# xxx is any of "jis", "sjis" and "euc".
;#
;# $jcode'z2hf{'xxx'}
;# $jcode'h2zf{'xxx'}
;# These are pointer to the corresponding function just
;# as $jcode'convf.
;#
;# ---------------------------------------------------------------
;#
;# &jcode'tr(*line, $from, $to [, $option]);
;# &jcode'tr emulates tr operator for 2 byte code. Only 'd'
;# is interpreted as option.
;#
;# Range operator like `A-Z' for 2 byte code is partially
;# supported. Code must be JIS or EUC, and first byte
;# should be same on first and last character.
;#
;# CAUTION: Handling range operator is a kind of trick
;# and it is not perfect. So if you need to transfer `-'
;# character, please be sure to put it at the beginning
;# or the end of $from and $to strings.
;#
;# &jcode'trans($line, $from, $to [, $option);
;# Same as &jcode'tr but accept string and return string
;# after translation.
;#
;# ---------------------------------------------------------------
;#
;# &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 of JIS conversion.
;#
;# 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 it is not called yet. This may sound
;# strange but it makes easy to embed the jcode.pl at the end of
;# 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.]+)/ ? $1 : 'unkown';
$re_bin = '[\000-\006\177\377]';
$re_jis1978 = '\e\$\@';
$re_jis1983 = '\e\$B';
$re_jis1990 = '\e&\@\e\$B';
$re_jp = "$re_jis1978|$re_jis1983|$re_jis1990";
$re_asc = '\e\([BJ]';
$re_kana = '\e\(I';
($esc_jp, $esc_asc, $esc_kana) = ("\e\$B", "\e(B", "\e(I");
$re_sjis_c = '[\201-\237\340-\374][\100-\176\200-\374]';
$re_sjis_kana = '[\241-\337]';
$re_euc_c = '[\241-\376][\241-\376]';
$re_euc_kana = '\216[\241-\337]';
# These variables are retained only for backward compatibility.
$re_euc_s = "($re_euc_c)+";
$re_sjis_s = "($re_sjis_c)+";
$cache = 1;
# X0201 -> X0208 KANA conversion table. Looks weird? Not that
# much. This is simply JIS text without escape sequences.
($h2z_high = $h2z = <<'__TABLE_END__') =~ tr/\021-\176/\221-\376/;
! !# $ !" % !& " !V # !W
^ !+ _ !, 0 !<
' %! ( %# ) %% * %' + %)
, %c - %e . %g / %C
1 %" 2 %$ 3 %& 4 %( 5 %*
6 %+ 7 %- 8 %/ 9 %1 : %3
6^ %, 7^ %. 8^ %0 9^ %2 :^ %4
; %5 < %7 = %9 > %; ? %=
;^ %6 <^ %8 =^ %: >^ %< ?^ %>
@ %? A %A B %D C %F D %H
@^ %@ A^ %B B^ %E C^ %G D^ %I
E %J F %K G %L H %M I %N
J %O K %R L %U M %X N %[
J^ %P K^ %S L^ %V M^ %Y N^ %\
J_ %Q K_ %T L_ %W M_ %Z N_ %]
O %^ P %_ Q %` R %a S %b
T %d U %f V %h
W %i X %j Y %k Z %l [ %m
\ %o ] %s & %r 3^ %t
__TABLE_END__
%h2z = split(/\s+/, $h2z . $h2z_high);
%z2h = reverse %h2z;
$_ = '';
for $f ('jis', 'sjis', 'euc') {
for $t ('jis', 'sjis', 'euc') {
$_ .= "\$convf{'$f', '$t'} = *${f}2${t};\n";
}
$_ .= "\$h2zf{'$f'} = *h2z_${f};\n\$z2hf{'$f'} = *z2h_${f};\n";
}
eval $_;
}
;#
;# Set escape sequences which should be put before and after Japanese
;# (JIS X0208) string.
;#
sub jis_inout {
$esc_jp = shift || $esc_jp;
$esc_jp = "\e\$$esc_jp" if length($esc_jp) == 1;
$esc_asc = shift || $esc_asc;
$esc_asc = "\e\($esc_asc" if length($esc_asc) == 1;
($esc_jp, $esc_asc);
}
;#
;# Get JIS in and out sequences from the string.
;#
sub get_inout {
local($esc_jp, $esc_asc);
$_[$[] =~ /$re_jp/o && ($esc_jp = $&);
$_[$[] =~ /$re_asc/o && ($esc_asc = $&);
($esc_jp, $esc_asc);
}
;#
;# Recognize character code.
;#
sub getcode {
local(*_) = @_;
local($matched, $code);
if (!/[\e\200-\377]/) { # not Japanese
$matched = 0;
$code = undef;
} # 'jis'
elsif (/$re_jp|$re_asc|$re_kana/o) {
$matched = 1;
$code = 'jis';
}
elsif (/$re_bin/o) { # 'binary'
$matched = 0;
$code = 'binary';
}
else { # should be 'euc' or 'sjis'
local($sjis, $euc);
$sjis += length($&) while /($re_sjis_c)+/go;
$euc += length($&) while /($re_euc_c)+/go;
$matched = &max($sjis, $euc);
$code = ('euc', undef, 'sjis')[($sjis<=>$euc) + $[ + 1];
}
wantarray ? ($matched, $code) : $code;
}
sub max { $_[ $[ + ($_[$[] < $_[$[+1]) ]; }
;#
;# Convert any code to specified code.
;#
sub convert {
local(*_, $ocode, $icode, $opt) = @_;
return (undef, undef) unless $icode = $icode || &getcode(*_);
return (undef, $icode) if $icode eq 'binary';
$ocode = 'jis' unless $ocode;
$ocode = $icode if $ocode eq 'noconv';
local(*convf) = $convf{$icode, $ocode};
do convf(*_, $opt);
(*convf, $icode);
}
;#
;# Easy return-by-value interfaces.
;#
sub jis { &to('jis', @_); }
sub euc { &to('euc', @_); }
sub sjis { &to('sjis', @_); }
sub to {
local($ocode, $_, $icode, $opt) = @_;
&convert(*_, $ocode, $icode, $opt);
$_;
}
sub what {
local($_) = @_;
&getcode(*_);
}
sub trans {
local($_) = shift;
&tr(*_, @_);
$_;
}
;#
;# SJIS to JIS
;#
sub sjis2jis {
local(*_, $opt, $n) = @_;
&sjis2sjis(*_, $opt) if $opt;
s/($re_sjis_kana)+|($re_sjis_c)+/&_sjis2jis($&, $')/geo;
$n;
}
sub _sjis2jis {
local($_) = shift;
local($post) = $_[$[] =~ /^($re_sjis_kana|$re_sjis_c)/o ? "" : $esc_asc;
if (/^$re_sjis_kana/o) {
$n += tr/\241-\337/\041-\137/;
$esc_kana . $_ . $post;
} else {
$n += s/$re_sjis_c/$s2e{$&}||&s2e($&)/geo;
tr/\241-\376/\041-\176/;
$esc_jp . $_ . $post;
}
}
;#
;# EUC to JIS
;#
sub euc2jis {
local(*_, $opt, $n) = @_;
&euc2euc(*_, $opt) if $opt;
s/($re_euc_kana)+|($re_euc_c)+/&_euc2jis($&, $')/geo;
$n;
}
sub _euc2jis {
local($_) = shift;
local($pre) = tr/\216//d ? $esc_kana : $esc_jp;
local($post) = $_[$[] =~ /^($re_euc_kana|$re_euc_c)/o ? "" : $esc_asc;
$n += tr/\241-\376/\041-\176/;
$pre . $_ . $post;
}
;#
;# JIS to EUC
;#
sub jis2euc {
local(*_, $opt, $n) = @_;
s/($re_jp|$re_asc|$re_kana)([^\e]*)/&_jis2euc($1,$2)/geo;
&euc2euc(*_, $opt) if $opt;
$n;
}
sub _jis2euc {
local($esc, $_) = @_;
if ($esc !~ /$re_asc/o) {
$n += tr/\041-\176/\241-\376/;
s/[\241-\337]/\216$&/g if $esc =~ /$re_kana/o;
}
$_;
}
;#
;# JIS to SJIS
;#
sub jis2sjis {
local(*_, $opt, $n) = @_;
&jis2jis(*_, $opt) if $opt;
s/($re_jp|$re_asc|$re_kana)([^\e]*)/&_jis2sjis($1,$2)/geo;
$n;
}
sub _jis2sjis {
local($esc, $_) = @_;
if ($esc !~ /$re_asc/o) {
$n += tr/\041-\176/\241-\376/;
s/$re_euc_c/$e2s{$&}||&e2s($&)/geo if $esc =~ /$re_jp/o;
}
$_;
}
;#
;# SJIS to EUC
;#
sub sjis2euc {
local(*_, $opt,$n) = @_;
$n = s/$re_sjis_kana|$re_sjis_c/$s2e{$&}||&s2e($&)/geo;
&euc2euc(*_, $opt) if $opt;
$n;
}
sub s2e {
local($c1, $c2, $code);
($c1, $c2) = unpack('CC', $code = shift);
if (0xa1 <= $c1 && $c1 <= 0xdf) {
$c2 = $c1;
$c1 = 0x8e;
} elsif (0x9f <= $c2) {
$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(*_, $opt,$n) = @_;
&euc2euc(*_, $opt) if $opt;
$n = s/$re_euc_c|$re_euc_kana/$e2s{$&}||&e2s($&)/geo;
}
sub e2s {
local($c1, $c2, $code);
($c1, $c2) = unpack('CC', $code = shift);
if ($c1 == 0x8e) {
return substr($code, 1, 1);
} elsif ($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);
}
}
;#
;# JIS to JIS, SJIS to SJIS, EUC to EUC
;#
sub jis2jis {
local(*_, $opt) = @_;
s/$re_jp/$esc_jp/go;
s/$re_asc/$esc_asc/go;
&h2z_jis(*_) if $opt =~ /z/;
&z2h_jis(*_) if $opt =~ /h/;
}
sub sjis2sjis {
local(*_, $opt) = @_;
&h2z_sjis(*_) if $opt =~ /z/;
&z2h_sjis(*_) if $opt =~ /h/;
}