From 065f5380863e93fc838c663a83e97b62c2739289 Mon Sep 17 00:00:00 2001 From: knu Date: Sat, 24 May 2008 20:07:54 +0000 Subject: * array.c (rb_ary_slice_bang): Be consistent with Array#slice() and String#slice!(). Just return nil when a negative length or out of boundary index is given instead of raising an exception via internal functions. (rb_ary_slice_bang): should not use rb_ary_subseq() which shares internal pointer. splice modifies the receiver right after subseq. [ruby-dev:34005] (rb_ary_slice_bang): should adjust length before making sub-array. * enumerator.c (Init_Enumerator): Override Enumerable::Enumerator#each_with_index with #with_index. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@16582 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- array.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'array.c') diff --git a/array.c b/array.c index a0b608c21d..3026f195c8 100644 --- a/array.c +++ b/array.c @@ -2078,17 +2078,24 @@ rb_ary_slice_bang(argc, argv, ary) VALUE ary; { VALUE arg1, arg2; - long pos, len; + long pos, len, orig_len; if (rb_scan_args(argc, argv, "11", &arg1, &arg2) == 2) { pos = NUM2LONG(arg1); len = NUM2LONG(arg2); delete_pos_len: + if (len < 0) return Qnil; + orig_len = RARRAY_LEN(ary); if (pos < 0) { - pos = RARRAY(ary)->len + pos; + pos += orig_len; if (pos < 0) return Qnil; } - arg2 = rb_ary_subseq(ary, pos, len); + else if (orig_len <= pos) return Qnil; + if (orig_len < pos + len) { + len = orig_len - pos; + } + arg2 = rb_ary_new4(len, RARRAY_PTR(ary)+pos); + RBASIC(arg2)->klass = rb_obj_class(ary); rb_ary_splice(ary, pos, len, Qnil); /* Qnil/rb_ary_new2(0) */ return arg2; } -- cgit v1.2.3