Normalize negative indices in bounded array slice expressions - #1081
Open
fudianchn wants to merge 1 commit into
Open
Normalize negative indices in bounded array slice expressions#1081fudianchn wants to merge 1 commit into
fudianchn wants to merge 1 commit into
Conversation
ArraySliceToken.sliceBetween only clamped the upper bound (`to = Math.min(length, to)`), so a negative `from` or `to` was not resolved against the array length — unlike sliceFrom and sliceTo, which both do `index = length + index` for negatives. As a result e.g. `$[-2:4]` on a 5-element array looped from -2 and wrapped around to `[3,4,0,1,2,3]` (duplicated), and `$[1:-1]` returned an empty list (json-path#1076). Mirror the sibling normalization in sliceBetween so negative `from`/`to` are resolved against the length before slicing. Closes json-path#1076 Signed-off-by: 付典 <fudianchn@gmail.com>
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.
Problem
ArraySliceToken.sliceBetweenonly clamped the upper bound (to = Math.min(length, to)), so a negativefromortowas never resolved against the array length — unlikesliceFromandsliceTo, which both doindex = length + indexfor negatives.Consequently, e.g.
$[-2:4]on a 5-element array loopedifrom-2to3, and sincehandleArrayIndexwraps negatives Python-style, it produced[3,4,0,1,2,3](a duplicated/shifted result).$[1:-1]returned an empty list becauseMath.min(length, -1)madefrom >= to(#1076).Fix
Mirror the sibling normalization (
sliceFrom/sliceTo) insliceBetween:Verification
The slice logic in isolation (matching
sliceBetween+handleArrayIndex's negative wrap):Added
slice_between_with_negative_from/slice_between_with_negative_totoArraySlicingTest. (The repo's Gradle build was too heavy to run locally — the slice logic is verified above and the tests codify it for CI.)Closes #1076