>>>>> <[EMAIL PROTECTED]>
>>>>> Thomas E Enebo <[EMAIL PROTECTED]> :
> How about this one:
> JRUBY-867: Iconv character set options (//ignore, //translit) not supported
> That is last known iconv bug for 1.0.0.
Thanks for feeding me another task. Please check the attached patch
for it.
I think it's difficult to support `//translit' with java.nio.charset API,
so I just changed not to raise Exceptions.
thanks,
--
___/_ __/ ___/ ITOCHU TECHNO-SOLUTIONS Corp.
/ / / Advanced Technology Team - Koichiro Ohba
____/ __/ ____/ mailto:[EMAIL PROTECTED]
Index: test/test_iconv.rb
===================================================================
--- test/test_iconv.rb (revision 3642)
+++ test/test_iconv.rb (working copy)
@@ -58,4 +58,32 @@
sjis << "\n"
assert_equal(sjis, output)
end
+
+ def test_ignore_option
+ euc = ["a4a2a4a4a4a6a4a8a4aa"].pack("H*")
+ sjis = ["82a082a282a482a682a8"].pack("H*")
+ iconv = Iconv.new('SHIFT_JIS', 'EUC-JP//ignore')
+ str = iconv.iconv(euc)
+ str << iconv.iconv(nil)
+ assert_equal( sjis, str )
+
+ iconv = Iconv.new('SHIFT_JIS//IGNORE', 'EUC-JP//ignore')
+ str = iconv.iconv(euc)
+ str << iconv.iconv(nil)
+ assert_equal( sjis, str )
+ end
+
+ def test_translit_option
+ euc = ["a4a2a4a4a4a6a4a8a4aa"].pack("H*")
+ sjis = ["82a082a282a482a682a8"].pack("H*")
+ iconv = Iconv.new('SHIFT_JIS', 'EUC-JP//ignore')
+ str = iconv.iconv(euc)
+ str << iconv.iconv(nil)
+ assert_equal( sjis, str )
+
+ iconv = Iconv.new('SHIFT_JIS//TRANSLIT', 'EUC-JP//translit//ignore')
+ str = iconv.iconv(euc)
+ str << iconv.iconv(nil)
+ assert_equal( sjis, str )
+ end
end
Index: src/org/jruby/RubyIconv.java
===================================================================
--- src/org/jruby/RubyIconv.java (revision 3642)
+++ src/org/jruby/RubyIconv.java (working copy)
@@ -49,6 +49,9 @@
import org.jruby.util.ByteList;
public class RubyIconv extends RubyObject {
+ static private final String TRANSLIT = "//translit";
+ static private final String IGNORE = "//ignore";
+
private CharsetDecoder fromEncoding;
private CharsetEncoder toEncoding;
@@ -160,6 +163,19 @@
}
*/
+ private static String getCharset(String encoding) {
+ int index = encoding.indexOf("//");
+ if (index == -1) return encoding;
+ return encoding.substring(0, index);
+ }
+
+ private static boolean isTranslit(String encoding) {
+ return encoding.toLowerCase().indexOf(TRANSLIT) != -1 ? true : false;
+ }
+
+ private static boolean isIgnore(String encoding) {
+ return encoding.toLowerCase().indexOf(IGNORE) != -1 ? true : false;
+ }
public static IRubyObject open(IRubyObject recv, IRubyObject to,
IRubyObject from, Block block) {
Ruby runtime = recv.getRuntime();
@@ -193,11 +209,11 @@
try {
- fromEncoding = Charset.forName(from).newDecoder();
- toEncoding = Charset.forName(to).newEncoder();
+ fromEncoding = Charset.forName(getCharset(from)).newDecoder();
+ toEncoding = Charset.forName(getCharset(to)).newEncoder();
- fromEncoding.onUnmappableCharacter(CodingErrorAction.REPORT);
- toEncoding.onUnmappableCharacter(CodingErrorAction.REPORT);
+ if (!isIgnore(from))
fromEncoding.onUnmappableCharacter(CodingErrorAction.REPORT);
+ if (!isIgnore(to))
toEncoding.onUnmappableCharacter(CodingErrorAction.REPORT);
} catch (IllegalCharsetNameException e) {
throw runtime.newArgumentError("invalid encoding");
@@ -231,11 +247,7 @@
throw runtime.newTypeError("can't convert " +
args[0].getMetaClass() + " into String");
}
if (!args[1].isNil()) start = RubyNumeric.fix2int(args[1]);
- if (!args[2].isNil()) {
- length = RubyNumeric.fix2int(args[2]);
- } else {
- length = -1;
- }
+ if (!args[2].isNil()) length = RubyNumeric.fix2int(args[2]);
IRubyObject result = _iconv(args[0].convertToString(), start, length);
return result;
@@ -306,13 +318,15 @@
ByteList bytes = original.getByteList();
ByteBuffer buf = ByteBuffer.wrap(bytes.unsafeBytes(),
bytes.begin(), bytes.length());
- CharsetDecoder decoder =
Charset.forName(fromEncoding).newDecoder();
+ CharsetDecoder decoder =
Charset.forName(getCharset(fromEncoding)).newDecoder();
- decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+ if (!isIgnore(fromEncoding))
decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+
CharBuffer cbuf = decoder.decode(buf);
- CharsetEncoder encoder = Charset.forName(toEncoding).newEncoder();
+ CharsetEncoder encoder =
Charset.forName(getCharset(toEncoding)).newEncoder();
- encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+ if (!isIgnore(toEncoding))
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
+
buf = encoder.encode(cbuf);
byte[] arr = buf.array();
return RubyString.newString(original.getRuntime(), new
ByteList(arr,0,buf.limit()));
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email