Also reject literal newline in " and ' quoted strings.
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/
5139045
if quote != '"' && quote != '\'' {
return "", os.EINVAL
}
+ if strings.Index(s, "\n") >= 0 {
+ return "", os.EINVAL
+ }
+
+ // Is it trivial? Avoid allocation.
+ if strings.Index(s, `\`) < 0 && strings.IndexRune(s, int(quote)) < 0 {
+ switch quote {
+ case '"':
+ return s, nil
+ case '\'':
+ r, size := utf8.DecodeRuneInString(s)
+ if size == len(s) && (r != utf8.RuneError || size != 1) {
+ return s, nil
+ }
+ }
+ }
var buf bytes.Buffer
for len(s) > 0 {
{"`\\xFF`", `\xFF`},
{"`\\377`", `\377`},
{"`\\`", `\`},
+ {"`\n`", "\n"},
{"` `", ` `},
{"` `", ` `},
}
"`\"",
`"\'"`,
`'\"'`,
+ "\"\n\"",
+ "\"\\n\n\"",
+ "'\n'",
}
func TestUnquote(t *testing.T) {
}
}
}
+
+func BenchmarkUnquoteEasy(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ Unquote(`"Give me a rock, paper and scissors and I will move the world."`)
+ }
+}
+
+func BenchmarkUnquoteHard(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ Unquote(`"\x47ive me a \x72ock, \x70aper and \x73cissors and \x49 will move the world."`)
+ }
+}