1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
diff --git a/lib/xsd/iconvcharset.rb b/lib/xsd/iconvcharset.rb
index 57733a8..7901098 100644
--- a/lib/xsd/iconvcharset.rb
+++ b/lib/xsd/iconvcharset.rb
@@ -5,29 +5,12 @@
# redistribute it and/or modify it under the same terms of Ruby's license;
# either the dual license version in 2003, or any later version.
-
-require 'iconv'
-
-
module XSD
-
class IconvCharset
def self.safe_iconv(to, from, str)
- 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
+ str.force_encoding(from).chars.map { |c| c.valid_encoding? && c || '?' }.join.encode(to)
end
-end
-
+end if RUBY_VERSION > '1.9'
end
diff --git a/test/xsd/test_iconvcharset.rb b/test/xsd/test_iconvcharset.rb
new file mode 100644
index 0000000..452291c
--- /dev/null
+++ b/test/xsd/test_iconvcharset.rb
@@ -0,0 +1,20 @@
+# encoding: UTF-8
+
+require 'test/unit'
+require 'xsd/iconvcharset'
+
+class TestIconvCharset < Test::Unit::TestCase
+
+ def test_iso88591_utf8
+ assert_equal "á", XSD::IconvCharset.safe_iconv("utf-8", "iso-8859-1", "\xE1")
+ end
+
+ def test_utf8_iso88591
+ assert_equal "\xE1".force_encoding('iso-8859-1'), XSD::IconvCharset.safe_iconv("iso-8859-1", "utf-8", "á")
+ end
+
+ def test_invalid_encoding
+ assert_equal "á?á".encode('iso-8859-1'), XSD::IconvCharset.safe_iconv("iso-8859-1", "utf-8", "á\x8Dá".force_encoding('ascii-8bit'))
+ end
+
+end if RUBY_VERSION > '1.9'
|