* transcode.c (econv_finish): new method.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2008-08-28 18:00:02 +00:00
Родитель 23d10cbb00
Коммит 2c80188627
3 изменённых файлов: 67 добавлений и 0 удалений

Просмотреть файл

@ -1,3 +1,7 @@
Fri Aug 29 02:59:35 2008 Tanaka Akira <akr@fsij.org>
* transcode.c (econv_finish): new method.
Fri Aug 29 02:45:29 2008 Tanaka Akira <akr@fsij.org>
* transcode.c (econv_convert): new method.

Просмотреть файл

@ -568,4 +568,17 @@ class TestEncodingConverter < Test::Unit::TestCase
assert_equal(:finished, ret)
assert_raise(ArgumentError) { ec.convert("a") }
end
def test_finish_iso2022jp
ec = Encoding::Converter.new("utf-8", "iso-2022-jp")
assert_equal("\e$B$\"".force_encoding("iso-2022-jp"), ec.convert("\u3042"))
assert_equal("\e(B".force_encoding("iso-2022-jp"), ec.finish)
end
def test_finish_incomplete_error
ec = Encoding::Converter.new("utf-8", "euc-jp")
ec.convert("\xEF")
assert_raise(Encoding::InvalidByteSequence) { ec.finish }
end
end

Просмотреть файл

@ -2499,6 +2499,11 @@ econv_primitive_convert(int argc, VALUE *argv, VALUE self)
*
* source_string is assumed as a part of source.
* i.e. Encoding::Converter::PARTIAL_INPUT is used internally.
* finish method should be used at last.
*
* ec = Encoding::Converter.new("utf-8", "euc-jp")
* puts ec.convert("\u3042").dump #=> "\xA4\xA2"
* puts ec.finish.dump #=> ""
*
* If a conversion error occur,
* Encoding::ConversionUndefined or
@ -2544,6 +2549,50 @@ econv_convert(VALUE self, VALUE source_string)
return dst;
}
/*
* call-seq:
* finish -> string
*
* finishes the converter.
* It returns the last part of converted string.
*
* ec = Encoding::Converter.new("utf-8", "iso-2022-jp")
* p ec.convert("\u3042") #=> "\e$B$\""
* p ec.finish #=> "\e(B"
*/
static VALUE
econv_finish(VALUE self)
{
VALUE ret, dst;
VALUE av[5];
int ac;
rb_econv_t *ec = check_econv(self);
dst = rb_str_new(NULL, 0);
av[0] = Qnil;
av[1] = dst;
av[2] = Qnil;
av[3] = Qnil;
av[4] = INT2NUM(0);
ac = 5;
ret = econv_primitive_convert(ac, av, self);
if (ret == sym_invalid_byte_sequence ||
ret == sym_undefined_conversion ||
ret == sym_incomplete_input) {
VALUE exc = make_econv_exception(ec);
rb_exc_raise(exc);
}
if (ret != sym_finished) {
rb_bug("unexpected result of econv_primitive_convert");
}
return dst;
}
/*
* call-seq:
* primitive_errinfo -> array
@ -2775,6 +2824,7 @@ Init_transcode(void)
rb_define_method(rb_cEncodingConverter, "destination_encoding", econv_destination_encoding, 0);
rb_define_method(rb_cEncodingConverter, "primitive_convert", econv_primitive_convert, -1);
rb_define_method(rb_cEncodingConverter, "convert", econv_convert, 1);
rb_define_method(rb_cEncodingConverter, "finish", econv_finish, 0);
rb_define_method(rb_cEncodingConverter, "primitive_errinfo", econv_primitive_errinfo, 0);
rb_define_method(rb_cEncodingConverter, "primitive_insert_output", econv_primitive_insert_output, 1);
rb_define_method(rb_cEncodingConverter, "primitive_putback", econv_primitive_putback, 1);