Skip to content

Commit 1307524

Browse files
committed
Added a New function that creates a default DiffMatchPatch
1 parent 987f9cb commit 1307524

File tree

2 files changed

+38
-53
lines changed

2 files changed

+38
-53
lines changed

diff/dmp.go

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -184,33 +184,18 @@ func (patch *Patch) String() string {
184184
return unescaper.Replace(text.String())
185185
}
186186

187-
func createDMP() DiffMatchPatch {
188-
dmp := DiffMatchPatch{}
187+
// New creates a new DiffMatchPatch object with default parameters.
188+
func New() *DiffMatchPatch {
189189
// Defaults.
190-
// Set these on your diff_match_patch instance to override the defaults.
191-
192-
// Number of seconds to map a diff before giving up (0 for infinity).
193-
dmp.DiffTimeout = 1.0
194-
// Cost of an empty edit operation in terms of edit characters.
195-
dmp.DiffEditCost = 4
196-
// At what point is no match declared (0.0 = perfection, 1.0 = very loose).
197-
dmp.MatchThreshold = 0.5
198-
// How far to search for a match (0 = exact location, 1000+ = broad match).
199-
// A match this many characters away from the expected location will add
200-
// 1.0 to the score (0.0 is a perfect match).
201-
dmp.MatchDistance = 1000
202-
// When deleting a large block of text (over ~64 characters), how close
203-
// do the contents have to be to match the expected contents. (0.0 =
204-
// perfection, 1.0 = very loose). Note that Match_Threshold controls
205-
// how closely the end points of a delete need to match.
206-
dmp.PatchDeleteThreshold = 0.5
207-
// Chunk size for context length.
208-
dmp.PatchMargin = 4
209-
210-
// The number of bits in an int.
211-
dmp.MatchMaxBits = 32
212-
213-
return dmp
190+
return &DiffMatchPatch{
191+
DiffTimeout: 1.0,
192+
DiffEditCost: 4,
193+
MatchThreshold: 0.5,
194+
MatchDistance: 1000,
195+
PatchDeleteThreshold: 0.5,
196+
PatchMargin: 4,
197+
MatchMaxBits: 32,
198+
}
214199
}
215200

216201
// DiffMain finds the differences between two texts.

diff/dmp_test.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func diffRebuildtexts(diffs []Diff) []string {
116116
}
117117

118118
func Test_diffCommonPrefix(t *testing.T) {
119-
dmp := createDMP()
119+
dmp := New()
120120
// Detect any common suffix.
121121
// Null case.
122122
assert.Equal(t, 0, dmp.DiffCommonPrefix("abc", "xyz"), "'abc' and 'xyz' should not be equal")
@@ -129,7 +129,7 @@ func Test_diffCommonPrefix(t *testing.T) {
129129
}
130130

131131
func Test_diffCommonSuffixTest(t *testing.T) {
132-
dmp := createDMP()
132+
dmp := New()
133133
// Detect any common suffix.
134134
// Null case.
135135
assert.Equal(t, 0, dmp.DiffCommonSuffix("abc", "xyz"), "")
@@ -142,7 +142,7 @@ func Test_diffCommonSuffixTest(t *testing.T) {
142142
}
143143

144144
func Test_diffCommonOverlapTest(t *testing.T) {
145-
dmp := createDMP()
145+
dmp := New()
146146
// Detect any suffix/prefix overlap.
147147
// Null case.
148148
assert.Equal(t, 0, dmp.DiffCommonOverlap("", "abcd"), "")
@@ -163,7 +163,7 @@ func Test_diffCommonOverlapTest(t *testing.T) {
163163
}
164164

165165
func Test_diffHalfmatchTest(t *testing.T) {
166-
dmp := createDMP()
166+
dmp := New()
167167
dmp.DiffTimeout = 1
168168
// No match.
169169
softAssert(t, dmp.DiffHalfMatch("1234567890", "abcdef") == nil, "")
@@ -197,7 +197,7 @@ func Test_diffHalfmatchTest(t *testing.T) {
197197
}
198198

199199
func Test_diffLinesToChars(t *testing.T) {
200-
dmp := createDMP()
200+
dmp := New()
201201
// Convert lines down to characters.
202202
tmpVector := []string{"", "alpha\n", "beta\n"}
203203

@@ -246,7 +246,7 @@ func Test_diffLinesToChars(t *testing.T) {
246246
}
247247

248248
func Test_diffCharsToLines(t *testing.T) {
249-
dmp := createDMP()
249+
dmp := New()
250250
// Convert chars up to lines.
251251
diffs := []Diff{
252252
Diff{DiffEqual, "\u0001\u0002\u0001"},
@@ -281,7 +281,7 @@ func Test_diffCharsToLines(t *testing.T) {
281281
}
282282

283283
func Test_diffCleanupMerge(t *testing.T) {
284-
dmp := createDMP()
284+
dmp := New()
285285
// Cleanup a messy diff.
286286
// Null case.
287287
diffs := []Diff{}
@@ -346,7 +346,7 @@ func Test_diffCleanupMerge(t *testing.T) {
346346
}
347347

348348
func Test_diffCleanupSemanticLossless(t *testing.T) {
349-
dmp := createDMP()
349+
dmp := New()
350350
// Slide diffs to match logical boundaries.
351351
// Null case.
352352
diffs := []Diff{}
@@ -445,7 +445,7 @@ func Test_diffCleanupSemanticLossless(t *testing.T) {
445445
}
446446

447447
func Test_diffCleanupSemantic(t *testing.T) {
448-
dmp := createDMP()
448+
dmp := New()
449449
// Cleanup semantically trivial equalities.
450450
// Null case.
451451
diffs := []Diff{}
@@ -575,7 +575,7 @@ func Test_diffCleanupSemantic(t *testing.T) {
575575
}
576576

577577
func Test_diffCleanupEfficiency(t *testing.T) {
578-
dmp := createDMP()
578+
dmp := New()
579579
// Cleanup operationally trivial equalities.
580580
dmp.DiffEditCost = 4
581581
// Null case.
@@ -652,7 +652,7 @@ func Test_diffCleanupEfficiency(t *testing.T) {
652652

653653
/*
654654
func Test_diffPrettyHtml(t *testing.T) {
655-
dmp := createDMP()
655+
dmp := New()
656656
// Pretty print.
657657
diffs := []Diff{
658658
Diff{DiffEqual, "a\n"},
@@ -663,7 +663,7 @@ func Test_diffPrettyHtml(t *testing.T) {
663663
}*/
664664

665665
func Test_diffText(t *testing.T) {
666-
dmp := createDMP()
666+
dmp := New()
667667
// Compute the source and destination texts.
668668
diffs := []Diff{
669669
Diff{DiffEqual, "jump"},
@@ -678,7 +678,7 @@ func Test_diffText(t *testing.T) {
678678
}
679679

680680
func Test_diffDelta(t *testing.T) {
681-
dmp := createDMP()
681+
dmp := New()
682682
// Convert a diff into delta string.
683683
diffs := []Diff{
684684
Diff{DiffEqual, "jump"},
@@ -753,7 +753,7 @@ func Test_diffDelta(t *testing.T) {
753753
}
754754

755755
func Test_diffXIndex(t *testing.T) {
756-
dmp := createDMP()
756+
dmp := New()
757757
// Translate a location in text1 to text2.
758758
diffs := []Diff{
759759
Diff{DiffDelete, "a"},
@@ -769,7 +769,7 @@ func Test_diffXIndex(t *testing.T) {
769769
}
770770

771771
func Test_diffLevenshtein(t *testing.T) {
772-
dmp := createDMP()
772+
dmp := New()
773773
diffs := []Diff{
774774
Diff{DiffDelete, "abc"},
775775
Diff{DiffInsert, "1234"},
@@ -790,7 +790,7 @@ func Test_diffLevenshtein(t *testing.T) {
790790
}
791791

792792
func Test_diffBisect(t *testing.T) {
793-
dmp := createDMP()
793+
dmp := New()
794794
// Normal.
795795
a := "cat"
796796
b := "map"
@@ -812,7 +812,7 @@ func Test_diffBisect(t *testing.T) {
812812
}
813813

814814
func Test_diffMain(t *testing.T) {
815-
dmp := createDMP()
815+
dmp := New()
816816
// Perform a trivial diff.
817817
diffs := []Diff{}
818818
assertDiffEqual(t, diffs, dmp.DiffMain("", "", false))
@@ -908,7 +908,7 @@ func Test_diffMain(t *testing.T) {
908908
}
909909

910910
func Test_match_alphabet(t *testing.T) {
911-
dmp := createDMP()
911+
dmp := New()
912912
// Initialise the bitmasks for Bitap.
913913
bitmask := map[byte]int{
914914
'a': 4,
@@ -926,7 +926,7 @@ func Test_match_alphabet(t *testing.T) {
926926
}
927927

928928
func Test_match_bitap(t *testing.T) {
929-
dmp := createDMP()
929+
dmp := New()
930930

931931
// Bitap algorithm.
932932
dmp.MatchDistance = 100
@@ -973,7 +973,7 @@ func Test_match_bitap(t *testing.T) {
973973
}
974974

975975
func Test_MatchMain(t *testing.T) {
976-
dmp := createDMP()
976+
dmp := New()
977977
// Full match.
978978
assert.Equal(t, 0, dmp.MatchMain("abcdef", "abcdef", 1000), "MatchMain: Equality.")
979979

@@ -1015,7 +1015,7 @@ func Test_patch_patchObj(t *testing.T) {
10151015
}
10161016

10171017
func Test_patch_fromText(t *testing.T) {
1018-
dmp := createDMP()
1018+
dmp := New()
10191019

10201020
_v1, _ := dmp.PatchFromText("")
10211021
softAssert(t, len(_v1) == 0, "patch_fromText: #0.")
@@ -1038,7 +1038,7 @@ func Test_patch_fromText(t *testing.T) {
10381038
}
10391039

10401040
func Test_patch_toText(t *testing.T) {
1041-
dmp := createDMP()
1041+
dmp := New()
10421042
strp := "@@ -21,18 +22,17 @@\n jump\n-s\n+ed\n over \n-the\n+a\n laz\n"
10431043
var patches []Patch
10441044
patches, _ = dmp.PatchFromText(strp)
@@ -1052,7 +1052,7 @@ func Test_patch_toText(t *testing.T) {
10521052
}
10531053

10541054
func Test_patch_addContext(t *testing.T) {
1055-
dmp := createDMP()
1055+
dmp := New()
10561056
dmp.PatchMargin = 4
10571057
var p Patch
10581058
_p, _ := dmp.PatchFromText("@@ -21,4 +21,10 @@\n-jump\n+somersault\n")
@@ -1077,7 +1077,7 @@ func Test_patch_addContext(t *testing.T) {
10771077
}
10781078

10791079
func Test_patch_make(t *testing.T) {
1080-
dmp := createDMP()
1080+
dmp := New()
10811081
var patches []Patch
10821082
patches = dmp.PatchMake("", "")
10831083
assert.Equal(t, "", dmp.PatchToText(patches), "patch_make: Null case.")
@@ -1131,7 +1131,7 @@ func Test_patch_make(t *testing.T) {
11311131

11321132
func Test_PatchSplitMax(t *testing.T) {
11331133
// Assumes that Match_MaxBits is 32.
1134-
dmp := createDMP()
1134+
dmp := New()
11351135
var patches []Patch
11361136

11371137
patches = dmp.PatchMake("abcdefghijklmnopqrstuvwxyz01234567890", "XabXcdXefXghXijXklXmnXopXqrXstXuvXwxXyzX01X23X45X67X89X0")
@@ -1153,7 +1153,7 @@ func Test_PatchSplitMax(t *testing.T) {
11531153
}
11541154

11551155
func Test_PatchAddPadding(t *testing.T) {
1156-
dmp := createDMP()
1156+
dmp := New()
11571157
var patches []Patch
11581158
patches = dmp.PatchMake("", "test")
11591159
assert.Equal(t, "@@ -0,0 +1,4 @@\n+test\n",
@@ -1184,7 +1184,7 @@ func Test_PatchAddPadding(t *testing.T) {
11841184
}
11851185

11861186
func Test_patchApply(t *testing.T) {
1187-
dmp := createDMP()
1187+
dmp := New()
11881188
dmp.MatchDistance = 1000
11891189
dmp.MatchThreshold = 0.5
11901190
dmp.PatchDeleteThreshold = 0.5

0 commit comments

Comments
 (0)