Commit 7c46f034 authored by Quentin Smith's avatar Quentin Smith

strconv: strip \r in raw strings passed to Unquote

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/31210Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
parent 6f4a6faf
......@@ -362,6 +362,16 @@ func Unquote(s string) (string, error) {
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 != '\'' {
......
......@@ -274,6 +274,7 @@ var unquotetests = []unQuoteTest{
{"`\n`", "\n"},
{"` `", ` `},
{"` `", ` `},
{"`a\rb`", "ab"},
}
var misquoted = []string{
......@@ -306,7 +307,7 @@ 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)
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment