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

Adjust heuristic for line-based versus byte-based diffing #299

Merged
merged 2 commits into from Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 17 additions & 0 deletions cmp/compare_test.go
Expand Up @@ -1403,6 +1403,23 @@ using the AllowUnexported option.`, "\n"),
[]byte("\xffoo"), []byte("foo"), []byte("barbaz"), []byte("added"), []byte("here"), []byte("hrmph\xff"),
},
reason: "should print text byte slices as strings except those with binary",
}, {
label: label + "/ManyEscapeCharacters",
x: `[
{"Base32": "NA======"},
{"Base32": "NBSQ===="},
{"Base32": "NBSWY==="},
{"Base32": "NBSWY3A="},
{"Base32": "NBSWY3DP"}
]`,
y: `[
{"Base32": "NB======"},
{"Base32": "NBSQ===="},
{"Base32": "NBSWY==="},
{"Base32": "NBSWY3A="},
{"Base32": "NBSWY3DP"}
]`,
reason: "should use line-based diffing since byte-based diffing is unreadable due to heavy amounts of escaping",
}}
}

Expand Down
5 changes: 4 additions & 1 deletion cmp/report_slices.go
Expand Up @@ -147,7 +147,10 @@ func (opts formatOptions) FormatDiffSlice(v *valueNode) textNode {
})
efficiencyLines := float64(esLines.Dist()) / float64(len(esLines))
efficiencyBytes := float64(esBytes.Dist()) / float64(len(esBytes))
isPureLinedText = efficiencyLines < 4*efficiencyBytes
quotedLength := len(strconv.Quote(sx + sy))
unquotedLength := len(sx) + len(sy)
escapeExpansionRatio := float64(quotedLength) / float64(unquotedLength)
isPureLinedText = efficiencyLines < 4*efficiencyBytes || escapeExpansionRatio > 1.1
}
}

Expand Down
12 changes: 12 additions & 0 deletions cmp/testdata/diffs
Expand Up @@ -1182,6 +1182,18 @@
+ {0x68, 0x72, 0x6d, 0x70, 0x68, 0xff},
}
>>> TestDiff/Reporter/SliceOfBytesBinary
<<< TestDiff/Reporter/ManyEscapeCharacters
(
"""
[
- {"Base32": "NA======"},
+ {"Base32": "NB======"},
{"Base32": "NBSQ===="},
{"Base32": "NBSWY==="},
... // 3 identical lines
"""
)
>>> TestDiff/Reporter/ManyEscapeCharacters
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This used to print:

  strings.Join({
  	"[\n\t{\"Base32\": \"N",
- 	"A",
+ 	"B",
  	"======\"},\n\t{\"Base32\": \"NBSQ====\"},\n\t{\"Base32\": \"NBSWY===\"},\n\t{\"B",
  	"ase32\": \"NBSWY3A=\"},\n\t{\"Base32\": \"NBSWY3DP\"}\n]",
  }, "")

<<< TestDiff/EmbeddedStruct/ParentStructA/Inequal
teststructs.ParentStructA{
privateStruct: teststructs.privateStruct{
Expand Down