diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-01-10 01:12:17 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-01-10 01:12:17 +0000 |
commit | d2da3d04e6873da6383f18355d6ac46e6d263fcc (patch) | |
tree | 2e091980c9cee49a2e1dbd5073dbc5e26e733d82 | |
parent | 07b87cd239db5b556a6a9bb38ea78cd2540e4682 (diff) | |
download | ruby-d2da3d04e6873da6383f18355d6ac46e6d263fcc.tar.gz |
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
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | array.c | 23 | ||||
-rw-r--r-- | test/ruby/test_array.rb | 10 |
3 files changed, 26 insertions, 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). @@ -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; } /* diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 04e66cce11..31f33dde1e 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -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 |