Skip to content
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

Use raw string literal syntax only for valid UTF-8 #211

Merged
merged 1 commit into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmp/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,11 @@ func reporterTests() []test {
)

return []test{{
label: "/InvalidUTF8",
x: MyString("\xed\xa0\x80"),
wantEqual: false,
reason: "invalid UTF-8 should format as quoted string",
}, {
label: label,
x: MyComposite{IntsA: []int8{11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}},
y: MyComposite{IntsA: []int8{10, 11, 21, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}},
Expand Down
3 changes: 2 additions & 1 deletion cmp/report_reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"
"strings"
"unicode"
"unicode/utf8"

"github.com/google/go-cmp/cmp/internal/flags"
"github.com/google/go-cmp/cmp/internal/value"
Expand Down Expand Up @@ -235,7 +236,7 @@ func formatString(s string) string {
rawInvalid := func(r rune) bool {
return r == '`' || r == '\n' || !(unicode.IsPrint(r) || r == '\t')
}
if strings.IndexFunc(s, rawInvalid) < 0 {
if utf8.ValidString(s) && strings.IndexFunc(s, rawInvalid) < 0 {
return "`" + s + "`"
}
return qs
Expand Down
5 changes: 5 additions & 0 deletions cmp/testdata/diffs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@
})),
}
>>> TestDiff/Transformer#05
<<< TestDiff//InvalidUTF8
interface{}(
- cmp_test.MyString("\xed\xa0\x80"),
)
>>> TestDiff//InvalidUTF8
<<< TestDiff/Reporter
cmp_test.MyComposite{
... // 3 identical fields
Expand Down