Repository navigation
Fix slicing with a negative step panicking and returning wrong items - #1002
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Slicing with a negative step computed its bounds differently from Python:
[][::-1]and""[::-1]panic with "index out of bounds: the len is 0".stopof0(or a negativestopthat clamps to0) was treated like a missing stop, so[1, 2, 3][:0:-1]returned[3, 2, 1]instead of[3, 2], andx[:-10:-1]on a list of 10 items included the first item.startpast the beginning, e.g.[1, 2, 3][-100::-1], returned[1]instead of[].range_step_backwardsnow clampsstartandstoplike Python (to-1..=len - 1) and walks down fromstarttostop, excludingstop. Positive steps are unchanged.The new lines in
tests/inputs/slicing.txtcover these cases; without the fixtest_vmpanics inops.rs. I also compared the result against Python's slicing for lists of length 0, 1, 2 and 5 with all combinations ofstart/stopinNone, -12, -6, -5, -4, -1, 0, 1, 2, 4, 5, 6, 12and steps-1, -2, -3, -7, 1, 2, 3(4732 cases) and they all match. The Go port already behaved like Python here.