Move even more functionality into diff
diff --git a/diffmatchpatch/diff.go b/diffmatchpatch/diff.go
index a3c672a..2f42aa1 100644
--- a/diffmatchpatch/diff.go
+++ b/diffmatchpatch/diff.go
@@ -15,12 +15,35 @@
"html"
"math"
"net/url"
+ "regexp"
"strconv"
"strings"
"time"
"unicode/utf8"
)
+// Operation defines the operation of a diff item.
+type Operation int8
+
+const (
+ // DiffDelete item represents a delete diff.
+ DiffDelete Operation = -1
+ // DiffInsert item represents an insert diff.
+ DiffInsert Operation = 1
+ // DiffEqual item represents an equal diff.
+ DiffEqual Operation = 0
+)
+
+// Diff represents one diff operation
+type Diff struct {
+ Type Operation
+ Text string
+}
+
+func splice(slice []Diff, index int, amount int, elements ...Diff) []Diff {
+ return append(slice[:index], append(elements, slice[index+amount:]...)...)
+}
+
// DiffMain finds the differences between two texts.
func (dmp *DiffMatchPatch) DiffMain(text1, text2 string, checklines bool) []Diff {
return dmp.DiffMainRunes([]rune(text1), []rune(text2), checklines)
@@ -752,6 +775,15 @@
return diffs
}
+// Define some regex patterns for matching boundaries.
+var (
+ nonAlphaNumericRegex = regexp.MustCompile(`[^a-zA-Z0-9]`)
+ whitespaceRegex = regexp.MustCompile(`\s`)
+ linebreakRegex = regexp.MustCompile(`[\r\n]`)
+ blanklineEndRegex = regexp.MustCompile(`\n\r?\n$`)
+ blanklineStartRegex = regexp.MustCompile(`^\r?\n\r?\n`)
+)
+
// DiffCleanupSemanticLossless looks for single edits surrounded on both sides by equalities
// which can be shifted sideways to align the edit to a word boundary.
// e.g: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.
diff --git a/diffmatchpatch/diff_test.go b/diffmatchpatch/diff_test.go
index 5b7de57..1fb6ccb 100644
--- a/diffmatchpatch/diff_test.go
+++ b/diffmatchpatch/diff_test.go
@@ -9,6 +9,7 @@
package diffmatchpatch
import (
+ "bytes"
"fmt"
"strconv"
"strings"
@@ -19,6 +20,44 @@
"github.com/stretchr/testify/assert"
)
+func pretty(diffs []Diff) string {
+ var w bytes.Buffer
+
+ for i, diff := range diffs {
+ _, _ = w.WriteString(fmt.Sprintf("%v. ", i))
+
+ switch diff.Type {
+ case DiffInsert:
+ _, _ = w.WriteString("DiffIns")
+ case DiffDelete:
+ _, _ = w.WriteString("DiffDel")
+ case DiffEqual:
+ _, _ = w.WriteString("DiffEql")
+ default:
+ _, _ = w.WriteString("Unknown")
+ }
+
+ _, _ = w.WriteString(fmt.Sprintf(": %v\n", diff.Text))
+ }
+
+ return w.String()
+}
+
+func diffRebuildTexts(diffs []Diff) []string {
+ texts := []string{"", ""}
+
+ for _, d := range diffs {
+ if d.Type != DiffInsert {
+ texts[0] += d.Text
+ }
+ if d.Type != DiffDelete {
+ texts[1] += d.Text
+ }
+ }
+
+ return texts
+}
+
func TestDiffCommonPrefix(t *testing.T) {
type TestCase struct {
Name string
diff --git a/diffmatchpatch/diffmatchpatch.go b/diffmatchpatch/diffmatchpatch.go
index f1ec0c2..fd3da27 100644
--- a/diffmatchpatch/diffmatchpatch.go
+++ b/diffmatchpatch/diffmatchpatch.go
@@ -11,28 +11,11 @@
package diffmatchpatch
import (
- "regexp"
"strings"
"time"
"unicode/utf8"
)
-// The data structure representing a diff is an array of tuples:
-// [[DiffDelete, 'Hello'], [DiffInsert, 'Goodbye'], [DiffEqual, ' world.']]
-// which means: delete 'Hello', add 'Goodbye' and keep ' world.'
-
-// Operation defines the operation of a diff item.
-type Operation int8
-
-const (
- // DiffDelete item represents a delete diff.
- DiffDelete Operation = -1
- // DiffInsert item represents an insert diff.
- DiffInsert Operation = 1
- // DiffEqual item represents an equal diff.
- DiffEqual Operation = 0
-)
-
// unescaper unescapes selected chars for compatibility with JavaScript's encodeURI.
// In speed critical applications this could be dropped since the
// receiving application will certainly decode these fine.
@@ -48,19 +31,6 @@
"%40", "@", "%26", "&", "%3D", "=",
"%2B", "+", "%24", "$", "%2C", ",", "%23", "#", "%2A", "*")
-// Define some regex patterns for matching boundaries.
-var (
- nonAlphaNumericRegex = regexp.MustCompile(`[^a-zA-Z0-9]`)
- whitespaceRegex = regexp.MustCompile(`\s`)
- linebreakRegex = regexp.MustCompile(`[\r\n]`)
- blanklineEndRegex = regexp.MustCompile(`\n\r?\n$`)
- blanklineStartRegex = regexp.MustCompile(`^\r?\n\r?\n`)
-)
-
-func splice(slice []Diff, index int, amount int, elements ...Diff) []Diff {
- return append(slice[:index], append(elements, slice[index+amount:]...)...)
-}
-
// indexOf returns the first index of pattern in str, starting at str[i].
func indexOf(str string, pattern string, i int) int {
if i > len(str)-1 {
@@ -140,12 +110,6 @@
return -1
}
-// Diff represents one diff operation
-type Diff struct {
- Type Operation
- Text string
-}
-
// DiffMatchPatch holds the configuration for diff-match-patch operations.
type DiffMatchPatch struct {
// Number of seconds to map a diff before giving up (0 for infinity).
diff --git a/diffmatchpatch/diffmatchpatch_test.go b/diffmatchpatch/diffmatchpatch_test.go
index 9f62f7b..0d843ad 100644
--- a/diffmatchpatch/diffmatchpatch_test.go
+++ b/diffmatchpatch/diffmatchpatch_test.go
@@ -9,7 +9,6 @@
package diffmatchpatch
import (
- "bytes"
"fmt"
"io/ioutil"
"runtime"
@@ -27,44 +26,6 @@
return fmt.Sprintf("at line %d ", line)
}
-func pretty(diffs []Diff) string {
- var w bytes.Buffer
-
- for i, diff := range diffs {
- _, _ = w.WriteString(fmt.Sprintf("%v. ", i))
-
- switch diff.Type {
- case DiffInsert:
- _, _ = w.WriteString("DiffIns")
- case DiffDelete:
- _, _ = w.WriteString("DiffDel")
- case DiffEqual:
- _, _ = w.WriteString("DiffEql")
- default:
- _, _ = w.WriteString("Unknown")
- }
-
- _, _ = w.WriteString(fmt.Sprintf(": %v\n", diff.Text))
- }
-
- return w.String()
-}
-
-func diffRebuildTexts(diffs []Diff) []string {
- texts := []string{"", ""}
-
- for _, d := range diffs {
- if d.Type != DiffInsert {
- texts[0] += d.Text
- }
- if d.Type != DiffDelete {
- texts[1] += d.Text
- }
- }
-
- return texts
-}
-
func readFile(filepath string) string {
data, err := ioutil.ReadFile(filepath)
if err != nil {