Parent Directory
|
Revision Log
| 1 | wakaba | 1.1 | package jcode; |
| 2 | ;###################################################################### | ||
| 3 | ;# | ||
| 4 | ;# jcode.pl: Perl library for Japanese character code conversion | ||
| 5 | ;# | ||
| 6 | ;# Copyright (c) 1995-1999 Kazumasa Utashiro <[email protected]> | ||
| 7 | ;# Internet Initiative Japan Inc. | ||
| 8 | ;# 3-13 Kanda Nishiki-cho, Chiyoda-ku, Tokyo 101-0054, Japan | ||
| 9 | ;# | ||
| 10 | ;# Copyright (c) 1992,1993,1994 Kazumasa Utashiro | ||
| 11 | ;# Software Research Associates, Inc. | ||
| 12 | ;# | ||
| 13 | ;# Use and redistribution for ANY PURPOSE are granted as long as all | ||
| 14 | ;# copyright notices are retained. Redistribution with modification | ||
| 15 | ;# is allowed provided that you make your modified version obviously | ||
| 16 | ;# distinguishable from the original one. THIS SOFTWARE IS PROVIDED | ||
| 17 | ;# BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES ARE | ||
| 18 | ;# DISCLAIMED. | ||
| 19 | ;# | ||
| 20 | ;# Original version was developed under the name of [email protected] | ||
| 21 | ;# February 1992 and it was called kconv.pl at the beginning. This | ||
| 22 | ;# address was a pen name for group of individuals and it is no longer | ||
| 23 | ;# valid. | ||
| 24 | ;# | ||
| 25 | ;# The latest version is available here: | ||
| 26 | ;# | ||
| 27 | ;# ftp://ftp.iij.ad.jp/pub/IIJ/dist/utashiro/perl/ | ||
| 28 | ;# | ||
| 29 | ;; $rcsid = q$Id: jcode.pl,v 2.11 1999/12/26 17:16:47 utashiro Exp $; | ||
| 30 | ;# | ||
| 31 | ;###################################################################### | ||
| 32 | ;# | ||
| 33 | ;# PERL4 INTERFACE: | ||
| 34 | ;# | ||
| 35 | ;# &jcode'getcode(*line) | ||
| 36 | ;# Return 'jis', 'sjis', 'euc' or undef according to | ||
| 37 | ;# Japanese character code in $line. Return 'binary' if | ||
| 38 | ;# the data has non-character code. | ||
| 39 | ;# | ||
| 40 | ;# When evaluated in array context, it returns a list | ||
| 41 | ;# contains two items. First value is the number of | ||
| 42 | ;# characters which matched to the expected code, and | ||
| 43 | ;# second value is the code name. It is useful if and | ||
| 44 | ;# only if the number is not 0 and the code is undef; | ||
| 45 | ;# that case means it couldn't tell 'euc' or 'sjis' | ||
| 46 | ;# because the evaluation score was exactly same. This | ||
| 47 | ;# interface is too tricky, though. | ||
| 48 | ;# | ||
| 49 | ;# Code detection between euc and sjis is very difficult | ||
| 50 | ;# or sometimes impossible or even lead to wrong result | ||
| 51 | ;# when it includes JIS X0201 KANA characters. So JIS | ||
| 52 | ;# X0201 KANA is ignored for automatic code detection. | ||
| 53 | ;# | ||
| 54 | ;# &jcode'convert(*line, $ocode [, $icode [, $option]]) | ||
| 55 | ;# Convert the contents of $line to the specified | ||
| 56 | ;# Japanese code given in the second argument $ocode. | ||
| 57 | ;# $ocode can be any of "jis", "sjis" or "euc", or use | ||
| 58 | ;# "noconv" when you don't want the code conversion. | ||
| 59 | ;# Input code is recognized automatically from the line | ||
| 60 | ;# itself when $icode is not supplied (JIS X0201 KANA is | ||
| 61 | ;# ignored in code detection. See the above descripton | ||
| 62 | ;# of &getcode). $icode also can be specified, but | ||
| 63 | ;# xxx2yyy routine is more efficient when both codes are | ||
| 64 | ;# known. | ||
| 65 | ;# | ||
| 66 | ;# It returns the code of input string in scalar context, | ||
| 67 | ;# and a list of pointer of convert subroutine and the | ||
| 68 | ;# input code in array context. | ||
| 69 | ;# | ||
| 70 | ;# Japanese character code JIS X0201, X0208, X0212 and | ||
| 71 | ;# ASCII code are supported. X0212 characters can not be | ||
| 72 | ;# represented in SJIS and they will be replased by | ||
| 73 | ;# "geta" character when converted to SJIS. | ||
| 74 | ;# | ||
| 75 | ;# See next paragraph for $option parameter. | ||
| 76 | ;# | ||
| 77 | ;# &jcode'xxx2yyy(*line [, $option]) | ||
| 78 | ;# Convert the Japanese code from xxx to yyy. String xxx | ||
| 79 | ;# and yyy are any convination from "jis", "euc" or | ||
| 80 | ;# "sjis". They return *approximate* number of converted | ||
| 81 | ;# bytes. So return value 0 means the line was not | ||
| 82 | ;# converted at all. | ||
| 83 | ;# | ||
| 84 | ;# Optional parameter $option is used to specify optional | ||
| 85 | ;# conversion method. String "z" is for JIS X0201 KANA | ||
| 86 | ;# to X0208 KANA, and "h" is for reverse. | ||
| 87 | ;# | ||
| 88 | ;# $jcode'convf{'xxx', 'yyy'} | ||
| 89 | ;# The value of this associative array is pointer to the | ||
| 90 | ;# subroutine jcode'xxx2yyy(). | ||
| 91 | ;# | ||
| 92 | ;# &jcode'to($ocode, $line [, $icode [, $option]]) | ||
| 93 | ;# &jcode'jis($line [, $icode [, $option]]) | ||
| 94 | ;# &jcode'euc($line [, $icode [, $option]]) | ||
| 95 | ;# &jcode'sjis($line [, $icode [, $option]]) | ||
| 96 | ;# These functions are prepared for easy use of | ||
| 97 | ;# call/return-by-value interface. You can use these | ||
| 98 | ;# funcitons in s///e operation or any other place for | ||
| 99 | ;# convenience. | ||
| 100 | ;# | ||
| 101 | ;# &jcode'jis_inout($in, $out) | ||
| 102 | ;# Set or inquire JIS start and end sequences. Default | ||
| 103 | ;# is "ESC-$-B" and "ESC-(-B". If you supplied only one | ||
| 104 | ;# character, "ESC-$" or "ESC-(" is prepended for each | ||
| 105 | ;# character respectively. Acutually "ESC-(-B" is not a | ||
| 106 | ;# sequence to end JIS code but a sequence to start ASCII | ||
| 107 | ;# code set. So `in' and `out' are somewhat misleading. | ||
| 108 | ;# | ||
| 109 | ;# &jcode'get_inout($string) | ||
| 110 | ;# Get JIS start and end sequences from $string. | ||
| 111 | ;# | ||
| 112 | ;# &jcode'cache() | ||
| 113 | ;# &jcode'nocache() | ||
| 114 | ;# &jcode'flush() | ||
| 115 | ;# Usually, converted character is cached in memory to | ||
| 116 | ;# avoid same calculations have to be done many times. | ||
| 117 | ;# To disable this caching, call &jcode'nocache(). It | ||
| 118 | ;# can be revived by &jcode'cache() and cache is flushed | ||
| 119 | ;# by calling &jcode'flush(). &cache() and &nocache() | ||
| 120 | ;# functions return previous caching state. | ||
| 121 | ;# | ||
| 122 | ;# --------------------------------------------------------------- | ||
| 123 | ;# | ||
| 124 | ;# &jcode'h2z_xxx(*line) | ||
| 125 | ;# JIS X0201 KANA (so-called Hankaku-KANA) to X0208 KANA | ||
| 126 | ;# (Zenkaku-KANA) code conversion routine. String xxx is | ||
| 127 | ;# any of "jis", "sjis" and "euc". From the difficulty | ||
| 128 | ;# of recognizing code set from 1-byte KATAKANA string, | ||
| 129 | ;# automatic code recognition is not supported. | ||
| 130 | ;# | ||
| 131 | ;# &jcode'z2h_xxx(*line) | ||
| 132 | ;# X0208 to X0201 KANA code conversion routine. String | ||
| 133 | ;# xxx is any of "jis", "sjis" and "euc". | ||
| 134 | ;# | ||
| 135 | ;# $jcode'z2hf{'xxx'} | ||
| 136 | ;# $jcode'h2zf{'xxx'} | ||
| 137 | ;# These are pointer to the corresponding function just | ||
| 138 | ;# as $jcode'convf. | ||
| 139 | ;# | ||
| 140 | ;# --------------------------------------------------------------- | ||
| 141 | ;# | ||
| 142 | ;# &jcode'tr(*line, $from, $to [, $option]) | ||
| 143 | ;# &jcode'tr emulates tr operator for 2 byte code. Only 'd' | ||
| 144 | ;# is interpreted as an option. | ||
| 145 | ;# | ||
| 146 | ;# Range operator like `A-Z' for 2 byte code is partially | ||
| 147 | ;# supported. Code must be JIS or EUC, and first byte | ||
| 148 | ;# have to be same on first and last character. | ||
| 149 | ;# | ||
| 150 | ;# CAUTION: Handling range operator is a kind of trick | ||
| 151 | ;# and it is not perfect. So if you need to transfer `-' | ||
| 152 | ;# character, please be sure to put it at the beginning | ||
| 153 | ;# or the end of $from and $to strings. | ||
| 154 | ;# | ||
| 155 | ;# &jcode'trans($line, $from, $to [, $option) | ||
| 156 | ;# Same as &jcode'tr but accept string and return string | ||
| 157 | ;# after translation. | ||
| 158 | ;# | ||
| 159 | ;# --------------------------------------------------------------- | ||
| 160 | ;# | ||
| 161 | ;# &jcode'init() | ||
| 162 | ;# Initialize the variables used in this package. You | ||
| 163 | ;# don't have to call this when using jocde.pl by `do' or | ||
| 164 | ;# `require' interface. Call it first if you embedded | ||
| 165 | ;# the jcode.pl at the end of your script. | ||
| 166 | ;# | ||
| 167 | ;###################################################################### | ||
| 168 | ;# | ||
| 169 | ;# PERL5 INTERFACE: | ||
| 170 | ;# | ||
| 171 | ;# Current jcode.pl is written in Perl 4 but it is possible to use | ||
| 172 | ;# from Perl 5 using `references'. Fully perl5 capable version is | ||
| 173 | ;# future issue. | ||
| 174 | ;# | ||
| 175 | ;# Since lexical variable is not a subject of typeglob, *string style | ||
| 176 | ;# call doesn't work if the variable is declared as `my'. Same thing | ||
| 177 | ;# happens to special variable $_ if the perl is compiled to use | ||
| 178 | ;# thread capability. So using reference is generally recommented to | ||
| 179 | ;# avoid the mysterious error. | ||
| 180 | ;# | ||
| 181 | ;# jcode::getcode(\$line) | ||
| 182 | ;# jcode::convert(\$line, $ocode [, $icode [, $option]]) | ||
| 183 | ;# jcode::xxx2yyy(\$line [, $option]) | ||
| 184 |