range.c: return nil for empty range

* range.c (range_last): return nil for empty range, or in the case the
  predecessor is smaller than the begin.  [Bug #8739]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42407 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-08-06 12:59:59 +00:00
Родитель 814b7b5448
Коммит 9deecfc453
3 изменённых файлов: 16 добавлений и 2 удалений

Просмотреть файл

@ -1,3 +1,8 @@
Tue Aug 6 21:59:56 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* range.c (range_last): return nil for empty range, or in the case the
predecessor is smaller than the begin. [Bug #8739]
Tue Aug 6 21:48:31 2013 Kouji Takao <kouji.takao@gmail.com> Tue Aug 6 21:48:31 2013 Kouji Takao <kouji.takao@gmail.com>
* ext/readline/readline.c (readline_s_set_point, Init_readline): * ext/readline/readline.c (readline_s_set_point, Init_readline):

11
range.c
Просмотреть файл

@ -891,8 +891,15 @@ range_last(int argc, VALUE *argv, VALUE range)
VALUE e = RANGE_END(range); VALUE e = RANGE_END(range);
if (!EXCL(range)) return e; /* inclusive, the end is the last */ if (!EXCL(range)) return e; /* inclusive, the end is the last */
/* exclusive, the last is previous to the end */ /* exclusive, the last is previous to the end */
if (FIXNUM_P(e) || rb_obj_is_kind_of(e, rb_cNumeric)) if (FIXNUM_P(e) || rb_obj_is_kind_of(e, rb_cNumeric)) {
return rb_int_pred(e); VALUE pred = rb_int_pred(e);
if (!r_lt(RANGE_BEG(range), pred)) {
/* TODO: what should be returned, or should raise an
* exception? */
pred = Qnil;
}
return pred;
}
/* fallback to Array */ /* fallback to Array */
} }

Просмотреть файл

@ -254,6 +254,7 @@ class TestRange < Test::Unit::TestCase
assert_equal(10, (0..10).last) assert_equal(10, (0..10).last)
assert_equal("a", ("a".."c").first) assert_equal("a", ("a".."c").first)
assert_equal("c", ("a".."c").last) assert_equal("c", ("a".."c").last)
assert_equal(0, (2..0).last)
bug8739 = '[ruby-dev:47587] [Bug #8739] from exclusive range' bug8739 = '[ruby-dev:47587] [Bug #8739] from exclusive range'
assert_equal([0, 1, 2], (0...10).first(3), bug8739) assert_equal([0, 1, 2], (0...10).first(3), bug8739)
@ -262,6 +263,7 @@ class TestRange < Test::Unit::TestCase
assert_equal(9, (0...10).last, bug8739) assert_equal(9, (0...10).last, bug8739)
assert_equal("a", ("a"..."c").first, bug8739) assert_equal("a", ("a"..."c").first, bug8739)
assert_equal("b", ("a"..."c").last, bug8739) assert_equal("b", ("a"..."c").last, bug8739)
assert_nil((2...0).last)
end end
def test_to_s def test_to_s