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>
This commit is contained in:
Jeremy Evans 2022-08-11 03:16:49 -07:00 committed by GitHub
parent 32d1ce96e0
commit cfb9624460
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
Notes: git 2022-08-11 19:17:12 +09:00
Merged-By: mrkn <mrkn@ruby-lang.org>
8 changed files with 431 additions and 1 deletions

View file

@ -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;