diff options
author | Jeremy Evans <code@jeremyevans.net> | 2022-04-21 19:46:17 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2022-06-06 09:59:22 -0700 |
commit | c5475f42694eff35465c3332e0182c0611ca5918 (patch) | |
tree | 580a6b99eac40d94b8b6c528f70d11aff9e9dbff /range.c | |
parent | 7cda7fbbdc14f4262afaa94cdeb5a5987f1eb01a (diff) |
Fix Range#cover? returning true for beginless ranges of different types
Previously `(2..).cover?("2"..)` was false, but
`(..2).cover?(.."2")` was true. This changes it so both are false,
treating beginless ranges the same as endless ranges in regards to
type checks.
This also adds documentation to #cover? to describe behavior with
beginless and endless ranges, testing each documentation example,
which is how this bug was found.
Fixes [Bug #18155]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/5831
Diffstat (limited to 'range.c')
-rw-r--r-- | range.c | 54 |
1 files changed, 53 insertions, 1 deletions
@@ -1888,6 +1888,49 @@ static int r_cover_range_p(VALUE range, VALUE beg, VALUE end, VALUE val); * - An internal call to <tt><=></tt> returns +nil+; * that is, the operands are not comparable. * + * Beginless ranges cover all values of the same type before the end, + * excluding the end for exclusive ranges. Beginless ranges cover + * ranges that end before the end of the beginless range, or at the + * end of the beginless range for inclusive ranges. + * + * (..2).cover?(1) # => true + * (..2).cover?(2) # => true + * (..2).cover?(3) # => false + * (...2).cover?(2) # => false + * (..2).cover?("2") # => false + * (..2).cover?(..2) # => true + * (..2).cover?(...2) # => true + * (..2).cover?(.."2") # => false + * (...2).cover?(..2) # => false + * + * Endless ranges cover all values of the same type after the + * beginning. Endless exclusive ranges do not cover endless + * inclusive ranges. + * + * (2..).cover?(1) # => false + * (2..).cover?(3) # => true + * (2...).cover?(3) # => true + * (2..).cover?(2) # => true + * (2..).cover?("2") # => false + * (2..).cover?(2..) # => true + * (2..).cover?(2...) # => true + * (2..).cover?("2"..) # => false + * (2...).cover?(2..) # => false + * (2...).cover?(3...) # => true + * (2...).cover?(3..) # => false + * (3..).cover?(2..) # => false + * + * Ranges that are both beginless and endless cover all values and + * ranges, and return true for all arguments, with the exception that + * beginless and endless exclusive ranges do not cover endless + * inclusive ranges. + * + * (nil...).cover?(Object.new) # => true + * (nil...).cover?(nil...) # => true + * (nil..).cover?(nil...) # => true + * (nil...).cover?(nil..) # => false + * (nil...).cover?(1..) # => false + * * Related: Range#include?. * */ @@ -1926,7 +1969,16 @@ r_cover_range_p(VALUE range, VALUE beg, VALUE end, VALUE val) if (!NIL_P(val_beg) && !NIL_P(val_end) && r_less(val_beg, val_end) > (EXCL(val) ? -1 : 0)) return FALSE; if (!NIL_P(val_beg) && !r_cover_p(range, beg, end, val_beg)) return FALSE; - cmp_end = r_less(end, val_end); + + if (!NIL_P(val_end) && !NIL_P(end)) { + VALUE r_cmp_end = rb_funcall(end, id_cmp, 1, val_end); + if (NIL_P(r_cmp_end)) return FALSE; + cmp_end = rb_cmpint(r_cmp_end, end, val_end); + } + else { + cmp_end = r_less(end, val_end); + } + if (EXCL(range) == EXCL(val)) { return cmp_end >= 0; |