Module: Kconv

Defined in:
lib/kconv.rb

Overview

Kanji Converter for Ruby.

Constant Summary collapse

AUTO =

Auto-Detect

NKF::AUTO
JIS =

ISO-2022-JP

NKF::JIS
EUC =

EUC-JP

NKF::EUC
SJIS =

Shift_JIS

NKF::SJIS
BINARY =

BINARY

NKF::BINARY
NOCONV =

NOCONV

NKF::NOCONV
ASCII =

ASCII

NKF::ASCII
UTF8 =

UTF-8

NKF::UTF8
UTF16 =

UTF-16

NKF::UTF16
UTF32 =

UTF-32

NKF::UTF32
UNKNOWN =

UNKNOWN

NKF::UNKNOWN
REVISION =

Revision of kconv.rb

%q$Revision: 11708 $
RegexpShiftjis =

Regexp of Shift_JIS string (private constant)

/\A(?:
 [\x00-\x7f\xa1-\xdf] |
 [\x81-\x9f\xe0-\xfc][\x40-\x7e\x80-\xfc] 
)*\z/nx
RegexpEucjp =

Regexp of EUC-JP string (private constant)

/\A(?:
 [\x00-\x7f]                         |
 \x8e        [\xa1-\xdf]             |
 \x8f        [\xa1-\xfe] [\xa1-\xfe] |
 [\xa1-\xfe] [\xa1-\xfe]
)*\z/nx
RegexpUtf8 =

Regexp of UTF-8 string (private constant)

/\A(?:
 [\x00-\x7f]                                     |
 [\xc2-\xdf] [\x80-\xbf]                         |
 \xe0        [\xa0-\xbf] [\x80-\xbf]             |
 [\xe1-\xef] [\x80-\xbf] [\x80-\xbf]             |
 \xf0        [\x90-\xbf] [\x80-\xbf] [\x80-\xbf] |
 [\xf1-\xf3] [\x80-\xbf] [\x80-\xbf] [\x80-\xbf] |
 \xf4        [\x80-\x8f] [\x80-\xbf] [\x80-\xbf]
)*\z/nx

Instance Method Summary collapse

Instance Method Details

#guess(str) ⇒ Object

call-seq:

Kconv.guess(str)   -> integer

Guess input encoding by NKF.guess2



213
214
215
# File 'lib/kconv.rb', line 213

def guess(str)
  ::NKF::guess(str)
end

#guess_old(str) ⇒ Object

call-seq:

Kconv.guess_old(str)   -> integer

Guess input encoding by NKF.guess1



222
223
224
# File 'lib/kconv.rb', line 222

def guess_old(str)
  ::NKF::guess1(str)
end

#iseuc(str) ⇒ Object

call-seq:

Kconv.iseuc(str)   -> obj or nil

Returns whether input encoding is EUC-JP or not.

Note don???t expect this return value is MatchData.



237
238
239
# File 'lib/kconv.rb', line 237

def iseuc(str)
  RegexpEucjp.match( str )
end

#issjis(str) ⇒ Object

call-seq:

Kconv.issjis(str)   -> obj or nil

Returns whether input encoding is Shift_JIS or not.

Note don???t expect this return value is MatchData.



248
249
250
# File 'lib/kconv.rb', line 248

def issjis(str)
  RegexpShiftjis.match( str )
end

#isutf8(str) ⇒ Object

call-seq:

Kconv.isutf8(str)   -> obj or nil

Returns whether input encoding is UTF-8 or not.

Note don???t expect this return value is MatchData.



259
260
261
# File 'lib/kconv.rb', line 259

def isutf8(str)
  RegexpUtf8.match( str )
end

#kconv(str, out_code, in_code = AUTO) ⇒ Object

call-seq:

Kconv.kconv(str, out_code, in_code = Kconv::AUTO)

Convert str to out_code. out_code and in_code are given as constants of Kconv.

Note This method decode MIME encoded string and convert halfwidth katakana to fullwidth katakana. If you don???t want to decode them, use NKF.nkf.



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
# File 'lib/kconv.rb', line 95

def kconv(str, out_code, in_code = AUTO)
  opt = '-'
  case in_code
  when ::NKF::JIS
    opt << 'J'
  when ::NKF::EUC
    opt << 'E'
  when ::NKF::SJIS
    opt << 'S'
  when ::NKF::UTF8
    opt << 'W'
  when ::NKF::UTF16
    opt << 'W16'
  end

  case out_code
  when ::NKF::JIS
    opt << 'j'
  when ::NKF::EUC
    opt << 'e'
  when ::NKF::SJIS
    opt << 's'
  when ::NKF::UTF8
    opt << 'w'
  when ::NKF::UTF16
    opt << 'w16'
  when ::NKF::NOCONV
    return str
  end

  opt = '' if opt == '-'

  ::NKF::nkf(opt, str)
end

#toeuc(str) ⇒ Object

call-seq:

Kconv.toeuc(str)   -> string

Convert str to EUC-JP

Note This method decode MIME encoded string and convert halfwidth katakana to fullwidth katakana. If you don???t want it, use NKF.nkf(???-exm0???, str).



158
159
160
# File 'lib/kconv.rb', line 158

def toeuc(str)
  ::NKF::nkf('-em', str)
end

#tojis(str) ⇒ Object

call-seq:

Kconv.tojis(str)   -> string

Convert str to ISO-2022-JP

Note This method decode MIME encoded string and convert halfwidth katakana to fullwidth katakana. If you don???t want it, use NKF.nkf(???-jxm0???, str).



144
145
146
# File 'lib/kconv.rb', line 144

def tojis(str)
  ::NKF::nkf('-jm', str)
end

#tosjis(str) ⇒ Object

call-seq:

Kconv.tosjis(str)   -> string

Convert str to Shift_JIS

Note This method decode MIME encoded string and convert halfwidth katakana to fullwidth katakana. If you don???t want it, use NKF.nkf(???-sxm0???, str).



172
173
174
# File 'lib/kconv.rb', line 172

def tosjis(str)
  ::NKF::nkf('-sm', str)
end

#toutf16(str) ⇒ Object

call-seq:

Kconv.toutf16(str)   -> string

Convert str to UTF-16

Note This method decode MIME encoded string and convert halfwidth katakana to fullwidth katakana. If you don???t want it, use NKF.nkf(???-w16xm0???, str).



200
201
202
# File 'lib/kconv.rb', line 200

def toutf16(str)
  ::NKF::nkf('-w16m', str)
end

#toutf8(str) ⇒ Object

call-seq:

Kconv.toutf8(str)   -> string

Convert str to UTF-8

Note This method decode MIME encoded string and convert halfwidth katakana to fullwidth katakana. If you don???t want it, use NKF.nkf(???-wxm0???, str).



186
187
188
# File 'lib/kconv.rb', line 186

def toutf8(str)
  ::NKF::nkf('-wm', str)
end