# frozen_string_literal: false
# kconv.rb - Kanji Converter.
# $Id: kconv.rb 53143 2015-12-16 05:31:54Z naruse $
# kconv.rb implements the Kconv class for Kanji Converter. Additionally,
# some methods in String classes are added to allow easy conversion.
# Kanji Converter for Ruby.
# Kconv.kconv(str, to_enc, from_enc=nil)
# Convert <code>str</code> to <code>to_enc</code>.
# <code>to_enc</code> and <code>from_enc</code> are given as constants of Kconv or Encoding objects.
def kconv(str, to_enc, from_enc=nil)
opt += ' --ic=' + from_enc.to_s if from_enc
opt += ' --oc=' + to_enc.to_s if to_enc
# Kconv.tojis(str) => string
# Convert <code>str</code> to ISO-2022-JP
# Kconv.toeuc(str) => string
# Convert <code>str</code> to EUC-JP
# Kconv.tosjis(str) => string
# Convert <code>str</code> to Shift_JIS
# Kconv.toutf8(str) => string
# Convert <code>str</code> to UTF-8
# Kconv.toutf16(str) => string
# Convert <code>str</code> to UTF-16
# Kconv.toutf32(str) => string
# Convert <code>str</code> to UTF-32
# Kconv.tolocale => string
# Convert <code>self</code> to locale encoding
kconv(str, Encoding.locale_charmap)
module_function :tolocale
# Kconv.guess(str) => encoding
# Guess input encoding by NKF.guess
# Kconv.iseuc(str) => true or false
# Returns whether input encoding is EUC-JP or not.
# *Note* don't expect this return value is MatchData.
str.dup.force_encoding(EUC).valid_encoding?
# Kconv.issjis(str) => true or false
# Returns whether input encoding is Shift_JIS or not.
str.dup.force_encoding(SJIS).valid_encoding?
# Kconv.isjis(str) => true or false
# Returns whether input encoding is ISO-2022-JP or not.
(?:\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
# Kconv.isutf8(str) => true or false
# Returns whether input encoding is UTF-8 or not.
str.dup.force_encoding(UTF8).valid_encoding?
# String#kconv(to_enc, from_enc)
# Convert <code>self</code> to <code>to_enc</code>.
# <code>to_enc</code> and <code>from_enc</code> are given as constants of Kconv or Encoding objects.
def kconv(to_enc, from_enc=nil)
from_enc = self.encoding if !from_enc && self.encoding != Encoding.list[0]
Kconv::kconv(self, to_enc, from_enc)
# Convert <code>self</code> to ISO-2022-JP
def tojis; Kconv.tojis(self) end
# Convert <code>self</code> to EUC-JP
def toeuc; Kconv.toeuc(self) end
# String#tosjis => string
# Convert <code>self</code> to Shift_JIS
def tosjis; Kconv.tosjis(self) end
# String#toutf8 => string
# Convert <code>self</code> to UTF-8
def toutf8; Kconv.toutf8(self) end
# String#toutf16 => string
# Convert <code>self</code> to UTF-16
def toutf16; Kconv.toutf16(self) end
# String#toutf32 => string
# Convert <code>self</code> to UTF-32
def toutf32; Kconv.toutf32(self) end
# String#tolocale => string
# Convert <code>self</code> to locale encoding
def tolocale; Kconv.tolocale(self) end
# String#iseuc => true or false
# Returns whether <code>self</code>'s encoding is EUC-JP or not.
def iseuc; Kconv.iseuc(self) end
# String#issjis => true or false
# Returns whether <code>self</code>'s encoding is Shift_JIS or not.
def issjis; Kconv.issjis(self) end
# String#isjis => true or false
# Returns whether <code>self</code>'s encoding is ISO-2022-JP or not.
def isjis; Kconv.isjis(self) end
# String#isutf8 => true or false
# Returns whether <code>self</code>'s encoding is UTF-8 or not.
def isutf8; Kconv.isutf8(self) end