Skip to content

Instantly share code, notes, and snippets.

@k2wanko
Last active October 17, 2016 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k2wanko/a2b5627799ace224067c856de2ccf11a to your computer and use it in GitHub Desktop.
Save k2wanko/a2b5627799ace224067c856de2ccf11a to your computer and use it in GitHub Desktop.
go-diff-demo
package main
import (
"bufio"
"bytes"
"fmt"
"net/url"
"github.com/sergi/go-diff/diffmatchpatch"
)
func main() {
text1 := `ガルパンはいいぞ`
text2 := `SHOW BY ROCKはいいぞ`
dmp := diffmatchpatch.New()
a, b, la := dmp.DiffLinesToRunes(text1, text2)
diffs := dmp.DiffMainRunes(a, b, false)
diffs = dmp.DiffCharsToLines(diffs, la)
patches := dmp.PatchMake(diffs)
patch := dmp.PatchToText(patches)
s := bufio.NewScanner(bytes.NewBufferString(patch))
for s.Scan() {
var err error
text := s.Text()
if text[0] == '+' {
text = text[1:len(text)]
text, err = url.QueryUnescape(text)
text = "+" + text
} else {
text, err = url.QueryUnescape(text)
}
if err != nil {
panic(err)
}
fmt.Printf("%s\n", text)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment