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

Function ExampleDiffMatchPatch_DiffLinesToChars

diffmatchpatch/example_test.go:55–74  ·  view source on GitHub ↗

Diff two texts line by line by reducing each line to a character, diffing the reduced texts and expanding the result back to lines.

()

Source from the content-addressed store, hash-verified

53// Diff two texts line by line by reducing each line to a character,
54// diffing the reduced texts and expanding the result back to lines.
55func ExampleDiffMatchPatch_DiffLinesToChars() {
56 dmp := diffmatchpatch.New()
57
58 text1 := "foo\nbar\nbaz\n"
59 text2 := "foo\nbaz\nqux\n"
60
61 chars1, chars2, lines := dmp.DiffLinesToChars(text1, text2)
62 diffs := dmp.DiffMain(chars1, chars2, false)
63 diffs = dmp.DiffCharsToLines(diffs, lines)
64
65 for _, diff := range diffs {
66 fmt.Printf("%s %q\n", diff.Type, diff.Text)
67 }
68
69 // Output:
70 // Equal "foo\n"
71 // Delete "bar\n"
72 // Equal "baz\n"
73 // Insert "qux\n"
74}
75
76// Make a patch, convert it to text, and apply that text to the
77// original later, possibly elsewhere.

Callers

nothing calls this directly

Calls 4

NewFunction · 0.92
DiffLinesToCharsMethod · 0.80
DiffMainMethod · 0.80
DiffCharsToLinesMethod · 0.80

Tested by

no test coverage detected