summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-09-05 17:23:49 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-09-05 17:23:49 +0000
commit134ca08dd14585e30fab91cb909a254a259d6657 (patch)
tree6b0d57437d0e626166f290190b282f0d3a01e46e
parenta1892af3d6f940222d84bf53fb32f00a8cd7c97b (diff)
* transcode.c (econv_s_stateless_encoding): new method.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19156 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog4
-rw-r--r--test/ruby/test_econv.rb9
-rw-r--r--transcode.c42
3 files changed, 55 insertions, 0 deletions
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 <akr@fsij.org>
+
+ * transcode.c (econv_s_stateless_encoding): new method.
+
Sat Sep 6 02:01:59 2008 Tanaka Akira <akr@fsij.org>
* 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
@@ -2336,6 +2336,47 @@ make_dummy_encoding(const char *name)
/*
* 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") #=> #<Encoding:EUC-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)
* Encoding::Converter.new(source_encoding, destination_encoding, opt)
*
@@ -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);