Skip to content

Commit 9d8fe78

Browse files
authored
Fix: DiffLinesToChars hash error.
Fix: DiffLinesToChars can not to hash same line string to same value.
1 parent 849d7eb commit 9d8fe78

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

diffmatchpatch/diff.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,18 +1312,19 @@ func (dmp *DiffMatchPatch) DiffFromDelta(text1 string, delta string) (diffs []Di
13121312
func (dmp *DiffMatchPatch) diffLinesToStrings(text1, text2 string) (string, string, []string) {
13131313
// '\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.
13141314
lineArray := []string{""} // e.g. lineArray[4] == 'Hello\n'
1315+
lineHash := make(map[string]int)
1316+
lineHash[""] = 0
13151317

13161318
//Each string has the index of lineArray which it points to
1317-
strIndexArray1 := dmp.diffLinesToStringsMunge(text1, &lineArray)
1318-
strIndexArray2 := dmp.diffLinesToStringsMunge(text2, &lineArray)
1319+
strIndexArray1 := dmp.diffLinesToStringsMunge(text1, &lineArray, lineHash)
1320+
strIndexArray2 := dmp.diffLinesToStringsMunge(text2, &lineArray, lineHash)
13191321

13201322
return intArrayToString(strIndexArray1), intArrayToString(strIndexArray2), lineArray
13211323
}
13221324

13231325
// diffLinesToStringsMunge splits a text into an array of strings, and reduces the texts to a []string.
1324-
func (dmp *DiffMatchPatch) diffLinesToStringsMunge(text string, lineArray *[]string) []uint32 {
1326+
func (dmp *DiffMatchPatch) diffLinesToStringsMunge(text string, lineArray *[]string, lineHash map[string]int) []uint32 {
13251327
// 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.
1326-
lineHash := map[string]int{} // e.g. lineHash['Hello\n'] == 4
13271328
lineStart := 0
13281329
lineEnd := -1
13291330
strs := []uint32{}

0 commit comments

Comments
 (0)