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

Function cleanupSurroundingIdentical

cmp/report_slices.go:553–614  ·  view source on GitHub ↗

cleanupSurroundingIdentical scans through all unequal groups, and moves any leading sequence of equal elements to the preceding equal group and moves and trailing sequence of equal elements to the succeeding equal group. This is necessary since coalesceInterveningIdentical may coalesce edit groups

(groups []diffStats, eq func(i, j int) bool)

Source from the content-addressed store, hash-verified

551// {NumIdentical: 64}, // incremented by 10
552// ]
553func cleanupSurroundingIdentical(groups []diffStats, eq func(i, j int) bool) []diffStats {
554 var ix, iy int // indexes into sequence x and y
555 for i, ds := range groups {
556 // Handle equal group.
557 if ds.NumDiff() == 0 {
558 ix += ds.NumIdentical
559 iy += ds.NumIdentical
560 continue
561 }
562
563 // Handle unequal group.
564 nx := ds.NumIdentical + ds.NumRemoved + ds.NumModified
565 ny := ds.NumIdentical + ds.NumInserted + ds.NumModified
566 var numLeadingIdentical, numTrailingIdentical int
567 for j := 0; j < nx && j < ny && eq(ix+j, iy+j); j++ {
568 numLeadingIdentical++
569 }
570 for j := 0; j < nx && j < ny && eq(ix+nx-1-j, iy+ny-1-j); j++ {
571 numTrailingIdentical++
572 }
573 if numIdentical := numLeadingIdentical + numTrailingIdentical; numIdentical > 0 {
574 if numLeadingIdentical > 0 {
575 // Remove leading identical span from this group and
576 // insert it into the preceding group.
577 if i-1 >= 0 {
578 groups[i-1].NumIdentical += numLeadingIdentical
579 } else {
580 // No preceding group exists, so prepend a new group,
581 // but do so after we finish iterating over all groups.
582 defer func() {
583 groups = append([]diffStats{{Name: groups[0].Name, NumIdentical: numLeadingIdentical}}, groups...)
584 }()
585 }
586 // Increment indexes since the preceding group would have handled this.
587 ix += numLeadingIdentical
588 iy += numLeadingIdentical
589 }
590 if numTrailingIdentical > 0 {
591 // Remove trailing identical span from this group and
592 // insert it into the succeeding group.
593 if i+1 < len(groups) {
594 groups[i+1].NumIdentical += numTrailingIdentical
595 } else {
596 // No succeeding group exists, so append a new group,
597 // but do so after we finish iterating over all groups.
598 defer func() {
599 groups = append(groups, diffStats{Name: groups[len(groups)-1].Name, NumIdentical: numTrailingIdentical})
600 }()
601 }
602 // Do not increment indexes since the succeeding group will handle this.
603 }
604
605 // Update this group since some identical elements were removed.
606 nx -= numIdentical
607 ny -= numIdentical
608 groups[i] = diffStats{Name: ds.Name, NumRemoved: nx, NumInserted: ny}
609 }
610 ix += nx

Callers 1

formatDiffSliceMethod · 0.85

Calls 1

NumDiffMethod · 0.80

Tested by

no test coverage detected