Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: kconv.rb
#
[0] Fix | Delete
# kconv.rb - Kanji Converter.
[1] Fix | Delete
#
[2] Fix | Delete
# $Id: kconv.rb 11708 2007-02-12 23:01:19Z shyouhei $
[3] Fix | Delete
#
[4] Fix | Delete
# ----
[5] Fix | Delete
#
[6] Fix | Delete
# kconv.rb implements the Kconv class for Kanji Converter. Additionally,
[7] Fix | Delete
# some methods in String classes are added to allow easy conversion.
[8] Fix | Delete
#
[9] Fix | Delete
[10] Fix | Delete
require 'nkf'
[11] Fix | Delete
[12] Fix | Delete
#
[13] Fix | Delete
# Kanji Converter for Ruby.
[14] Fix | Delete
#
[15] Fix | Delete
module Kconv
[16] Fix | Delete
#
[17] Fix | Delete
# Public Constants
[18] Fix | Delete
#
[19] Fix | Delete
[20] Fix | Delete
#Constant of Encoding
[21] Fix | Delete
[22] Fix | Delete
# Auto-Detect
[23] Fix | Delete
AUTO = NKF::AUTO
[24] Fix | Delete
# ISO-2022-JP
[25] Fix | Delete
JIS = NKF::JIS
[26] Fix | Delete
# EUC-JP
[27] Fix | Delete
EUC = NKF::EUC
[28] Fix | Delete
# Shift_JIS
[29] Fix | Delete
SJIS = NKF::SJIS
[30] Fix | Delete
# BINARY
[31] Fix | Delete
BINARY = NKF::BINARY
[32] Fix | Delete
# NOCONV
[33] Fix | Delete
NOCONV = NKF::NOCONV
[34] Fix | Delete
# ASCII
[35] Fix | Delete
ASCII = NKF::ASCII
[36] Fix | Delete
# UTF-8
[37] Fix | Delete
UTF8 = NKF::UTF8
[38] Fix | Delete
# UTF-16
[39] Fix | Delete
UTF16 = NKF::UTF16
[40] Fix | Delete
# UTF-32
[41] Fix | Delete
UTF32 = NKF::UTF32
[42] Fix | Delete
# UNKNOWN
[43] Fix | Delete
UNKNOWN = NKF::UNKNOWN
[44] Fix | Delete
[45] Fix | Delete
#
[46] Fix | Delete
# Private Constants
[47] Fix | Delete
#
[48] Fix | Delete
[49] Fix | Delete
# Revision of kconv.rb
[50] Fix | Delete
REVISION = %q$Revision: 11708 $
[51] Fix | Delete
[52] Fix | Delete
#Regexp of Encoding
[53] Fix | Delete
[54] Fix | Delete
# Regexp of Shift_JIS string (private constant)
[55] Fix | Delete
RegexpShiftjis = /\A(?:
[56] Fix | Delete
[\x00-\x7f\xa1-\xdf] |
[57] Fix | Delete
[\x81-\x9f\xe0-\xfc][\x40-\x7e\x80-\xfc]
[58] Fix | Delete
)*\z/nx
[59] Fix | Delete
[60] Fix | Delete
# Regexp of EUC-JP string (private constant)
[61] Fix | Delete
RegexpEucjp = /\A(?:
[62] Fix | Delete
[\x00-\x7f] |
[63] Fix | Delete
\x8e [\xa1-\xdf] |
[64] Fix | Delete
\x8f [\xa1-\xfe] [\xa1-\xfe] |
[65] Fix | Delete
[\xa1-\xfe] [\xa1-\xfe]
[66] Fix | Delete
)*\z/nx
[67] Fix | Delete
[68] Fix | Delete
# Regexp of UTF-8 string (private constant)
[69] Fix | Delete
RegexpUtf8 = /\A(?:
[70] Fix | Delete
[\x00-\x7f] |
[71] Fix | Delete
[\xc2-\xdf] [\x80-\xbf] |
[72] Fix | Delete
\xe0 [\xa0-\xbf] [\x80-\xbf] |
[73] Fix | Delete
[\xe1-\xef] [\x80-\xbf] [\x80-\xbf] |
[74] Fix | Delete
\xf0 [\x90-\xbf] [\x80-\xbf] [\x80-\xbf] |
[75] Fix | Delete
[\xf1-\xf3] [\x80-\xbf] [\x80-\xbf] [\x80-\xbf] |
[76] Fix | Delete
\xf4 [\x80-\x8f] [\x80-\xbf] [\x80-\xbf]
[77] Fix | Delete
)*\z/nx
[78] Fix | Delete
[79] Fix | Delete
#
[80] Fix | Delete
# Public Methods
[81] Fix | Delete
#
[82] Fix | Delete
[83] Fix | Delete
# call-seq:
[84] Fix | Delete
# Kconv.kconv(str, out_code, in_code = Kconv::AUTO)
[85] Fix | Delete
#
[86] Fix | Delete
# Convert <code>str</code> to out_code.
[87] Fix | Delete
# <code>out_code</code> and <code>in_code</code> are given as constants of Kconv.
[88] Fix | Delete
#
[89] Fix | Delete
# *Note*
[90] Fix | Delete
# This method decode MIME encoded string and
[91] Fix | Delete
# convert halfwidth katakana to fullwidth katakana.
[92] Fix | Delete
# If you don't want to decode them, use NKF.nkf.
[93] Fix | Delete
def kconv(str, out_code, in_code = AUTO)
[94] Fix | Delete
opt = '-'
[95] Fix | Delete
case in_code
[96] Fix | Delete
when ::NKF::JIS
[97] Fix | Delete
opt << 'J'
[98] Fix | Delete
when ::NKF::EUC
[99] Fix | Delete
opt << 'E'
[100] Fix | Delete
when ::NKF::SJIS
[101] Fix | Delete
opt << 'S'
[102] Fix | Delete
when ::NKF::UTF8
[103] Fix | Delete
opt << 'W'
[104] Fix | Delete
when ::NKF::UTF16
[105] Fix | Delete
opt << 'W16'
[106] Fix | Delete
end
[107] Fix | Delete
[108] Fix | Delete
case out_code
[109] Fix | Delete
when ::NKF::JIS
[110] Fix | Delete
opt << 'j'
[111] Fix | Delete
when ::NKF::EUC
[112] Fix | Delete
opt << 'e'
[113] Fix | Delete
when ::NKF::SJIS
[114] Fix | Delete
opt << 's'
[115] Fix | Delete
when ::NKF::UTF8
[116] Fix | Delete
opt << 'w'
[117] Fix | Delete
when ::NKF::UTF16
[118] Fix | Delete
opt << 'w16'
[119] Fix | Delete
when ::NKF::NOCONV
[120] Fix | Delete
return str
[121] Fix | Delete
end
[122] Fix | Delete
[123] Fix | Delete
opt = '' if opt == '-'
[124] Fix | Delete
[125] Fix | Delete
::NKF::nkf(opt, str)
[126] Fix | Delete
end
[127] Fix | Delete
module_function :kconv
[128] Fix | Delete
[129] Fix | Delete
#
[130] Fix | Delete
# Encode to
[131] Fix | Delete
#
[132] Fix | Delete
[133] Fix | Delete
# call-seq:
[134] Fix | Delete
# Kconv.tojis(str) -> string
[135] Fix | Delete
#
[136] Fix | Delete
# Convert <code>str</code> to ISO-2022-JP
[137] Fix | Delete
#
[138] Fix | Delete
# *Note*
[139] Fix | Delete
# This method decode MIME encoded string and
[140] Fix | Delete
# convert halfwidth katakana to fullwidth katakana.
[141] Fix | Delete
# If you don't want it, use NKF.nkf('-jxm0', str).
[142] Fix | Delete
def tojis(str)
[143] Fix | Delete
::NKF::nkf('-jm', str)
[144] Fix | Delete
end
[145] Fix | Delete
module_function :tojis
[146] Fix | Delete
[147] Fix | Delete
# call-seq:
[148] Fix | Delete
# Kconv.toeuc(str) -> string
[149] Fix | Delete
#
[150] Fix | Delete
# Convert <code>str</code> to EUC-JP
[151] Fix | Delete
#
[152] Fix | Delete
# *Note*
[153] Fix | Delete
# This method decode MIME encoded string and
[154] Fix | Delete
# convert halfwidth katakana to fullwidth katakana.
[155] Fix | Delete
# If you don't want it, use NKF.nkf('-exm0', str).
[156] Fix | Delete
def toeuc(str)
[157] Fix | Delete
::NKF::nkf('-em', str)
[158] Fix | Delete
end
[159] Fix | Delete
module_function :toeuc
[160] Fix | Delete
[161] Fix | Delete
# call-seq:
[162] Fix | Delete
# Kconv.tosjis(str) -> string
[163] Fix | Delete
#
[164] Fix | Delete
# Convert <code>str</code> to Shift_JIS
[165] Fix | Delete
#
[166] Fix | Delete
# *Note*
[167] Fix | Delete
# This method decode MIME encoded string and
[168] Fix | Delete
# convert halfwidth katakana to fullwidth katakana.
[169] Fix | Delete
# If you don't want it, use NKF.nkf('-sxm0', str).
[170] Fix | Delete
def tosjis(str)
[171] Fix | Delete
::NKF::nkf('-sm', str)
[172] Fix | Delete
end
[173] Fix | Delete
module_function :tosjis
[174] Fix | Delete
[175] Fix | Delete
# call-seq:
[176] Fix | Delete
# Kconv.toutf8(str) -> string
[177] Fix | Delete
#
[178] Fix | Delete
# Convert <code>str</code> to UTF-8
[179] Fix | Delete
#
[180] Fix | Delete
# *Note*
[181] Fix | Delete
# This method decode MIME encoded string and
[182] Fix | Delete
# convert halfwidth katakana to fullwidth katakana.
[183] Fix | Delete
# If you don't want it, use NKF.nkf('-wxm0', str).
[184] Fix | Delete
def toutf8(str)
[185] Fix | Delete
::NKF::nkf('-wm', str)
[186] Fix | Delete
end
[187] Fix | Delete
module_function :toutf8
[188] Fix | Delete
[189] Fix | Delete
# call-seq:
[190] Fix | Delete
# Kconv.toutf16(str) -> string
[191] Fix | Delete
#
[192] Fix | Delete
# Convert <code>str</code> to UTF-16
[193] Fix | Delete
#
[194] Fix | Delete
# *Note*
[195] Fix | Delete
# This method decode MIME encoded string and
[196] Fix | Delete
# convert halfwidth katakana to fullwidth katakana.
[197] Fix | Delete
# If you don't want it, use NKF.nkf('-w16xm0', str).
[198] Fix | Delete
def toutf16(str)
[199] Fix | Delete
::NKF::nkf('-w16m', str)
[200] Fix | Delete
end
[201] Fix | Delete
module_function :toutf16
[202] Fix | Delete
[203] Fix | Delete
#
[204] Fix | Delete
# guess
[205] Fix | Delete
#
[206] Fix | Delete
[207] Fix | Delete
# call-seq:
[208] Fix | Delete
# Kconv.guess(str) -> integer
[209] Fix | Delete
#
[210] Fix | Delete
# Guess input encoding by NKF.guess2
[211] Fix | Delete
def guess(str)
[212] Fix | Delete
::NKF::guess(str)
[213] Fix | Delete
end
[214] Fix | Delete
module_function :guess
[215] Fix | Delete
[216] Fix | Delete
# call-seq:
[217] Fix | Delete
# Kconv.guess_old(str) -> integer
[218] Fix | Delete
#
[219] Fix | Delete
# Guess input encoding by NKF.guess1
[220] Fix | Delete
def guess_old(str)
[221] Fix | Delete
::NKF::guess1(str)
[222] Fix | Delete
end
[223] Fix | Delete
module_function :guess_old
[224] Fix | Delete
[225] Fix | Delete
#
[226] Fix | Delete
# isEncoding
[227] Fix | Delete
#
[228] Fix | Delete
[229] Fix | Delete
# call-seq:
[230] Fix | Delete
# Kconv.iseuc(str) -> obj or nil
[231] Fix | Delete
#
[232] Fix | Delete
# Returns whether input encoding is EUC-JP or not.
[233] Fix | Delete
#
[234] Fix | Delete
# *Note* don't expect this return value is MatchData.
[235] Fix | Delete
def iseuc(str)
[236] Fix | Delete
RegexpEucjp.match( str )
[237] Fix | Delete
end
[238] Fix | Delete
module_function :iseuc
[239] Fix | Delete
[240] Fix | Delete
# call-seq:
[241] Fix | Delete
# Kconv.issjis(str) -> obj or nil
[242] Fix | Delete
#
[243] Fix | Delete
# Returns whether input encoding is Shift_JIS or not.
[244] Fix | Delete
#
[245] Fix | Delete
# *Note* don't expect this return value is MatchData.
[246] Fix | Delete
def issjis(str)
[247] Fix | Delete
RegexpShiftjis.match( str )
[248] Fix | Delete
end
[249] Fix | Delete
module_function :issjis
[250] Fix | Delete
[251] Fix | Delete
# call-seq:
[252] Fix | Delete
# Kconv.isutf8(str) -> obj or nil
[253] Fix | Delete
#
[254] Fix | Delete
# Returns whether input encoding is UTF-8 or not.
[255] Fix | Delete
#
[256] Fix | Delete
# *Note* don't expect this return value is MatchData.
[257] Fix | Delete
def isutf8(str)
[258] Fix | Delete
RegexpUtf8.match( str )
[259] Fix | Delete
end
[260] Fix | Delete
module_function :isutf8
[261] Fix | Delete
[262] Fix | Delete
end
[263] Fix | Delete
[264] Fix | Delete
class String
[265] Fix | Delete
# call-seq:
[266] Fix | Delete
# String#kconv(out_code, in_code = Kconv::AUTO)
[267] Fix | Delete
#
[268] Fix | Delete
# Convert <code>self</code> to out_code.
[269] Fix | Delete
# <code>out_code</code> and <code>in_code</code> are given as constants of Kconv.
[270] Fix | Delete
#
[271] Fix | Delete
# *Note*
[272] Fix | Delete
# This method decode MIME encoded string and
[273] Fix | Delete
# convert halfwidth katakana to fullwidth katakana.
[274] Fix | Delete
# If you don't want to decode them, use NKF.nkf.
[275] Fix | Delete
def kconv(out_code, in_code=Kconv::AUTO)
[276] Fix | Delete
Kconv::kconv(self, out_code, in_code)
[277] Fix | Delete
end
[278] Fix | Delete
[279] Fix | Delete
#
[280] Fix | Delete
# to Encoding
[281] Fix | Delete
#
[282] Fix | Delete
[283] Fix | Delete
# call-seq:
[284] Fix | Delete
# String#tojis -> string
[285] Fix | Delete
#
[286] Fix | Delete
# Convert <code>self</code> to ISO-2022-JP
[287] Fix | Delete
#
[288] Fix | Delete
# *Note*
[289] Fix | Delete
# This method decode MIME encoded string and
[290] Fix | Delete
# convert halfwidth katakana to fullwidth katakana.
[291] Fix | Delete
# If you don't want it, use NKF.nkf('-jxm0', str).
[292] Fix | Delete
def tojis; Kconv.tojis(self) end
[293] Fix | Delete
[294] Fix | Delete
# call-seq:
[295] Fix | Delete
# String#toeuc -> string
[296] Fix | Delete
#
[297] Fix | Delete
# Convert <code>self</code> to EUC-JP
[298] Fix | Delete
#
[299] Fix | Delete
# *Note*
[300] Fix | Delete
# This method decode MIME encoded string and
[301] Fix | Delete
# convert halfwidth katakana to fullwidth katakana.
[302] Fix | Delete
# If you don't want it, use NKF.nkf('-exm0', str).
[303] Fix | Delete
def toeuc; Kconv.toeuc(self) end
[304] Fix | Delete
[305] Fix | Delete
# call-seq:
[306] Fix | Delete
# String#tosjis -> string
[307] Fix | Delete
#
[308] Fix | Delete
# Convert <code>self</code> to Shift_JIS
[309] Fix | Delete
#
[310] Fix | Delete
# *Note*
[311] Fix | Delete
# This method decode MIME encoded string and
[312] Fix | Delete
# convert halfwidth katakana to fullwidth katakana.
[313] Fix | Delete
# If you don't want it, use NKF.nkf('-sxm0', str).
[314] Fix | Delete
def tosjis; Kconv.tosjis(self) end
[315] Fix | Delete
[316] Fix | Delete
# call-seq:
[317] Fix | Delete
# String#toutf8 -> string
[318] Fix | Delete
#
[319] Fix | Delete
# Convert <code>self</code> to UTF-8
[320] Fix | Delete
#
[321] Fix | Delete
# *Note*
[322] Fix | Delete
# This method decode MIME encoded string and
[323] Fix | Delete
# convert halfwidth katakana to fullwidth katakana.
[324] Fix | Delete
# If you don't want it, use NKF.nkf('-wxm0', str).
[325] Fix | Delete
def toutf8; Kconv.toutf8(self) end
[326] Fix | Delete
[327] Fix | Delete
# call-seq:
[328] Fix | Delete
# String#toutf16 -> string
[329] Fix | Delete
#
[330] Fix | Delete
# Convert <code>self</code> to UTF-16
[331] Fix | Delete
#
[332] Fix | Delete
# *Note*
[333] Fix | Delete
# This method decode MIME encoded string and
[334] Fix | Delete
# convert halfwidth katakana to fullwidth katakana.
[335] Fix | Delete
# If you don't want it, use NKF.nkf('-w16xm0', str).
[336] Fix | Delete
def toutf16; Kconv.toutf16(self) end
[337] Fix | Delete
[338] Fix | Delete
#
[339] Fix | Delete
# is Encoding
[340] Fix | Delete
#
[341] Fix | Delete
[342] Fix | Delete
# call-seq:
[343] Fix | Delete
# String#iseuc -> obj or nil
[344] Fix | Delete
#
[345] Fix | Delete
# Returns whether <code>self</code>'s encoding is EUC-JP or not.
[346] Fix | Delete
#
[347] Fix | Delete
# *Note* don't expect this return value is MatchData.
[348] Fix | Delete
def iseuc; Kconv.iseuc(self) end
[349] Fix | Delete
[350] Fix | Delete
# call-seq:
[351] Fix | Delete
# String#issjis -> obj or nil
[352] Fix | Delete
#
[353] Fix | Delete
# Returns whether <code>self</code>'s encoding is Shift_JIS or not.
[354] Fix | Delete
#
[355] Fix | Delete
# *Note* don't expect this return value is MatchData.
[356] Fix | Delete
def issjis; Kconv.issjis(self) end
[357] Fix | Delete
[358] Fix | Delete
# call-seq:
[359] Fix | Delete
# String#isutf8 -> obj or nil
[360] Fix | Delete
#
[361] Fix | Delete
# Returns whether <code>self</code>'s encoding is UTF-8 or not.
[362] Fix | Delete
#
[363] Fix | Delete
# *Note* don't expect this return value is MatchData.
[364] Fix | Delete
def isutf8; Kconv.isutf8(self) end
[365] Fix | Delete
end
[366] Fix | Delete
[367] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function