From 7c52f1ed7a25b61d6efda6d4a005ab9c1da541c6 Mon Sep 17 00:00:00 2001 From: knu Date: Fri, 23 May 2008 09:01:19 +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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16559 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ array.c | 11 +++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index b0d1133d95..fcb2ad7c1f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Fri May 23 17:55:11 2008 Akinori MUSHA + + * 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. + Fri May 23 16:44:34 2008 Akinori MUSHA * enumerator.c (Init_Enumerator): Override diff --git a/array.c b/array.c index 4cb74ac4f7..3e8be3f280 100644 --- a/array.c +++ b/array.c @@ -1803,18 +1803,21 @@ static VALUE rb_ary_slice_bang(int argc, VALUE *argv, 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_LEN(ary) + pos; + pos += orig_len; if (pos < 0) return Qnil; } - if (RARRAY_LEN(ary) < len || RARRAY_LEN(ary) < pos + len) { - len = RARRAY_LEN(ary) - pos; + 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); -- cgit v1.2.3