Kanji Converter for Ruby.
Auto-Detect
ISO-2022-JP
EUC-JP
Shift_JIS
UTF-8
UTF-16
UTF-32
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 141
def guess(str)
::NKF::guess(str)
end
Guess input encoding by NKF.guess
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 156
def iseuc(str)
str.dup.force_encoding(EUC).valid_encoding?
end
Returns whether input encoding is EUC-JP or not.
Note don’t expect this return value is MatchData
.
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 174
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
Returns whether input encoding is ISO-2022-JP or not.
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 165
def issjis(str)
str.dup.force_encoding(SJIS).valid_encoding?
end
Returns whether input encoding is Shift_JIS or not.
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 193
def isutf8(str)
str.dup.force_encoding(UTF8).valid_encoding?
end
Returns whether input encoding is UTF-8 or not.
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 57
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
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 83
def toeuc(str)
kconv(str, EUC)
end
Convert str
to EUC-JP
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 74
def tojis(str)
kconv(str, JIS)
end
Convert str
to ISO-2022-JP
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 128
def tolocale(str)
kconv(str, Encoding.locale_charmap)
end
Convert self
to locale encoding
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 92
def tosjis(str)
kconv(str, SJIS)
end
Convert str
to Shift_JIS
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 110
def toutf16(str)
kconv(str, UTF16)
end
Convert str
to UTF-16
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 119
def toutf32(str)
kconv(str, UTF32)
end
Convert str
to UTF-32
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 101
def toutf8(str)
kconv(str, UTF8)
end
Convert str
to UTF-8
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 141
def guess(str)
::NKF::guess(str)
end
Guess input encoding by NKF.guess
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 156
def iseuc(str)
str.dup.force_encoding(EUC).valid_encoding?
end
Returns whether input encoding is EUC-JP or not.
Note don’t expect this return value is MatchData
.
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 174
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
Returns whether input encoding is ISO-2022-JP or not.
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 165
def issjis(str)
str.dup.force_encoding(SJIS).valid_encoding?
end
Returns whether input encoding is Shift_JIS or not.
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 193
def isutf8(str)
str.dup.force_encoding(UTF8).valid_encoding?
end
Returns whether input encoding is UTF-8 or not.
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 57
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
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 83
def toeuc(str)
kconv(str, EUC)
end
Convert str
to EUC-JP
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 74
def tojis(str)
kconv(str, JIS)
end
Convert str
to ISO-2022-JP
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 128
def tolocale(str)
kconv(str, Encoding.locale_charmap)
end
Convert self
to locale encoding
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 92
def tosjis(str)
kconv(str, SJIS)
end
Convert str
to Shift_JIS
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 110
def toutf16(str)
kconv(str, UTF16)
end
Convert str
to UTF-16
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 119
def toutf32(str)
kconv(str, UTF32)
end
Convert str
to UTF-32
# File tmp/rubies/ruby-2.4.10/ext/nkf/lib/kconv.rb, line 101
def toutf8(str)
kconv(str, UTF8)
end
Convert str
to UTF-8