Repository navigation
Fix slice when beginIndex is negative and endIndex <= 0 - #3392
Open
waw666waw666 wants to merge 1 commit into
Open
waw666waw666 wants to merge 1 commit into
waw666waw666 wants to merge 1 commit into
Conversation
Stream.slice and Array.slice compared the raw beginIndex with endIndex before clamping it to 0, so a call like slice(-1, 0) was not treated as empty. Stream returned its head ([1] for Stream.of(1, 2, 3)) and Array threw NegativeArraySizeException for slice(-3, -1). Clamp first, then check.
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.
Seq.sliceclamps a negativebeginIndexto 0, soslice(-1, 0)should be empty, like it is forList,Vector,QueueandCharSeq. Two implementations get this wrong:Both check
beginIndex >= endIndexbefore clampingbeginIndex. WithbeginIndex = -1, endIndex = 0the check passes,Streamthen conses the head, andArrayallocates an array of sizemin(endIndex, length) - 0, which is negative whenendIndexis negative.The fix clamps first and compares the clamped index with
endIndex. InArraythe "whole array" shortcut now uses the clamped index too.Added
shouldComputeSliceWhenBeginIndexIsNegativeAndEndIndexIsNotPositivetoAbstractSeqTest, so it runs for everySeq. It fails on main forStreamTest(wrong result) andArrayTest(exception) and passes with the fix../mvnw -pl vavr verify: 23477 tests, 0 failures;spotless:checkpasses.