Class: Iconv

Inherits:
Object show all
Defined in:
lib/sup/util.rb

Class Method Summary collapse

Class Method Details

.easy_decode(target, orig_charset, text) ⇒ Object



699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
# File 'lib/sup/util.rb', line 699

def self.easy_decode target, orig_charset, text
  if text.respond_to? :force_encoding
    text = text.dup
    text.force_encoding Encoding::BINARY
  end
  charset = case orig_charset
    when /UTF[-_ ]?8/i then "utf-8"
    when /(iso[-_ ])?latin[-_ ]?1$/i then "ISO-8859-1"
    when /iso[-_ ]?8859[-_ ]?15/i then 'ISO-8859-15'
    when /unicode[-_ ]1[-_ ]1[-_ ]utf[-_]7/i then "utf-7"
    when /^euc$/i then 'EUC-JP' # XXX try them all?
    when /^(x-unknown|unknown[-_ ]?8bit|ascii[-_ ]?7[-_ ]?bit)$/i then 'ASCII'
    else orig_charset
  end

  begin
    returning(Iconv.iconv(target + "//IGNORE", charset, text + " ").join[0 .. -2]) { |str| str.check }
  rescue Errno::EINVAL, Iconv::InvalidEncoding, Iconv::InvalidCharacter, Iconv::IllegalSequence, String::CheckError
    debug "couldn't transcode text from #{orig_charset} (#{charset}) to #{target} (#{text[0 ... 20].inspect}...): got #{$!.class} (#{$!.message})"
    text.ascii
  end
end