Module LocaleFilter::CodeConverter
In: localefilter.rb

Summary

  • 文字コード名と NKF の文字コード定数を相互変換する.
  • 文字コード・文字コード名を判定する. (Version 2.5.0 Feature)
  • 文字列の文字コードを変換する. (Version 4.0.0 Feature)

Methods

Public Methods

文字コード変換:convert, convert_str, tosjis, toeuc, tojis, toutf8, toutf16
文字コード変換(破壊的):convert!, convert_str!, tosjis!, toeuc!, tojis!, toutf8!, toutf16!
文字コード名変換:txt2num, num2txt, env2num
文字コード判定:str2num, guess, guess_str, compare_str, japanese?, text?
文字コード判定(個別):sjis?, euc?, jis?, utf8?, utf16?, ascii?, binary?
文字コード名判定:compare_code, japanese_code?, text_code?
文字コード名判定(個別):sjis_code?, euc_code?, jis_code?, utf8_code?, utf16_code?, ascii_code?, binary_code?
NKF オプション変換:make_opt

Private Methods

文字コード変換:kconv

Methods

Included Modules

Table

Classes and Modules

Module LocaleFilter::CodeConverter::Table

Constants

SJIS = ::NKF::SJIS   Shift_JIS の文字コード定数
EUC = ::NKF::EUC   EUC-JP の文字コード定数
JIS = ::NKF::JIS   JIS の文字コード定数
UTF8 = ::NKF::UTF8   UTF-8 の文字コード定数
UTF16 = ::NKF::UTF16   UTF-16 の文字コード定数
BINARY = ::NKF::BINARY   BINARY の文字コード定数
ASCII = ::NKF::ASCII   ASCII の文字コード定数
UNKNOWN = ::NKF::UNKNOWN   UNKNOWN の文字コード定数
RegexpSJIS = /\b(?:(?:s(?:hift[-_])?jis|pck)|#{SJIS})\b/i   文字コード名 Shift_JIS にマッチする正規表現
RegexpEUC = /\b(?:(?:euc|euc(?:-)?jp|ujis)|#{EUC})\b/i   文字コード名 EUC-JP にマッチする正規表現
RegexpJIS = /\b(?:(?:jis(?:7)?|iso-2022-jp)|#{JIS})\b/i   文字コード名 JIS にマッチする正規表現
RegexpUTF8 = /\b(?:utf(?:-)?8|#{UTF8})\b/i   文字コード名 UTF-8 にマッチする正規表現
RegexpUTF16 = /\b(?:utf(?:-)?16|#{UTF16})\b/i   文字コード名 UTF-16 にマッチする正規表現
RegexpBINARY = /\b(?:bin(?:ary)?|#{BINARY})\b/i   文字コード名 BINARY にマッチする正規表現
RegexpASCII = /\b(?:ascii|c|#{ASCII})\b/i   文字コード名 ASCII にマッチする正規表現
OPT_DEF = 'xm0'   NKF#nkf に渡されるオプションのデフォルト値

Public Instance methods

str の文字コードが ASCII なら true を返す.

[Source]

# File localefilter.rb, line 388
    def ascii?(str)
      compare_str(str, ASCII)
    end

文字コード名 txtASCII なら true を返す.

[Source]

# File localefilter.rb, line 438
    def ascii_code?(txt)
      compare_code(txt, ASCII)
    end

str の文字コードが BINARY なら true を返す.

[Source]

# File localefilter.rb, line 393
    def binary?(str)
      compare_str(str, BINARY)
    end

文字コード名 txtBINARY なら true を返す.

[Source]

# File localefilter.rb, line 443
    def binary_code?(txt)
      compare_code(txt, BINARY)
    end

文字コード名 txt が文字コード定数 num と同じなら true を返す.

[Source]

# File localefilter.rb, line 408
    def compare_code(txt, num)
      txt2num(txt) == num
    end

str の文字コードが txt と同じなら true を返す.

[Source]

# File localefilter.rb, line 358
    def compare_str(str, txt)
      str2num(str) == txt2num(txt)
    end

str の文字コードを convert_str で, 改行コードを LineConverter#convert_line で変換する.

[Source]

# File localefilter.rb, line 285
    def convert(str, line, to = env2num, from = nil, opt = OPT_DEF)
      convert_str(LocaleFilter.convert_line(str, line), to, from, opt)
    end

convert の破壊的メソッド.

[Source]

# File localefilter.rb, line 290
    def convert!(str, line, to = env2num, from = nil, opt = OPT_DEF)
      str.replace(convert(str, line, to, from, opt))
    end

str を NKF#nkf で文字コード名 to に変換する. 変換元の文字コード名を from で, NKF#nkf の追加オプションを opt で指定できる.

[Source]

# File localefilter.rb, line 297
    def convert_str(str, to = env2num, from = nil, opt = OPT_DEF)
      opt = make_opt(txt2num(to), txt2num(from), opt) or return str
      NKF.nkf(opt, str.to_s)
    end

convert_str の破壊的メソッド.

[Source]

# File localefilter.rb, line 303
    def convert_str!(str, to = env2num, from = nil, opt = OPT_DEF)
      str.replace(convert_str(str, to, from, opt))
    end

環境変数 env を元に NKF の文字コード定数を返す.

[Source]

# File localefilter.rb, line 241
    def env2num(*env)
      env << LOCALE_ENV
      env.flatten.each do |i|
        return txt2num(ENV[i])  if ENV[i]
      end
      return UNKNOWN
    end

str の文字コードが EUC-JP なら true を返す.

[Source]

# File localefilter.rb, line 368
    def euc?(str)
      compare_str(str, EUC)
    end

文字コード名 txtEUC-JP なら true を返す.

[Source]

# File localefilter.rb, line 418
    def euc_code?(txt)
      compare_code(txt, EUC)
    end

str の文字コードを guess_str で, 改行コードを LineConverter#guess_line で判別して Hash で返す.

[Source]

# File localefilter.rb, line 256
    def guess(str)
      guess_str(str).update(LocaleFilter.guess_line(str))
    end

str の文字コードを str2num で判別し, 文字コード名と NKF の文字コード定数を格納した Hash を返す.

[Source]

# File localefilter.rb, line 262
    def guess_str(str)
      list = Hash.new
      list[:num] = str2num(str)
      list[:txt] = num2txt(list[:num])
      return list
    end

str の文字コードが日本語なら文字コード名を返す.

[Source]

# File localefilter.rb, line 398
    def japanese?(str)
      japanese_code?(str2num(str))
    end

文字コード名 txt が日本語なら文字コード名を返す.

[Source]

# File localefilter.rb, line 448
    def japanese_code?(txt)
      num = txt2num(txt)
      CODE_JP.include?(num) and num2txt(num)
    end

str の文字コードが JIS なら true を返す.

[Source]

# File localefilter.rb, line 373
    def jis?(str)
      compare_str(str, JIS)
    end

文字コード名 txtJIS なら true を返す.

[Source]

# File localefilter.rb, line 423
    def jis_code?(txt)
      compare_code(txt, JIS)
    end

引数を元に NKF#nkf の変換オプションを作成して返す. 不正な値や同じ文字コードだった場合は nil を返す.

[Source]

# File localefilter.rb, line 271
    def make_opt(to, from = nil, opt_add = OPT_DEF)
      return nil  if from == to
      opt_to   = OPT_TO[to] or return nil
      opt_from = OPT_FROM[from]

      opt = '-' + opt_to
      opt << opt_from  if opt_from
      opt << opt_add   if opt_add

      return opt
    end

NKF の文字コード定数 num を文字コード名に変換して返す.

[Source]

# File localefilter.rb, line 236
    def num2txt(num)
      CODE_NAME[num] or CODE_NAME[UNKNOWN]
    end

str の文字コードが Shift_JIS なら true を返す.

[Source]

# File localefilter.rb, line 363
    def sjis?(str)
      compare_str(str, SJIS)
    end

文字コード名 txtShift_JIS なら true を返す.

[Source]

# File localefilter.rb, line 413
    def sjis_code?(txt)
      compare_code(txt, SJIS)
    end

str の文字コードを NKF#guess で判定し, NKF の文字コード定数を返す.

[Source]

# File localefilter.rb, line 250
    def str2num(str)
      NKF.guess(str.to_s)
    end

str の文字コードが BINARY 以外なら文字コード名を返す.

[Source]

# File localefilter.rb, line 403
    def text?(str)
      text_code?(str2num(str))
    end

文字コード名 txtBINARY 以外なら文字コード名を返す.

[Source]

# File localefilter.rb, line 454
    def text_code?(txt)
      case num = txt2num(txt)
      when BINARY, UNKNOWN
        return false
      else
        return num2txt(num)
      end
    end

strkconvEUC-JP に変換する.

[Source]

# File localefilter.rb, line 313
    def toeuc(str)
      kconv(str, EUC)
    end

toeuc の破壊的メソッド.

[Source]

# File localefilter.rb, line 338
    def toeuc!(str)
      str.replace(kconv(str, EUC))
    end

strkconvJIS に変換する.

[Source]

# File localefilter.rb, line 318
    def tojis(str)
      kconv(str, JIS)
    end

tojis の破壊的メソッド.

[Source]

# File localefilter.rb, line 343
    def tojis!(str)
      str.replace(kconv(str, JIS))
    end

strkconvShift_JIS に変換する.

[Source]

# File localefilter.rb, line 308
    def tosjis(str)
      kconv(str, SJIS)
    end

tosjis の破壊的メソッド.

[Source]

# File localefilter.rb, line 333
    def tosjis!(str)
      str.replace(kconv(str, SJIS))
    end

strkconvUTF-16 に変換する.

[Source]

# File localefilter.rb, line 328
    def toutf16(str)
      kconv(str, UTF16)
    end

toutf16 の破壊的メソッド.

[Source]

# File localefilter.rb, line 353
    def toutf16!(str)
      str.replace(kconv(str, UTF16))
    end

strkconvUTF-8 に変換する.

[Source]

# File localefilter.rb, line 323
    def toutf8(str)
      kconv(str, UTF8)
    end

toutf8 の破壊的メソッド.

[Source]

# File localefilter.rb, line 348
    def toutf8!(str)
      str.replace(kconv(str, UTF8))
    end

文字コード名 txt を NKF の文字コード定数に変換して返す.

[Source]

# File localefilter.rb, line 214
    def txt2num(txt)
      case txt.to_s
      when RegexpSJIS
        SJIS
      when RegexpEUC
        EUC
      when RegexpJIS
        JIS
      when RegexpUTF8
        UTF8
      when RegexpUTF16
        UTF16
      when RegexpBINARY
        BINARY
      when RegexpASCII
        ASCII
      else
        UNKNOWN
      end
    end

str の文字コードが UTF-16 なら true を返す.

[Source]

# File localefilter.rb, line 383
    def utf16?(str)
      compare_str(str, UTF16)
    end

文字コード名 txtUTF-16 なら true を返す.

[Source]

# File localefilter.rb, line 433
    def utf16_code?(txt)
      compare_code(txt, UTF16)
    end

str の文字コードが UTF-8 なら true を返す.

[Source]

# File localefilter.rb, line 378
    def utf8?(str)
      compare_str(str, UTF8)
    end

文字コード名 txtUTF-8 なら true を返す.

[Source]

# File localefilter.rb, line 428
    def utf8_code?(txt)
      compare_code(txt, UTF8)
    end

Private Instance methods

str を NKF#nkf で文字コード to_code に変換する.

[Source]

# File localefilter.rb, line 466
    def kconv(str, to_code)
      opt = make_opt(to_code) or return str
      NKF.nkf(opt, str.to_s)
    end

[Validate]