summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-07-03 04:44:53 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-07-03 04:44:53 +0000
commitf029920531e553a28be401495fc0500f6d013ec8 (patch)
treeccfc1c7e20f3f84af8591f988d323baeea150407
parent780869b27fb39d6c2bc61506490e371b877cd26a (diff)
* array.c (ary_reject_bang): should not remove elements which are
not yielded. [Bug #2545] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@32373 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--array.c35
-rw-r--r--test/ruby/test_array.rb4
3 files changed, 20 insertions, 24 deletions
diff --git a/ChangeLog b/ChangeLog
index 101569e8f8..811dc86fd4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Sun Jul 3 13:44:51 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * array.c (ary_reject_bang): should not remove elements which are
+ not yielded. [Bug #2545]
+
Sat Jul 2 07:17:45 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
* array.c (rb_ary_reject_bang, rb_ary_delete_if): rejected
diff --git a/array.c b/array.c
index cb91c1883b..c63899479a 100644
--- a/array.c
+++ b/array.c
@@ -2205,44 +2205,35 @@ ary_reject(orig, result)
VALUE orig, result;
{
long i;
- int rejected = 0;
for (i = 0; i < RARRAY_LEN(orig); i++) {
VALUE v = RARRAY_PTR(orig)[i];
if (!RTEST(rb_yield(v))) {
rb_ary_push(result, v);
}
- else {
- rejected = 1;
- }
}
- return rejected ? result : 0;
-}
-
-static VALUE
-ary_protecting_reject(arg)
- VALUE arg;
-{
- VALUE *args = (VALUE *)arg;
- return ary_reject(args[0], args[1]);
+ return result;
}
static VALUE
ary_reject_bang(ary)
VALUE ary;
{
- VALUE args[2];
- int state = 0;
+ long i;
+ VALUE result = Qnil;
rb_ary_modify_check(ary);
- args[0] = ary;
- args[1] = rb_ary_new();
- if (!rb_protect(ary_protecting_reject, (VALUE)args, &state)) {
- return Qnil;
+ for (i = 0; i < RARRAY_LEN(ary); ) {
+ VALUE v = RARRAY_PTR(ary)[i];
+ if (RTEST(rb_yield(v))) {
+ rb_ary_delete_at(ary, i);
+ result = ary;
+ }
+ else {
+ i++;
+ }
}
- rb_ary_replace(ary, args[1]);
- if (state) rb_jump_tag(state);
- return ary;
+ return result;
}
/*
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index c18f51ef9c..5a571efbbe 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -601,7 +601,7 @@ class TestArray < Test::Unit::TestCase
bug2545 = '[ruby-core:27366]'
a = @cls[ 5, 6, 7, 8, 9, 10 ]
assert_equal(9, a.delete_if {|i| break i if i > 8; i < 7})
- assert_equal(@cls[7, 8], a, bug2545)
+ assert_equal(@cls[7, 8, 9, 10], a, bug2545)
end
def test_dup
@@ -976,7 +976,7 @@ class TestArray < Test::Unit::TestCase
bug2545 = '[ruby-core:27366]'
a = @cls[ 5, 6, 7, 8, 9, 10 ]
assert_equal(9, a.reject! {|i| break i if i > 8; i < 7})
- assert_equal(@cls[7, 8], a, bug2545)
+ assert_equal(@cls[7, 8, 9, 10], a, bug2545)
end
def test_replace