diff --git a/ChangeLog b/ChangeLog index e4308a3639..9fed625817 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Sat Sep 6 02:23:18 2008 Tanaka Akira + + * transcode.c (econv_s_stateless_encoding): new method. + Sat Sep 6 02:01:59 2008 Tanaka Akira * transcode.c (enc_arg): extracted from str_transcode_enc_args. diff --git a/test/ruby/test_econv.rb b/test/ruby/test_econv.rb index 2e4409dbc5..c7f270c523 100644 --- a/test/ruby/test_econv.rb +++ b/test/ruby/test_econv.rb @@ -27,6 +27,15 @@ class TestEncodingConverter < Test::Unit::TestCase ec.primitive_errinfo) end + def test_s_stateless_encoding + assert_equal(Encoding::EUC_JP, Encoding::Converter.stateless_encoding("ISO-2022-JP")) + assert_equal(Encoding::EUC_JP, Encoding::Converter.stateless_encoding(Encoding::ISO_2022_JP)) + assert_nil(Encoding::Converter.stateless_encoding("EUC-JP")) + assert_nil(Encoding::Converter.stateless_encoding("UTF-8")) + assert_nil(Encoding::Converter.stateless_encoding("UTF-16BE")) + assert_nil(Encoding::Converter.stateless_encoding(Encoding::UTF_8)) + 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)) diff --git a/transcode.c b/transcode.c index 6d9cd7ba5f..429c6f4115 100644 --- a/transcode.c +++ b/transcode.c @@ -2334,6 +2334,47 @@ make_dummy_encoding(const char *name) return enc; } +/* + * call-seq: + * Encoding::Converter.stateless_encoding(string) => encoding or nil + * Encoding::Converter.stateless_encoding(encoding) => encoding or nil + * + * returns the corresponding stateless encoding. + * + * It returns nil if the argument is stateless encoding. + * + * "corresponding stateless encoding" is a stateless encoding which + * can represent all characters in the statefull encoding. + * + * So, no conversion undefined error occur from the stateful encoding to the stateless encoding. + * + * Currently, EUC-JP is the corresponding stateless encoding of ISO-2022-JP. + * + * Encoding::Converter.stateless_encoding("ISO-2022-JP") #=> # + * + * (This may be changed in future because EUC-JP cannot distinguish JIS X 0208 1978 and 1983.) + */ +static VALUE +econv_s_stateless_encoding(VALUE klass, VALUE arg) +{ + const char *stateful_name, *stateless_name; + rb_encoding *stateful_enc, *stateless_enc; + + enc_arg(arg, &stateful_name, &stateful_enc); + + stateless_name = rb_econv_stateless_encoding(stateful_name); + + if (stateless_name == NULL) + return Qnil; + + stateless_enc = rb_enc_find(stateless_name); + + if (!stateless_enc) + stateless_enc = make_dummy_encoding(stateless_name); + + return rb_enc_from_encoding(stateless_enc); +} + /* * call-seq: * Encoding::Converter.new(source_encoding, destination_encoding) @@ -3346,6 +3387,7 @@ Init_transcode(void) rb_cEncodingConverter = rb_define_class_under(rb_cEncoding, "Converter", rb_cData); rb_define_alloc_func(rb_cEncodingConverter, econv_s_allocate); + rb_define_singleton_method(rb_cEncodingConverter, "stateless_encoding", econv_s_stateless_encoding, 1); rb_define_method(rb_cEncodingConverter, "initialize", econv_init, -1); rb_define_method(rb_cEncodingConverter, "inspect", econv_inspect, 0); rb_define_method(rb_cEncodingConverter, "source_encoding", econv_source_encoding, 0);