1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* array.c (rb_ary_slice_bang): If an invalid negative index (<=

-size) is given, do not raise an exception but return nil just
  like slice() does.

* test/ruby/test_array.rb (TestArray::test_slice,
  TestArray::test_slice!): Pull in test cases from trunk.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@14184 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2007-12-10 15:08:31 +00:00
parent 39a6f7e34e
commit b0388e77e3
4 changed files with 124 additions and 5 deletions

View file

@ -1,3 +1,24 @@
Tue Dec 11 00:04:05 2007 Akinori MUSHA <knu@iDaemons.org>
* array.c (rb_ary_slice_bang): If an invalid negative index (<=
-size) is given, do not raise an exception but return nil just
like slice() does.
* test/ruby/test_array.rb (TestArray::test_slice,
TestArray::test_slice!): Pull in test cases from trunk.
Mon Dec 10 21:47:53 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
* transcode.c (str_transcode): allow non-registered encodings.
[ruby-dev:32520]
Mon Dec 10 21:00:30 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* array.c (rb_ary_slice_bang): should return nil if position out
of range. a patch from Akinori MUSHA <knu AT iDaemons.org>.
[ruby-dev:32518]
Mon Dec 10 18:28:06 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* lib/uri/common.rb (URI::REGEXP::PATTERN): typo in REG_NAME

15
array.c
View file

@ -1963,14 +1963,25 @@ rb_ary_slice_bang(argc, argv, ary)
delete_pos_len:
if (pos < 0) {
pos = RARRAY(ary)->len + pos;
if (pos < 0) return Qnil;
}
arg2 = rb_ary_subseq(ary, pos, len);
rb_ary_splice(ary, pos, len, Qnil); /* Qnil/rb_ary_new2(0) */
return arg2;
}
if (!FIXNUM_P(arg1) && rb_range_beg_len(arg1, &pos, &len, RARRAY(ary)->len, 1)) {
goto delete_pos_len;
if (!FIXNUM_P(arg1)) {
switch (rb_range_beg_len(arg1, &pos, &len, RARRAY_LEN(ary), 0)) {
case Qtrue:
/* valid range */
goto delete_pos_len;
case Qnil:
/* invalid range */
return Qnil;
default:
/* not a range */
break;
}
}
return rb_ary_delete_at(ary, NUM2LONG(arg1));

View file

@ -142,4 +142,91 @@ class TestArray < Test::Unit::TestCase
assert_equal([0, 1, 12, 13, 14, 5], [0, 1, 2, 3, 4, 5].fill(2..-2){|i| i+10})
assert_equal([0, 1, 12, 13, 4, 5], [0, 1, 2, 3, 4, 5].fill(2...-2){|i| i+10})
end
# From rubicon
def setup
@cls = Array
end
def test_slice
a = @cls[*(1..100).to_a]
assert_equal(1, a.slice(0))
assert_equal(100, a.slice(99))
assert_nil(a.slice(100))
assert_equal(100, a.slice(-1))
assert_equal(99, a.slice(-2))
assert_equal(1, a.slice(-100))
assert_nil(a.slice(-101))
assert_equal(@cls[1], a.slice(0,1))
assert_equal(@cls[100], a.slice(99,1))
assert_equal(@cls[], a.slice(100,1))
assert_equal(@cls[100], a.slice(99,100))
assert_equal(@cls[100], a.slice(-1,1))
assert_equal(@cls[99], a.slice(-2,1))
assert_equal(@cls[10, 11, 12], a.slice(9, 3))
assert_equal(@cls[10, 11, 12], a.slice(-91, 3))
assert_nil(a.slice(-101, 2))
assert_equal(@cls[1], a.slice(0..0))
assert_equal(@cls[100], a.slice(99..99))
assert_equal(@cls[], a.slice(100..100))
assert_equal(@cls[100], a.slice(99..200))
assert_equal(@cls[100], a.slice(-1..-1))
assert_equal(@cls[99], a.slice(-2..-2))
assert_equal(@cls[10, 11, 12], a.slice(9..11))
assert_equal(@cls[10, 11, 12], a.slice(-91..-89))
assert_nil(a.slice(-101..-1))
assert_nil(a.slice(10, -3))
# Ruby 1.8 feature change:
# Array#slice[size..x] always returns [].
#assert_nil(a.slice(10..7))
assert_equal @cls[], a.slice(10..7)
end
def test_slice!
a = @cls[1, 2, 3, 4, 5]
assert_equal(3, a.slice!(2))
assert_equal(@cls[1, 2, 4, 5], a)
a = @cls[1, 2, 3, 4, 5]
assert_equal(4, a.slice!(-2))
assert_equal(@cls[1, 2, 3, 5], a)
a = @cls[1, 2, 3, 4, 5]
assert_equal(@cls[3,4], a.slice!(2,2))
assert_equal(@cls[1, 2, 5], a)
a = @cls[1, 2, 3, 4, 5]
assert_equal(@cls[4,5], a.slice!(-2,2))
assert_equal(@cls[1, 2, 3], a)
a = @cls[1, 2, 3, 4, 5]
assert_equal(@cls[3,4], a.slice!(2..3))
assert_equal(@cls[1, 2, 5], a)
a = @cls[1, 2, 3, 4, 5]
assert_equal(nil, a.slice!(20))
assert_equal(@cls[1, 2, 3, 4, 5], a)
a = @cls[1, 2, 3, 4, 5]
assert_equal(nil, a.slice!(-6))
assert_equal(@cls[1, 2, 3, 4, 5], a)
a = @cls[1, 2, 3, 4, 5]
assert_equal(nil, a.slice!(-6..4))
assert_equal(@cls[1, 2, 3, 4, 5], a)
a = @cls[1, 2, 3, 4, 5]
assert_equal(nil, a.slice!(-6,2))
assert_equal(@cls[1, 2, 3, 4, 5], a)
end
end

View file

@ -1,7 +1,7 @@
#define RUBY_VERSION "1.8.6"
#define RUBY_RELEASE_DATE "2007-12-10"
#define RUBY_RELEASE_DATE "2007-12-11"
#define RUBY_VERSION_CODE 186
#define RUBY_RELEASE_CODE 20071210
#define RUBY_RELEASE_CODE 20071211
#define RUBY_PATCHLEVEL 5000
#define RUBY_VERSION_MAJOR 1
@ -9,7 +9,7 @@
#define RUBY_VERSION_TEENY 6
#define RUBY_RELEASE_YEAR 2007
#define RUBY_RELEASE_MONTH 12
#define RUBY_RELEASE_DAY 10
#define RUBY_RELEASE_DAY 11
#ifdef RUBY_EXTERN
RUBY_EXTERN const char ruby_version[];