mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 21:49:06 +02:00
array.c: keep consistency
* array.c (rb_ary_select_bang): keep the array consistent by removing unselected values soon. [ruby-dev:48805] [Bug #10722] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49196 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
07b87cd239
commit
d2da3d04e6
3 changed files with 26 additions and 12 deletions
23
array.c
23
array.c
|
@ -2843,23 +2843,22 @@ rb_ary_select(VALUE ary)
|
|||
static VALUE
|
||||
rb_ary_select_bang(VALUE ary)
|
||||
{
|
||||
long i1, i2;
|
||||
long i;
|
||||
VALUE result = Qnil;
|
||||
|
||||
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
|
||||
rb_ary_modify(ary);
|
||||
for (i1 = i2 = 0; i1 < RARRAY_LEN(ary); i1++) {
|
||||
VALUE v = RARRAY_AREF(ary, i1);
|
||||
if (!RTEST(rb_yield(v))) continue;
|
||||
if (i1 != i2) {
|
||||
rb_ary_store(ary, i2, v);
|
||||
for (i = 0; i < RARRAY_LEN(ary); ) {
|
||||
VALUE v = RARRAY_AREF(ary, i);
|
||||
if (!RTEST(rb_yield(v))) {
|
||||
rb_ary_delete_at(ary, i);
|
||||
result = ary;
|
||||
}
|
||||
else {
|
||||
i++;
|
||||
}
|
||||
i2++;
|
||||
}
|
||||
|
||||
if (i1 == i2) return Qnil;
|
||||
if (i2 < i1)
|
||||
ARY_SET_LEN(ary, i2);
|
||||
return ary;
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue