Skip to content

diffmatchpatch: invalid diff output with CRLF line endings #115

Open
@stamblerre

Description

@stamblerre

I believe the fix for #89 introduced regressions for files with \r\n line endings. This reproduces as of commit db1b095.

The following repro program illustrates the issue. It compares two strings, whose only differences are line endings. (This was caught by a gopls test in https://golang.org/cl/275439).

package main

import (
	"fmt"
	"strings"

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

func main() {
	const before = `package main

import (
	"fmt"
)

/*
func upload(c echo.Context) error {
	if err := r.ParseForm(); err != nil {
		fmt.Fprintf(w, "ParseForm() err: %v", err)
		return
	}
	fmt.Fprintf(w, "POST request successful")
	path_ver := r.FormValue("path_ver")
	ukclin_ver := r.FormValue("ukclin_ver")

	fmt.Fprintf(w, "Name = %s\n", path_ver)
	fmt.Fprintf(w, "Address = %s\n", ukclin_ver)
}
*/
`
	after := strings.ReplaceAll(before, "\n", "\r\n")
	diffs := diffmatchpatch.New().DiffMain(before, after, true)
	fmt.Println(diffmatchpatch.New().DiffPrettyText(diffs))
}

Before db1b095, the diffs produced are correct. After, they are:

package main
                fmt.Fprintf(w, "ParseForm() err: %v", err)

                fmt.Fprintf(w, "ParseForm() err: %v", err)
import (
import (
)

/*
import (
        "fmt"
import (
)
import (
/*
import (
func upload(c echo.Context) error {
import (
        if err := r.ParseForm(); err != nil {
import (
                fmt.Fprintf(w, "ParseForm() err: %v", err)
import (
                return
        "fmt"

                fmt.Fprintf(w, "ParseForm() err: %v", err)
        "fmt"

        "fmt"
import (
        "fmt"
        "fmt"


)
        "fmt.Fprintf(w, "Address = %s\n", ukclin_ver
)
}
*/

which is clearly invalid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions