Class: XSD::IconvCharset

Inherits:
Object show all
Defined in:
lib/xsd/iconvcharset.rb

Class Method Summary collapse

Class Method Details

.safe_iconv(to, from, str) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/xsd/iconvcharset.rb', line 15

def self.safe_iconv(to, from, str)
  return str.encode(to, from, :invalid => :replace, :undef => :replace, :replace => '?') if ''.respond_to? :encode
  iconv = Iconv.new(to, from)
  out = ""
  begin
    out << iconv.iconv(str)
  rescue Iconv::IllegalSequence => e
    out << e.success
    ch, str = e.failed.split(//, 2)
    out << '?'
    warn("Failed to convert #{ch}")
    retry
  end
  return out
end