mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05: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
|
@ -1,3 +1,8 @@
|
|||
Sat Jan 10 10:12:15 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* array.c (rb_ary_select_bang): keep the array consistent by
|
||||
removing unselected values soon. [ruby-dev:48805] [Bug #10722]
|
||||
|
||||
Fri Jan 9 23:20:04 2015 SHIBATA Hiroshi <shibata.hiroshi@gmail.com>
|
||||
|
||||
* lib/rubygems: Update to RubyGems HEAD(e53c54a).
|
||||
|
|
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;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -2019,6 +2019,16 @@ class TestArray < Test::Unit::TestCase
|
|||
a = @cls[ 1, 2, 3, 4, 5 ]
|
||||
assert_equal(a, a.select! { |i| i > 3 })
|
||||
assert_equal(@cls[4, 5], a)
|
||||
|
||||
bug10722 = '[ruby-dev:48805] [Bug #10722]'
|
||||
a = @cls[ 5, 6, 7, 8, 9, 10 ]
|
||||
r = a.select! {|i|
|
||||
break i if i > 8
|
||||
# assert_equal(a[0], i, "should be selected values only") if i == 7
|
||||
i >= 7
|
||||
}
|
||||
assert_equal(9, r)
|
||||
assert_equal(@cls[7, 8, 9, 10], a, bug10722)
|
||||
end
|
||||
|
||||
def test_delete2
|
||||
|
|
Loading…
Add table
Reference in a new issue