MCPcopy Create free account
hub / github.com/sergi/go-diff / DiffXIndex

Method DiffXIndex

diffmatchpatch/diff.go:1085–1115  ·  view source on GitHub ↗

DiffXIndex returns the equivalent location in s2.

(diffs []Diff, loc int)

Source from the content-addressed store, hash-verified

1083
1084// DiffXIndex returns the equivalent location in s2.
1085func (dmp *DiffMatchPatch) DiffXIndex(diffs []Diff, loc int) int {
1086 chars1 := 0
1087 chars2 := 0
1088 lastChars1 := 0
1089 lastChars2 := 0
1090 lastDiff := Diff{}
1091 for i := 0; i < len(diffs); i++ {
1092 aDiff := diffs[i]
1093 if aDiff.Type != DiffInsert {
1094 // Equality or deletion.
1095 chars1 += len(aDiff.Text)
1096 }
1097 if aDiff.Type != DiffDelete {
1098 // Equality or insertion.
1099 chars2 += len(aDiff.Text)
1100 }
1101 if chars1 > loc {
1102 // Overshot the location.
1103 lastDiff = aDiff
1104 break
1105 }
1106 lastChars1 = chars1
1107 lastChars2 = chars2
1108 }
1109 if lastDiff.Type == DiffDelete {
1110 // The location was deleted.
1111 return lastChars2
1112 }
1113 // Add the remaining character length.
1114 return lastChars2 + (loc - lastChars1)
1115}
1116
1117// DiffPrettyHtml converts a []Diff into a pretty HTML report.
1118// It is intended as an example from which to write one's own display functions.

Callers 2

PatchApplyMethod · 0.95
TestDiffXIndexFunction · 0.80

Calls

no outgoing calls

Tested by 1

TestDiffXIndexFunction · 0.64