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
Class Method Summary collapse
-
.guess(str) ⇒ Object
call-seq: Kconv.guess(str) => encoding.
-
.iseuc(str) ⇒ Object
call-seq: Kconv.iseuc(str) => true or false.
-
.isjis(str) ⇒ Object
call-seq: Kconv.isjis(str) => true or false.
-
.issjis(str) ⇒ Object
call-seq: Kconv.issjis(str) => true or false.
-
.isutf8(str) ⇒ Object
call-seq: Kconv.isutf8(str) => true or false.
-
.kconv(str, to_enc, from_enc = nil) ⇒ Object
call-seq: Kconv.kconv(str, to_enc, from_enc=nil).
-
.toeuc(str) ⇒ Object
call-seq: Kconv.toeuc(str) => string.
-
.tojis(str) ⇒ Object
call-seq: Kconv.tojis(str) => string.
-
.tolocale(str) ⇒ Object
call-seq: Kconv.tolocale => string.
-
.tosjis(str) ⇒ Object
call-seq: Kconv.tosjis(str) => string.
-
.toutf16(str) ⇒ Object
call-seq: Kconv.toutf16(str) => string.
-
.toutf32(str) ⇒ Object
call-seq: Kconv.toutf32(str) => string.
-
.toutf8(str) ⇒ Object
call-seq: Kconv.toutf8(str) => string.
Class Method Details
.guess(str) ⇒ Object
140 141 142 |
# File 'lib/kconv.rb', line 140 def guess(str) ::NKF::guess(str) end |
.iseuc(str) ⇒ Object
call-seq:
Kconv.iseuc(str) => true or false
Returns whether input encoding is EUC-JP or not.
Note don’t expect this return value is MatchData.
155 156 157 |
# File 'lib/kconv.rb', line 155 def iseuc(str) str.dup.force_encoding(EUC).valid_encoding? end |
.isjis(str) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/kconv.rb', line 173 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) ⇒ Object
164 165 166 |
# File 'lib/kconv.rb', line 164 def issjis(str) str.dup.force_encoding(SJIS).valid_encoding? end |
.isutf8(str) ⇒ Object
192 193 194 |
# File 'lib/kconv.rb', line 192 def isutf8(str) str.dup.force_encoding(UTF8).valid_encoding? end |
.kconv(str, to_enc, from_enc = nil) ⇒ Object
call-seq:
Kconv.kconv(str, to_enc, from_enc=nil)
Convert str
to to_enc
. to_enc
and from_enc
are given as constants of Kconv or Encoding objects.
56 57 58 59 60 61 62 |
# File 'lib/kconv.rb', line 56 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 |
.tolocale(str) ⇒ Object
127 128 129 |
# File 'lib/kconv.rb', line 127 def tolocale(str) kconv(str, Encoding.locale_charmap) end |
.toutf16(str) ⇒ Object
109 110 111 |
# File 'lib/kconv.rb', line 109 def toutf16(str) kconv(str, UTF16) end |
.toutf32(str) ⇒ Object
118 119 120 |
# File 'lib/kconv.rb', line 118 def toutf32(str) kconv(str, UTF32) end |