| 1 |
package jcode; |
|---|
| 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 |
;; $rcsid = q$Id: jcode.pl,v 1.1.1.1 2003/01/02 08:45:08 q Exp $; |
|---|
| 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 |
&init unless defined $version; |
|---|
| 255 |
|
|---|
| 256 |
; |
|---|
| 257 |
; |
|---|
| 258 |
; |
|---|
| 259 |
sub init { |
|---|
| 260 |
$version = $rcsid =~ /,v ([\d.]+)/ ? $1 : 'unknown'; |
|---|
| 261 |
|
|---|
| 262 |
$re_bin = '[\000-\006\177\377]'; |
|---|
| 263 |
|
|---|
| 264 |
$re_jis0208_1978 = '\e\$\@'; |
|---|
| 265 |
$re_jis0208_1983 = '\e\$B'; |
|---|
| 266 |
$re_jis0208_1990 = '\e&\@\e\$B'; |
|---|
| 267 |
$re_jis0208 = "$re_jis0208_1978|$re_jis0208_1983|$re_jis0208_1990"; |
|---|
| 268 |
$re_jis0212 = '\e\$\(D'; |
|---|
| 269 |
$re_jp = "$re_jis0208|$re_jis0212"; |
|---|
| 270 |
$re_asc = '\e\([BJ]'; |
|---|
| 271 |
$re_kana = '\e\(I'; |
|---|
| 272 |
|
|---|
| 273 |
$esc_0208 = "\e\$B"; |
|---|
| 274 |
$esc_0212 = "\e\$(D"; |
|---|
| 275 |
$esc_asc = "\e(B"; |
|---|
| 276 |
$esc_kana = "\e(I"; |
|---|
| 277 |
|
|---|
| 278 |
$re_sjis_c = '[\201-\237\340-\374][\100-\176\200-\374]'; |
|---|
| 279 |
$re_sjis_kana = '[\241-\337]'; |
|---|
| 280 |
|
|---|
| 281 |
$re_euc_c = '[\241-\376][\241-\376]'; |
|---|
| 282 |
$re_euc_kana = '\216[\241-\337]'; |
|---|
| 283 |
$re_euc_0212 = '\217[\241-\376][\241-\376]'; |
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
$undef_sjis = "\x81\xac"; |
|---|
| 287 |
|
|---|
| 288 |
$cache = 1; |
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
($h2z_high = $h2z = <<'__TABLE_END__') =~ tr/\041-\176/\241-\376/; |
|---|
| 293 |
! ! |
|---|
| 294 |
^ !+ _ !, 0 !< |
|---|
| 295 |
' %! ( %# ) %% * %' + %) |
|---|
| 296 |
, %c - %e . %g / %C |
|---|
| 297 |
1 %" 2 %$ 3 %& 4 %( 5 %* |
|---|
| 298 |
6 %+ 7 %- 8 %/ 9 %1 : %3 |
|---|
| 299 |
6^ %, 7^ %. 8^ %0 9^ %2 :^ %4 |
|---|
| 300 |
; %5 < %7 = %9 > %; ? %= |
|---|
| 301 |
;^ %6 <^ %8 =^ %: >^ %< ?^ %> |
|---|
| 302 |
@ %? A %A B %D C %F D %H |
|---|
| 303 |
@^ %@ A^ %B B^ %E C^ %G D^ %I |
|---|
| 304 |
E %J F %K G %L H %M I %N |
|---|
| 305 |
J %O K %R L %U M %X N %[ |
|---|
| 306 |
J^ %P K^ %S L^ %V M^ %Y N^ %\ |
|---|
| 307 |
J_ %Q K_ %T L_ %W M_ %Z N_ %] |
|---|
| 308 |
O %^ P %_ Q %` R %a S %b |
|---|
| 309 |
T %d U %f V %h |
|---|
| 310 |
W %i X %j Y %k Z %l [ %m |
|---|
| 311 |
\ %o ] %s & %r 3^ %t |
|---|
| 312 |
__TABLE_END__ |
|---|
| 313 |
%h2z = split(/\s+/, $h2z . $h2z_high); |
|---|
| 314 |
%z2h = reverse %h2z; |
|---|
| 315 |
|
|---|
| 316 |
$convf{'jis' , 'jis' } = *jis2jis; |
|---|
| 317 |
$convf{'jis' , 'sjis'} = *jis2sjis; |
|---|
| 318 |
$convf{'jis' , 'euc' } = *jis2euc; |
|---|
| 319 |
$convf{'euc' , 'jis' } = *euc2jis; |
|---|
| 320 |
$convf{'euc' , 'sjis'} = *euc2sjis; |
|---|
| 321 |
$convf{'euc' , 'euc' } = *euc2euc; |
|---|
| 322 |
$convf{'sjis' , 'jis' } = *sjis2jis; |
|---|
| 323 |
$convf{'sjis' , 'sjis'} = *sjis2sjis; |
|---|
| 324 |
$convf{'sjis' , 'euc' } = *sjis2euc; |
|---|
| 325 |
$h2zf{'jis' } = *h2z_jis; |
|---|
| 326 |
$z2hf{'jis' } = *z2h_jis; |
|---|
| 327 |
$h2zf{'euc' } = *h2z_euc; |
|---|
| 328 |
$z2hf{'euc' } = *z2h_euc; |
|---|
| 329 |
$h2zf{'sjis'} = *h2z_sjis; |
|---|
| 330 |
$z2hf{'sjis'} = *z2h_sjis; |
|---|
| 331 |
} |
|---|
| 332 |
|
|---|
| 333 |
;# |
|---|
| 334 |
;# Set escape sequences which should be put before and after Japanese |
|---|
| 335 |
;# (JIS X0208) string. |
|---|
| 336 |
;# |
|---|
| 337 |
sub jis_inout { |
|---|
| 338 |
$esc_0208 = shift || $esc_0208; |
|---|
| 339 |
$esc_0208 = "\e\$$esc_0208" if length($esc_0208) == 1; |
|---|
| 340 |
$esc_asc = shift || $esc_asc; |
|---|
| 341 |
$esc_asc = "\e\($esc_asc" if length($esc_asc) == 1; |
|---|
| 342 |
($esc_0208, $esc_asc); |
|---|
| 343 |
} |
|---|
| 344 |
|
|---|
| 345 |
;# |
|---|
| 346 |
;# Get JIS in and out sequences from the string. |
|---|
| 347 |
;# |
|---|
| 348 |
sub get_inout { |
|---|
| 349 |
local($esc_0208, $esc_asc); |
|---|
| 350 |
$_[$[] =~ /($re_jis0208)/o && ($esc_0208 = $1); |
|---|
| 351 |
$_[$[] =~ /($re_asc)/o && ($esc_asc = $1); |
|---|
| 352 |
($esc_0208, $esc_asc); |
|---|
| 353 |
} |
|---|
| 354 |
|
|---|
| 355 |
;# |
|---|
| 356 |
;# Recognize character code. |
|---|
| 357 |
;# |
|---|
| 358 |
sub getcode { |
|---|
| 359 |
local(*s) = @_; |
|---|
| 360 |
local($matched, $code); |
|---|
| 361 |
|
|---|
| 362 |
if ($s !~ /[\e\200-\377]/) { # not Japanese |
|---|
| 363 |
$matched = 0; |
|---|
| 364 |
$code = undef; |
|---|
| 365 |
} # 'jis' |
|---|
| 366 |
elsif ($s =~ /$re_jp|$re_asc|$re_kana/o) { |
|---|
| 367 |
$matched = 1; |
|---|
| 368 |
$code = 'jis'; |
|---|
| 369 |
} |
|---|
| 370 |
elsif ($s =~ /$re_bin/o) { # 'binary' |
|---|
| 371 |
$matched = 0; |
|---|
| 372 |
$code = 'binary'; |
|---|
| 373 |
} |
|---|
| 374 |
else { # should be 'euc' or 'sjis' |
|---|
| 375 |
local($sjis, $euc) = (0, 0); |
|---|
| 376 |
|
|---|
| 377 |
while ($s =~ /(($re_sjis_c)+)/go) { |
|---|
| 378 |
$sjis += length($1); |
|---|
| 379 |
} |
|---|
| 380 |
while ($s =~ /(($re_euc_c|$re_euc_kana|$re_euc_0212)+)/go) { |
|---|
| 381 |
$euc += length($1); |
|---|
| 382 |
} |
|---|
| 383 |
$matched = &max($sjis, $euc); |
|---|
| 384 |
$code = ('euc', undef, 'sjis')[($sjis<=>$euc) + $[ + 1]; |
|---|
| 385 |
} |
|---|
| 386 |
wantarray ? ($matched, $code) : $code; |
|---|
| 387 |
} |
|---|
| 388 |
sub max { $_[ $[ + ($_[ $[ ] < $_[ $[ + 1 ]) ]; } |
|---|
| 389 |
|
|---|
| 390 |
;# |
|---|
| 391 |
;# Convert any code to specified code. |
|---|
| 392 |
;# |
|---|
| 393 |
sub convert { |
|---|
| 394 |
local(*s, $ocode, $icode, $opt) = @_; |
|---|
| 395 |
return (undef, undef) unless $icode = $icode || &getcode(*s); |
|---|
| 396 |
return (undef, $icode) if $icode eq 'binary'; |
|---|
| 397 |
$ocode = 'jis' unless $ocode; |
|---|
| 398 |
$ocode = $icode if $ocode eq 'noconv'; |
|---|
| 399 |
local(*f) = $convf{$icode, $ocode}; |
|---|
| 400 |
&f(*s, $opt); |
|---|
| 401 |
wantarray ? (*f, $icode) : $icode; |
|---|
| 402 |
} |
|---|
| 403 |
|
|---|
| 404 |
;# |
|---|
| 405 |
;# Easy return-by-value interfaces. |
|---|
| 406 |
;# |
|---|
| 407 |
sub jis { &to('jis', @_); } |
|---|
| 408 |
sub euc { &to('euc', @_); } |
|---|
| 409 |
sub sjis { &to('sjis', @_); } |
|---|
| 410 |
sub to { |
|---|
| 411 |
local($ocode, $s, $icode, $opt) = @_; |
|---|
| 412 |
&convert(*s, $ocode, $icode, $opt); |
|---|
| 413 |
$s; |
|---|
| 414 |
} |
|---|
| 415 |
sub what { |
|---|
| 416 |
local($s) = @_; |
|---|
| 417 |
&getcode(*s); |
|---|
| 418 |
} |
|---|
| 419 |
sub trans { |
|---|
| 420 |
local($s) = shift; |
|---|
| 421 |
&tr(*s, @_); |
|---|
| 422 |
$s; |
|---|
| 423 |
} |
|---|
| 424 |
|
|---|
| 425 |
;# |
|---|
| 426 |
;# SJIS to JIS |
|---|
| 427 |
;# |
|---|
| 428 |
sub sjis2jis { |
|---|
| 429 |
local(*s, $opt, $n) = @_; |
|---|
| 430 |
&sjis2sjis(*s, $opt) if $opt; |
|---|
| 431 |
$s =~ s/(($re_sjis_c|$re_sjis_kana)+)/&_sjis2jis($1) . $esc_asc/geo; |
|---|
| 432 |
$n; |
|---|
| 433 |
} |
|---|
| 434 |
sub _sjis2jis { |
|---|
| 435 |
local($s) = shift; |
|---|
| 436 |
$s =~ s/(($re_sjis_c)+|($re_sjis_kana)+)/&__sjis2jis($1)/geo; |
|---|
| 437 |
$s; |
|---|
| 438 |
} |
|---|
| 439 |
sub __sjis2jis { |
|---|
| 440 |
local($s) = shift; |
|---|
| 441 |
if ($s =~ /^$re_sjis_kana/o) { |
|---|
| 442 |
$n += $s =~ tr/\241-\337/\041-\137/; |
|---|
| 443 |
$esc_kana . $s; |
|---|
| 444 |
} else { |
|---|
| 445 |
$n += $s =~ s/($re_sjis_c)/$s2e{$1}||&s2e($1)/geo; |
|---|
| 446 |
$s =~ tr/\241-\376/\041-\176/; |
|---|
| 447 |
$esc_0208 . $s; |
|---|
| 448 |
} |
|---|
| 449 |
} |
|---|
| 450 |
|
|---|
| 451 |
;# |
|---|
| 452 |
;# EUC to JIS |
|---|
| 453 |
;# |
|---|
| 454 |
sub euc2jis { |
|---|
| 455 |
local(*s, $opt, $n) = @_; |
|---|
| 456 |
&euc2euc(*s, $opt) if $opt; |
|---|
| 457 |
$s =~ s/(($re_euc_c|$re_euc_kana|$re_euc_0212)+)/ |
|---|
| 458 |
&_euc2jis($1) . $esc_asc |
|---|
| 459 |
/geo; |
|---|
| 460 |
$n; |
|---|
| 461 |
} |
|---|
| 462 |
sub _euc2jis { |
|---|
| 463 |
local($s) = shift; |
|---|
| 464 |
$s =~ s/(($re_euc_c)+|($re_euc_kana)+|($re_euc_0212)+)/&__euc2jis($1)/geo; |
|---|
| 465 |
$s; |
|---|
| 466 |
} |
|---|
| 467 |
sub __euc2jis { |
|---|
| 468 |
local($s) = shift; |
|---|
| 469 |
local($esc); |
|---|
| 470 |
|
|---|
| 471 |
if ($s =~ tr/\216//d) { |
|---|
| 472 |
$esc = $esc_kana; |
|---|
| 473 |
} elsif ($s =~ tr/\217//d) { |
|---|
| 474 |
$esc = $esc_0212; |
|---|
| 475 |
} else { |
|---|
| 476 |
$esc = $esc_0208; |
|---|
| 477 |
} |
|---|
| 478 |
|
|---|
| 479 |
$n += $s =~ tr/\241-\376/\041-\176/; |
|---|
| 480 |
$esc . $s; |
|---|
| 481 |
} |
|---|
| 482 |
|
|---|
| 483 |
;# |
|---|
| 484 |
;# JIS to EUC |
|---|
| 485 |
;# |
|---|
| 486 |
sub jis2euc { |
|---|
| 487 |
local(*s, $opt, $n) = @_; |
|---|
| 488 |
$s =~ s/($re_jp|$re_asc|$re_kana)([^\e]*)/&_jis2euc($1,$2)/geo; |
|---|
| 489 |
&euc2euc(*s, $opt) if $opt; |
|---|
| 490 |
$n; |
|---|
| 491 |
} |
|---|
| 492 |
sub _jis2euc { |
|---|
| 493 |
local($esc, $s) = @_; |
|---|
| 494 |
if ($esc !~ /^$re_asc/o) { |
|---|
| 495 |
$n += $s =~ tr/\041-\176/\241-\376/; |
|---|
| 496 |
if ($esc =~ /^$re_kana/o) { |
|---|
| 497 |
$s =~ s/([\241-\337])/\216$1/g; |
|---|
| 498 |
} |
|---|
| 499 |
elsif ($esc =~ /^$re_jis0212/o) { |
|---|
| 500 |
$s =~ s/([\241-\376][\241-\376])/\217$1/g; |
|---|
| 501 |
} |
|---|
| 502 |
} |
|---|
| 503 |
$s; |
|---|
| 504 |
} |
|---|
| 505 |
|
|---|
| 506 |
;# |
|---|
| 507 |
;# JIS to SJIS |
|---|
| 508 |
;# |
|---|
| 509 |
sub jis2sjis { |
|---|
| 510 |
local(*s, $opt, $n) = @_; |
|---|
| 511 |
&jis2jis(*s, $opt) if $opt; |
|---|
| 512 |
$s =~ s/($re_jp|$re_asc|$re_kana)([^\e]*)/&_jis2sjis($1,$2)/geo; |
|---|
| 513 |
$n; |
|---|
| 514 |
} |
|---|
| 515 |
sub _jis2sjis { |
|---|
| 516 |
local($esc, $s) = @_; |
|---|
| 517 |
if ($esc =~ /^$re_jis0212/o) { |
|---|
| 518 |
$s =~ s/../$undef_sjis/g; |
|---|
| 519 |
$n = length; |
|---|
| 520 |
} |
|---|
| 521 |
elsif ($esc !~ /^$re_asc/o) { |
|---|
| 522 |
$n += $s =~ tr/\041-\176/\241-\376/; |
|---|
| 523 |
if ($esc =~ /^$re_jp/o) { |
|---|
| 524 |
$s =~ s/($re_euc_c)/$e2s{$1}||&e2s($1)/geo; |
|---|
| 525 |
} |
|---|
| 526 |
} |
|---|
| 527 |
$s; |
|---|
| 528 |
} |
|---|
| 529 |
|
|---|
| 530 |
;# |
|---|
| 531 |
;# SJIS to EUC |
|---|
| 532 |
;# |
|---|
| 533 |
sub sjis2euc { |
|---|
| 534 |
local(*s, $opt,$n) = @_; |
|---|
| 535 |
$n = $s =~ s/($re_sjis_c|$re_sjis_kana)/$s2e{$1}||&s2e($1)/geo; |
|---|
| 536 |
&euc2euc(*s, $opt) if $opt; |
|---|
| 537 |
$n; |
|---|
| 538 |
} |
|---|
| 539 |
sub s2e { |
|---|
| 540 |
local($c1, $c2, $code); |
|---|
| 541 |
($c1, $c2) = unpack('CC', $code = shift); |
|---|
| 542 |
|
|---|
| 543 |
if (0xa1 <= $c1 && $c1 <= 0xdf) { |
|---|
| 544 |
$c2 = $c1; |
|---|
| 545 |
$c1 = 0x8e; |
|---|
| 546 |
} elsif (0x9f <= $c2) { |
|---|
| 547 |
$c1 = $c1 * 2 - ($c1 >= 0xe0 ? 0xe0 : 0x60); |
|---|
| 548 |
$c2 += 2; |
|---|
| 549 |
} else { |
|---|
| 550 |
$c1 = $c1 * 2 - ($c1 >= 0xe0 ? 0xe1 : 0x61); |
|---|
| 551 |
$c2 += 0x60 + ($c2 < 0x7f); |
|---|
| 552 |
} |
|---|
| 553 |
if ($cache) { |
|---|
| 554 |
$s2e{$code} = pack('CC', $c1, $c2); |
|---|
| 555 |
} else { |
|---|
| 556 |
pack('CC', $c1, $c2); |
|---|
| 557 |
} |
|---|
| 558 |
} |
|---|
| 559 |
|
|---|
| 560 |
;# |
|---|
| 561 |
;# EUC to SJIS |
|---|
| 562 |
;# |
|---|
| 563 |
sub euc2sjis { |
|---|
| 564 |
local(*s, $opt,$n) = @_; |
|---|
| 565 |
&euc2euc(*s, $opt) if $opt; |
|---|
| 566 |
$n = $s =~ s/($re_euc_c|$re_euc_kana|$re_euc_0212)/$e2s{$1}||&e2s($1)/geo; |
|---|
| 567 |
} |
|---|
| 568 |
sub e2s { |
|---|
| 569 |
local($c1, $c2, $code); |
|---|
| 570 |
($c1, $c2) = unpack('CC', $code = shift); |
|---|
| 571 |
|
|---|
| 572 |
if ($c1 == 0x8e) { # SS2 |
|---|
| 573 |
return substr($code, 1, 1); |
|---|
| 574 |
} elsif ($c1 == 0x8f) { # SS3 |
|---|
| 575 |
return $undef_sjis; |
|---|
| 576 |
} elsif ($c1 % 2) { |
|---|
| 577 |
$c1 = ($c1>>1) + ($c1 < 0xdf ? 0x31 : 0x71); |
|---|
| 578 |
$c2 -= 0x60 + ($c2 < 0xe0); |
|---|
| 579 |
} else { |
|---|
| 580 |
$c1 = ($c1>>1) + ($c1 < 0xdf ? 0x30 : 0x70); |
|---|
| 581 |
$c2 -= 2; |
|---|
| 582 |
} |
|---|
| 583 |
if ($cache) { |
|---|
| 584 |
$e2s{$code} = pack('CC', $c1, $c2); |
|---|
| 585 |
} else { |
|---|
| 586 |
pack('CC', $c1, $c2); |
|---|
| 587 |
} |
|---|
| 588 |
} |
|---|
| 589 |
|
|---|
| 590 |
;# |
|---|
| 591 |
;# JIS to JIS, SJIS to SJIS, EUC to EUC |
|---|
| 592 |
;# |
|---|
| 593 |
sub jis2jis { |
|---|
| 594 |
local(*s, $opt) = @_; |
|---|
| 595 |
$s =~ s/$re_jis0208/$esc_0208/go; |
|---|
| 596 |
$s =~ s/$re_asc/$esc_asc/go; |
|---|
| 597 |
&h2z_jis(*s) if $opt =~ /z/; |
|---|
| 598 |
&z2h_jis(*s) if $opt =~ /h/; |
|---|
| 599 |
} |
|---|
| 600 |
sub sjis2sjis { |
|---|
| 601 |
local(*s, $opt) = @_; |
|---|
| 602 |
&h2z_sjis(*s) if $opt =~ /z/; |
|---|
| 603 |
&z2h_sjis(*s) if $opt =~ /h/; |
|---|
| 604 |
} |
|---|
| 605 |
sub euc2euc { |
|---|
| 606 |
local(*s, $opt) = @_; |
|---|
| 607 |
&h2z_euc(*s) if $opt =~ /z/; |
|---|
| 608 |
&z2h_euc(*s) if $opt =~ /h/; |
|---|
| 609 |
} |
|---|
| 610 |
|
|---|
| 611 |
;# |
|---|
| 612 |
;# Cache control functions |
|---|
| 613 |
;# |
|---|
| 614 |
sub cache { |
|---|
| 615 |
($cache, $cache = 1)[$[]; |
|---|
| 616 |
} |
|---|
| 617 |
sub nocache { |
|---|
| 618 |
($cache, $cache = 0)[$[]; |
|---|
| 619 |
} |
|---|
| 620 |
sub flushcache { |
|---|
| 621 |
undef %e2s; |
|---|
| 622 |
undef %s2e; |
|---|
| 623 |
} |
|---|
| 624 |
|
|---|
| 625 |
;# |
|---|
| 626 |
;# X0201 -> X0208 KANA conversion routine |
|---|
| 627 |
;# |
|---|
| 628 |
sub h2z_jis { |
|---|
| 629 |
local(*s, $n) = @_; |
|---|
| 630 |
if ($s =~ s/$re_kana([^\e]*)/$esc_0208 . &_h2z_jis($1)/geo) { |
|---|
| 631 |
1 while $s =~ s/(($re_jis0208)[^\e]*)($re_jis0208)/$1/o; |
|---|
| 632 |
} |
|---|
| 633 |
$n; |
|---|
| 634 |
} |
|---|
| 635 |
sub _h2z_jis { |
|---|
| 636 |
local($s) = @_; |
|---|
| 637 |
$n += $s =~ s/(([\041-\137])([\136\137])?)/ |
|---|
| 638 |
$h2z{$1} || $h2z{$2} . $h2z{$3} |
|---|
| 639 |
/ge; |
|---|
| 640 |
$s; |
|---|
| 641 |
} |
|---|
| 642 |
|
|---|
| 643 |
sub h2z_euc { |
|---|
| 644 |
local(*s) = @_; |
|---|
| 645 |
$s =~ s/\216([\241-\337])(\216([\336\337]))?/ |
|---|
| 646 |
$h2z{"$1$3"} || $h2z{$1} . $h2z{$3} |
|---|
| 647 |
/ge; |
|---|
| 648 |
} |
|---|
| 649 |
|
|---|
| 650 |
sub h2z_sjis { |
|---|
| 651 |
local(*s, $n) = @_; |
|---|
| 652 |
$s =~ s/(($re_sjis_c)+)|(([\241-\337])([\336\337])?)/ |
|---|
| 653 |
$1 || ($n++, $h2z{$3} ? $e2s{$h2z{$3}} || &e2s($h2z{$3}) |
|---|
| 654 |
: &e2s($h2z{$4}) . ($5 && &e2s($h2z{$5}))) |
|---|
| 655 |
/geo; |
|---|
| 656 |
$n; |
|---|
| 657 |
} |
|---|
| 658 |
|
|---|
| 659 |
;# |
|---|
| 660 |
;# X0208 -> X0201 KANA conversion routine |
|---|
| 661 |
;# |
|---|
| 662 |
sub z2h_jis { |
|---|
| 663 |
local(*s, $n) = @_; |
|---|
| 664 |
$s =~ s/($re_jis0208)([^\e]+)/&_z2h_jis($2)/geo; |
|---|
| 665 |
$n; |
|---|
| 666 |
} |
|---|
| 667 |
sub _z2h_jis { |
|---|
| 668 |
local($s) = @_; |
|---|
| 669 |
$s =~ s/((\%[!-~]|![\#\"&VW+,<])+|([^!%][!-~]|![^\#\"&VW+,<])+)/ |
|---|
| 670 |
&__z2h_jis($1) |
|---|
| 671 |
/ge; |
|---|
| 672 |
$s; |
|---|
| 673 |
} |
|---|
| 674 |
sub __z2h_jis { |
|---|
| 675 |
local($s) = @_; |
|---|
| 676 |
return $esc_0208 . $s unless $s =~ /^%/ || $s =~ /^![\#\"&VW+,<]/; |
|---|
| 677 |
$n += length($s) / 2; |
|---|
| 678 |
$s =~ s/(..)/$z2h{$1}/g; |
|---|
| 679 |
$esc_kana . $s; |
|---|
| 680 |
} |
|---|
| 681 |
|
|---|
| 682 |
sub z2h_euc { |
|---|
| 683 |
local(*s, $n) = @_; |
|---|
| 684 |
&init_z2h_euc unless defined %z2h_euc; |
|---|
| 685 |
$s =~ s/($re_euc_c|$re_euc_kana)/ |
|---|
| 686 |
$z2h_euc{$1} ? ($n++, $z2h_euc{$1}) : $1 |
|---|
| 687 |
/geo; |
|---|
| 688 |
$n; |
|---|
| 689 |
} |
|---|
| 690 |
|
|---|
| 691 |
sub z2h_sjis { |
|---|
| 692 |
local(*s, $n) = @_; |
|---|
| 693 |
&init_z2h_sjis unless defined %z2h_sjis; |
|---|
| 694 |
$s =~ s/($re_sjis_c)/$z2h_sjis{$1} ? ($n++, $z2h_sjis{$1}) : $1/geo; |
|---|
| 695 |
$n; |
|---|
| 696 |
} |
|---|
| 697 |
|
|---|
| 698 |
;# |
|---|
| 699 |
;# Initializing JIS X0208 to X0201 KANA table for EUC and SJIS. This |
|---|
| 700 |
;# can be done in &init but it's not worth doing. Similarly, |
|---|
| 701 |
;# precalculated table is not worth to occupy the file space and |
|---|
| 702 |
;# reduce the readability. The author personnaly discourages to use |
|---|
| 703 |
;# X0201 Kana character in the any situation. |
|---|
| 704 |
;# |
|---|
| 705 |
sub init_z2h_euc { |
|---|
| 706 |
local($k, $s); |
|---|
| 707 |
while (($k, $s) = each %z2h) { |
|---|
| 708 |
$s =~ s/([\241-\337])/\216$1/g && ($z2h_euc{$k} = $s); |
|---|
| 709 |
} |
|---|
| 710 |
} |
|---|
| 711 |
sub init_z2h_sjis { |
|---|
| 712 |
local($s, $v); |
|---|
| 713 |
while (($s, $v) = each %z2h) { |
|---|
| 714 |
$s =~ /[\200-\377]/ && ($z2h_sjis{&e2s($s)} = $v); |
|---|
| 715 |
} |
|---|
| 716 |
} |
|---|
| 717 |
|
|---|
| 718 |
;# |
|---|
| 719 |
;# TR function for 2-byte code |
|---|
| 720 |
;# |
|---|
| 721 |
sub tr { |
|---|
| 722 |
# $prev_from, $prev_to, %table are persistent variables |
|---|
| 723 |
local(*s, $from, $to, $opt) = @_; |
|---|
| 724 |
local(@from, @to); |
|---|
| 725 |
local($jis, $n) = (0, 0); |
|---|
| 726 |
|
|---|
| 727 |
$jis++, &jis2euc(*s) if $s =~ /$re_jp|$re_asc|$re_kana/o; |
|---|
| 728 |
$jis++ if $to =~ /$re_jp|$re_asc|$re_kana/o; |
|---|
| 729 |
|
|---|
| 730 |
if (!defined($prev_from) || $from ne $prev_from || $to ne $prev_to) { |
|---|
| 731 |
($prev_from, $prev_to) = ($from, $to); |
|---|
| 732 |
undef %table; |
|---|
| 733 |
&_maketable; |
|---|
| 734 |
} |
|---|
| 735 |
|
|---|
| 736 |
$s =~ s/([\200-\377][\000-\377]|[\000-\377])/ |
|---|
| 737 |
defined($table{$1}) && ++$n ? $table{$1} : $1 |
|---|
| 738 |
/ge; |
|---|
| 739 |
|
|---|
| 740 |
&euc2jis(*s) if $jis; |
|---|
| 741 |
|
|---|
| 742 |
$n; |
|---|
| 743 |
} |
|---|
| 744 |
|
|---|
| 745 |
sub _maketable { |
|---|
| 746 |
local($ascii) = '(\\\\[\\-\\\\]|[\0-\133\135-\177])'; |
|---|
| 747 |
|
|---|
| 748 |
&jis2euc(*to) if $to =~ /$re_jp|$re_asc|$re_kana/o; |
|---|
| 749 |
&jis2euc(*from) if $from =~ /$re_jp|$re_asc|$re_kana/o; |
|---|
| 750 |
|
|---|
| 751 |
grep(s/(([\200-\377])[\200-\377]-\2[\200-\377])/&_expnd2($1)/ge, |
|---|
| 752 |
$from, $to); |
|---|
| 753 |
grep(s/($ascii-$ascii)/&_expnd1($1)/geo, |
|---|
| 754 |
$from, $to); |
|---|
| 755 |
|
|---|
| 756 |
@to = $to =~ /[\200-\377][\000-\377]|[\000-\377]/g; |
|---|
| 757 |
@from = $from =~ /[\200-\377][\000-\377]|[\000-\377]/g; |
|---|
| 758 |
push(@to, ($opt =~ /d/ ? '' : $to[$#to]) x (@from - @to)) if @to < @from; |
|---|
| 759 |
@table{@from} = @to; |
|---|
| 760 |
} |
|---|
| 761 |
|
|---|
| 762 |
sub _expnd1 { |
|---|
| 763 |
local($s) = @_; |
|---|
| 764 |
$s =~ s/\\(.)/$1/g; |
|---|
| 765 |
local($c1, $c2) = unpack('CxC', $s); |
|---|
| 766 |
if ($c1 <= $c2) { |
|---|
| 767 |
for ($s = ''; $c1 <= $c2; $c1++) { |
|---|
| 768 |
$s .= pack('C', $c1); |
|---|
| 769 |
} |
|---|
| 770 |
} |
|---|
| 771 |
$s; |
|---|
| 772 |
} |
|---|
| 773 |
|
|---|
| 774 |
sub _expnd2 { |
|---|
| 775 |
local($s) = @_; |
|---|
| 776 |
local($c1, $c2, $c3, $c4) = unpack('CCxCC', $s); |
|---|
| 777 |
if ($c1 == $c3 && $c2 <= $c4) { |
|---|
| 778 |
for ($s = ''; $c2 <= $c4; $c2++) { |
|---|
| 779 |
$s .= pack('CC', $c1, $c2); |
|---|
| 780 |
} |
|---|
| 781 |
} |
|---|
| 782 |
$s; |
|---|
| 783 |
} |
|---|
| 784 |
|
|---|
| 785 |
1; |
|---|
| 786 |
|
|---|