Skip to content

Commit 9f85913

Browse files
committed
Move util functionality into *util*.go
1 parent b3021ff commit 9f85913

File tree

5 files changed

+233
-203
lines changed

5 files changed

+233
-203
lines changed

diffmatchpatch/diffmatchpatch.go

-96
Original file line numberDiff line numberDiff line change
@@ -11,105 +11,9 @@
1111
package diffmatchpatch
1212

1313
import (
14-
"strings"
1514
"time"
16-
"unicode/utf8"
1715
)
1816

19-
// unescaper unescapes selected chars for compatibility with JavaScript's encodeURI.
20-
// In speed critical applications this could be dropped since the
21-
// receiving application will certainly decode these fine.
22-
// Note that this function is case-sensitive. Thus "%3F" would not be
23-
// unescaped. But this is ok because it is only called with the output of
24-
// HttpUtility.UrlEncode which returns lowercase hex.
25-
//
26-
// Example: "%3f" -> "?", "%24" -> "$", etc.
27-
var unescaper = strings.NewReplacer(
28-
"%21", "!", "%7E", "~", "%27", "'",
29-
"%28", "(", "%29", ")", "%3B", ";",
30-
"%2F", "/", "%3F", "?", "%3A", ":",
31-
"%40", "@", "%26", "&", "%3D", "=",
32-
"%2B", "+", "%24", "$", "%2C", ",", "%23", "#", "%2A", "*")
33-
34-
// indexOf returns the first index of pattern in str, starting at str[i].
35-
func indexOf(str string, pattern string, i int) int {
36-
if i > len(str)-1 {
37-
return -1
38-
}
39-
if i <= 0 {
40-
return strings.Index(str, pattern)
41-
}
42-
ind := strings.Index(str[i:], pattern)
43-
if ind == -1 {
44-
return -1
45-
}
46-
return ind + i
47-
}
48-
49-
// lastIndexOf returns the last index of pattern in str, starting at str[i].
50-
func lastIndexOf(str string, pattern string, i int) int {
51-
if i < 0 {
52-
return -1
53-
}
54-
if i >= len(str) {
55-
return strings.LastIndex(str, pattern)
56-
}
57-
_, size := utf8.DecodeRuneInString(str[i:])
58-
return strings.LastIndex(str[:i+size], pattern)
59-
}
60-
61-
// Return the index of pattern in target, starting at target[i].
62-
func runesIndexOf(target, pattern []rune, i int) int {
63-
if i > len(target)-1 {
64-
return -1
65-
}
66-
if i <= 0 {
67-
return runesIndex(target, pattern)
68-
}
69-
ind := runesIndex(target[i:], pattern)
70-
if ind == -1 {
71-
return -1
72-
}
73-
return ind + i
74-
}
75-
76-
func min(x, y int) int {
77-
if x < y {
78-
return x
79-
}
80-
return y
81-
}
82-
83-
func max(x, y int) int {
84-
if x > y {
85-
return x
86-
}
87-
return y
88-
}
89-
90-
func runesEqual(r1, r2 []rune) bool {
91-
if len(r1) != len(r2) {
92-
return false
93-
}
94-
for i, c := range r1 {
95-
if c != r2[i] {
96-
return false
97-
}
98-
}
99-
return true
100-
}
101-
102-
// The equivalent of strings.Index for rune slices.
103-
func runesIndex(r1, r2 []rune) int {
104-
last := len(r1) - len(r2)
105-
for i := 0; i <= last; i++ {
106-
if runesEqual(r1[i:i+len(r2)], r2) {
107-
return i
108-
}
109-
}
110-
return -1
111-
}
112-
11317
// DiffMatchPatch holds the configuration for diff-match-patch operations.
11418
type DiffMatchPatch struct {
11519
// Number of seconds to map a diff before giving up (0 for infinity).

diffmatchpatch/diffmatchpatch_test.go

-107
Original file line numberDiff line numberDiff line change
@@ -7,110 +7,3 @@
77
// http://code.google.com/p/google-diff-match-patch/
88

99
package diffmatchpatch
10-
11-
import (
12-
"fmt"
13-
"testing"
14-
15-
"github.com/stretchr/testify/assert"
16-
)
17-
18-
func TestRunesIndexOf(t *testing.T) {
19-
type TestCase struct {
20-
Pattern string
21-
Start int
22-
23-
Expected int
24-
}
25-
26-
for i, tc := range []TestCase{
27-
{"abc", 0, 0},
28-
{"cde", 0, 2},
29-
{"e", 0, 4},
30-
{"cdef", 0, -1},
31-
{"abcdef", 0, -1},
32-
{"abc", 2, -1},
33-
{"cde", 2, 2},
34-
{"e", 2, 4},
35-
{"cdef", 2, -1},
36-
{"abcdef", 2, -1},
37-
{"e", 6, -1},
38-
} {
39-
actual := runesIndexOf([]rune("abcde"), []rune(tc.Pattern), tc.Start)
40-
assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
41-
}
42-
}
43-
44-
func TestIndexOf(t *testing.T) {
45-
type TestCase struct {
46-
String string
47-
Pattern string
48-
Position int
49-
50-
Expected int
51-
}
52-
53-
for i, tc := range []TestCase{
54-
{"hi world", "world", -1, 3},
55-
{"hi world", "world", 0, 3},
56-
{"hi world", "world", 1, 3},
57-
{"hi world", "world", 2, 3},
58-
{"hi world", "world", 3, 3},
59-
{"hi world", "world", 4, -1},
60-
{"abbc", "b", -1, 1},
61-
{"abbc", "b", 0, 1},
62-
{"abbc", "b", 1, 1},
63-
{"abbc", "b", 2, 2},
64-
{"abbc", "b", 3, -1},
65-
{"abbc", "b", 4, -1},
66-
// The greek letter beta is the two-byte sequence of "\u03b2".
67-
{"a\u03b2\u03b2c", "\u03b2", -1, 1},
68-
{"a\u03b2\u03b2c", "\u03b2", 0, 1},
69-
{"a\u03b2\u03b2c", "\u03b2", 1, 1},
70-
{"a\u03b2\u03b2c", "\u03b2", 3, 3},
71-
{"a\u03b2\u03b2c", "\u03b2", 5, -1},
72-
{"a\u03b2\u03b2c", "\u03b2", 6, -1},
73-
} {
74-
actual := indexOf(tc.String, tc.Pattern, tc.Position)
75-
assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
76-
}
77-
}
78-
79-
func TestLastIndexOf(t *testing.T) {
80-
type TestCase struct {
81-
String string
82-
Pattern string
83-
Position int
84-
85-
Expected int
86-
}
87-
88-
for i, tc := range []TestCase{
89-
{"hi world", "world", -1, -1},
90-
{"hi world", "world", 0, -1},
91-
{"hi world", "world", 1, -1},
92-
{"hi world", "world", 2, -1},
93-
{"hi world", "world", 3, -1},
94-
{"hi world", "world", 4, -1},
95-
{"hi world", "world", 5, -1},
96-
{"hi world", "world", 6, -1},
97-
{"hi world", "world", 7, 3},
98-
{"hi world", "world", 8, 3},
99-
{"abbc", "b", -1, -1},
100-
{"abbc", "b", 0, -1},
101-
{"abbc", "b", 1, 1},
102-
{"abbc", "b", 2, 2},
103-
{"abbc", "b", 3, 2},
104-
{"abbc", "b", 4, 2},
105-
// The greek letter beta is the two-byte sequence of "\u03b2".
106-
{"a\u03b2\u03b2c", "\u03b2", -1, -1},
107-
{"a\u03b2\u03b2c", "\u03b2", 0, -1},
108-
{"a\u03b2\u03b2c", "\u03b2", 1, 1},
109-
{"a\u03b2\u03b2c", "\u03b2", 3, 3},
110-
{"a\u03b2\u03b2c", "\u03b2", 5, 3},
111-
{"a\u03b2\u03b2c", "\u03b2", 6, 3},
112-
} {
113-
actual := lastIndexOf(tc.String, tc.Pattern, tc.Position)
114-
assert.Equal(t, tc.Expected, actual, fmt.Sprintf("Test case #%d, %#v", i, tc))
115-
}
116-
}

diffmatchpatch/mathutil.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2012-2016 The go-diff authors. All rights reserved.
2+
// https://github.com/sergi/go-diff
3+
// See the included LICENSE file for license details.
4+
//
5+
// go-diff is a Go implementation of Google's Diff, Match, and Patch library
6+
// Original library is Copyright (c) 2006 Google Inc.
7+
// http://code.google.com/p/google-diff-match-patch/
8+
9+
package diffmatchpatch
10+
11+
func min(x, y int) int {
12+
if x < y {
13+
return x
14+
}
15+
return y
16+
}
17+
18+
func max(x, y int) int {
19+
if x > y {
20+
return x
21+
}
22+
return y
23+
}

diffmatchpatch/stringutil.go

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) 2012-2016 The go-diff authors. All rights reserved.
2+
// https://github.com/sergi/go-diff
3+
// See the included LICENSE file for license details.
4+
//
5+
// go-diff is a Go implementation of Google's Diff, Match, and Patch library
6+
// Original library is Copyright (c) 2006 Google Inc.
7+
// http://code.google.com/p/google-diff-match-patch/
8+
9+
package diffmatchpatch
10+
11+
import (
12+
"strings"
13+
"unicode/utf8"
14+
)
15+
16+
// unescaper unescapes selected chars for compatibility with JavaScript's encodeURI.
17+
// In speed critical applications this could be dropped since the
18+
// receiving application will certainly decode these fine.
19+
// Note that this function is case-sensitive. Thus "%3F" would not be
20+
// unescaped. But this is ok because it is only called with the output of
21+
// HttpUtility.UrlEncode which returns lowercase hex.
22+
//
23+
// Example: "%3f" -> "?", "%24" -> "$", etc.
24+
var unescaper = strings.NewReplacer(
25+
"%21", "!", "%7E", "~", "%27", "'",
26+
"%28", "(", "%29", ")", "%3B", ";",
27+
"%2F", "/", "%3F", "?", "%3A", ":",
28+
"%40", "@", "%26", "&", "%3D", "=",
29+
"%2B", "+", "%24", "$", "%2C", ",", "%23", "#", "%2A", "*")
30+
31+
// indexOf returns the first index of pattern in str, starting at str[i].
32+
func indexOf(str string, pattern string, i int) int {
33+
if i > len(str)-1 {
34+
return -1
35+
}
36+
if i <= 0 {
37+
return strings.Index(str, pattern)
38+
}
39+
ind := strings.Index(str[i:], pattern)
40+
if ind == -1 {
41+
return -1
42+
}
43+
return ind + i
44+
}
45+
46+
// lastIndexOf returns the last index of pattern in str, starting at str[i].
47+
func lastIndexOf(str string, pattern string, i int) int {
48+
if i < 0 {
49+
return -1
50+
}
51+
if i >= len(str) {
52+
return strings.LastIndex(str, pattern)
53+
}
54+
_, size := utf8.DecodeRuneInString(str[i:])
55+
return strings.LastIndex(str[:i+size], pattern)
56+
}
57+
58+
// Return the index of pattern in target, starting at target[i].
59+
func runesIndexOf(target, pattern []rune, i int) int {
60+
if i > len(target)-1 {
61+
return -1
62+
}
63+
if i <= 0 {
64+
return runesIndex(target, pattern)
65+
}
66+
ind := runesIndex(target[i:], pattern)
67+
if ind == -1 {
68+
return -1
69+
}
70+
return ind + i
71+
}
72+
73+
func runesEqual(r1, r2 []rune) bool {
74+
if len(r1) != len(r2) {
75+
return false
76+
}
77+
for i, c := range r1 {
78+
if c != r2[i] {
79+
return false
80+
}
81+
}
82+
return true
83+
}
84+
85+
// The equivalent of strings.Index for rune slices.
86+
func runesIndex(r1, r2 []rune) int {
87+
last := len(r1) - len(r2)
88+
for i := 0; i <= last; i++ {
89+
if runesEqual(r1[i:i+len(r2)], r2) {
90+
return i
91+
}
92+
}
93+
return -1
94+
}

0 commit comments

Comments
 (0)