ruby-changes:71138
From: Jeremy <ko1@a...>
Date: Thu, 10 Feb 2022 12:47:56 +0900 (JST)
Subject: [ruby-changes:71138] fd710d7e99 (master): Fix Range#include? for beginless exclusive string ranges
https://git.ruby-lang.org/ruby.git/commit/?id=fd710d7e99 From fd710d7e9995679db80b7adf35bbda2cd4db90c6 Mon Sep 17 00:00:00 2001 From: Jeremy Evans <code@j...> Date: Wed, 9 Feb 2022 09:58:05 -0800 Subject: 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] --- range.c | 3 +++ test/ruby/test_range.rb | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/range.c b/range.c index 2390c5d38c..27f681767f 100644 --- a/range.c +++ b/range.c @@ -1792,6 +1792,9 @@ range_include_internal(VALUE range, VALUE val, int string_use_cover) https://github.com/ruby/ruby/blob/trunk/range.c#L1792 else if (NIL_P(beg)) { VALUE r = rb_funcall(val, id_cmp, 1, end); 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); } else if (NIL_P(end)) { diff --git a/test/ruby/test_range.rb b/test/ruby/test_range.rb index 8ac1930be6..8789eca749 100644 --- a/test/ruby/test_range.rb +++ b/test/ruby/test_range.rb @@ -604,6 +604,10 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L604 assert_include(0...10, 5) assert_include(5..., 10) assert_not_include(5..., 0) + assert_include(.."z", "z") + assert_not_include(..."z", "z") + assert_include(..10, 10) + assert_not_include(...10, 10) end def test_cover -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/