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

Fix Range#include? for beginless exclusive string ranges

Previously, include? would return true for the end of the range,
when it should return false because the range is exclusive.

Research and Analysis by Victor Shepelev.

Fixes [Bug #18577]
This commit is contained in:
Jeremy Evans 2022-02-09 09:58:05 -08:00
parent 05b1944c53
commit fd710d7e99
Notes: git 2022-02-10 12:48:02 +09:00
2 changed files with 7 additions and 0 deletions

View file

@ -1792,6 +1792,9 @@ range_include_internal(VALUE range, VALUE val, int string_use_cover)
else if (NIL_P(beg)) { else if (NIL_P(beg)) {
VALUE r = rb_funcall(val, id_cmp, 1, end); VALUE r = rb_funcall(val, id_cmp, 1, end);
if (NIL_P(r)) return Qfalse; if (NIL_P(r)) return Qfalse;
if (RANGE_EXCL(range)) {
return RBOOL(rb_cmpint(r, val, end) < 0);
}
return RBOOL(rb_cmpint(r, val, end) <= 0); return RBOOL(rb_cmpint(r, val, end) <= 0);
} }
else if (NIL_P(end)) { else if (NIL_P(end)) {

View file

@ -604,6 +604,10 @@ class TestRange < Test::Unit::TestCase
assert_include(0...10, 5) assert_include(0...10, 5)
assert_include(5..., 10) assert_include(5..., 10)
assert_not_include(5..., 0) assert_not_include(5..., 0)
assert_include(.."z", "z")
assert_not_include(..."z", "z")
assert_include(..10, 10)
assert_not_include(...10, 10)
end end
def test_cover def test_cover