comparison lib/redmine/codeset_util.rb @ 511:107d36338b70 live

Merge from branch "cannam"
author Chris Cannam
date Thu, 14 Jul 2011 10:43:07 +0100
parents cbce1fd3b1b7
children cbb26bc654de
comparison
equal deleted inserted replaced
451:a9f6345cb43d 511:107d36338b70
1 require 'iconv'
2
3 module Redmine
4 module CodesetUtil
5
6 def self.replace_invalid_utf8(str)
7 return str if str.nil?
8 if str.respond_to?(:force_encoding)
9 str.force_encoding('UTF-8')
10 if ! str.valid_encoding?
11 str = str.encode("US-ASCII", :invalid => :replace,
12 :undef => :replace, :replace => '?').encode("UTF-8")
13 end
14 else
15 ic = Iconv.new('UTF-8', 'UTF-8')
16 txtar = ""
17 begin
18 txtar += ic.iconv(str)
19 rescue Iconv::IllegalSequence
20 txtar += $!.success
21 str = '?' + $!.failed[1,$!.failed.length]
22 retry
23 rescue
24 txtar += $!.success
25 end
26 str = txtar
27 end
28 str
29 end
30 end
31 end