Move util functionality into *util*.go
diff --git a/diffmatchpatch/diffmatchpatch.go b/diffmatchpatch/diffmatchpatch.go
index fd3da27..2ca8a48 100644
--- a/diffmatchpatch/diffmatchpatch.go
+++ b/diffmatchpatch/diffmatchpatch.go
@@ -11,105 +11,9 @@
package diffmatchpatch
import (
- "strings"
"time"
- "unicode/utf8"
)
-// 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.
-// Note that this function is case-sensitive. Thus "%3F" would not be
-// unescaped. But this is ok because it is only called with the output of
-// HttpUtility.UrlEncode which returns lowercase hex.
-//
-// Example: "%3f" -> "?", "%24" -> "$", etc.
-var unescaper = strings.NewReplacer(
- "%21", "!", "%7E", "~", "%27", "'",
- "%28", "(", "%29", ")", "%3B", ";",
- "%2F", "/", "%3F", "?", "%3A", ":",
- "%40", "@", "%26", "&", "%3D", "=",
- "%2B", "+", "%24", "$", "%2C", ",", "%23", "#", "%2A", "*")
-
-// 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 {
- return -1
- }
- if i <= 0 {
- return strings.Index(str, pattern)
- }
- ind := strings.Index(str[i:], pattern)
- if ind == -1 {
- return -1
- }
- return ind + i
-}
-
-// lastIndexOf returns the last index of pattern in str, starting at str[i].
-func lastIndexOf(str string, pattern string, i int) int {
- if i < 0 {
- return -1
- }
- if i >= len(str) {
- return strings.LastIndex(str, pattern)
- }
- _, size := utf8.DecodeRuneInString(str[i:])
- return strings.LastIndex(str[:i+size], pattern)
-}
-
-// Return the index of pattern in target, starting at target[i].
-func runesIndexOf(target, pattern []rune, i int) int {
- if i > len(target)-1 {
- return -1
- }
- if i <= 0 {
- return runesIndex(target, pattern)
- }
- ind := runesIndex(target[i:], pattern)
- if ind == -1 {
- return -1
- }
- return ind + i
-}
-
-func min(x, y int) int {
- if x < y {
- return x
- }
- return y
-}
-
-func max(x, y int) int {
- if x > y {
- return x
- }
- return y
-}
-
-func runesEqual(r1, r2 []rune) bool {
- if len(r1) != len(r2) {
- return false
- }
- for i, c := range r1 {
- if c != r2[i] {
- return false
- }
- }
- return true
-}
-
-// The equivalent of strings.Index for rune slices.
-func runesIndex(r1, r2 []rune) int {
- last := len(r1) - len(r2)
- for i := 0; i <= last; i++ {
- if runesEqual(r1[i:i+len(r2)], r2) {
- return i
- }
- }
- return -1
-}
-
// 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 830e4bd..8bc5f88 100644
--- a/diffmatchpatch/diffmatchpatch_test.go
+++ b/diffmatchpatch/diffmatchpatch_test.go
@@ -7,110 +7,3 @@
// http://code.google.com/p/google-diff-match-patch/
package diffmatchpatch
-
-import (
- "fmt"
- "testing"
-
- "github.com/stretchr/testify/assert"
-)
-
-func TestRunesIndexOf(t *testing.T) {
- type TestCase struct {
- Pattern string
- Start int
-
- Expected int
- }
-
- for i, tc := range []TestCase{
- {"abc", 0, 0},
- {"cde", 0, 2},
- {"e", 0, 4},
- {"cdef", 0, -1},
- {"abcdef", 0, -1},
- {"abc", 2, -1},
- {"cde", 2, 2},
- {"e", 2, 4},
- {"cdef", 2, -1},
- {"abcdef", 2, -1},
- {"e", 6, -1},
- } {
- actual := runesIndexOf([]rune("abcde"), []rune(tc.Pattern), tc.Start)
- assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
- }
-}
-
-func TestIndexOf(t *testing.T) {
- type TestCase struct {
- String string
- Pattern string
- Position int
-
- Expected int
- }
-
- for i, tc := range []TestCase{
- {"hi world", "world", -1, 3},
- {"hi world", "world", 0, 3},
- {"hi world", "world", 1, 3},
- {"hi world", "world", 2, 3},
- {"hi world", "world", 3, 3},
- {"hi world", "world", 4, -1},
- {"abbc", "b", -1, 1},
- {"abbc", "b", 0, 1},
- {"abbc", "b", 1, 1},
- {"abbc", "b", 2, 2},
- {"abbc", "b", 3, -1},
- {"abbc", "b", 4, -1},
- // The greek letter beta is the two-byte sequence of "\u03b2".
- {"a\u03b2\u03b2c", "\u03b2", -1, 1},
- {"a\u03b2\u03b2c", "\u03b2", 0, 1},
- {"a\u03b2\u03b2c", "\u03b2", 1, 1},
- {"a\u03b2\u03b2c", "\u03b2", 3, 3},
- {"a\u03b2\u03b2c", "\u03b2", 5, -1},
- {"a\u03b2\u03b2c", "\u03b2", 6, -1},
- } {
- actual := indexOf(tc.String, tc.Pattern, tc.Position)
- assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
- }
-}
-
-func TestLastIndexOf(t *testing.T) {
- type TestCase struct {
- String string
- Pattern string
- Position int
-
- Expected int
- }
-
- for i, tc := range []TestCase{
- {"hi world", "world", -1, -1},
- {"hi world", "world", 0, -1},
- {"hi world", "world", 1, -1},
- {"hi world", "world", 2, -1},
- {"hi world", "world", 3, -1},
- {"hi world", "world", 4, -1},
- {"hi world", "world", 5, -1},
- {"hi world", "world", 6, -1},
- {"hi world", "world", 7, 3},
- {"hi world", "world", 8, 3},
- {"abbc", "b", -1, -1},
- {"abbc", "b", 0, -1},
- {"abbc", "b", 1, 1},
- {"abbc", "b", 2, 2},
- {"abbc", "b", 3, 2},
- {"abbc", "b", 4, 2},
- // The greek letter beta is the two-byte sequence of "\u03b2".
- {"a\u03b2\u03b2c", "\u03b2", -1, -1},
- {"a\u03b2\u03b2c", "\u03b2", 0, -1},
- {"a\u03b2\u03b2c", "\u03b2", 1, 1},
- {"a\u03b2\u03b2c", "\u03b2", 3, 3},
- {"a\u03b2\u03b2c", "\u03b2", 5, 3},
- {"a\u03b2\u03b2c", "\u03b2", 6, 3},
- } {
- actual := lastIndexOf(tc.String, tc.Pattern, tc.Position)
- assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
- }
-}
diff --git a/diffmatchpatch/mathutil.go b/diffmatchpatch/mathutil.go
new file mode 100644
index 0000000..aed242b
--- /dev/null
+++ b/diffmatchpatch/mathutil.go
@@ -0,0 +1,23 @@
+// Copyright (c) 2012-2016 The go-diff authors. All rights reserved.
+// https://github.com/sergi/go-diff
+// See the included LICENSE file for license details.
+//
+// go-diff is a Go implementation of Google's Diff, Match, and Patch library
+// Original library is Copyright (c) 2006 Google Inc.
+// http://code.google.com/p/google-diff-match-patch/
+
+package diffmatchpatch
+
+func min(x, y int) int {
+ if x < y {
+ return x
+ }
+ return y
+}
+
+func max(x, y int) int {
+ if x > y {
+ return x
+ }
+ return y
+}
diff --git a/diffmatchpatch/stringutil.go b/diffmatchpatch/stringutil.go
new file mode 100644
index 0000000..ef2867c
--- /dev/null
+++ b/diffmatchpatch/stringutil.go
@@ -0,0 +1,94 @@
+// Copyright (c) 2012-2016 The go-diff authors. All rights reserved.
+// https://github.com/sergi/go-diff
+// See the included LICENSE file for license details.
+//
+// go-diff is a Go implementation of Google's Diff, Match, and Patch library
+// Original library is Copyright (c) 2006 Google Inc.
+// http://code.google.com/p/google-diff-match-patch/
+
+package diffmatchpatch
+
+import (
+ "strings"
+ "unicode/utf8"
+)
+
+// 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.
+// Note that this function is case-sensitive. Thus "%3F" would not be
+// unescaped. But this is ok because it is only called with the output of
+// HttpUtility.UrlEncode which returns lowercase hex.
+//
+// Example: "%3f" -> "?", "%24" -> "$", etc.
+var unescaper = strings.NewReplacer(
+ "%21", "!", "%7E", "~", "%27", "'",
+ "%28", "(", "%29", ")", "%3B", ";",
+ "%2F", "/", "%3F", "?", "%3A", ":",
+ "%40", "@", "%26", "&", "%3D", "=",
+ "%2B", "+", "%24", "$", "%2C", ",", "%23", "#", "%2A", "*")
+
+// 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 {
+ return -1
+ }
+ if i <= 0 {
+ return strings.Index(str, pattern)
+ }
+ ind := strings.Index(str[i:], pattern)
+ if ind == -1 {
+ return -1
+ }
+ return ind + i
+}
+
+// lastIndexOf returns the last index of pattern in str, starting at str[i].
+func lastIndexOf(str string, pattern string, i int) int {
+ if i < 0 {
+ return -1
+ }
+ if i >= len(str) {
+ return strings.LastIndex(str, pattern)
+ }
+ _, size := utf8.DecodeRuneInString(str[i:])
+ return strings.LastIndex(str[:i+size], pattern)
+}
+
+// Return the index of pattern in target, starting at target[i].
+func runesIndexOf(target, pattern []rune, i int) int {
+ if i > len(target)-1 {
+ return -1
+ }
+ if i <= 0 {
+ return runesIndex(target, pattern)
+ }
+ ind := runesIndex(target[i:], pattern)
+ if ind == -1 {
+ return -1
+ }
+ return ind + i
+}
+
+func runesEqual(r1, r2 []rune) bool {
+ if len(r1) != len(r2) {
+ return false
+ }
+ for i, c := range r1 {
+ if c != r2[i] {
+ return false
+ }
+ }
+ return true
+}
+
+// The equivalent of strings.Index for rune slices.
+func runesIndex(r1, r2 []rune) int {
+ last := len(r1) - len(r2)
+ for i := 0; i <= last; i++ {
+ if runesEqual(r1[i:i+len(r2)], r2) {
+ return i
+ }
+ }
+ return -1
+}
diff --git a/diffmatchpatch/stringutil_test.go b/diffmatchpatch/stringutil_test.go
new file mode 100644
index 0000000..ab2bc10
--- /dev/null
+++ b/diffmatchpatch/stringutil_test.go
@@ -0,0 +1,116 @@
+// Copyright (c) 2012-2016 The go-diff authors. All rights reserved.
+// https://github.com/sergi/go-diff
+// See the included LICENSE file for license details.
+//
+// go-diff is a Go implementation of Google's Diff, Match, and Patch library
+// Original library is Copyright (c) 2006 Google Inc.
+// http://code.google.com/p/google-diff-match-patch/
+
+package diffmatchpatch
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestRunesIndexOf(t *testing.T) {
+ type TestCase struct {
+ Pattern string
+ Start int
+
+ Expected int
+ }
+
+ for i, tc := range []TestCase{
+ {"abc", 0, 0},
+ {"cde", 0, 2},
+ {"e", 0, 4},
+ {"cdef", 0, -1},
+ {"abcdef", 0, -1},
+ {"abc", 2, -1},
+ {"cde", 2, 2},
+ {"e", 2, 4},
+ {"cdef", 2, -1},
+ {"abcdef", 2, -1},
+ {"e", 6, -1},
+ } {
+ actual := runesIndexOf([]rune("abcde"), []rune(tc.Pattern), tc.Start)
+ assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
+ }
+}
+
+func TestIndexOf(t *testing.T) {
+ type TestCase struct {
+ String string
+ Pattern string
+ Position int
+
+ Expected int
+ }
+
+ for i, tc := range []TestCase{
+ {"hi world", "world", -1, 3},
+ {"hi world", "world", 0, 3},
+ {"hi world", "world", 1, 3},
+ {"hi world", "world", 2, 3},
+ {"hi world", "world", 3, 3},
+ {"hi world", "world", 4, -1},
+ {"abbc", "b", -1, 1},
+ {"abbc", "b", 0, 1},
+ {"abbc", "b", 1, 1},
+ {"abbc", "b", 2, 2},
+ {"abbc", "b", 3, -1},
+ {"abbc", "b", 4, -1},
+ // The greek letter beta is the two-byte sequence of "\u03b2".
+ {"a\u03b2\u03b2c", "\u03b2", -1, 1},
+ {"a\u03b2\u03b2c", "\u03b2", 0, 1},
+ {"a\u03b2\u03b2c", "\u03b2", 1, 1},
+ {"a\u03b2\u03b2c", "\u03b2", 3, 3},
+ {"a\u03b2\u03b2c", "\u03b2", 5, -1},
+ {"a\u03b2\u03b2c", "\u03b2", 6, -1},
+ } {
+ actual := indexOf(tc.String, tc.Pattern, tc.Position)
+ assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
+ }
+}
+
+func TestLastIndexOf(t *testing.T) {
+ type TestCase struct {
+ String string
+ Pattern string
+ Position int
+
+ Expected int
+ }
+
+ for i, tc := range []TestCase{
+ {"hi world", "world", -1, -1},
+ {"hi world", "world", 0, -1},
+ {"hi world", "world", 1, -1},
+ {"hi world", "world", 2, -1},
+ {"hi world", "world", 3, -1},
+ {"hi world", "world", 4, -1},
+ {"hi world", "world", 5, -1},
+ {"hi world", "world", 6, -1},
+ {"hi world", "world", 7, 3},
+ {"hi world", "world", 8, 3},
+ {"abbc", "b", -1, -1},
+ {"abbc", "b", 0, -1},
+ {"abbc", "b", 1, 1},
+ {"abbc", "b", 2, 2},
+ {"abbc", "b", 3, 2},
+ {"abbc", "b", 4, 2},
+ // The greek letter beta is the two-byte sequence of "\u03b2".
+ {"a\u03b2\u03b2c", "\u03b2", -1, -1},
+ {"a\u03b2\u03b2c", "\u03b2", 0, -1},
+ {"a\u03b2\u03b2c", "\u03b2", 1, 1},
+ {"a\u03b2\u03b2c", "\u03b2", 3, 3},
+ {"a\u03b2\u03b2c", "\u03b2", 5, 3},
+ {"a\u03b2\u03b2c", "\u03b2", 6, 3},
+ } {
+ actual := lastIndexOf(tc.String, tc.Pattern, tc.Position)
+ assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
+ }
+}