зеркало из https://github.com/github/ruby.git
* enum.c: Enumerable#chunk and Enumerable#slice_before no longer takes
the initial_state argument. [Feature #10958] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50174 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
9db2bb2a5b
Коммит
774f682952
|
@ -1,3 +1,8 @@
|
||||||
|
Mon Apr 6 22:52:35 2015 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* enum.c: Enumerable#chunk and Enumerable#slice_before no longer takes
|
||||||
|
the initial_state argument. [Feature #10958]
|
||||||
|
|
||||||
Mon Apr 6 16:09:58 2015 Koichi Sasada <ko1@atdot.net>
|
Mon Apr 6 16:09:58 2015 Koichi Sasada <ko1@atdot.net>
|
||||||
|
|
||||||
* vm_args.c: protect value stack from calling other methods
|
* vm_args.c: protect value stack from calling other methods
|
||||||
|
|
5
NEWS
5
NEWS
|
@ -25,6 +25,11 @@ with all sufficient information, see the ChangeLog file.
|
||||||
* Array#flatten and Array#flatten! no longer try to call #to_ary
|
* Array#flatten and Array#flatten! no longer try to call #to_ary
|
||||||
method on elements beyond the given level. [Bug #10748]
|
method on elements beyond the given level. [Bug #10748]
|
||||||
|
|
||||||
|
* Enumerable
|
||||||
|
* Enumerable#chunk and Enumerable#slice_before no longer takes the
|
||||||
|
initial_state argument. [Feature #10958]
|
||||||
|
Use a local variable instead to maintain a state.
|
||||||
|
|
||||||
* IO
|
* IO
|
||||||
* IO#close doesn't raise when the IO object is closed. [Feature #10718]
|
* IO#close doesn't raise when the IO object is closed. [Feature #10718]
|
||||||
|
|
||||||
|
|
39
enum.c
39
enum.c
|
@ -2706,7 +2706,6 @@ enum_cycle(int argc, VALUE *argv, VALUE obj)
|
||||||
|
|
||||||
struct chunk_arg {
|
struct chunk_arg {
|
||||||
VALUE categorize;
|
VALUE categorize;
|
||||||
VALUE state;
|
|
||||||
VALUE prev_value;
|
VALUE prev_value;
|
||||||
VALUE prev_elts;
|
VALUE prev_elts;
|
||||||
VALUE yielder;
|
VALUE yielder;
|
||||||
|
@ -2722,10 +2721,7 @@ chunk_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _argp))
|
||||||
|
|
||||||
ENUM_WANT_SVALUE();
|
ENUM_WANT_SVALUE();
|
||||||
|
|
||||||
if (NIL_P(argp->state))
|
v = rb_funcall(argp->categorize, id_call, 1, i);
|
||||||
v = rb_funcall(argp->categorize, id_call, 1, i);
|
|
||||||
else
|
|
||||||
v = rb_funcall(argp->categorize, id_call, 2, i, argp->state);
|
|
||||||
|
|
||||||
if (v == alone) {
|
if (v == alone) {
|
||||||
if (!NIL_P(argp->prev_value)) {
|
if (!NIL_P(argp->prev_value)) {
|
||||||
|
@ -2771,14 +2767,10 @@ chunk_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator))
|
||||||
|
|
||||||
enumerable = rb_ivar_get(enumerator, rb_intern("chunk_enumerable"));
|
enumerable = rb_ivar_get(enumerator, rb_intern("chunk_enumerable"));
|
||||||
memo->categorize = rb_ivar_get(enumerator, rb_intern("chunk_categorize"));
|
memo->categorize = rb_ivar_get(enumerator, rb_intern("chunk_categorize"));
|
||||||
memo->state = rb_ivar_get(enumerator, rb_intern("chunk_initial_state"));
|
|
||||||
memo->prev_value = Qnil;
|
memo->prev_value = Qnil;
|
||||||
memo->prev_elts = Qnil;
|
memo->prev_elts = Qnil;
|
||||||
memo->yielder = yielder;
|
memo->yielder = yielder;
|
||||||
|
|
||||||
if (!NIL_P(memo->state))
|
|
||||||
memo->state = rb_obj_dup(memo->state);
|
|
||||||
|
|
||||||
rb_block_call(enumerable, id_each, 0, 0, chunk_ii, arg);
|
rb_block_call(enumerable, id_each, 0, 0, chunk_ii, arg);
|
||||||
memo = MEMO_FOR(struct chunk_arg, arg);
|
memo = MEMO_FOR(struct chunk_arg, arg);
|
||||||
if (!NIL_P(memo->prev_elts))
|
if (!NIL_P(memo->prev_elts))
|
||||||
|
@ -2789,7 +2781,6 @@ chunk_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator))
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* enum.chunk { |elt| ... } -> an_enumerator
|
* enum.chunk { |elt| ... } -> an_enumerator
|
||||||
* enum.chunk(initial_state) { |elt, state| ... } -> an_enumerator (deprecated)
|
|
||||||
*
|
*
|
||||||
* Enumerates over the items, chunking them together based on the return
|
* Enumerates over the items, chunking them together based on the return
|
||||||
* value of the block.
|
* value of the block.
|
||||||
|
@ -2875,22 +2866,16 @@ chunk_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator))
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static VALUE
|
static VALUE
|
||||||
enum_chunk(int argc, VALUE *argv, VALUE enumerable)
|
enum_chunk(VALUE enumerable)
|
||||||
{
|
{
|
||||||
VALUE initial_state;
|
|
||||||
VALUE enumerator;
|
VALUE enumerator;
|
||||||
int n;
|
|
||||||
|
|
||||||
if (!rb_block_given_p())
|
if (!rb_block_given_p())
|
||||||
rb_raise(rb_eArgError, "no block given");
|
rb_raise(rb_eArgError, "no block given");
|
||||||
n = rb_scan_args(argc, argv, "01", &initial_state);
|
|
||||||
if (n != 0)
|
|
||||||
rb_warn("initial_state given for chunk. (Use local variables.)");
|
|
||||||
|
|
||||||
enumerator = rb_obj_alloc(rb_cEnumerator);
|
enumerator = rb_obj_alloc(rb_cEnumerator);
|
||||||
rb_ivar_set(enumerator, rb_intern("chunk_enumerable"), enumerable);
|
rb_ivar_set(enumerator, rb_intern("chunk_enumerable"), enumerable);
|
||||||
rb_ivar_set(enumerator, rb_intern("chunk_categorize"), rb_block_proc());
|
rb_ivar_set(enumerator, rb_intern("chunk_categorize"), rb_block_proc());
|
||||||
rb_ivar_set(enumerator, rb_intern("chunk_initial_state"), initial_state);
|
|
||||||
rb_block_call(enumerator, idInitialize, 0, 0, chunk_i, enumerator);
|
rb_block_call(enumerator, idInitialize, 0, 0, chunk_i, enumerator);
|
||||||
return enumerator;
|
return enumerator;
|
||||||
}
|
}
|
||||||
|
@ -2899,7 +2884,6 @@ enum_chunk(int argc, VALUE *argv, VALUE enumerable)
|
||||||
struct slicebefore_arg {
|
struct slicebefore_arg {
|
||||||
VALUE sep_pred;
|
VALUE sep_pred;
|
||||||
VALUE sep_pat;
|
VALUE sep_pat;
|
||||||
VALUE state;
|
|
||||||
VALUE prev_elts;
|
VALUE prev_elts;
|
||||||
VALUE yielder;
|
VALUE yielder;
|
||||||
};
|
};
|
||||||
|
@ -2914,10 +2898,8 @@ slicebefore_ii(RB_BLOCK_CALL_FUNC_ARGLIST(i, _argp))
|
||||||
|
|
||||||
if (!NIL_P(argp->sep_pat))
|
if (!NIL_P(argp->sep_pat))
|
||||||
header_p = rb_funcall(argp->sep_pat, id_eqq, 1, i);
|
header_p = rb_funcall(argp->sep_pat, id_eqq, 1, i);
|
||||||
else if (NIL_P(argp->state))
|
|
||||||
header_p = rb_funcall(argp->sep_pred, id_call, 1, i);
|
|
||||||
else
|
else
|
||||||
header_p = rb_funcall(argp->sep_pred, id_call, 2, i, argp->state);
|
header_p = rb_funcall(argp->sep_pred, id_call, 1, i);
|
||||||
if (RTEST(header_p)) {
|
if (RTEST(header_p)) {
|
||||||
if (!NIL_P(argp->prev_elts))
|
if (!NIL_P(argp->prev_elts))
|
||||||
rb_funcall(argp->yielder, id_lshift, 1, argp->prev_elts);
|
rb_funcall(argp->yielder, id_lshift, 1, argp->prev_elts);
|
||||||
|
@ -2943,13 +2925,9 @@ slicebefore_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator))
|
||||||
enumerable = rb_ivar_get(enumerator, rb_intern("slicebefore_enumerable"));
|
enumerable = rb_ivar_get(enumerator, rb_intern("slicebefore_enumerable"));
|
||||||
memo->sep_pred = rb_attr_get(enumerator, rb_intern("slicebefore_sep_pred"));
|
memo->sep_pred = rb_attr_get(enumerator, rb_intern("slicebefore_sep_pred"));
|
||||||
memo->sep_pat = NIL_P(memo->sep_pred) ? rb_ivar_get(enumerator, rb_intern("slicebefore_sep_pat")) : Qnil;
|
memo->sep_pat = NIL_P(memo->sep_pred) ? rb_ivar_get(enumerator, rb_intern("slicebefore_sep_pat")) : Qnil;
|
||||||
memo->state = rb_attr_get(enumerator, rb_intern("slicebefore_initial_state"));
|
|
||||||
memo->prev_elts = Qnil;
|
memo->prev_elts = Qnil;
|
||||||
memo->yielder = yielder;
|
memo->yielder = yielder;
|
||||||
|
|
||||||
if (!NIL_P(memo->state))
|
|
||||||
memo->state = rb_obj_dup(memo->state);
|
|
||||||
|
|
||||||
rb_block_call(enumerable, id_each, 0, 0, slicebefore_ii, arg);
|
rb_block_call(enumerable, id_each, 0, 0, slicebefore_ii, arg);
|
||||||
memo = MEMO_FOR(struct slicebefore_arg, arg);
|
memo = MEMO_FOR(struct slicebefore_arg, arg);
|
||||||
if (!NIL_P(memo->prev_elts))
|
if (!NIL_P(memo->prev_elts))
|
||||||
|
@ -2961,7 +2939,6 @@ slicebefore_i(RB_BLOCK_CALL_FUNC_ARGLIST(yielder, enumerator))
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* enum.slice_before(pattern) -> an_enumerator
|
* enum.slice_before(pattern) -> an_enumerator
|
||||||
* enum.slice_before { |elt| bool } -> an_enumerator
|
* enum.slice_before { |elt| bool } -> an_enumerator
|
||||||
* enum.slice_before(initial_state) { |elt, state| bool } -> an_enumerator (deprecated)
|
|
||||||
*
|
*
|
||||||
* Creates an enumerator for each chunked elements.
|
* Creates an enumerator for each chunked elements.
|
||||||
* The beginnings of chunks are defined by _pattern_ and the block.
|
* The beginnings of chunks are defined by _pattern_ and the block.
|
||||||
|
@ -3107,14 +3084,10 @@ enum_slice_before(int argc, VALUE *argv, VALUE enumerable)
|
||||||
VALUE enumerator;
|
VALUE enumerator;
|
||||||
|
|
||||||
if (rb_block_given_p()) {
|
if (rb_block_given_p()) {
|
||||||
VALUE initial_state;
|
if (argc != 0)
|
||||||
int n;
|
rb_error_arity(argc, 0, 0);
|
||||||
n = rb_scan_args(argc, argv, "01", &initial_state);
|
|
||||||
if (n != 0)
|
|
||||||
rb_warn("initial_state given for slice_before. (Use local variables.)");
|
|
||||||
enumerator = rb_obj_alloc(rb_cEnumerator);
|
enumerator = rb_obj_alloc(rb_cEnumerator);
|
||||||
rb_ivar_set(enumerator, rb_intern("slicebefore_sep_pred"), rb_block_proc());
|
rb_ivar_set(enumerator, rb_intern("slicebefore_sep_pred"), rb_block_proc());
|
||||||
rb_ivar_set(enumerator, rb_intern("slicebefore_initial_state"), initial_state);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
VALUE sep_pat;
|
VALUE sep_pat;
|
||||||
|
@ -3455,7 +3428,7 @@ Init_Enumerable(void)
|
||||||
rb_define_method(rb_mEnumerable, "drop", enum_drop, 1);
|
rb_define_method(rb_mEnumerable, "drop", enum_drop, 1);
|
||||||
rb_define_method(rb_mEnumerable, "drop_while", enum_drop_while, 0);
|
rb_define_method(rb_mEnumerable, "drop_while", enum_drop_while, 0);
|
||||||
rb_define_method(rb_mEnumerable, "cycle", enum_cycle, -1);
|
rb_define_method(rb_mEnumerable, "cycle", enum_cycle, -1);
|
||||||
rb_define_method(rb_mEnumerable, "chunk", enum_chunk, -1);
|
rb_define_method(rb_mEnumerable, "chunk", enum_chunk, 0);
|
||||||
rb_define_method(rb_mEnumerable, "slice_before", enum_slice_before, -1);
|
rb_define_method(rb_mEnumerable, "slice_before", enum_slice_before, -1);
|
||||||
rb_define_method(rb_mEnumerable, "slice_after", enum_slice_after, -1);
|
rb_define_method(rb_mEnumerable, "slice_after", enum_slice_after, -1);
|
||||||
rb_define_method(rb_mEnumerable, "slice_when", enum_slice_when, 0);
|
rb_define_method(rb_mEnumerable, "slice_when", enum_slice_when, 0);
|
||||||
|
|
|
@ -481,22 +481,6 @@ class TestEnumerable < Test::Unit::TestCase
|
||||||
e = @obj.chunk {|elt| elt & 2 == 0 ? false : true }
|
e = @obj.chunk {|elt| elt & 2 == 0 ? false : true }
|
||||||
assert_equal([[false, [1]], [true, [2, 3]], [false, [1]], [true, [2]]], e.to_a)
|
assert_equal([[false, [1]], [true, [2, 3]], [false, [1]], [true, [2]]], e.to_a)
|
||||||
|
|
||||||
e = @obj.chunk(acc: 0) {|elt, h| h[:acc] += elt; h[:acc].even? }
|
|
||||||
assert_equal([[false, [1,2]], [true, [3]], [false, [1,2]]], e.to_a)
|
|
||||||
assert_equal([[false, [1,2]], [true, [3]], [false, [1,2]]], e.to_a) # this tests h is duplicated.
|
|
||||||
|
|
||||||
hs = [{}]
|
|
||||||
e = [:foo].chunk(hs[0]) {|elt, h|
|
|
||||||
hs << h
|
|
||||||
true
|
|
||||||
}
|
|
||||||
assert_equal([[true, [:foo]]], e.to_a)
|
|
||||||
assert_equal([[true, [:foo]]], e.to_a)
|
|
||||||
assert_equal([{}, {}, {}], hs)
|
|
||||||
assert_not_same(hs[0], hs[1])
|
|
||||||
assert_not_same(hs[0], hs[2])
|
|
||||||
assert_not_same(hs[1], hs[2])
|
|
||||||
|
|
||||||
e = @obj.chunk {|elt| elt < 3 ? :_alone : true }
|
e = @obj.chunk {|elt| elt < 3 ? :_alone : true }
|
||||||
assert_equal([[:_alone, [1]],
|
assert_equal([[:_alone, [1]],
|
||||||
[:_alone, [2]],
|
[:_alone, [2]],
|
||||||
|
@ -526,22 +510,6 @@ class TestEnumerable < Test::Unit::TestCase
|
||||||
e = @obj.slice_before {|elt| elt.odd? }
|
e = @obj.slice_before {|elt| elt.odd? }
|
||||||
assert_equal([[1,2], [3], [1,2]], e.to_a)
|
assert_equal([[1,2], [3], [1,2]], e.to_a)
|
||||||
|
|
||||||
e = @obj.slice_before(acc: 0) {|elt, h| h[:acc] += elt; h[:acc].even? }
|
|
||||||
assert_equal([[1,2], [3,1,2]], e.to_a)
|
|
||||||
assert_equal([[1,2], [3,1,2]], e.to_a) # this tests h is duplicated.
|
|
||||||
|
|
||||||
hs = [{}]
|
|
||||||
e = [:foo].slice_before(hs[0]) {|elt, h|
|
|
||||||
hs << h
|
|
||||||
true
|
|
||||||
}
|
|
||||||
assert_equal([[:foo]], e.to_a)
|
|
||||||
assert_equal([[:foo]], e.to_a)
|
|
||||||
assert_equal([{}, {}, {}], hs)
|
|
||||||
assert_not_same(hs[0], hs[1])
|
|
||||||
assert_not_same(hs[0], hs[2])
|
|
||||||
assert_not_same(hs[1], hs[2])
|
|
||||||
|
|
||||||
ss = %w[abc defg h ijk l mno pqr st u vw xy z]
|
ss = %w[abc defg h ijk l mno pqr st u vw xy z]
|
||||||
assert_equal([%w[abc defg h], %w[ijk l], %w[mno], %w[pqr st u vw xy z]],
|
assert_equal([%w[abc defg h], %w[ijk l], %w[mno], %w[pqr st u vw xy z]],
|
||||||
ss.slice_before(/\A...\z/).to_a)
|
ss.slice_before(/\A...\z/).to_a)
|
||||||
|
|
Загрузка…
Ссылка в новой задаче