Commit 8412fd099e772bbd846c919aeb461b84e7c183e7

  • avatar
  • William Morgan <wmorgan-sup @masa…in.net> (Committer)
  • Fri Jan 01 18:21:14 CET 2010
  • avatar
  • Rich Lane <rlane @cl…b.cc.cmu.edu> (Author)
  • Fri Jan 01 00:36:53 CET 2010
fixup Iconv#easy_decode for Ruby 1.9
  
664664end
665665
666666class Iconv
667 def self.easy_decode target, charset, text
668 return text if charset =~ /^(x-unknown|unknown[-_ ]?8bit|ascii[-_ ]?7[-_ ]?bit)$/i
669 charset = case charset
667 def self.easy_decode target, orig_charset, text
668 if text.respond_to? :force_encoding
669 text = text.dup
670 text.force_encoding Encoding::BINARY
671 end
672 charset = case orig_charset
670673 when /UTF[-_ ]?8/i then "utf-8"
671674 when /(iso[-_ ])?latin[-_ ]?1$/i then "ISO-8859-1"
672675 when /iso[-_ ]?8859[-_ ]?15/i then 'ISO-8859-15'
673676 when /unicode[-_ ]1[-_ ]1[-_ ]utf[-_]7/i then "utf-7"
674 else charset
677 when /^euc$/i then 'EUC-JP' # XXX try them all?
678 when /^(x-unknown|unknown[-_ ]?8bit|ascii[-_ ]?7[-_ ]?bit)$/i then 'ASCII'
679 else orig_charset
675680 end
676681
677682 begin
678 Iconv.iconv(target + "//IGNORE", charset, text + " ").join[0 .. -2]
679 rescue Errno::EINVAL, Iconv::InvalidEncoding, Iconv::InvalidCharacter, Iconv::IllegalSequence => e
680 warn "couldn't transcode text from #{charset} to #{target} (\"#{text[0 ... 20]}\"...) (got #{e.message}); using original as is"
681 text
683 returning(Iconv.iconv(target, charset, text + " ").join[0 .. -2]) { |str| str.check }
684 rescue Errno::EINVAL, Iconv::InvalidEncoding, Iconv::InvalidCharacter, Iconv::IllegalSequence, String::CheckError
685 warn "couldn't transcode text from #{orig_charset} (#{charset}) to #{target}) (#{text[0 ... 20].inspect}...) (got #{$!.message} (#{$!.class}))"
686 text.ascii
682687 end
683688 end
684689end