This repository was archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Handle quoting U+0000 properly #64
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
535f594
add tests to highligh null quoting issue
bzz 399f6ed
test: add fixtures to reproduce the failure
bzz 7d784b9
strconv: porting more details from stdlib
bzz 9799217
test: refactor to table
bzz a769379
sem: fix processing \0 escaped sequence in string literal
bzz bda63b1
sem: update semantic annotations after the fix
bzz 631f385
review: replace copy-paste \w stdlib call
bzz 2527d5a
test: refactoring to simplify reverse case
bzz ea68ea3
review: replace strconv.ErrSyntax
bzz fc24125
review: more tests for corner cases
bzz ec16186
test: adjust for quote/unquote asymmetry
bzz 8c08ccf
unqoteSingle: switch to simple strings-based impl
bzz c521aea
review: update godoc
creachadair 8659709
doc: better wording though review
bzz 5c5255c
test: refactoring to use subtests
bzz 3f88050
test: refactoring benchmark
bzz 9085dab
test: drop experimental benchmark
bzz bfd8c9b
test: cosmetic - consistent string literal syntax
bzz 7f92d74
test: better doc for the canonicalQuoted cases
bzz e7a074f
test: refactor benchmark, breaking down by testcase
bzz 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ | |
.sdk | ||
.config | ||
build | ||
node_modules | ||
.vscode |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package normalizer | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var testCasesUnquote = []struct { | ||
quoted string | ||
unquoted string | ||
// In some cases unquoting and then re-quoting a quoted string does not produce a | ||
// string that is bitwise identical to the original, even though they denote the same bytes. | ||
// This can happen, e.g, if we switch between hex and octal encoding of a byte. | ||
// Test cases where this happens set canonicalUnquoted to the string that is expected | ||
// to be decoded via Go's native rules to the byte sequence we want. | ||
canonicalQuoted string | ||
}{ | ||
{`'a'`, "a", `'a'`}, | ||
{`'\x00'`, "\u0000", `'\x00'`}, | ||
{`'\0'`, "\u0000", "'\\x00'"}, | ||
{`'\0something\0'`, "\u0000something\u0000", "'\\x00something\\x00'"}, | ||
{`'\0something\0else'`, "\u0000something\u0000else", "'\\x00something\\x00else'"}, | ||
{`'\u0000123\0s'`, "\u0000123\u0000s", "'\\x00123\\x00s'"}, | ||
} | ||
|
||
func TestUnquoteSingle(t *testing.T) { | ||
for _, test := range testCasesUnquote { | ||
t.Run("", func(t *testing.T) { | ||
s, err := unquoteSingle(test.quoted) | ||
require.NoError(t, err) | ||
require.Equal(t, test.unquoted, s) | ||
}) | ||
} | ||
} | ||
|
||
func TestUnquoteSingleAndQuoteBack(t *testing.T) { | ||
for _, test := range testCasesUnquote { | ||
t.Run("", func(t *testing.T) { | ||
u, err := unquoteSingle(test.quoted) | ||
require.NoError(t, err) | ||
|
||
q := quoteSingle(u) | ||
assertEquals(t, test.canonicalQuoted, q) | ||
}) | ||
} | ||
} | ||
|
||
func assertEquals(t *testing.T, quoted, actual string) { | ||
if !assert.Equal(t, quoted, actual) { | ||
printDebug(t, quoted, actual) | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func printDebug(t *testing.T, quoted, actual string) { | ||
t.Logf("\texpected: len=%d", len(quoted)) | ||
for _, c := range quoted { | ||
t.Logf("%x - %#U", c, c) | ||
} | ||
t.Logf("\n\tactual: len=%d", len(actual)) | ||
for _, c := range actual { | ||
t.Logf("%x - %#U", c, c) | ||
} | ||
} | ||
|
||
func BenchmarkReplacingNullEscape(b *testing.B) { | ||
for _, test := range testCasesUnquote { | ||
b.Run("", func(b *testing.B) { | ||
b.ReportAllocs() | ||
for n := 0; n < b.N; n++ { | ||
replaceEscapedMaybe(test.quoted, "\\0", "\x00") | ||
} | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
a = "a" | ||
u8 = "ё" | ||
bc = "b\nc" | ||
|
||
var escSlash = '\0SLASH'+Math.random()+'\0'; | ||
var escOpen = '\0OPEN'+Math.random()+'\0'; | ||
var escClose = '\0CLOSE'+Math.random()+'\0'; | ||
var escComma = '\0COMMA'+Math.random()+'\0'; | ||
var escPeriod = '\0PERIOD'+Math.random()+'\0'; |
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.
Uh oh!
There was an error while loading. Please reload this page.