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/ then "utf-8"
when /(iso[-_ ])?latin[-_ ]?1$/ then "ISO-8859-1"
when /iso[-_ ]?8859[-_ ]?15/ then 'ISO-8859-15'
when /unicode[-_ ]1[-_ ]1[-_ ]utf[-_]7/ then "utf-7"
when /^euc$/ then 'EUC-JP'
when /^(x-unknown|unknown[-_ ]?8bit|ascii[-_ ]?7[-_ ]?bit)$/ then 'ASCII'
else orig_charset
end
begin
returning(Iconv.iconv(target, 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 #{$!.message} (#{$!.class}))"
text.ascii
end
end