1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
akr 2008-09-05 17:23:49 +00:00
parent a1892af3d6
commit 134ca08dd1
3 changed files with 55 additions and 0 deletions

View file

@ -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.

View file

@ -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))

View file

@ -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") #=> #<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)
@ -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);