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

* 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
This commit is contained in:
nobu 2009-07-10 07:30:08 +00:00
parent 5e2a28d13d
commit af8f8e5b0e
5 changed files with 33 additions and 39 deletions

View file

@ -1,7 +1,15 @@
Fri Jul 10 16:30:03 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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 <naruse@ruby-lang.org>
* marshal.c (r_object0): set encoding only if the encoding
is not US-ASCII.
Fri Jul 10 14:44:03 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* random.c (struct MT): ruby already assumes int has 32bit a

11
array.c
View file

@ -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;
}

31
enum.c
View file

@ -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);
}
/*

View file

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

View file

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