To match the language spec, strconv.Unquote needs to strip carriage
returns from the raw string.
Also fixes TestUnquote to not be a noop.
Fixes #15997
Change-Id: I2456f50f2ad3830f37e545f4f6774ced9fe609d7
Reviewed-on: https://go-review.googlesource.com/31210
Reviewed-by: Robert Griesemer <gri@golang.org>
if contains(s, '`') {
return "", ErrSyntax
}
+ if contains(s, '\r') {
+ // -1 because we know there is at least one \r to remove.
+ buf := make([]byte, 0, len(s)-1)
+ for i := 0; i < len(s); i++ {
+ if s[i] != '\r' {
+ buf = append(buf, s[i])
+ }
+ }
+ return string(buf), nil
+ }
return s, nil
}
if quote != '"' && quote != '\'' {
{"`\n`", "\n"},
{"` `", ` `},
{"` `", ` `},
+ {"`a\rb`", "ab"},
}
var misquoted = []string{
func TestUnquote(t *testing.T) {
for _, tt := range unquotetests {
- if out, err := Unquote(tt.in); err != nil && out != tt.out {
+ if out, err := Unquote(tt.in); err != nil || out != tt.out {
t.Errorf("Unquote(%#q) = %q, %v want %q, nil", tt.in, out, err, tt.out)
}
}