Skip to content

Commit a97318b

Browse files
dsnetneild
andauthored
Adjust heuristic for line-based versus byte-based diffing (#299)
If the string has many characters that require escape sequences to print, then we need to take that into consideration and avoid byte-by-byte diffing. Co-authored-by: Damien Neil <[email protected]>
1 parent 377d283 commit a97318b

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

cmp/compare_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,23 @@ using the AllowUnexported option.`, "\n"),
14031403
[]byte("\xffoo"), []byte("foo"), []byte("barbaz"), []byte("added"), []byte("here"), []byte("hrmph\xff"),
14041404
},
14051405
reason: "should print text byte slices as strings except those with binary",
1406+
}, {
1407+
label: label + "/ManyEscapeCharacters",
1408+
x: `[
1409+
{"Base32": "NA======"},
1410+
{"Base32": "NBSQ===="},
1411+
{"Base32": "NBSWY==="},
1412+
{"Base32": "NBSWY3A="},
1413+
{"Base32": "NBSWY3DP"}
1414+
]`,
1415+
y: `[
1416+
{"Base32": "NB======"},
1417+
{"Base32": "NBSQ===="},
1418+
{"Base32": "NBSWY==="},
1419+
{"Base32": "NBSWY3A="},
1420+
{"Base32": "NBSWY3DP"}
1421+
]`,
1422+
reason: "should use line-based diffing since byte-based diffing is unreadable due to heavy amounts of escaping",
14061423
}}
14071424
}
14081425

cmp/report_slices.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ func (opts formatOptions) FormatDiffSlice(v *valueNode) textNode {
147147
})
148148
efficiencyLines := float64(esLines.Dist()) / float64(len(esLines))
149149
efficiencyBytes := float64(esBytes.Dist()) / float64(len(esBytes))
150-
isPureLinedText = efficiencyLines < 4*efficiencyBytes
150+
quotedLength := len(strconv.Quote(sx + sy))
151+
unquotedLength := len(sx) + len(sy)
152+
escapeExpansionRatio := float64(quotedLength) / float64(unquotedLength)
153+
isPureLinedText = efficiencyLines < 4*efficiencyBytes || escapeExpansionRatio > 1.1
151154
}
152155
}
153156

cmp/testdata/diffs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,18 @@
11821182
+ {0x68, 0x72, 0x6d, 0x70, 0x68, 0xff},
11831183
}
11841184
>>> TestDiff/Reporter/SliceOfBytesBinary
1185+
<<< TestDiff/Reporter/ManyEscapeCharacters
1186+
(
1187+
"""
1188+
[
1189+
- {"Base32": "NA======"},
1190+
+ {"Base32": "NB======"},
1191+
{"Base32": "NBSQ===="},
1192+
{"Base32": "NBSWY==="},
1193+
... // 3 identical lines
1194+
"""
1195+
)
1196+
>>> TestDiff/Reporter/ManyEscapeCharacters
11851197
<<< TestDiff/EmbeddedStruct/ParentStructA/Inequal
11861198
teststructs.ParentStructA{
11871199
privateStruct: teststructs.privateStruct{

0 commit comments

Comments
 (0)