Skip to content

Commit

Permalink
Use raw string literal syntax only for valid UTF-8 (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsnet committed Jun 10, 2020
1 parent 88849e8 commit a171aa7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
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

0 comments on commit a171aa7

Please sign in to comment.