module Kconv - RDoc Documentation (original) (raw)

Kanji Converter for Ruby.

Constants

ASCII

ASCII

AUTO

Auto-Detect

BINARY

BINARY

EUC

EUC-JP

JIS

ISO-2022-JP

NOCONV

NOCONV

SJIS

Shift_JIS

UNKNOWN

UNKNOWN

UTF16

UTF-16

UTF32

UTF-32

UTF8

UTF-8

Public Class Methods

guess(str) → encoding click to toggle source

Guess input encoding by NKF.guess

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

iseuc(str) → true or false click to toggle source

Returns whether input encoding is EUC-JP or not.

Note don't expect this return value is MatchData.

def iseuc(str) str.dup.force_encoding(EUC).valid_encoding? end

isjis(str) → true or false click to toggle source

Returns whether input encoding is ISO-2022-JP or not.

def isjis(str) /\A [\t\n\r\x20-\x7E]* (?: (?:\x1b \x28 I [\x21-\x7E]* |\x1b \x28 J [\x21-\x7E]* |\x1b \x24 @ (?:[\x21-\x7E]{2})* |\x1b \x24 B (?:[\x21-\x7E]{2})* |\x1b \x24 \x28 D (?:[\x21-\x7E]{2})* )* \x1b \x28 B [\t\n\r\x20-\x7E]* )* \z/nox =~ str.dup.force_encoding('BINARY') ? true : false end

issjis(str) → true or false click to toggle source

Returns whether input encoding is Shift_JIS or not.

def issjis(str) str.dup.force_encoding(SJIS).valid_encoding? end

isutf8(str) → true or false click to toggle source

Returns whether input encoding is UTF-8 or not.

def isutf8(str) str.dup.force_encoding(UTF8).valid_encoding? end

kconv(str, to_enc, from_enc=nil) click to toggle source

Convert str to to_enc. to_enc and from_enc are given as constants of Kconv or Encoding objects.

def kconv(str, to_enc, from_enc=nil) opt = '' opt += ' --ic=' + from_enc.to_s if from_enc opt += ' --oc=' + to_enc.to_s if to_enc

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

toeuc(str) → string click to toggle source

Convert str to EUC-JP

def toeuc(str) kconv(str, EUC) end

tojis(str) → string click to toggle source

Convert str to ISO-2022-JP

def tojis(str) kconv(str, JIS) end

tolocale → string click to toggle source

Convert self to locale encoding

def tolocale(str) kconv(str, Encoding.locale_charmap) end

tosjis(str) → string click to toggle source

Convert str to Shift_JIS

def tosjis(str) kconv(str, SJIS) end

toutf16(str) → string click to toggle source

Convert str to UTF-16

def toutf16(str) kconv(str, UTF16) end

toutf32(str) → string click to toggle source

Convert str to UTF-32

def toutf32(str) kconv(str, UTF32) end

toutf8(str) → string click to toggle source

Convert str to UTF-8

def toutf8(str) kconv(str, UTF8) end

Private Instance Methods

guess(str) → encoding click to toggle source

Guess input encoding by NKF.guess

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

iseuc(str) → true or false click to toggle source

Returns whether input encoding is EUC-JP or not.

Note don't expect this return value is MatchData.

def iseuc(str) str.dup.force_encoding(EUC).valid_encoding? end

isjis(str) → true or false click to toggle source

Returns whether input encoding is ISO-2022-JP or not.

def isjis(str) /\A [\t\n\r\x20-\x7E]* (?: (?:\x1b \x28 I [\x21-\x7E]* |\x1b \x28 J [\x21-\x7E]* |\x1b \x24 @ (?:[\x21-\x7E]{2})* |\x1b \x24 B (?:[\x21-\x7E]{2})* |\x1b \x24 \x28 D (?:[\x21-\x7E]{2})* )* \x1b \x28 B [\t\n\r\x20-\x7E]* )* \z/nox =~ str.dup.force_encoding('BINARY') ? true : false end

issjis(str) → true or false click to toggle source

Returns whether input encoding is Shift_JIS or not.

def issjis(str) str.dup.force_encoding(SJIS).valid_encoding? end

isutf8(str) → true or false click to toggle source

Returns whether input encoding is UTF-8 or not.

def isutf8(str) str.dup.force_encoding(UTF8).valid_encoding? end

kconv(str, to_enc, from_enc=nil) click to toggle source

Convert str to to_enc. to_enc and from_enc are given as constants of Kconv or Encoding objects.

def kconv(str, to_enc, from_enc=nil) opt = '' opt += ' --ic=' + from_enc.to_s if from_enc opt += ' --oc=' + to_enc.to_s if to_enc

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

toeuc(str) → string click to toggle source

Convert str to EUC-JP

def toeuc(str) kconv(str, EUC) end

tojis(str) → string click to toggle source

Convert str to ISO-2022-JP

def tojis(str) kconv(str, JIS) end

tolocale → string click to toggle source

Convert self to locale encoding

def tolocale(str) kconv(str, Encoding.locale_charmap) end

tosjis(str) → string click to toggle source

Convert str to Shift_JIS

def tosjis(str) kconv(str, SJIS) end

toutf16(str) → string click to toggle source

Convert str to UTF-16

def toutf16(str) kconv(str, UTF16) end

toutf32(str) → string click to toggle source

Convert str to UTF-32

def toutf32(str) kconv(str, UTF32) end

toutf8(str) → string click to toggle source

Convert str to UTF-8

def toutf8(str) kconv(str, UTF8) end