@@ -511,18 +511,18 @@ func (dmp *DiffMatchPatch) DiffLinesToChars(text1, text2 string) (string, string
511
511
return chars1 , chars2 , lineArray
512
512
}
513
513
514
- // DiffLinesToChars split two texts into a list of strings. Reduces the texts to a string of
515
- // hashes where each Unicode character represents one line .
514
+ // DiffWordsToChars split two texts into a list of strings. Reduces the texts to a string of
515
+ // hashes where each Unicode character represents one word .
516
516
func (dmp * DiffMatchPatch ) DiffWordsToChars (text1 , text2 string ) (string , string , []string ) {
517
517
// '\x00' is a valid character, but various debuggers don't like it.
518
518
// So we'll insert a junk entry to avoid generating a null character.
519
- lineArray := []string {"" } // e.g. lineArray[4] == 'Hello\n'
520
- lineHash := map [string ]int {} // e.g. lineHash['Hello\n'] == 4
519
+ wordArray := []string {"" } // e.g. lineArray[4] == 'Hello\n'
520
+ wordHash := map [string ]int {} // e.g. lineHash['Hello\n'] == 4
521
521
522
- chars1 := dmp .diffWordsToCharsMunge (text1 , & lineArray , lineHash )
523
- chars2 := dmp .diffWordsToCharsMunge (text2 , & lineArray , lineHash )
522
+ chars1 := dmp .diffWordsToCharsMunge (text1 , & wordArray , wordHash )
523
+ chars2 := dmp .diffWordsToCharsMunge (text2 , & wordArray , wordHash )
524
524
525
- return chars1 , chars2 , lineArray
525
+ return chars1 , chars2 , wordArray
526
526
}
527
527
528
528
// diffLinesToCharsMunge splits a text into an array of strings. Reduces the texts to a string of
@@ -559,40 +559,38 @@ func (dmp *DiffMatchPatch) diffLinesToCharsMunge(text string, lineArray *[]strin
559
559
return string (runes )
560
560
}
561
561
562
- // diffLinesToCharsMunge splits a text into an array of strings. Reduces the texts to a string of
563
- // hashes where each Unicode character represents one line.
564
- // Modifies linearray and linehash through being a closure.
565
- func (dmp * DiffMatchPatch ) diffWordsToCharsMunge (text string , lineArray * []string , lineHash map [string ]int ) string {
566
- // Walk the text, pulling out a substring for each line.
567
- // text.split('\n') would would temporarily double our memory footprint.
568
- // Modifying text would create many large strings to garbage collect.
569
- lineStart := 0
570
- lineEnd := - 1
562
+ // diffWordsToCharsMunge splits a text into an array of strings. Reduces the texts to a string of
563
+ // hashes where each Unicode character represents one word.
564
+ // Modifies wordarray and wordhash through being a closure.
565
+ func (dmp * DiffMatchPatch ) diffWordsToCharsMunge (text string , wordArray * []string , wordHash map [string ]int ) string {
566
+ // Walk the text, pulling out a substring for each word.
567
+ wordStart := 0
568
+ wordEnd := - 1
571
569
runes := []rune {}
572
570
573
- for lineEnd < len (text )- 1 {
574
- lineIndex := indexOf (text , "\n " , lineStart )
575
- wordIndex := indexOf (text , " " , lineStart )
571
+ for wordEnd < len (text )- 1 {
572
+ lineIndex := indexOf (text , "\n " , wordStart )
573
+ wordIndex := indexOf (text , " " , wordStart )
576
574
if lineIndex < wordIndex && lineIndex != - 1 || wordIndex == - 1 {
577
- lineEnd = lineIndex
575
+ wordEnd = lineIndex
578
576
} else {
579
- lineEnd = wordIndex
577
+ wordEnd = wordIndex
580
578
}
581
579
582
- if lineEnd == - 1 {
583
- lineEnd = len (text ) - 1
580
+ if wordEnd == - 1 {
581
+ wordEnd = len (text ) - 1
584
582
}
585
583
586
- line := text [lineStart : lineEnd + 1 ]
587
- lineStart = lineEnd + 1
588
- lineValue_ , ok := lineHash [ line ]
584
+ word := text [wordStart : wordEnd + 1 ]
585
+ wordStart = wordEnd + 1
586
+ wordValue_ , ok := wordHash [ word ]
589
587
590
588
if ok {
591
- runes = append (runes , rune (lineValue_ ))
589
+ runes = append (runes , rune (wordValue_ ))
592
590
} else {
593
- * lineArray = append (* lineArray , line )
594
- lineHash [ line ] = len (* lineArray ) - 1
595
- runes = append (runes , rune (len (* lineArray )- 1 ))
591
+ * wordArray = append (* wordArray , word )
592
+ wordHash [ word ] = len (* wordArray ) - 1
593
+ runes = append (runes , rune (len (* wordArray )- 1 ))
596
594
}
597
595
}
598
596
0 commit comments