[ruby-core:127084] [Ruby Bug#22432] Range#include? returns true for the end of an empty range of single-character Strings
Issue #22432 has been reported by sferik (Erik Berlin). ---------------------------------------- Bug #22432: Range#include? returns true for the end of an empty range of single-character Strings https://bugs.ruby-lang.org/issues/22432 * Author: sferik (Erik Berlin) * Status: Open * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- A Range whose ends are both single ASCII characters reports that it includes its end even when the range is empty. ```ruby r = "z".."a" r.to_a #=> [] r.include?("a") #=> true r.member?("a") #=> true r.cover?("a") #=> false r === "a" #=> false ``` I expect `include?` and `member?` to return false here, as `cover?` does and as iterating the range implies. The same happens with digits, for example `("1".."0").include?("0")`. Only the end is affected. Other values are correctly reported as not included: ```ruby ("z".."a").include?("z") #=> false ("z".."a").include?("m") #=> false ``` `Range#include?` has a shortcut for ranges of single-character Strings, which compares the characters instead of iterating. It is in `rb_str_include_range_p` in string.c: ```c if (ISASCII(b) && ISASCII(e) && ISASCII(v)) { if (b <= v && v < e) return Qtrue; return RBOOL(!RTEST(exclusive) && v == e); } ``` The last line returns true whenever the value equals the end, without checking that the begin is not after the end. -- https://bugs.ruby-lang.org/
Issue #22432 has been updated by eightbitraptor (Matt V-H). Thanks for the report. Addressed in https://github.com/ruby/ruby/pull/19317 ---------------------------------------- Bug #22432: Range#include? returns true for the end of an empty range of single-character Strings https://bugs.ruby-lang.org/issues/22432#change-119483 * Author: sferik (Erik Berlin) * Status: Open * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- A Range whose ends are both single ASCII characters reports that it includes its end even when the range is empty. ```ruby r = "z".."a" r.to_a #=> [] r.include?("a") #=> true r.member?("a") #=> true r.cover?("a") #=> false r === "a" #=> false ``` I expect `include?` and `member?` to return false here, as `cover?` does and as iterating the range implies. The same happens with digits, for example `("1".."0").include?("0")`. Only the end is affected. Other values are correctly reported as not included: ```ruby ("z".."a").include?("z") #=> false ("z".."a").include?("m") #=> false ``` `Range#include?` has a shortcut for ranges of single-character Strings, which compares the characters instead of iterating. It is in `rb_str_include_range_p` in string.c: ```c if (ISASCII(b) && ISASCII(e) && ISASCII(v)) { if (b <= v && v < e) return Qtrue; return RBOOL(!RTEST(exclusive) && v == e); } ``` The last line returns true whenever the value equals the end, without checking that the begin is not after the end. -- https://bugs.ruby-lang.org/
Issue #22432 has been updated by sferik (Erik Berlin). @eightbitraptor: Thanks. I previously submitted a nearly identical test for this in https://github.com/ruby/ruby/pull/19313. CI is already green there. ---------------------------------------- Bug #22432: Range#include? returns true for the end of an empty range of single-character Strings https://bugs.ruby-lang.org/issues/22432#change-119484 * Author: sferik (Erik Berlin) * Status: Open * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- A Range whose ends are both single ASCII characters reports that it includes its end even when the range is empty. ```ruby r = "z".."a" r.to_a #=> [] r.include?("a") #=> true r.member?("a") #=> true r.cover?("a") #=> false r === "a" #=> false ``` I expect `include?` and `member?` to return false here, as `cover?` does and as iterating the range implies. The same happens with digits, for example `("1".."0").include?("0")`. Only the end is affected. Other values are correctly reported as not included: ```ruby ("z".."a").include?("z") #=> false ("z".."a").include?("m") #=> false ``` `Range#include?` has a shortcut for ranges of single-character Strings, which compares the characters instead of iterating. It is in `rb_str_include_range_p` in string.c: ```c if (ISASCII(b) && ISASCII(e) && ISASCII(v)) { if (b <= v && v < e) return Qtrue; return RBOOL(!RTEST(exclusive) && v == e); } ``` The last line returns true whenever the value equals the end, without checking that the begin is not after the end. -- https://bugs.ruby-lang.org/
Issue #22432 has been updated by sferik (Erik Berlin). ### Backport PRs 3.3: https://github.com/ruby/ruby/pull/19395 3.4: https://github.com/ruby/ruby/pull/19396 4.0: https://github.com/ruby/ruby/pull/19397 ---------------------------------------- Bug #22432: Range#include? returns true for the end of an empty range of single-character Strings https://bugs.ruby-lang.org/issues/22432#change-119551 * Author: sferik (Erik Berlin) * Status: Open * Backport: 3.3: REQUIRED, 3.4: REQUIRED, 4.0: REQUIRED ---------------------------------------- A Range whose ends are both single ASCII characters reports that it includes its end even when the range is empty. ```ruby r = "z".."a" r.to_a #=> [] r.include?("a") #=> true r.member?("a") #=> true r.cover?("a") #=> false r === "a" #=> false ``` I expect `include?` and `member?` to return false here, as `cover?` does and as iterating the range implies. The same happens with digits, for example `("1".."0").include?("0")`. Only the end is affected. Other values are correctly reported as not included: ```ruby ("z".."a").include?("z") #=> false ("z".."a").include?("m") #=> false ``` `Range#include?` has a shortcut for ranges of single-character Strings, which compares the characters instead of iterating. It is in `rb_str_include_range_p` in string.c: ```c if (ISASCII(b) && ISASCII(e) && ISASCII(v)) { if (b <= v && v < e) return Qtrue; return RBOOL(!RTEST(exclusive) && v == e); } ``` The last line returns true whenever the value equals the end, without checking that the begin is not after the end. -- https://bugs.ruby-lang.org/
Issue #22432 has been updated by nagachika (Tomoyuki Chikanaga). Backport changed from 3.3: REQUIRED, 3.4: REQUIRED, 4.0: REQUIRED to 3.3: REQUIRED, 3.4: DONE, 4.0: REQUIRED Thank you for creating PRs. Merged into `ruby_3_4`at https://github.com/ruby/ruby/commit/26a4d835f7512f0615f6e0d7ed483c3438373b7e. ---------------------------------------- Bug #22432: Range#include? returns true for the end of an empty range of single-character Strings https://bugs.ruby-lang.org/issues/22432#change-119557 * Author: sferik (Erik Berlin) * Status: Open * Backport: 3.3: REQUIRED, 3.4: DONE, 4.0: REQUIRED ---------------------------------------- A Range whose ends are both single ASCII characters reports that it includes its end even when the range is empty. ```ruby r = "z".."a" r.to_a #=> [] r.include?("a") #=> true r.member?("a") #=> true r.cover?("a") #=> false r === "a" #=> false ``` I expect `include?` and `member?` to return false here, as `cover?` does and as iterating the range implies. The same happens with digits, for example `("1".."0").include?("0")`. Only the end is affected. Other values are correctly reported as not included: ```ruby ("z".."a").include?("z") #=> false ("z".."a").include?("m") #=> false ``` `Range#include?` has a shortcut for ranges of single-character Strings, which compares the characters instead of iterating. It is in `rb_str_include_range_p` in string.c: ```c if (ISASCII(b) && ISASCII(e) && ISASCII(v)) { if (b <= v && v < e) return Qtrue; return RBOOL(!RTEST(exclusive) && v == e); } ``` The last line returns true whenever the value equals the end, without checking that the begin is not after the end. -- https://bugs.ruby-lang.org/
Issue #22432 has been updated by k0kubun (Takashi Kokubun). Backport changed from 3.3: REQUIRED, 3.4: DONE, 4.0: REQUIRED to 3.3: REQUIRED, 3.4: DONE, 4.0: DONE ruby_4_0 commit:4ba4954dd704bd08ebe3749fdfa3229344ddf5b1 merged revision(s) commit:1bf91309438d071359aa521328074cc6ec293299. ---------------------------------------- Bug #22432: Range#include? returns true for the end of an empty range of single-character Strings https://bugs.ruby-lang.org/issues/22432#change-120112 * Author: sferik (Erik Berlin) * Status: Open * Backport: 3.3: REQUIRED, 3.4: DONE, 4.0: DONE ---------------------------------------- A Range whose ends are both single ASCII characters reports that it includes its end even when the range is empty. ```ruby r = "z".."a" r.to_a #=> [] r.include?("a") #=> true r.member?("a") #=> true r.cover?("a") #=> false r === "a" #=> false ``` I expect `include?` and `member?` to return false here, as `cover?` does and as iterating the range implies. The same happens with digits, for example `("1".."0").include?("0")`. Only the end is affected. Other values are correctly reported as not included: ```ruby ("z".."a").include?("z") #=> false ("z".."a").include?("m") #=> false ``` `Range#include?` has a shortcut for ranges of single-character Strings, which compares the characters instead of iterating. It is in `rb_str_include_range_p` in string.c: ```c if (ISASCII(b) && ISASCII(e) && ISASCII(v)) { if (b <= v && v < e) return Qtrue; return RBOOL(!RTEST(exclusive) && v == e); } ``` The last line returns true whenever the value equals the end, without checking that the begin is not after the end. -- https://bugs.ruby-lang.org/
participants (4)
-
eightbitraptor (Matt V-H) -
k0kubun (Takashi Kokubun) -
nagachika (Tomoyuki Chikanaga) -
sferik (Erik Berlin)