summaryrefslogtreecommitdiff
path: root/range.c
diff options
context:
space:
mode:
authorNobuyoshi Nakada <[email protected]>2025-03-07 17:23:33 +0900
committerNobuyoshi Nakada <[email protected]>2025-03-07 17:23:33 +0900
commitcbe3156f82ee8b68e734be58badb9b6a3adc8aa6 ()
treea175cd5460bb7cd170d57ec6749f740989940f99 /range.c
parent8841f885bde7bbe571d2043830799059870dc70c (diff)
[Bug #21174] [Bug #21175] Fix `Range#max` on beginless integer range
Notes: Merged: https://.com/ruby/ruby/pull/12879
-rw-r--r--range.c72
1 files changed, 53 insertions, 19 deletions
@@ -1405,12 +1405,29 @@ range_first(int argc, VALUE *argv, VALUE range)
return ary[1];
}
static VALUE
rb_int_range_last(int argc, VALUE *argv, VALUE range)
{
static const VALUE ONE = INT2FIX(1);
- VALUE b, e, len_1, len, nv, ary;
int x;
long n;
@@ -1418,20 +1435,28 @@ rb_int_range_last(int argc, VALUE *argv, VALUE range)
b = RANGE_BEG(range);
e = RANGE_END(range);
- RUBY_ASSERT(RB_INTEGER_TYPE_P(b) && RB_INTEGER_TYPE_P(e));
x = EXCL(range);
- len_1 = rb_int_minus(e, b);
- if (x) {
- e = rb_int_minus(e, ONE);
- len = len_1;
}
else {
- len = rb_int_plus(len_1, ONE);
}
- if (FIXNUM_ZERO_P(len) || rb_num_negative_p(len)) {
return rb_ary_new_capa(0);
}
@@ -1442,7 +1467,7 @@ rb_int_range_last(int argc, VALUE *argv, VALUE range)
}
nv = LONG2NUM(n);
- if (RTEST(rb_int_gt(nv, len))) {
nv = len;
n = NUM2LONG(nv);
}
@@ -1496,17 +1521,11 @@ rb_int_range_last(int argc, VALUE *argv, VALUE range)
static VALUE
range_last(int argc, VALUE *argv, VALUE range)
{
- VALUE b, e;
-
if (NIL_P(RANGE_END(range))) {
rb_raise(rb_eRangeError, "cannot get the last element of endless range");
}
if (argc == 0) return RANGE_END(range);
-
- b = RANGE_BEG(range);
- e = RANGE_END(range);
- if (RB_INTEGER_TYPE_P(b) && RB_INTEGER_TYPE_P(e) &&
- RB_LIKELY(rb_method_basic_definition_p(rb_cRange, idEach))) {
return rb_int_range_last(argc, argv, range);
}
return rb_ary_last(argc, argv, rb_Array(range));
@@ -1714,12 +1733,27 @@ range_max(int argc, VALUE *argv, VALUE range)
VALUE b = RANGE_BEG(range);
- if (rb_block_given_p() || (EXCL(range) && !nm) || argc) {
if (NIL_P(b)) {
rb_raise(rb_eRangeError, "cannot get the maximum of beginless range with custom comparison method");
}
return rb_call_super(argc, argv);
}
else {
int c = NIL_P(b) ? -1 : OPTIMIZED_CMP(b, e);
@@ -1730,13 +1764,13 @@ range_max(int argc, VALUE *argv, VALUE range)
rb_raise(rb_eTypeError, "cannot exclude non Integer end value");
}
if (c == 0) return Qnil;
- if (!RB_INTEGER_TYPE_P(b)) {
rb_raise(rb_eTypeError, "cannot exclude end value with non Integer begin value");
}
if (FIXNUM_P(e)) {
return LONG2NUM(FIX2LONG(e) - 1);
}
- return rb_funcall(e, '-', 1, INT2FIX(1));
}
return e;
}