From af8f8e5b0e135570f9e5cd2aa8e949ec2e204b31 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 10 Jul 2009 07:30:08 +0000 Subject: * array.c (recursive_join): use obj to tell if recursion occurs. [ruby-core:24150] * enum.c (enum_join): reverted r23966. [ruby-core:24196] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24017 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++++++ array.c | 11 ++++++----- enum.c | 31 +------------------------------ test/ruby/test_array.rb | 5 +++++ test/ruby/test_enum.rb | 17 +++++++++++++---- 5 files changed, 33 insertions(+), 39 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1dc1060887..d4074c9815 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,15 @@ +Fri Jul 10 16:30:03 2009 Nobuyoshi Nakada + + * array.c (recursive_join): use obj to tell if recursion occurs. + [ruby-core:24150] + + * enum.c (enum_join): reverted r23966. [ruby-core:24196] + Fri Jul 10 14:41:34 2009 NARUSE, Yui * marshal.c (r_object0): set encoding only if the encoding is not US-ASCII. + Fri Jul 10 14:44:03 2009 Nobuyoshi Nakada * random.c (struct MT): ruby already assumes int has 32bit a diff --git a/array.c b/array.c index 059574026c..0b825dab5f 100644 --- a/array.c +++ b/array.c @@ -1519,7 +1519,7 @@ rb_ary_resurrect(VALUE ary) extern VALUE rb_output_fs; -static void ary_join_1(VALUE ary, VALUE sep, long i, VALUE result); +static void ary_join_1(VALUE obj, VALUE ary, VALUE sep, long i, VALUE result); static VALUE recursive_join(VALUE obj, VALUE argp, int recur) @@ -1533,7 +1533,7 @@ recursive_join(VALUE obj, VALUE argp, int recur) rb_str_buf_cat_ascii(result, "[...]"); } else { - ary_join_1(ary, sep, 0, result); + ary_join_1(obj, ary, sep, 0, result); } return Qnil; } @@ -1555,7 +1555,7 @@ ary_join_0(VALUE ary, VALUE sep, long max, VALUE result) } static void -ary_join_1(VALUE ary, VALUE sep, long i, VALUE result) +ary_join_1(VALUE obj, VALUE ary, VALUE sep, long i, VALUE result) { VALUE val, tmp; @@ -1581,7 +1581,7 @@ ary_join_1(VALUE ary, VALUE sep, long i, VALUE result) args[0] = val; args[1] = sep; args[2] = result; - rb_exec_recursive(recursive_join, ary, (VALUE)args); + rb_exec_recursive(recursive_join, obj, (VALUE)args); } break; default: @@ -1592,6 +1592,7 @@ ary_join_1(VALUE ary, VALUE sep, long i, VALUE result) } tmp = rb_check_convert_type(val, T_ARRAY, "Array", "to_a"); if (!NIL_P(tmp)) { + obj = val; val = tmp; goto ary_join; } @@ -1626,7 +1627,7 @@ rb_ary_join(VALUE ary, VALUE sep) if (taint) OBJ_TAINT(result); if (untrust) OBJ_UNTRUST(result); ary_join_0(ary, sep, i, result); - ary_join_1(ary, sep, i, result); + ary_join_1(ary, ary, sep, i, result); return result; } diff --git a/enum.c b/enum.c index 678ae8afb4..3379cf2c75 100644 --- a/enum.c +++ b/enum.c @@ -1802,35 +1802,6 @@ enum_cycle(int argc, VALUE *argv, VALUE obj) return Qnil; /* not reached */ } -static VALUE -join_i(VALUE i, VALUE args, int argc, VALUE *argv) -{ - VALUE *arg = (VALUE *)args; - ENUM_WANT_SVALUE(); - if (!arg[0]) { - arg[0] = rb_usascii_str_new(0, 0); - } - else if (!NIL_P(arg[1])) { - rb_str_buf_append(arg[0], arg[1]); - } - return rb_str_buf_append(arg[0], rb_obj_as_string(i)); -} - -VALUE -rb_enum_join(VALUE obj, VALUE sep) -{ - VALUE args[2]; - - args[0] = 0; - args[1] = sep; - if (!NIL_P(sep)) StringValue(args[1]); - rb_block_call(obj, id_each, 0, 0, join_i, (VALUE)args); - if (!args[0]) args[0] = rb_str_new(0, 0); - OBJ_INFECT(args[0], obj); - - return args[0]; -} - /* * call-seq: * enum.join(sep=$,) -> str @@ -1847,7 +1818,7 @@ enum_join(int argc, VALUE *argv, VALUE obj) rb_scan_args(argc, argv, "01", &sep); if (NIL_P(sep)) sep = rb_output_fs; - return rb_enum_join(obj, sep); + return rb_ary_join(enum_to_a(0, 0, obj), sep); } /* diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 7b0039310a..8a442087ae 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -1491,6 +1491,11 @@ class TestArray < Test::Unit::TestCase a = [] a << a assert_equal("[...]", a.join) + + def (a = Object.new).to_a + [self] + end + assert_equal("[...]", [a].join, , '[ruby-core:24150]') end def test_to_a2 diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb index e0e77b4869..660c59383a 100644 --- a/test/ruby/test_enum.rb +++ b/test/ruby/test_enum.rb @@ -295,14 +295,23 @@ class TestEnumerable < Test::Unit::TestCase assert_equal("123", (1..3).join()) assert_raise(TypeError, '[ruby-core:24172]') {("a".."c").join(1)} class << (e = Object.new.extend(Enumerable)) - def to_s - "e" - end def each yield self end end - assert_equal("e", e.join("")) + assert_equal("[...]", e.join(""), '[ruby-core:24150]') + assert_equal("[...]", [e].join(""), '[ruby-core:24150]') + e = Class.new { + include Enumerable + def initialize(*args) + @e = args + end + def each + @e.each {|e| yield e} + end + } + e = e.new(1, e.new(2, e.new(3, e.new(4, 5)))) + assert_equal("1:2:3:4:5", e.join(':'), '[ruby-core:24196]') ensure $, = ofs end -- cgit v1.2.1