sergi / go-diff

Diff, match and patch text in Go
MIT License
1.81k stars 207 forks source link

New line characters removed after applying patch #110

Closed nepsilon closed 4 years ago

nepsilon commented 4 years ago

Code used:

package main

import (
    "fmt"

    "github.com/sergi/go-diff/diffmatchpatch"
)

func main() {

    var oldtext string = `foo
bar
`

    var patchtxt string = `@@ -1,2 +1,2 @@
-foo
+foobaz
 bar
`
    dmp := diffmatchpatch.New()
    patch, _ := dmp.PatchFromText(patchtxt)

    newtext, _ := dmp.PatchApply(patch, oldtext)
    fmt.Println("new text:", newtext)
}

Output:

new text: foobazbar

Expected Output:

new text: foobaz
bar

Is there a way to apply the patch to conserve the new line \n characters?

nepsilon commented 4 years ago

@sergi I tried to look at test cases and finding ways to avoid the \n deletions but I couldn't figure it out. Do you know why this is happening? Thank you!