@@ -388,30 +388,31 @@ func (dmp *DiffMatchPatch) diffBisectSplit(runes1, runes2 []rune, x, y int,
388
388
}
389
389
390
390
// 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.
391
- // It's slightly faster to call DiffLinesToRunes first, followed by DiffMainRunes.
392
391
func (dmp * DiffMatchPatch ) DiffLinesToChars (text1 , text2 string ) (string , string , []string ) {
393
- chars1 , chars2 , lineArray := dmp .diffLinesToStrings (text1 , text2 )
394
- return chars1 , chars2 , lineArray
392
+ chars1 , chars2 , lineArray := dmp .diffLinesToIndexes (text1 , text2 )
393
+ return indexesToString ( chars1 ), indexesToString ( chars2 ) , lineArray
395
394
}
396
395
397
396
// DiffLinesToRunes splits two texts into a list of runes.
398
397
func (dmp * DiffMatchPatch ) DiffLinesToRunes (text1 , text2 string ) ([]rune , []rune , []string ) {
398
+ chars1 , chars2 , lineArray := dmp .diffLinesToIndexes (text1 , text2 )
399
+ return []rune (indexesToString (chars1 )), []rune (indexesToString (chars2 )), lineArray
400
+ }
401
+
402
+ func (dmp * DiffMatchPatch ) diffLinesToIndexes (text1 , text2 string ) ([]index , []index , []string ) {
399
403
chars1 , chars2 , lineArray := dmp .diffLinesToStrings (text1 , text2 )
400
- return [] rune ( chars1 ), [] rune ( chars2 ) , lineArray
404
+ return chars1 , chars2 , lineArray
401
405
}
402
406
403
407
// DiffCharsToLines rehydrates the text in a diff from a string of line hashes to real lines of text.
404
408
func (dmp * DiffMatchPatch ) DiffCharsToLines (diffs []Diff , lineArray []string ) []Diff {
405
409
hydrated := make ([]Diff , 0 , len (diffs ))
406
410
for _ , aDiff := range diffs {
407
- runes := []rune (aDiff .Text )
408
- text := make ([]string , len (runes ))
409
-
410
- for i , r := range runes {
411
- text [i ] = lineArray [runeToInt (r )]
411
+ var sb strings.Builder
412
+ for _ , i := range stringToIndex (aDiff .Text ) {
413
+ sb .WriteString (lineArray [i ])
412
414
}
413
-
414
- aDiff .Text = strings .Join (text , "" )
415
+ aDiff .Text = sb .String ()
415
416
hydrated = append (hydrated , aDiff )
416
417
}
417
418
return hydrated
@@ -1304,7 +1305,7 @@ func (dmp *DiffMatchPatch) DiffFromDelta(text1 string, delta string) (diffs []Di
1304
1305
}
1305
1306
1306
1307
// diffLinesToStrings splits two texts into a list of strings. Each string represents one line.
1307
- func (dmp * DiffMatchPatch ) diffLinesToStrings (text1 , text2 string ) (string , string , []string ) {
1308
+ func (dmp * DiffMatchPatch ) diffLinesToStrings (text1 , text2 string ) ([] index , [] index , []string ) {
1308
1309
// '\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.
1309
1310
lineArray := []string {"" } // e.g. lineArray[4] == 'Hello\n'
1310
1311
@@ -1313,15 +1314,15 @@ func (dmp *DiffMatchPatch) diffLinesToStrings(text1, text2 string) (string, stri
1313
1314
strIndexArray1 := dmp .diffLinesToStringsMunge (text1 , & lineArray , lineHash )
1314
1315
strIndexArray2 := dmp .diffLinesToStringsMunge (text2 , & lineArray , lineHash )
1315
1316
1316
- return intArrayToString ( strIndexArray1 ), intArrayToString ( strIndexArray2 ) , lineArray
1317
+ return strIndexArray1 , strIndexArray2 , lineArray
1317
1318
}
1318
1319
1319
1320
// diffLinesToStringsMunge splits a text into an array of strings, and reduces the texts to a []string.
1320
- func (dmp * DiffMatchPatch ) diffLinesToStringsMunge (text string , lineArray * []string , lineHash map [string ]int ) []uint32 {
1321
+ func (dmp * DiffMatchPatch ) diffLinesToStringsMunge (text string , lineArray * []string , lineHash map [string ]int ) []index {
1321
1322
// 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.
1322
1323
lineStart := 0
1323
1324
lineEnd := - 1
1324
- strs := []uint32 {}
1325
+ strs := []index {}
1325
1326
1326
1327
for lineEnd < len (text )- 1 {
1327
1328
lineEnd = indexOf (text , "\n " , lineStart )
@@ -1335,11 +1336,11 @@ func (dmp *DiffMatchPatch) diffLinesToStringsMunge(text string, lineArray *[]str
1335
1336
lineValue , ok := lineHash [line ]
1336
1337
1337
1338
if ok {
1338
- strs = append (strs , uint32 (lineValue ))
1339
+ strs = append (strs , index (lineValue ))
1339
1340
} else {
1340
1341
* lineArray = append (* lineArray , line )
1341
1342
lineHash [line ] = len (* lineArray ) - 1
1342
- strs = append (strs , uint32 (len (* lineArray )- 1 ))
1343
+ strs = append (strs , index (len (* lineArray )- 1 ))
1343
1344
}
1344
1345
}
1345
1346
0 commit comments