ruby-changes:7392
From: akr <ko1@a...>
Date: Fri, 29 Aug 2008 02:39:26 +0900 (JST)
Subject: [ruby-changes:7392] Ruby:r18911 (trunk): * transcode.c (econv_primitive_convert): accept nil as
akr 2008-08-29 02:39:02 +0900 (Fri, 29 Aug 2008) New Revision: 18911 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=18911 Log: * transcode.c (econv_primitive_convert): accept nil as destination_bytesize for unlimited destination size. Modified files: trunk/ChangeLog trunk/test/ruby/test_econv.rb trunk/transcode.c Index: ChangeLog =================================================================== --- ChangeLog (revision 18910) +++ ChangeLog (revision 18911) @@ -1,3 +1,8 @@ +Fri Aug 29 02:38:14 2008 Tanaka Akira <akr@f...> + + * transcode.c (econv_primitive_convert): accept nil as + destination_bytesize for unlimited destination size. + Fri Aug 29 02:11:46 2008 Tanaka Akira <akr@f...> * transcode.c (econv_primitive_convert): accept nil as input for empty Index: test/ruby/test_econv.rb =================================================================== --- test/ruby/test_econv.rb (revision 18910) +++ test/ruby/test_econv.rb (revision 18911) @@ -79,12 +79,31 @@ } end - def test_nil_input + def test_nil_source_buffer ec = Encoding::Converter.new("UTF-8", "EUC-JP") ret = ec.primitive_convert(nil, dst="", nil, 10) assert_equal(:finished, ret) end + def test_nil_destination_bytesize + ec = Encoding::Converter.new("Shift_JIS", "UTF-8") + n = 10000 + src = "\xa1".force_encoding("Shift_JIS") * n + ret = ec.primitive_convert(src, dst="", nil, nil) + assert_equal(:finished, ret) + assert_equal("\xEF\xBD\xA1".force_encoding("UTF-8") * n, dst) + end + + def test_nil_destination_bytesize_with_nonnli_byteoffset + ec = Encoding::Converter.new("Shift_JIS", "UTF-8") + n = 2000 + src = "\xa1".force_encoding("Shift_JIS") * n + dst = "abcd" * 2000 + ret = ec.primitive_convert(src, dst, 3, nil) + assert_equal(:finished, ret) + assert_equal("abc" + "\xEF\xBD\xA1".force_encoding("UTF-8") * n, dst) + end + def test_partial_input ec = Encoding::Converter.new("UTF-8", "EUC-JP") ret = ec.primitive_convert(src="", dst="", nil, 10, Encoding::Converter::PARTIAL_INPUT) Index: transcode.c =================================================================== --- transcode.c (revision 18910) +++ transcode.c (revision 18911) @@ -2342,11 +2342,12 @@ * source_buffer should be a string or nil. * nil means a empty string. * - * adestination_buffer should be a string. + * destination_buffer should be a string. * * destination_byteoffset should be an integer or nil. * - * destination_bytesize and flags should be an integer. + * destination_bytesize and flags should be an integer or nil. + * nil means that unlimited. * * primitive_convert convert the content of source_buffer from beginning * and store the result into destination_buffer. @@ -2357,6 +2358,8 @@ * If destination_byteoffset is nil, * destination_buffer.bytesize is used for appending the result. * destination_bytesize specifies maximum number of bytes. + * If destination_bytesize is nil, + * destination size is unlimited. * After conversion, destination_buffer is resized to * destination_byteoffset + actually converted number of bytes. * Also destination_buffer's encoding is set to destination_encoding. @@ -2407,14 +2410,17 @@ rb_scan_args(argc, argv, "41", &input, &output, &output_byteoffset_v, &output_bytesize_v, &flags_v); - if (output_byteoffset_v == Qnil) - output_byteoffset = 0; + if (NIL_P(output_byteoffset_v)) + output_byteoffset = 0; /* dummy */ else output_byteoffset = NUM2LONG(output_byteoffset_v); - output_bytesize = NUM2LONG(output_bytesize_v); + if (NIL_P(output_bytesize_v)) + output_bytesize = 0; /* dummy */ + else + output_bytesize = NUM2LONG(output_bytesize_v); - if (flags_v == Qnil) + if (NIL_P(flags_v)) flags = 0; else flags = NUM2INT(flags_v); @@ -2424,7 +2430,15 @@ StringValue(input); rb_str_modify(output); - if (output_byteoffset_v == Qnil) + if (NIL_P(output_bytesize_v)) { + output_bytesize = RSTRING_EMBED_LEN_MAX; + if (!NIL_P(input) && output_bytesize < RSTRING_LEN(input)) + output_bytesize = RSTRING_LEN(input); + } + + retry: + + if (NIL_P(output_byteoffset_v)) output_byteoffset = RSTRING_LEN(output); if (output_byteoffset < 0) @@ -2462,6 +2476,14 @@ if (!NIL_P(input)) rb_str_drop_bytes(input, ip - (unsigned char *)RSTRING_PTR(input)); + if (NIL_P(output_bytesize_v) && res == econv_destination_buffer_full) { + if (LONG_MAX / 2 < output_bytesize) + rb_raise(rb_eArgError, "too long conversion result"); + output_bytesize *= 2; + output_byteoffset_v = Qnil; + goto retry; + } + if (ec->destination_encoding) { rb_enc_associate(output, ec->destination_encoding); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/