annotate lib/redmine/codeset_util.rb @ 1172:60d42b9850d2 bug_367

Close obsolete branch bug_367
author Chris Cannam
date Fri, 03 Feb 2012 15:20:50 +0000
parents cbce1fd3b1b7
children cbb26bc654de
rev   line source
Chris@441 1 require 'iconv'
Chris@441 2
Chris@441 3 module Redmine
Chris@441 4 module CodesetUtil
Chris@441 5
Chris@441 6 def self.replace_invalid_utf8(str)
Chris@441 7 return str if str.nil?
Chris@441 8 if str.respond_to?(:force_encoding)
Chris@441 9 str.force_encoding('UTF-8')
Chris@441 10 if ! str.valid_encoding?
Chris@441 11 str = str.encode("US-ASCII", :invalid => :replace,
Chris@441 12 :undef => :replace, :replace => '?').encode("UTF-8")
Chris@441 13 end
Chris@441 14 else
Chris@441 15 ic = Iconv.new('UTF-8', 'UTF-8')
Chris@441 16 txtar = ""
Chris@441 17 begin
Chris@441 18 txtar += ic.iconv(str)
Chris@441 19 rescue Iconv::IllegalSequence
Chris@441 20 txtar += $!.success
Chris@441 21 str = '?' + $!.failed[1,$!.failed.length]
Chris@441 22 retry
Chris@441 23 rescue
Chris@441 24 txtar += $!.success
Chris@441 25 end
Chris@441 26 str = txtar
Chris@441 27 end
Chris@441 28 str
Chris@441 29 end
Chris@441 30 end
Chris@441 31 end