diff options
author | Jeremy Evans <code@jeremyevans.net> | 2022-08-11 03:16:49 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-11 19:16:49 +0900 |
commit | cfb9624460a295e4e1723301486d89058c228e07 (patch) | |
tree | c1fdcf97204e964f3f79cfc1d40f73473666f1ea /enumerator.c | |
parent | 32d1ce96e09773e809d575c17b916012d88d6ffc (diff) |
Fix Array#[] with ArithmeticSequence with negative steps (#5739)
* Fix Array#[] with ArithmeticSequence with negative steps
Previously, Array#[] when called with an ArithmeticSequence
with a negative step did not handle all cases correctly,
especially cases involving infinite ranges, inverted ranges,
and/or exclusive ends.
Fixes [Bug #18247]
* Add Array#slice tests for ArithmeticSequence with negative step to test_array
Add tests of rb_arithmetic_sequence_beg_len_step C-API function.
* Fix ext/-test-/arith_seq/beg_len_step/depend
* Rename local variables
* Fix a variable name
Co-authored-by: Kenta Murata <3959+mrkn@users.noreply.github.com>
Notes
Notes:
Merged-By: mrkn <mrkn@ruby-lang.org>
Diffstat (limited to 'enumerator.c')
-rw-r--r-- | enumerator.c | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/enumerator.c b/enumerator.c index d7546ee9e8..2c9858cda6 100644 --- a/enumerator.c +++ b/enumerator.c @@ -3802,6 +3802,13 @@ rb_arithmetic_sequence_beg_len_step(VALUE obj, long *begp, long *lenp, long *ste *stepp = step; if (step < 0) { + if (aseq.exclude_end && !NIL_P(aseq.end)) { + /* Handle exclusion before range reversal */ + aseq.end = LONG2NUM(NUM2LONG(aseq.end) + 1); + + /* Don't exclude the previous beginning */ + aseq.exclude_end = 0; + } VALUE tmp = aseq.begin; aseq.begin = aseq.end; aseq.end = tmp; |