summaryrefslogtreecommitdiff
path: root/array.c
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
commit6343e30c147dc00e34e9d45ea3f74f94873b9709 (patch)
treefb58e362055d888ad570a357c7c7095aebf3ab3a /array.c
parentb582f2a1233b1ca59e707b489bb0187349f500b7 (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/trunk@32373 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c34
1 files changed, 13 insertions, 21 deletions
diff --git a/array.c b/array.c
index 3a8d0e915c..7360a68e68 100644
--- a/array.c
+++ b/array.c
@@ -2567,42 +2567,34 @@ static VALUE
ary_reject(VALUE orig, VALUE 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_1(result, v);
}
- else {
- rejected = 1;
- }
}
- return rejected ? result : 0;
-}
-
-static VALUE
-ary_protecting_reject(VALUE arg)
-{
- VALUE *args = (VALUE *)arg;
- return ary_reject(args[0], args[1]);
+ return result;
}
static VALUE
ary_reject_bang(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;
}
/*