diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-08-14 12:35:19 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-08-14 12:35:19 +0000 |
commit | a6daaf16b063d0a0870d1ef0e42df1d4338b02f4 (patch) | |
tree | 37993a6a284f0b845abea31759cada2cfabeba6a | |
parent | d46728ec0e134c47a9cf7b1f63d5a0c88902162e (diff) | |
download | ruby-a6daaf16b063d0a0870d1ef0e42df1d4338b02f4.tar.gz |
* transcode.c (econv_init): accept Encoding object as source_encoding
and destination_encoding.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18623 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | test/ruby/test_econv.rb | 5 | ||||
-rw-r--r-- | transcode.c | 40 |
3 files changed, 38 insertions, 12 deletions
@@ -1,3 +1,8 @@ +Thu Aug 14 21:34:41 2008 Tanaka Akira <akr@fsij.org> + + * transcode.c (econv_init): accept Encoding object as source_encoding + and destination_encoding. + Thu Aug 14 21:10:00 2008 Tanaka Akira <akr@fsij.org> * encoding.c (rb_filesystem_encoding): use default external encoding diff --git a/test/ruby/test_econv.rb b/test/ruby/test_econv.rb index 1da9a34b4f..239489fdd9 100644 --- a/test/ruby/test_econv.rb +++ b/test/ruby/test_econv.rb @@ -20,6 +20,11 @@ class TestEncodingConverter < Test::Unit::TestCase [o, ret, i]) end + def test_new + assert_kind_of(Encoding::Converter, Encoding::Converter.new("UTF-8", "EUC-JP")) + assert_kind_of(Encoding::Converter, Encoding::Converter.new(Encoding::UTF_8, Encoding::EUC_JP)) + end + def test_output_region ec = Encoding::Converter.new("UTF-8", "EUC-JP") ec.primitive_convert(src="a", dst="b", nil, 1, Encoding::Converter::PARTIAL_INPUT) diff --git a/transcode.c b/transcode.c index e7277be4fd..268363b418 100644 --- a/transcode.c +++ b/transcode.c @@ -1412,33 +1412,49 @@ econv_s_allocate(VALUE klass) static VALUE econv_init(int argc, VALUE *argv, VALUE self) { - VALUE from_encoding, to_encoding, flags_v; - const char *from_e, *to_e; - rb_econv_t *ts; + VALUE source_encoding, destination_encoding, flags_v; + rb_encoding *senc, *denc; + const char *sname, *dname; + rb_econv_t *ec; int flags; - rb_scan_args(argc, argv, "21", &from_encoding, &to_encoding, &flags_v); + rb_scan_args(argc, argv, "21", &source_encoding, &destination_encoding, &flags_v); - StringValue(from_encoding); - StringValue(to_encoding); if (flags_v == Qnil) flags = 0; else flags = NUM2INT(flags_v); - from_e = RSTRING_PTR(from_encoding); - to_e = RSTRING_PTR(to_encoding); + senc = NULL; + if (TYPE(source_encoding) != T_STRING) { + senc = rb_to_encoding(source_encoding); + } + + denc = NULL; + if (TYPE(destination_encoding) != T_STRING) { + denc = rb_to_encoding(destination_encoding); + } + + if (senc) + sname = senc->name; + else + sname = RSTRING_PTR(source_encoding); + + if (denc) + dname = denc->name; + else + dname = RSTRING_PTR(destination_encoding); if (DATA_PTR(self)) { rb_raise(rb_eTypeError, "already initialized"); } - ts = rb_econv_open(from_e, to_e, flags); - if (!ts) { - rb_raise(rb_eArgError, "encoding convewrter not supported (from %s to %s)", from_e, to_e); + ec = rb_econv_open(sname, dname, flags); + if (!ec) { + rb_raise(rb_eArgError, "encoding convewrter not supported (from %s to %s)", sname, dname); } - DATA_PTR(self) = ts; + DATA_PTR(self) = ec; return self; } |