Skip to content
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

Use single replacer for string escaping #22335

Merged
merged 1 commit into from
Mar 8, 2018

Conversation

weswigham
Copy link
Member

The two-stage replacement was making non-null characters look like null chracters to the second replacer, which is no good. Now all the replacing is handled within one replacer, which also does the lookahead for potential octalyness conflicts.

Fixes #21828

function getReplacement(c: string, offset: number, input: string) {
if (c.charCodeAt(0) === CharacterCodes.nullCharacter) {
const lookAhead = input.charCodeAt(offset + c.length);
if (lookAhead >= CharacterCodes._0 && lookAhead <= CharacterCodes._9) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This technically only needs to be 0 through 7 (isOctalDigit), but I'm fine with this.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Mar 6, 2018

By the way, I wrote this code back when we couldn't use JSON.stringify, so a decent amount of logic here could probably be replaced with the JSON.stringify + the special cases I mentioned above + str.slice(1, -1).

But that can be another PR.

@weswigham weswigham merged commit 45eaf42 into microsoft:master Mar 8, 2018
@weswigham weswigham deleted the robust-string-escape branch March 8, 2018 01:36
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants