module Jpmobile::Util

Constants

BINARY
EM_DASH
FULLWIDTH_HYPHEN_MINUS
FULLWIDTH_MACRON
FULLWIDTH_TILDE
HORIZONTAL_BAR
JIS
JIS_WIN
MINUS_SIGN
OVERLINE
SJIS

SJIS = “Shift_JIS”

UTF8
WAVE_DASH

Public Instance Methods

ascii_8bit(str) click to toggle source
# File lib/jpmobile/util.rb, line 96
def ascii_8bit(str)
  if str.respond_to?(:force_encoding) and !ascii_8bit?(str)
    str.force_encoding(BINARY)
  end
  str
end
ascii_8bit?(str) click to toggle source
# File lib/jpmobile/util.rb, line 359
def ascii_8bit?(str)
  detect_encoding(str) == BINARY
end
ascii_compatible!(str) click to toggle source
# File lib/jpmobile/util.rb, line 103
def ascii_compatible!(str)
  if str.respond_to?(:encoding) and !str.encoding.ascii_compatible?
    str.force_encoding(BINARY)
  end
  str
end
check_charset(str, charset) click to toggle source
# File lib/jpmobile/util.rb, line 428
def check_charset(str, charset)
  if Object.const_defined?(:Encoding)
    # use NKF.guess
    ::Encoding.compatible?(guess_encoding(str), ::Encoding.find(charset))
  else
    true
  end
end
correct_encoding(str) click to toggle source
# File lib/jpmobile/util.rb, line 437
def correct_encoding(str)
  if guess_encoding(str) != str.encoding
    str.force_encoding(guess_encoding(str))
  end

  str
end
decode(str, encoding, charset) click to toggle source
# File lib/jpmobile/util.rb, line 413
def decode(str, encoding, charset)
  _str = case encoding
         when /quoted-printable/
           str.unpack('M').first.strip
         when /base64/
           str.unpack('m').first.strip
         else
           str
         end

  _extract_charset = Jpmobile::Util.extract_charset(_str)
  charset = _extract_charset unless _extract_charset.blank? or _extract_charset == charset
  Jpmobile::Util.set_encoding(_str, charset)
end
deep_apply(obj, &proc) click to toggle source
# File lib/jpmobile/util.rb, line 26
def deep_apply(obj, &proc)
  case obj
  when Hash
    obj.each_pair do |key, value|
      obj[key] = deep_apply(value, &proc)
    end
  when Array
    obj.collect!{|value| deep_apply(value, &proc)}
  when String
    obj = obj.to_param if obj.respond_to?(:to_param)
    proc.call(obj)
  else
    # NilClass, TrueClass, FalseClass, Tempfile, StringIO, etc...
    return obj
  end
end
deep_convert(obj, &proc) click to toggle source
# File lib/jpmobile/util.rb, line 43
def deep_convert(obj, &proc)
  case obj
  when Hash
    new_obj = {}
    obj.each_pair do |key, value|
      new_obj[deep_convert(key.dup, &proc)] = deep_convert(value, &proc)
    end
    new_obj
  when Array
    new_obj = obj.map do |value|
      deep_convert(value, &proc)
    end
  when Symbol
    new_obj = proc.call(obj.to_s).to_sym
  when String
    obj = obj.to_param if obj.respond_to?(:to_param)
    new_obj = proc.call(obj)
  else
    # NilClass, TrueClass, FalseClass, Tempfile, StringIO, etc...
    new_obj = obj
  end

  new_obj
end
detect_encoding(str) click to toggle source
# File lib/jpmobile/util.rb, line 329
def detect_encoding(str)
  if Object.const_defined?(:Encoding)
    case str.encoding
    when ::Encoding::ISO2022_JP
      JIS
    when ::Encoding::Shift_JIS, ::Encoding::Windows_31J, ::Encoding::CP932
      SJIS
    when ::Encoding::UTF_8
      UTF8
    when ::Encoding::ASCII_8BIT
      BINARY
    else
      BINARY
    end
  else
    case NKF.guess(str)
    when NKF::SJIS
      SJIS
    when NKF::JIS
      JIS
    when NKF::UTF8
      UTF8
    when NKF::BINARY
      BINARY
    else
      BINARY
    end
  end
