-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Ensure strings are consumed as-is when using internal segment()
#13608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4c64fad
ensure we handle strings as-in
RobinMalfait 2c04245
update changelog
RobinMalfait 9615e46
drop unnecessary test
RobinMalfait 2a1a276
ensure we utilities and variants defined
RobinMalfait 1f6c327
add example test that parses with unbalanced brackets inside quotes
RobinMalfait 5bea4c3
improve changelog entry
RobinMalfait a5c9c5d
hoist comment
RobinMalfait File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ const OPEN_PAREN = 0x28 | |
const CLOSE_PAREN = 0x29 | ||
const OPEN_BRACKET = 0x5b | ||
const CLOSE_BRACKET = 0x5d | ||
const DOUBLE_QUOTE = 0x22 | ||
const SINGLE_QUOTE = 0x27 | ||
|
||
// This is a shared buffer that is used to keep track of the current nesting level | ||
// of parens, brackets, and braces. It is used to determine if a character is at | ||
|
@@ -30,10 +32,11 @@ export function segment(input: string, separator: string) { | |
let stackPos = 0 | ||
let parts: string[] = [] | ||
let lastPos = 0 | ||
let len = input.length | ||
|
||
let separatorCode = separator.charCodeAt(0) | ||
|
||
for (let idx = 0; idx < input.length; idx++) { | ||
for (let idx = 0; idx < len; idx++) { | ||
let char = input.charCodeAt(idx) | ||
|
||
if (stackPos === 0 && char === separatorCode) { | ||
|
@@ -47,6 +50,27 @@ export function segment(input: string, separator: string) { | |
// The next character is escaped, so we skip it. | ||
idx += 1 | ||
break | ||
// Strings should be handled as-is until the end of the string. No need to | ||
// worry about balancing parens, brackets, or curlies inside a string. | ||
case SINGLE_QUOTE: | ||
case DOUBLE_QUOTE: | ||
while ( | ||
// Ensure we don't go out of bounds. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might look less weird with the comment above the |
||
++idx < len | ||
) { | ||
let nextChar = input.charCodeAt(idx) | ||
|
||
// The next character is escaped, so we skip it. | ||
if (nextChar === BACKSLASH) { | ||
idx += 1 | ||
continue | ||
} | ||
|
||
if (nextChar === char) { | ||
break | ||
} | ||
} | ||
break | ||
case OPEN_PAREN: | ||
closingBracketStack[stackPos] = CLOSE_PAREN | ||
stackPos++ | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we change this to represent the user-facing fix? Maybe something more like "Support unbalanced parentheses and braces in quotes in arbitrary values and variants".