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

Method DiffToDelta

diffmatchpatch/diff.go:1247–1275  ·  view source on GitHub ↗

DiffToDelta crushes the diff into an encoded string which describes the operations required to transform text1 into text2. E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'. Operations are tab-separated. Inserted text is escaped using %xx notation.

(diffs []Diff)

Source from the content-addressed store, hash-verified

1245// DiffToDelta crushes the diff into an encoded string which describes the operations required to transform text1 into text2.
1246// E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'. Operations are tab-separated. Inserted text is escaped using %xx notation.
1247func (dmp *DiffMatchPatch) DiffToDelta(diffs []Diff) string {
1248 var text bytes.Buffer
1249 for _, aDiff := range diffs {
1250 switch aDiff.Type {
1251 case DiffInsert:
1252 _, _ = text.WriteString("+")
1253 _, _ = text.WriteString(strings.Replace(url.QueryEscape(aDiff.Text), "+", " ", -1))
1254 _, _ = text.WriteString("\t")
1255 break
1256 case DiffDelete:
1257 _, _ = text.WriteString("-")
1258 _, _ = text.WriteString(strconv.Itoa(utf8.RuneCountInString(aDiff.Text)))
1259 _, _ = text.WriteString("\t")
1260 break
1261 case DiffEqual:
1262 _, _ = text.WriteString("=")
1263 _, _ = text.WriteString(strconv.Itoa(utf8.RuneCountInString(aDiff.Text)))
1264 _, _ = text.WriteString("\t")
1265 break
1266 }
1267 }
1268 delta := text.String()
1269 if len(delta) != 0 {
1270 // Strip off trailing tab character.
1271 delta = delta[0 : len(delta)-1]
1272 delta = unescaper.Replace(delta)
1273 }
1274 return delta
1275}
1276
1277// DiffFromDelta given the original text1, and an encoded string which describes the operations required to transform text1 into text2, comAdde the full diff.
1278func (dmp *DiffMatchPatch) DiffFromDelta(text1 string, delta string) (diffs []Diff, err error) {

Callers 1

TestDiffDeltaFunction · 0.80

Calls 1

StringMethod · 0.45

Tested by 1

TestDiffDeltaFunction · 0.64