end
emdash_to_horizontal_bar(utf8_str) click to toggle source
# File lib/jpmobile/util.rb, line 252
def emdash_to_horizontal_bar(utf8_str)
  utf8_str.gsub(EM_DASH, HORIZONTAL_BAR)
end
encode(str, charset) click to toggle source
# File lib/jpmobile/util.rb, line 209
def encode(str, charset)
  if (charset.nil? or charset == "" or str.nil? or str == "")
    str
  elsif utf8?(str) and charset.match(/iso-2022-jp/)
    utf8_to_jis(str)
  elsif utf8?(str) and charset.match(/shift_jis/)
    utf8_to_sjis(str)
  elsif utf8?(str) and charset.match(/utf-8/)
    str
  else
    if Object.const_defined?(:Encoding)
      str.encode(charset)
    else
      case charset
      when /iso-2022-jp/
        NKF.nkf("-j", str)
      when /shift_jis/
        NKF.nkf("-s", str)
      when /utf-8/
        NKF.nkf("-w", str)
      else
        str
      end
    end
  end
end
extract_charset(str) click to toggle source
# File lib/jpmobile/util.rb, line 316
def extract_charset(str)
  case str
  when /iso-2022-jp/
    "ISO-2022-JP"
  when /shift_jis/
    "Shift_JIS"
  when /utf-8/
    "UTF-8"
  else
    ""
  end
end
fold_text(str, size = 15) click to toggle source
# File lib/jpmobile/util.rb, line 372
def fold_text(str, size = 15)
  folded_texts = []

  while texts = split_text(str, size) and texts.first.size != 0
    folded_texts << texts.first
    str = texts.last
  end

  folded_texts
end
force_encode(str, from, to) click to toggle source
# File lib/jpmobile/util.rb, line 264
def force_encode(str, from, to)
  s = str.dup
  return str if detect_encoding(str) == to

  if Object.const_defined?(:Encoding)
    to = SJIS if to =~ /shift_jis/

    to_enc = ::Encoding.find(to)
    return str if s.encoding == to_enc

    if from
      from_enc = ::Encoding.find(from)
      s.force_encoding(from) unless s.encoding == from_enc
    end

    s.encode(to)
  else
    opt = []
    opt << case from
           when /iso-2022-jp/
             "-Jx"
           when /shift_jis/
             "-Sx"
           when /utf-8/
             "-Wx"
           else
             ""
           end
    opt << case to
           when /iso-2022-jp/
             "-j"
           when /shift_jis/, /windows_31j/
             "-s"
           when /utf-8/
             "-w"
           else
             ""
           end
    NKF.nkf(opt.join(" "), str)
  end
end
fullwidth_hyphen_minus_to_minus_sign(utf8_str) click to toggle source
# File lib/jpmobile/util.rb, line 260
def fullwidth_hyphen_minus_to_minus_sign(utf8_str)
  utf8_str.gsub(FULLWIDTH_HYPHEN_MINUS, MINUS_SIGN)
end
fullwidth_macron_to_overline(utf8_str) click to toggle source
# File lib/jpmobile/util.rb, line 248
def fullwidth_macron_to_overline(utf8_str)
  utf8_str.gsub(FULLWIDTH_MACRON, OVERLINE)
end
fullwidth_tilde_to_wavedash(utf8_str) click to toggle source
# File lib/jpmobile/util.rb, line 240
def fullwidth_tilde_to_wavedash(utf8_str)
  utf8_str.gsub(FULLWIDTH_TILDE, WAVE_DASH)
end
guess_encoding(str) click to toggle source
# File lib/jpmobile/util.rb, line 445
def guess_encoding(str)
  encoding = NKF.guess(str)
  # ISO-2022-JPにおいて、JIS X201半角カナエスケープシーケンスが含まれていたらCP50220とみなす
  if encoding == ::Encoding::ISO2022_JP && str.dup.force_encoding(BINARY).include?("\e(I")
    ::Encoding::CP50220
  else
    encoding
  end
