Skip to content

Commit 6b5d58a

Browse files
committed
HURRAY, only one comment style...
1 parent ac88a7c commit 6b5d58a

File tree

6 files changed

+66
-140
lines changed

6 files changed

+66
-140
lines changed

diffmatchpatch/diff.go

Lines changed: 44 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ func (dmp *DiffMatchPatch) diffMainRunes(text1, text2 []rune, checklines bool, d
9292
return dmp.DiffCleanupMerge(diffs)
9393
}
9494

95-
// diffCompute finds the differences between two rune slices. Assumes that the texts do not
96-
// have any common prefix or suffix.
95+
// diffCompute finds the differences between two rune slices. Assumes that the texts do not have any common prefix or suffix.
9796
func (dmp *DiffMatchPatch) diffCompute(text1, text2 []rune, checklines bool, deadline time.Time) []Diff {
9897
diffs := []Diff{}
9998
if len(text1) == 0 {
@@ -151,8 +150,7 @@ func (dmp *DiffMatchPatch) diffCompute(text1, text2 []rune, checklines bool, dea
151150
return dmp.diffBisect(text1, text2, deadline)
152151
}
153152

154-
// diffLineMode does a quick line-level diff on both []runes, then rediff the parts for
155-
// greater accuracy. This speedup can produce non-minimal diffs.
153+
// diffLineMode does a quick line-level diff on both []runes, then rediff the parts for greater accuracy. This speedup can produce non-minimal diffs.
156154
func (dmp *DiffMatchPatch) diffLineMode(text1, text2 []rune, deadline time.Time) []Diff {
157155
// Scan the text on a line-by-line basis first.
158156
text1, text2, linearray := dmp.diffLinesToRunes(text1, text2)
@@ -210,16 +208,14 @@ func (dmp *DiffMatchPatch) diffLineMode(text1, text2 []rune, deadline time.Time)
210208
return diffs[:len(diffs)-1] // Remove the dummy entry at the end.
211209
}
212210

213-
// DiffBisect finds the 'middle snake' of a diff, split the problem in two
214-
// and return the recursively constructed diff.
211+
// DiffBisect finds the 'middle snake' of a diff, split the problem in two and return the recursively constructed diff.
215212
// See Myers 1986 paper: An O(ND) Difference Algorithm and Its Variations.
216213
func (dmp *DiffMatchPatch) DiffBisect(text1, text2 string, deadline time.Time) []Diff {
217214
// Unused in this code, but retained for interface compatibility.
218215
return dmp.diffBisect([]rune(text1), []rune(text2), deadline)
219216
}
220217

221-
// diffBisect finds the 'middle snake' of a diff, splits the problem in two
222-
// and returns the recursively constructed diff.
218+
// diffBisect finds the 'middle snake' of a diff, splits the problem in two and returns the recursively constructed diff.
223219
// See Myers's 1986 paper: An O(ND) Difference Algorithm and Its Variations.
224220
func (dmp *DiffMatchPatch) diffBisect(runes1, runes2 []rune, deadline time.Time) []Diff {
225221
// Cache the text lengths to prevent multiple calls.
@@ -239,11 +235,9 @@ func (dmp *DiffMatchPatch) diffBisect(runes1, runes2 []rune, deadline time.Time)
239235
v2[vOffset+1] = 0
240236

241237
delta := runes1Len - runes2Len
242-
// If the total number of characters is odd, then the front path will collide
243-
// with the reverse path.
238+
// If the total number of characters is odd, then the front path will collide with the reverse path.
244239
front := (delta%2 != 0)
245-
// Offsets for start and end of k loop.
246-
// Prevents mapping of space beyond the grid.
240+
// Offsets for start and end of k loop. Prevents mapping of space beyond the grid.
247241
k1start := 0
248242
k1end := 0
249243
k2start := 0
@@ -331,8 +325,7 @@ func (dmp *DiffMatchPatch) diffBisect(runes1, runes2 []rune, deadline time.Time)
331325
}
332326
}
333327
}
334-
// Diff took too long and hit the deadline or
335-
// number of diffs equals number of characters, no commonality at all.
328+
// Diff took too long and hit the deadline or number of diffs equals number of characters, no commonality at all.
336329
return []Diff{
337330
Diff{DiffDelete, string(runes1)},
338331
Diff{DiffInsert, string(runes2)},
@@ -353,8 +346,7 @@ func (dmp *DiffMatchPatch) diffBisectSplit(runes1, runes2 []rune, x, y int,
353346
return append(diffs, diffsb...)
354347
}
355348

356-
// DiffLinesToChars splits two texts into a list of strings. Reduces the texts to a string of
357-
// hashes where each Unicode character represents one line.
349+
// DiffLinesToChars splits two texts into a list of strings, and educes the texts to a string of hashes where each Unicode character represents one line.
358350
// It's slightly faster to call DiffLinesToRunes first, followed by DiffMainRunes.
359351
func (dmp *DiffMatchPatch) DiffLinesToChars(text1, text2 string) (string, string, []string) {
360352
chars1, chars2, lineArray := dmp.DiffLinesToRunes(text1, text2)
@@ -363,8 +355,7 @@ func (dmp *DiffMatchPatch) DiffLinesToChars(text1, text2 string) (string, string
363355

364356
// DiffLinesToRunes splits two texts into a list of runes. Each rune represents one line.
365357
func (dmp *DiffMatchPatch) DiffLinesToRunes(text1, text2 string) ([]rune, []rune, []string) {
366-
// '\x00' is a valid character, but various debuggers don't like it.
367-
// So we'll insert a junk entry to avoid generating a null character.
358+
// '\x00' is a valid character, but various debuggers don't like it. So we'll insert a junk entry to avoid generating a null character.
368359
lineArray := []string{""} // e.g. lineArray[4] == 'Hello\n'
369360
lineHash := map[string]int{} // e.g. lineHash['Hello\n'] == 4
370361

@@ -378,13 +369,10 @@ func (dmp *DiffMatchPatch) diffLinesToRunes(text1, text2 []rune) ([]rune, []rune
378369
return dmp.DiffLinesToRunes(string(text1), string(text2))
379370
}
380371

381-
// diffLinesToRunesMunge splits a text into an array of strings. Reduces the
382-
// texts to a []rune where each Unicode character represents one line.
372+
// diffLinesToRunesMunge splits a text into an array of strings, and reduces the texts to a []rune where each Unicode character represents one line.
383373
// We use strings instead of []runes as input mainly because you can't use []rune as a map key.
384374
func (dmp *DiffMatchPatch) diffLinesToRunesMunge(text string, lineArray *[]string, lineHash map[string]int) []rune {
385-
// Walk the text, pulling out a substring for each line.
386-
// text.split('\n') would would temporarily double our memory footprint.
387-
// Modifying text would create many large strings to garbage collect.
375+
// Walk the text, pulling out a substring for each line. text.split('\n') would would temporarily double our memory footprint. Modifying text would create many large strings to garbage collect.
388376
lineStart := 0
389377
lineEnd := -1
390378
runes := []rune{}
@@ -412,8 +400,7 @@ func (dmp *DiffMatchPatch) diffLinesToRunesMunge(text string, lineArray *[]strin
412400
return runes
413401
}
414402

415-
// DiffCharsToLines rehydrates the text in a diff from a string of line hashes to real lines of
416-
// text.
403+
// DiffCharsToLines rehydrates the text in a diff from a string of line hashes to real lines of text.
417404
func (dmp *DiffMatchPatch) DiffCharsToLines(diffs []Diff, lineArray []string) []Diff {
418405
hydrated := make([]Diff, 0, len(diffs))
419406
for _, aDiff := range diffs {
@@ -509,9 +496,7 @@ func (dmp *DiffMatchPatch) DiffCommonOverlap(text1 string, text2 string) int {
509496
return textLength
510497
}
511498

512-
// Start by looking for a single character match
513-
// and increase length until no match is found.
514-
// Performance analysis: http://neil.fraser.name/news/2010/11/04/
499+
// Start by looking for a single character match and increase length until no match is found. Performance analysis: http://neil.fraser.name/news/2010/11/04/
515500
best := 0
516501
length := 1
517502
for {
@@ -530,8 +515,7 @@ func (dmp *DiffMatchPatch) DiffCommonOverlap(text1 string, text2 string) int {
530515
return best
531516
}
532517

533-
// DiffHalfMatch checks whether the two texts share a substring which is at
534-
// least half the length of the longer text. This speedup can produce non-minimal diffs.
518+
// DiffHalfMatch checks whether the two texts share a substring which is at least half the length of the longer text. This speedup can produce non-minimal diffs.
535519
func (dmp *DiffMatchPatch) DiffHalfMatch(text1, text2 string) []string {
536520
// Unused in this code, but retained for interface compatibility.
537521
runeSlices := dmp.diffHalfMatch([]rune(text1), []rune(text2))
@@ -596,12 +580,7 @@ func (dmp *DiffMatchPatch) diffHalfMatch(text1, text2 []rune) [][]rune {
596580
}
597581

598582
// diffHalfMatchI checks if a substring of shorttext exist within longtext such that the substring is at least half the length of longtext?
599-
// @param {string} longtext Longer string.
600-
// @param {string} shorttext Shorter string.
601-
// @param {number} i Start index of quarter length substring within longtext.
602-
// @return {Array.<string>} Five element Array, containing the prefix of
603-
// longtext, the suffix of longtext, the prefix of shorttext, the suffix
604-
// of shorttext and the common middle. Or null if there was no match.
583+
// Returns a slice containing the prefix of longtext, the suffix of longtext, the prefix of shorttext, the suffix of shorttext and the common middle, or null if there was no match.
605584
func (dmp *DiffMatchPatch) diffHalfMatchI(l, s []rune, i int) [][]rune {
606585
var bestCommonA []rune
607586
var bestCommonB []rune
@@ -642,8 +621,7 @@ func (dmp *DiffMatchPatch) diffHalfMatchI(l, s []rune, i int) [][]rune {
642621
}
643622
}
644623

645-
// DiffCleanupSemantic reduces the number of edits by eliminating
646-
// semantically trivial equalities.
624+
// DiffCleanupSemantic reduces the number of edits by eliminating semantically trivial equalities.
647625
func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {
648626
changes := false
649627
// Stack of indices where equalities are found.
@@ -662,7 +640,9 @@ func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {
662640
var lengthInsertions2, lengthDeletions2 int
663641

664642
for pointer < len(diffs) {
665-
if diffs[pointer].Type == DiffEqual { // Equality found.
643+
if diffs[pointer].Type == DiffEqual {
644+
// Equality found.
645+
666646
equalities = &equality{
667647
data: pointer,
668648
next: equalities,
@@ -672,14 +652,15 @@ func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {
672652
lengthInsertions2 = 0
673653
lengthDeletions2 = 0
674654
lastequality = diffs[pointer].Text
675-
} else { // An insertion or deletion.
655+
} else {
656+
// An insertion or deletion.
657+
676658
if diffs[pointer].Type == DiffInsert {
677659
lengthInsertions2 += len(diffs[pointer].Text)
678660
} else {
679661
lengthDeletions2 += len(diffs[pointer].Text)
680662
}
681-
// Eliminate an equality that is smaller or equal to the edits on both
682-
// sides of it.
663+
// Eliminate an equality that is smaller or equal to the edits on both sides of it.
683664
difference1 := int(math.Max(float64(lengthInsertions1), float64(lengthDeletions1)))
684665
difference2 := int(math.Max(float64(lengthInsertions2), float64(lengthDeletions2)))
685666
if len(lastequality) > 0 &&
@@ -739,12 +720,11 @@ func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {
739720
if float64(overlapLength1) >= float64(len(deletion))/2 ||
740721
float64(overlapLength1) >= float64(len(insertion))/2 {
741722

742-
// Overlap found. Insert an equality and trim the surrounding edits.
723+
// Overlap found. Insert an equality and trim the surrounding edits.
743724
diffs = append(
744725
diffs[:pointer],
745726
append([]Diff{Diff{DiffEqual, insertion[:overlapLength1]}}, diffs[pointer:]...)...)
746-
//diffs.splice(pointer, 0,
747-
// [DiffEqual, insertion[0 : overlapLength1)]]
727+
748728
diffs[pointer-1].Text =
749729
deletion[0 : len(deletion)-overlapLength1]
750730
diffs[pointer+1].Text = insertion[overlapLength1:]
@@ -753,14 +733,12 @@ func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {
753733
} else {
754734
if float64(overlapLength2) >= float64(len(deletion))/2 ||
755735
float64(overlapLength2) >= float64(len(insertion))/2 {
756-
// Reverse overlap found.
757-
// Insert an equality and swap and trim the surrounding edits.
736+
// Reverse overlap found. Insert an equality and swap and trim the surrounding edits.
758737
overlap := Diff{DiffEqual, deletion[:overlapLength2]}
759738
diffs = append(
760739
diffs[:pointer],
761740
append([]Diff{overlap}, diffs[pointer:]...)...)
762-
// diffs.splice(pointer, 0,
763-
// [DiffEqual, deletion[0 : overlapLength2)]]
741+
764742
diffs[pointer-1].Type = DiffInsert
765743
diffs[pointer-1].Text = insertion[0 : len(insertion)-overlapLength2]
766744
diffs[pointer+1].Type = DiffDelete
@@ -785,18 +763,15 @@ var (
785763
blanklineStartRegex = regexp.MustCompile(`^\r?\n\r?\n`)
786764
)
787765

788-
// diffCleanupSemanticScore computes a score representing whether the internal boundary falls on logical boundaries. Scores range from 6 (best) to 0 (worst). Closure, but does not reference any external variables.
766+
// diffCleanupSemanticScore computes a score representing whether the internal boundary falls on logical boundaries.
767+
// Scores range from 6 (best) to 0 (worst). Closure, but does not reference any external variables.
789768
func diffCleanupSemanticScore(one, two string) int {
790769
if len(one) == 0 || len(two) == 0 {
791770
// Edges are the best.
792771
return 6
793772
}
794773

795-
// Each port of this function behaves slightly differently due to
796-
// subtle differences in each language's definition of things like
797-
// 'whitespace'. Since this function's purpose is largely cosmetic,
798-
// the choice has been made to use each language's native features
799-
// rather than force total conformity.
774+
// Each port of this function behaves slightly differently due to subtle differences in each language's definition of things like 'whitespace'. Since this function's purpose is largely cosmetic, the choice has been made to use each language's native features rather than force total conformity.
800775
rune1, _ := utf8.DecodeLastRuneInString(one)
801776
rune2, _ := utf8.DecodeRuneInString(two)
802777
char1 := string(rune1)
@@ -830,9 +805,8 @@ func diffCleanupSemanticScore(one, two string) int {
830805
return 0
831806
}
832807

833-
// DiffCleanupSemanticLossless looks for single edits surrounded on both sides by equalities
834-
// which can be shifted sideways to align the edit to a word boundary.
835-
// e.g: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.
808+
// DiffCleanupSemanticLossless looks for single edits surrounded on both sides by equalities which can be shifted sideways to align the edit to a word boundary.
809+
// E.g: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.
836810
func (dmp *DiffMatchPatch) DiffCleanupSemanticLossless(diffs []Diff) []Diff {
837811
pointer := 1
838812

@@ -872,8 +846,7 @@ func (dmp *DiffMatchPatch) DiffCleanupSemanticLossless(diffs []Diff) []Diff {
872846
equality2 = equality2[sz:]
873847
score := diffCleanupSemanticScore(equality1, edit) +
874848
diffCleanupSemanticScore(edit, equality2)
875-
// The >= encourages trailing rather than leading whitespace on
876-
// edits.
849+
// The >= encourages trailing rather than leading whitespace on edits.
877850
if score >= bestScore {
878851
bestScore = score
879852
bestEquality1 = equality1
@@ -895,7 +868,6 @@ func (dmp *DiffMatchPatch) DiffCleanupSemanticLossless(diffs []Diff) []Diff {
895868
if len(bestEquality2) != 0 {
896869
diffs[pointer+1].Text = bestEquality2
897870
} else {
898-
//splice(diffs, pointer+1, 1)
899871
diffs = append(diffs[:pointer+1], diffs[pointer+2:]...)
900872
pointer--
901873
}
@@ -907,8 +879,7 @@ func (dmp *DiffMatchPatch) DiffCleanupSemanticLossless(diffs []Diff) []Diff {
907879
return diffs
908880
}
909881

910-
// DiffCleanupEfficiency reduces the number of edits by eliminating
911-
// operationally trivial equalities.
882+
// DiffCleanupEfficiency reduces the number of edits by eliminating operationally trivial equalities.
912883
func (dmp *DiffMatchPatch) DiffCleanupEfficiency(diffs []Diff) []Diff {
913884
changes := false
914885
// Stack of indices where equalities are found.
@@ -1019,7 +990,7 @@ func (dmp *DiffMatchPatch) DiffCleanupEfficiency(diffs []Diff) []Diff {
1019990
return diffs
1020991
}
1021992

1022-
// DiffCleanupMerge reorders and merges like edit sections. Merge equalities.
993+
// DiffCleanupMerge reorders and merges like edit sections. Merge equalities.
1023994
// Any edit section can move as long as it doesn't cross an equality.
1024995
func (dmp *DiffMatchPatch) DiffCleanupMerge(diffs []Diff) []Diff {
1025996
// Add a dummy entry at the end.
@@ -1112,9 +1083,7 @@ func (dmp *DiffMatchPatch) DiffCleanupMerge(diffs []Diff) []Diff {
11121083
diffs = diffs[0 : len(diffs)-1] // Remove the dummy entry at the end.
11131084
}
11141085

1115-
// Second pass: look for single edits surrounded on both sides by
1116-
// equalities which can be shifted sideways to eliminate an equality.
1117-
// e.g: A<ins>BA</ins>C -> <ins>AB</ins>AC
1086+
// Second pass: look for single edits surrounded on both sides by equalities which can be shifted sideways to eliminate an equality. E.g: A<ins>BA</ins>C -> <ins>AB</ins>AC
11181087
changes := false
11191088
pointer = 1
11201089
// Intentionally ignore the first and last element (don't need checking).
@@ -1150,9 +1119,6 @@ func (dmp *DiffMatchPatch) DiffCleanupMerge(diffs []Diff) []Diff {
11501119
}
11511120

11521121
// DiffXIndex returns the equivalent location in s2.
1153-
// loc is a location in text1, comAdde and return the equivalent location in
1154-
// text2.
1155-
// e.g. "The cat" vs "The big cat", 1->1, 5->8
11561122
func (dmp *DiffMatchPatch) DiffXIndex(diffs []Diff, loc int) int {
11571123
chars1 := 0
11581124
chars2 := 0
@@ -1186,8 +1152,7 @@ func (dmp *DiffMatchPatch) DiffXIndex(diffs []Diff, loc int) int {
11861152
}
11871153

11881154
// DiffPrettyHtml converts a []Diff into a pretty HTML report.
1189-
// It is intended as an example from which to write one's own
1190-
// display functions.
1155+
// It is intended as an example from which to write one's own display functions.
11911156
func (dmp *DiffMatchPatch) DiffPrettyHtml(diffs []Diff) string {
11921157
var buff bytes.Buffer
11931158
for _, diff := range diffs {
@@ -1258,8 +1223,7 @@ func (dmp *DiffMatchPatch) DiffText2(diffs []Diff) string {
12581223
return text.String()
12591224
}
12601225

1261-
// DiffLevenshtein computes the Levenshtein distance; the number of inserted, deleted or
1262-
// substituted characters.
1226+
// DiffLevenshtein computes the Levenshtein distance that is the number of inserted, deleted or substituted characters.
12631227
func (dmp *DiffMatchPatch) DiffLevenshtein(diffs []Diff) int {
12641228
levenshtein := 0
12651229
insertions := 0
@@ -1283,11 +1247,8 @@ func (dmp *DiffMatchPatch) DiffLevenshtein(diffs []Diff) int {
12831247
return levenshtein
12841248
}
12851249

1286-
// DiffToDelta crushes the diff into an encoded string which describes the operations
1287-
// required to transform text1 into text2.
1288-
// E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'.
1289-
// Operations are tab-separated. Inserted text is escaped using %xx
1290-
// notation.
1250+
// DiffToDelta crushes the diff into an encoded string which describes the operations required to transform text1 into text2.
1251+
// E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'. Operations are tab-separated. Inserted text is escaped using %xx notation.
12911252
func (dmp *DiffMatchPatch) DiffToDelta(diffs []Diff) string {
12921253
var text bytes.Buffer
12931254
for _, aDiff := range diffs {
@@ -1318,8 +1279,7 @@ func (dmp *DiffMatchPatch) DiffToDelta(diffs []Diff) string {
13181279
return delta
13191280
}
13201281

1321-
// DiffFromDelta given the original text1, and an encoded string which describes the
1322-
// operations required to transform text1 into text2, comAdde the full diff.
1282+
// DiffFromDelta given the original text1, and an encoded string which describes the operations required to transform text1 into text2, comAdde the full diff.
13231283
func (dmp *DiffMatchPatch) DiffFromDelta(text1, delta string) (diffs []Diff, err error) {
13241284
diffs = []Diff{}
13251285

@@ -1338,13 +1298,12 @@ func (dmp *DiffMatchPatch) DiffFromDelta(text1, delta string) (diffs []Diff, err
13381298
continue
13391299
}
13401300

1341-
// Each token begins with a one character parameter which specifies the
1342-
// operation of this token (delete, insert, equality).
1301+
// Each token begins with a one character parameter which specifies the operation of this token (delete, insert, equality).
13431302
param := token[1:]
13441303

13451304
switch op := token[0]; op {
13461305
case '+':
1347-
// decode would Diff all "+" to " "
1306+
// Decode would Diff all "+" to " "
13481307
param = strings.Replace(param, "+", "%2b", -1)
13491308
param, err = url.QueryUnescape(param)
13501309
if err != nil {
@@ -1362,7 +1321,7 @@ func (dmp *DiffMatchPatch) DiffFromDelta(text1, delta string) (diffs []Diff, err
13621321
return diffs, errors.New("Negative number in DiffFromDelta: " + param)
13631322
}
13641323

1365-
// remember that string slicing is by byte - we want by rune here.
1324+
// Remember that string slicing is by byte - we want by rune here.
13661325
text := string([]rune(text1)[pointer : pointer+int(n)])
13671326
pointer += int(n)
13681327

0 commit comments

Comments
 (0)