-
Notifications
You must be signed in to change notification settings - Fork 688
Add escape sequence support for string literals (\t, \n, \r, etc.) #2331
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
Draft
Copilot
wants to merge
3
commits into
develop
Choose a base branch
from
copilot/add-special-characters-support
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1460,9 +1460,37 @@ LogicValue | |
|
|
||
| StringValue | ||
| : STRING | ||
| { $$ = new yy.StringValue({value: $1.substr(1,$1.length-2).replace(/(\\\')/g,"'").replace(/(\'\')/g,"'")}); } | ||
| { | ||
| var str = $1.substr(1,$1.length-2).replace(/(\'\')/g,"'"); | ||
| // Process escape sequences: \n \t \r \\ \' \" | ||
| str = str.replace(/\\(n|t|r|\\|'|")/g, function(match, char) { | ||
| switch(char) { | ||
| case 'n': return '\n'; | ||
| case 't': return '\t'; | ||
| case 'r': return '\r'; | ||
| case '\\': return '\\'; | ||
|
Member
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. Please check if this is being solved via #2335 |
||
| case "'": return "'"; | ||
| case '"': return '"'; | ||
mathiasrw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| }); | ||
| $$ = new yy.StringValue({value: str}); | ||
| } | ||
| | NSTRING | ||
| { $$ = new yy.StringValue({value: $1.substr(2,$1.length-3).replace(/(\\\')/g,"'").replace(/(\'\')/g,"'")}); } | ||
| { | ||
| var str = $1.substr(2,$1.length-3).replace(/(\'\')/g,"'"); | ||
| // Process escape sequences: \n \t \r \\ \' \" | ||
| str = str.replace(/\\(n|t|r|\\|'|")/g, function(match, char) { | ||
| switch(char) { | ||
| case 'n': return '\n'; | ||
| case 't': return '\t'; | ||
| case 'r': return '\r'; | ||
| case '\\': return '\\'; | ||
| case "'": return "'"; | ||
| case '"': return '"'; | ||
| } | ||
| }); | ||
| $$ = new yy.StringValue({value: str}); | ||
| } | ||
| ; | ||
|
|
||
| NullValue | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| if (typeof exports === 'object') { | ||
| var assert = require('assert'); | ||
| var alasql = require('..'); | ||
| } | ||
|
|
||
| describe('Test 134-B - Escape sequences in strings', function () { | ||
| const test = '134B'; | ||
|
|
||
| before(function () { | ||
| alasql('create database test' + test); | ||
| alasql('use test' + test); | ||
| }); | ||
|
|
||
| after(function () { | ||
| alasql('drop database test' + test); | ||
| }); | ||
|
|
||
| it('A) Tab escape sequence', function () { | ||
| var res = alasql("SELECT 'hello\\tworld' AS result"); | ||
| assert.deepEqual(res, [{result: 'hello\tworld'}]); | ||
| }); | ||
|
|
||
| it('B) Newline escape sequence', function () { | ||
| var res = alasql("SELECT 'line1\\nline2' AS result"); | ||
| assert.deepEqual(res, [{result: 'line1\nline2'}]); | ||
| }); | ||
|
|
||
| it('C) Carriage return escape sequence', function () { | ||
| var res = alasql("SELECT 'text\\rmore' AS result"); | ||
| assert.deepEqual(res, [{result: 'text\rmore'}]); | ||
| }); | ||
|
|
||
| it('D) Backslash escape sequence', function () { | ||
| var res = alasql("SELECT 'back\\\\slash' AS result"); | ||
| assert.deepEqual(res, [{result: 'back\\slash'}]); | ||
| }); | ||
|
|
||
| it('E) Double quote escape sequence', function () { | ||
| var res = alasql('SELECT "quote\\"here" AS result'); | ||
| assert.deepEqual(res, [{result: 'quote"here'}]); | ||
| }); | ||
|
|
||
| it('F) Mixed escape sequences', function () { | ||
| var res = alasql("SELECT 'Line 1\\nLine 2\\tTabbed\\rCarriage' AS result"); | ||
| assert.deepEqual(res, [{result: 'Line 1\nLine 2\tTabbed\rCarriage'}]); | ||
| }); | ||
|
|
||
| it('G) Escape sequence in WHERE clause', function () { | ||
| alasql('CREATE TABLE test_escapes (id INT, text STRING)'); | ||
| alasql("INSERT INTO test_escapes VALUES (1, 'hello\\tworld')"); | ||
| alasql("INSERT INTO test_escapes VALUES (2, 'no tabs here')"); | ||
|
|
||
| var res = alasql("SELECT * FROM test_escapes WHERE text = 'hello\\tworld'"); | ||
| assert.deepEqual(res, [{id: 1, text: 'hello\tworld'}]); | ||
| }); | ||
|
|
||
| it('H) Single quote still works', function () { | ||
| var res = alasql("SELECT 'Cote d\\'Ivoir' AS result"); | ||
| assert.deepEqual(res, [{result: "Cote d'Ivoir"}]); | ||
|
|
||
| var res2 = alasql("SELECT 'Cote d''Ivoir' AS result"); | ||
| assert.deepEqual(res2, [{result: "Cote d'Ivoir"}]); | ||
| }); | ||
| }); |
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.
@copilot
What is we have a string of data that contains \n, will it then get converted?
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.
Yes, this is the correct behavior. When data contains actual newline characters (after SQL parsing converts
\nto a newline),escapeq()converts them back to\nwhen generating JavaScript code. This ensures the generated JS code is syntactically valid.Flow:
'hello\nworld'→ Parser converts to actual newline → Stored in memory ashello<LF>worldescapeq()converts back to\n→ Generated code:"hello\nworld"Without
escapeq(), strings with newlines/quotes would produce invalid JavaScript syntax.