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



663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
# File 'lib/sup/util.rb', line 663

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