end
hash_to_utf8(hash) click to toggle source
# File lib/jpmobile/util.rb, line 177
def hash_to_utf8(hash)
  new_hash = {}
  hash.each do |keu, value|
    new_hash[utf8(key)] = utf8(value)
  end
end
invert_table(hash) click to toggle source
# File lib/jpmobile/util.rb, line 399
def invert_table(hash)
  result = {}
  hash.keys.each do |key|
    if result[hash[key]]
      if !key.kind_of?(Array) and !result[hash[key]].kind_of?(Array) and result[hash[key]] > key
        result[hash[key]] = key
      end
    else
      result[hash[key]] = key
    end
  end
  result
end
jis(str) click to toggle source
# File lib/jpmobile/util.rb, line 82
def jis(str)
  if str.respond_to?(:force_encoding) and !jis?(str)
    str.force_encoding(JIS)
  end
  str
end
jis?(str) click to toggle source
# File lib/jpmobile/util.rb, line 368
def jis?(str)
  detect_encoding(str) == JIS
end
jis_regexp(jis) click to toggle source
# File lib/jpmobile/util.rb, line 194
def jis_regexp(jis)
  jis_str = jis.kind_of?(Numeric) ? [jis].pack('n') : jis

  if Object.const_defined?(:Encoding)
    # Regexp.compile(Regexp.escape(jis_str.force_encoding("stateless-ISO-2022-JP-KDDI"))) # for au only
    Regexp.compile(Regexp.escape(jis_str.force_encoding(BINARY))) # for au only
  else
    Regexp.compile(Regexp.escape(jis_str,"j"),nil,'j')
  end
end
jis_string_regexp() click to toggle source
# File lib/jpmobile/util.rb, line 205
def jis_string_regexp
  Regexp.compile(Regexp.escape(ascii_8bit("\x1b\x24\x42")) + "(.+?)" + Regexp.escape(ascii_8bit("\x1b\x28\x42")))
end
jis_to_utf8(jis_str) click to toggle source
# File lib/jpmobile/util.rb, line 153
def jis_to_utf8(jis_str)
  if jis_str.respond_to?(:encode)
    jis_str.encode(UTF8, :universal_newline => true)
  else
    NKF.nkf("-m0 -x -Jw", jis_str).gsub(/\r\n?/, "\n")
  end
end
jis_win(str) click to toggle source
# File lib/jpmobile/util.rb, line 89
def jis_win(str)
  if str.respond_to?(:force_encoding) and !jis?(str)
    str.force_encoding(JIS_WIN)
  end
  str
end
minus_sign_to_fullwidth_hyphen_minus(utf8_str) click to toggle source
# File lib/jpmobile/util.rb, line 256
def minus_sign_to_fullwidth_hyphen_minus(utf8_str)
  utf8_str.gsub(MINUS_SIGN, FULLWIDTH_HYPHEN_MINUS)
end
overline_to_fullwidth_macron(utf8_str) click to toggle source
# File lib/jpmobile/util.rb, line 244
def overline_to_fullwidth_macron(utf8_str)
  utf8_str.gsub(OVERLINE, FULLWIDTH_MACRON)
end
regexp_to_sjis(sjis_str) click to toggle source
# File lib/jpmobile/util.rb, line 169
def regexp_to_sjis(sjis_str)
  if Object.const_defined?(:Encoding)
    Regexp.compile(Regexp.escape(sjis(sjis_str)))
  else
    Regexp.compile(Regexp.escape(sjis_str,"s"),nil,'s')
  end
end
regexp_utf8_to_sjis(utf8_str) click to toggle source
# File lib/jpmobile/util.rb, line 161
def regexp_utf8_to_sjis(utf8_str)
  if Object.const_defined?(:Encoding)
    Regexp.compile(Regexp.escape(utf8_to_sjis(utf8_str)))
  else
    Regexp.compile(Regexp.escape(utf8_to_sjis(utf8_str),"s"),nil,'s')
  end
end
set_encoding(str, encoding) click to toggle source
# File lib/jpmobile/util.rb, line 306
def set_encoding(str, encoding)
  if encoding and Object.const_defined?(:Encoding)
    encoding = SJIS if encoding =~ /shift_jis/

    str.force_encoding(encoding)
  end

  str
