MCPcopy
hub / github.com/google/go-cmp / formatDiffSlice

Method formatDiffSlice

cmp/report_slices.go:357–439  ·  cmp/report_slices.go::formatOptions.formatDiffSlice
(
	vx, vy reflect.Value, chunkSize int, name string,
	makeRec func(reflect.Value, diffMode) textRecord,
)

Source from the content-addressed store, hash-verified

355}
356
357func (opts formatOptions) formatDiffSlice(
358 vx, vy reflect.Value, chunkSize int, name string,
359 makeRec func(reflect.Value, diffMode) textRecord,
360) (list textList) {
361 eq := func(ix, iy int) bool {
362 return vx.Index(ix).Interface() == vy.Index(iy).Interface()
363 }
364 es := diff.Difference(vx.Len(), vy.Len(), func(ix, iy int) diff.Result {
365 return diff.BoolResult(eq(ix, iy))
366 })
367
368 appendChunks := func(v reflect.Value, d diffMode) int {
369 n0 := v.Len()
370 for v.Len() > 0 {
371 n := chunkSize
372 if n > v.Len() {
373 n = v.Len()
374 }
375 list = append(list, makeRec(v.Slice(0, n), d))
376 v = v.Slice(n, v.Len())
377 }
378 return n0 - v.Len()
379 }
380
381 var numDiffs int
382 maxLen := -1
383 if opts.LimitVerbosity {
384 maxLen = (1 << opts.verbosity()) << 2 // 4, 8, 16, 32, 64, etc...
385 opts.VerbosityLevel--
386 }
387
388 groups := coalesceAdjacentEdits(name, es)
389 groups = coalesceInterveningIdentical(groups, chunkSize/4)
390 groups = cleanupSurroundingIdentical(groups, eq)
391 maxGroup := diffStats{Name: name}
392 for i, ds := range groups {
393 if maxLen >= 0 && numDiffs >= maxLen {
394 maxGroup = maxGroup.Append(ds)
395 continue
396 }
397
398 // Print equal.
399 if ds.NumDiff() == 0 {
400 // Compute the number of leading and trailing equal bytes to print.
401 var numLo, numHi int
402 numEqual := ds.NumIgnored + ds.NumIdentical
403 for numLo < chunkSize*numContextRecords && numLo+numHi < numEqual && i != 0 {
404 numLo++
405 }
406 for numHi < chunkSize*numContextRecords && numLo+numHi < numEqual && i != len(groups)-1 {
407 numHi++
408 }
409 if numEqual-(numLo+numHi) <= chunkSize && ds.NumIgnored == 0 {
410 numHi = numEqual - numLo // Avoid pointless coalescing of single equal row
411 }
412
413 // Print the equal bytes.
414 appendChunks(vx.Slice(0, numLo), diffIdentical)

Callers 1

FormatDiffSliceMethod · 0.95

Calls 13

verbosityMethod · 0.95
AppendMethod · 0.95
IsZeroMethod · 0.95
DifferenceFunction · 0.92
BoolResultFunction · 0.92
coalesceAdjacentEditsFunction · 0.85
assertFunction · 0.85
NumDiffMethod · 0.80
AppendEllipsisMethod · 0.80
LenMethod · 0.65

Tested by

no test coverage detected