summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-01-10 01:12:17 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-01-10 01:12:17 (GMT)
commitd2da3d04e6873da6383f18355d6ac46e6d263fcc (patch)
tree2e091980c9cee49a2e1dbd5073dbc5e26e733d82 /array.c
parent07b87cd239db5b556a6a9bb38ea78cd2540e4682 (diff)
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
Diffstat (limited to 'array.c')
-rw-r--r--array.c23
1 files changed, 11 insertions, 12 deletions
diff --git a/array.c b/array.c
index 00d2a7f..0de7231 100644
--- a/array.c
+++ b/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;
}
/*