@@ -388,24 +388,29 @@ 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
+ // diffLinesToIndexes splits two texts into a list of indexes
403
+ func (dmp * DiffMatchPatch ) diffLinesToIndexes (text1 , text2 string ) ([]index , []index , []string ) {
399
404
chars1 , chars2 , lineArray := dmp .diffLinesToStrings (text1 , text2 )
400
- return [] rune ( chars1 ), [] rune ( chars2 ) , lineArray
405
+ return chars1 , chars2 , lineArray
401
406
}
402
407
403
408
// DiffCharsToLines rehydrates the text in a diff from a string of line hashes to real lines of text.
404
409
func (dmp * DiffMatchPatch ) DiffCharsToLines (diffs []Diff , lineArray []string ) []Diff {
405
410
hydrated := make ([]Diff , 0 , len (diffs ))
406
411
for _ , aDiff := range diffs {
407
412
var sb strings.Builder
408
- for _ , i := range [] rune (aDiff .Text ) {
413
+ for _ , i := range stringToIndex (aDiff .Text ) {
409
414
sb .WriteString (lineArray [i ])
410
415
}
411
416
aDiff .Text = sb .String ()
@@ -1301,24 +1306,24 @@ func (dmp *DiffMatchPatch) DiffFromDelta(text1 string, delta string) (diffs []Di
1301
1306
}
1302
1307
1303
1308
// diffLinesToStrings splits two texts into a list of strings. Each string represents one line.
1304
- func (dmp * DiffMatchPatch ) diffLinesToStrings (text1 , text2 string ) (string , string , []string ) {
1309
+ func (dmp * DiffMatchPatch ) diffLinesToStrings (text1 , text2 string ) ([] index , [] index , []string ) {
1305
1310
// '\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.
1306
1311
lineArray := []string {"" } // e.g. lineArray[4] == 'Hello\n'
1307
1312
1308
1313
//Each string has the index of lineArray which it points to
1309
1314
strIndexArray1 := dmp .diffLinesToStringsMunge (text1 , & lineArray )
1310
1315
strIndexArray2 := dmp .diffLinesToStringsMunge (text2 , & lineArray )
1311
1316
1312
- return intArrayToString ( strIndexArray1 ), intArrayToString ( strIndexArray2 ) , lineArray
1317
+ return strIndexArray1 , strIndexArray2 , lineArray
1313
1318
}
1314
1319
1315
1320
// diffLinesToStringsMunge splits a text into an array of strings, and reduces the texts to a []string.
1316
- func (dmp * DiffMatchPatch ) diffLinesToStringsMunge (text string , lineArray * []string ) []uint32 {
1321
+ func (dmp * DiffMatchPatch ) diffLinesToStringsMunge (text string , lineArray * []string ) []index {
1317
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.
1318
1323
lineHash := map [string ]int {} // e.g. lineHash['Hello\n'] == 4
1319
1324
lineStart := 0
1320
1325
lineEnd := - 1
1321
- strs := []uint32 {}
1326
+ strs := []index {}
1322
1327
1323
1328
for lineEnd < len (text )- 1 {
1324
1329
lineEnd = indexOf (text , "\n " , lineStart )
@@ -1332,11 +1337,11 @@ func (dmp *DiffMatchPatch) diffLinesToStringsMunge(text string, lineArray *[]str
1332
1337
lineValue , ok := lineHash [line ]
1333
1338
1334
1339
if ok {
1335
- strs = append (strs , uint32 (lineValue ))
1340
+ strs = append (strs , index (lineValue ))
1336
1341
} else {
1337
1342
* lineArray = append (* lineArray , line )
1338
1343
lineHash [line ] = len (* lineArray ) - 1
1339
- strs = append (strs , uint32 (len (* lineArray )- 1 ))
1344
+ strs = append (strs , index (len (* lineArray )- 1 ))
1340
1345
}
1341
1346
}
1342
1347
0 commit comments