* transcode.c (rb_trans_conv): report last transcode_obuf_full.
(econv_max_output): new method Encoding::Converter#max_output. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18547 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
85c41f4fbc
commit
9d4bb3b969
@ -1,3 +1,8 @@
|
|||||||
|
Wed Aug 13 07:41:03 2008 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* transcode.c (rb_trans_conv): report last transcode_obuf_full.
|
||||||
|
(econv_max_output): new method Encoding::Converter#max_output.
|
||||||
|
|
||||||
Wed Aug 13 02:46:01 2008 NARUSE, Yui <naruse@ruby-lang.org>
|
Wed Aug 13 02:46:01 2008 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
* error.c (rb_eEncCompatError): add Exception.
|
* error.c (rb_eEncCompatError): add Exception.
|
||||||
|
@ -1,24 +1,53 @@
|
|||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
|
|
||||||
class TestEncodingConverter < Test::Unit::TestCase
|
class TestEncodingConverter < Test::Unit::TestCase
|
||||||
def assert_econv(ret_expected, src_expected, dst_expected, from, to, src, dst, flags=0)
|
def assert_econv(ret_expected, dst_expected, src_expected, to, from, src, opt={})
|
||||||
|
opt[:obuf_len] ||= 100
|
||||||
|
src = src.dup
|
||||||
ec = Encoding::Converter.new(from, to)
|
ec = Encoding::Converter.new(from, to)
|
||||||
ret = ec.primitive_convert(src, dst, flags)
|
dst = ''
|
||||||
assert_equal(ret_expected, ret)
|
while true
|
||||||
assert_equal(src_expected, src)
|
ret = ec.primitive_convert(src, dst2=" "*opt[:obuf_len], 0)
|
||||||
assert_equal(dst_expected, dst)
|
dst << dst2
|
||||||
|
#p [ret, dst, src]
|
||||||
|
break if ret != :obuf_full
|
||||||
|
end
|
||||||
|
assert_equal([ret_expected, dst_expected, src_expected], [ret, dst, src])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_eucjp_to_utf8
|
def test_eucjp_to_utf8
|
||||||
assert_econv(:finished, "", "", "EUC-JP", "UTF-8", "", "")
|
assert_econv(:finished, "", "", "EUC-JP", "UTF-8", "")
|
||||||
assert_econv(:ibuf_empty, "", "", "EUC-JP", "UTF-8", "", "", Encoding::Converter::PARTIAL_INPUT)
|
assert_econv(:finished, "a", "", "EUC-JP", "UTF-8", "a")
|
||||||
assert_econv(:finished, "", "", "EUC-JP", "UTF-8", "", " "*10)
|
end
|
||||||
assert_econv(:obuf_full, "", "", "EUC-JP", "UTF-8", "a", "")
|
|
||||||
|
def test_iso2022jp
|
||||||
|
assert_econv(:finished, "", "", "ISO-2022-JP", "Shift_JIS", "")
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_invalid
|
def test_invalid
|
||||||
assert_econv(:invalid_input, "", "", "EUC-JP", "UTF-8", "\x80", " "*10)
|
assert_econv(:invalid_input, "", "", "EUC-JP", "UTF-8", "\x80")
|
||||||
assert_econv(:invalid_input, "", "a", "EUC-JP", "UTF-8", "a\x80", " "*10)
|
assert_econv(:invalid_input, "a", "", "EUC-JP", "UTF-8", "a\x80")
|
||||||
assert_econv(:invalid_input, "\x80", "a", "EUC-JP", "UTF-8", "a\x80\x80", " "*10)
|
assert_econv(:invalid_input, "a", "\x80", "EUC-JP", "UTF-8", "a\x80\x80")
|
||||||
|
assert_econv(:invalid_input, "abc", "def", "EUC-JP", "UTF-8", "abc\xFFdef")
|
||||||
|
assert_econv(:invalid_input, "abc", "def", "EUC-JP", "Shift_JIS", "abc\xFFdef")
|
||||||
|
assert_econv(:invalid_input, "abc", "def", "EUC-JP", "Shift_JIS", "abc\xFFdef", :obuf_len=>1)
|
||||||
|
assert_econv(:invalid_input, "abc", "def", "Shift_JIS", "ISO-2022-JP", "abc\xFFdef")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_errors
|
||||||
|
ec = Encoding::Converter.new("UTF-16BE", "EUC-JP")
|
||||||
|
src = "\xFF\xFE\x00A\xDC\x00"
|
||||||
|
ret = ec.primitive_convert(src, dst=" "*10, 0)
|
||||||
|
assert_equal("", src)
|
||||||
|
assert_equal("", dst)
|
||||||
|
assert_equal(:undefined_conversion, ret)
|
||||||
|
ret = ec.primitive_convert(src, dst=" "*10, 0)
|
||||||
|
assert_equal("", src)
|
||||||
|
assert_equal("A", dst)
|
||||||
|
assert_equal(:invalid_input, ret)
|
||||||
|
ret = ec.primitive_convert(src, dst=" "*10, 0)
|
||||||
|
assert_equal("", src)
|
||||||
|
assert_equal("", dst)
|
||||||
|
assert_equal(:finished, ret)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
38
transcode.c
38
transcode.c
@ -749,7 +749,7 @@ rb_trans_conv(rb_trans_t *ts,
|
|||||||
int flags)
|
int flags)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int start, err_index, no_error;
|
int start, err_index;
|
||||||
|
|
||||||
unsigned char empty_buf;
|
unsigned char empty_buf;
|
||||||
unsigned char *empty_ptr = &empty_buf;
|
unsigned char *empty_ptr = &empty_buf;
|
||||||
@ -764,34 +764,27 @@ rb_trans_conv(rb_trans_t *ts,
|
|||||||
output_stop = empty_ptr;
|
output_stop = empty_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
no_error = 1;
|
|
||||||
err_index = -1;
|
err_index = -1;
|
||||||
for (i = ts->num_trans-1; 0 <= i; i--) {
|
for (i = ts->num_trans-1; 0 <= i; i--) {
|
||||||
if (ts->elems[i].last_result == transcode_invalid_input ||
|
if (ts->elems[i].last_result != transcode_ibuf_empty) {
|
||||||
ts->elems[i].last_result == transcode_undefined_conversion) {
|
|
||||||
if (no_error) {
|
|
||||||
/* last error */
|
|
||||||
no_error = 0;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
/* second last error */
|
|
||||||
err_index = i;
|
err_index = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
start = err_index + 1;
|
start = err_index + 1;
|
||||||
err_index = trans_sweep(ts, input_ptr, input_stop, output_ptr, output_stop, flags, start);
|
err_index = trans_sweep(ts, input_ptr, input_stop, output_ptr, output_stop, flags, start);
|
||||||
} while (err_index != -1 && err_index != ts->num_trans-1);
|
} while (err_index != -1 && err_index != ts->num_trans-1);
|
||||||
|
|
||||||
if (err_index == ts->num_trans-1)
|
for (i = ts->num_trans-1; 0 <= i; i--) {
|
||||||
return ts->elems[ts->num_trans-1].last_result;
|
if (ts->elems[i].last_result != transcode_ibuf_empty) {
|
||||||
else if (start == 0)
|
rb_trans_result_t res = ts->elems[i].last_result;
|
||||||
return ts->elems[ts->num_trans-1].last_result;
|
ts->elems[i].last_result = transcode_ibuf_empty;
|
||||||
else
|
return res;
|
||||||
return ts->elems[start-1].last_result;
|
}
|
||||||
|
}
|
||||||
|
return transcode_ibuf_empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -1303,6 +1296,16 @@ econv_primitive_convert(VALUE self, VALUE input, VALUE output, VALUE flags_v)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
econv_max_output(VALUE self)
|
||||||
|
{
|
||||||
|
rb_trans_t *ts = check_econv(self);
|
||||||
|
int n;
|
||||||
|
n = ts->elems[ts->num_trans-1].tc->transcoder->max_output;
|
||||||
|
|
||||||
|
return INT2FIX(n);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Init_transcode(void)
|
Init_transcode(void)
|
||||||
{
|
{
|
||||||
@ -1323,5 +1326,6 @@ Init_transcode(void)
|
|||||||
rb_define_alloc_func(rb_cEncodingConverter, econv_s_allocate);
|
rb_define_alloc_func(rb_cEncodingConverter, econv_s_allocate);
|
||||||
rb_define_method(rb_cEncodingConverter, "initialize", econv_init, 2);
|
rb_define_method(rb_cEncodingConverter, "initialize", econv_init, 2);
|
||||||
rb_define_method(rb_cEncodingConverter, "primitive_convert", econv_primitive_convert, 3);
|
rb_define_method(rb_cEncodingConverter, "primitive_convert", econv_primitive_convert, 3);
|
||||||
|
rb_define_method(rb_cEncodingConverter, "max_output", econv_max_output, 0);
|
||||||
rb_define_const(rb_cEncodingConverter, "PARTIAL_INPUT", INT2FIX(PARTIAL_INPUT));
|
rb_define_const(rb_cEncodingConverter, "PARTIAL_INPUT", INT2FIX(PARTIAL_INPUT));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user