end
shift_jis?(str) click to toggle source
# File lib/jpmobile/util.rb, line 365
def shift_jis?(str)
  detect_encoding(str) == SJIS
end
sjis(str) click to toggle source
# File lib/jpmobile/util.rb, line 68
def sjis(str)
  if str.respond_to?(:force_encoding) and !shift_jis?(str)
    str.force_encoding(SJIS)
  end
  str
end
sjis_regexp(sjis) click to toggle source
# File lib/jpmobile/util.rb, line 184
def sjis_regexp(sjis)
  sjis_str = sjis.kind_of?(Numeric) ? [sjis].pack('n') : sjis

  if Object.const_defined?(:Encoding)
    Regexp.compile(Regexp.escape(sjis_str.force_encoding(SJIS)))
  else
    Regexp.compile(Regexp.escape(sjis_str,"s"),nil,'s')
  end
end
sjis_to_utf8(sjis_str) click to toggle source
# File lib/jpmobile/util.rb, line 129
def sjis_to_utf8(sjis_str)
  utf8_str = if sjis_str.respond_to?(:encode)
               sjis_str.encode("UTF-8", :universal_newline => true)
             else
               NKF.nkf("-m0 -x -w --ic=cp932", sjis_str).gsub(/\r\n?/, "\n")
             end

  # 波ダッシュ対策
  fullwidth_tilde_to_wavedash(utf8_str)
end
split_text(str, size = 15) click to toggle source
# File lib/jpmobile/util.rb, line 383
def split_text(str, size = 15)
  return nil if str.nil? or str == ''

  if Object.const_defined?(:Encoding)
    [str[0..(size-1)], str[size..-1]]
  else
    str    = str.split(//)
    text   = str[0..(size-1)]
    text   = text.join if text
    remain = str[size..-1]
    remain = remain.join if remain
    [text, remain]
  end

end
utf8(str) click to toggle source
# File lib/jpmobile/util.rb, line 75
def utf8(str)
  if str.respond_to?(:force_encoding) and !utf8?(str)
    str.force_encoding(UTF8)
  end
  str
end
utf8?(str) click to toggle source
# File lib/jpmobile/util.rb, line 362
def utf8?(str)
  detect_encoding(str) == UTF8
end
utf8_to_jis(utf8_str) click to toggle source
# File lib/jpmobile/util.rb, line 140
def utf8_to_jis(utf8_str)
  # 波ダッシュ対策
  utf8_str = fullwidth_tilde_to_wavedash(utf8_str)

  if utf8_str.respond_to?(:encode)
    utf8_str.
    gsub(/(\r\n|\r|\n)/, "\r\n").
    encode(JIS, :undef => :replace, :replace => '?')
  else
    NKF.nkf("-m0 -x -Wj --fb-subchar=63", utf8_str).gsub(/(\r\n|\r|\n)/, "\r\n")
  end
end
utf8_to_sjis(utf8_str) click to toggle source
# File lib/jpmobile/util.rb, line 110
def utf8_to_sjis(utf8_str)
  # 波ダッシュ対策
  utf8_str = wavedash_to_fullwidth_tilde(utf8_str)
  # オーバーライン対策(不可逆的)
  utf8_str = overline_to_fullwidth_macron(utf8_str)
  # ダッシュ対策(不可逆的)
  utf8_str = emdash_to_horizontal_bar(utf8_str)
  # マイナス対策(不可逆的)
  utf8_str = minus_sign_to_fullwidth_hyphen_minus(utf8_str)

  if utf8_str.respond_to?(:encode)
    utf8_str.
    gsub(/(\r\n|\r|\n)/, "\r\n").
    encode(SJIS, :undef => :replace, :replace => '?')
  else
    NKF.nkf("-m0 -x -W --oc=cp932 --fb-subchar=63", utf8_str).gsub(/(\r\n|\r|\n)/, "\r\n")
  end
end
wavedash_to_fullwidth_tilde(utf8_str) click to toggle source
# File lib/jpmobile/util.rb, line 236
def wavedash_to_fullwidth_tilde(utf8_str)
  utf8_str.gsub(WAVE_DASH, FULLWIDTH_TILDE)
end