@@ -34,8 +34,6 @@ const (
34
34
DiffInsert Operation = 1
35
35
// DiffEqual item represents an equal diff.
36
36
DiffEqual Operation = 0
37
- // IndexSeparator is used to seperate the array indexes in an index string
38
- IndexSeparator = ","
39
37
)
40
38
41
39
// Diff represents one diff operation
@@ -205,7 +203,7 @@ func (dmp *DiffMatchPatch) diffCompute(
205
203
// then rediff the parts for greater accuracy. This speedup can produce non-minimal diffs.
206
204
func (dmp * DiffMatchPatch ) diffLineMode (text1 , text2 []rune , deadline time.Time ) []Diff {
207
205
// Scan the text on a line-by-line basis first.
208
- text1 , text2 , linearray := dmp .DiffLinesToRunes ( string ( text1 ), string ( text2 ) )
206
+ text1 , text2 , linearray := dmp .diffLinesToRunes ( text1 , text2 )
209
207
210
208
diffs := dmp .diffMainRunes (text1 , text2 , false , deadline )
211
209
@@ -406,28 +404,73 @@ func (dmp *DiffMatchPatch) diffBisectSplit(runes1, runes2 []rune, x, y int,
406
404
// a string of hashes where each Unicode character represents one line.
407
405
// It's slightly faster to call DiffLinesToRunes first, followed by DiffMainRunes.
408
406
func (dmp * DiffMatchPatch ) DiffLinesToChars (text1 , text2 string ) (string , string , []string ) {
409
- chars1 , chars2 , lineArray := dmp .diffLinesToStrings (text1 , text2 )
410
- return chars1 , chars2 , lineArray
407
+ chars1 , chars2 , lineArray := dmp .DiffLinesToRunes (text1 , text2 )
408
+ return string ( chars1 ), string ( chars2 ) , lineArray
411
409
}
412
410
413
- // DiffLinesToRunes splits two texts into a list of runes.
411
+ // DiffLinesToRunes splits two texts into a list of runes. Each rune represents one line.
414
412
func (dmp * DiffMatchPatch ) DiffLinesToRunes (text1 , text2 string ) ([]rune , []rune , []string ) {
415
- chars1 , chars2 , lineArray := dmp .diffLinesToStrings (text1 , text2 )
416
- return []rune (chars1 ), []rune (chars2 ), lineArray
413
+ // '\x00' is a valid character, but various debuggers don't like it.
414
+ // So we'll insert a junk entry to avoid generating a null character.
415
+ lineArray := []string {"" } // e.g. lineArray[4] == 'Hello\n'
416
+ lineHash := map [string ]int {} // e.g. lineHash['Hello\n'] == 4
417
+
418
+ chars1 := dmp .diffLinesToRunesMunge (text1 , & lineArray , lineHash )
419
+ chars2 := dmp .diffLinesToRunesMunge (text2 , & lineArray , lineHash )
420
+
421
+ return chars1 , chars2 , lineArray
422
+ }
423
+
424
+ func (dmp * DiffMatchPatch ) diffLinesToRunes (text1 , text2 []rune ) ([]rune , []rune , []string ) {
425
+ return dmp .DiffLinesToRunes (string (text1 ), string (text2 ))
426
+ }
427
+
428
+ // diffLinesToRunesMunge splits a text into an array of strings, and reduces the texts to a []rune
429
+ // where each Unicode character represents one line.
430
+ // We use strings instead of []runes as input mainly because you can't use []rune as a map key.
431
+ func (dmp * DiffMatchPatch ) diffLinesToRunesMunge (
432
+ text string ,
433
+ lineArray * []string ,
434
+ lineHash map [string ]int ,
435
+ ) []rune {
436
+ // Walk the text, pulling out a substring for each line. text.split('\n')
437
+ // would would temporarily double our memory footprint. Modifying text would create many large strings to garbage collect.
438
+ lineStart := 0
439
+ lineEnd := - 1
440
+ runes := []rune {}
441
+
442
+ for lineEnd < len (text )- 1 {
443
+ lineEnd = indexOf (text , "\n " , lineStart )
444
+
445
+ if lineEnd == - 1 {
446
+ lineEnd = len (text ) - 1
447
+ }
448
+
449
+ line := text [lineStart : lineEnd + 1 ]
450
+ lineStart = lineEnd + 1
451
+ lineValue , ok := lineHash [line ]
452
+
453
+ if ok {
454
+ runes = append (runes , rune (lineValue ))
455
+ } else {
456
+ * lineArray = append (* lineArray , line )
457
+ lineHash [line ] = len (* lineArray ) - 1
458
+ runes = append (runes , rune (len (* lineArray )- 1 ))
459
+ }
460
+ }
461
+
462
+ return runes
417
463
}
418
464
419
465
// DiffCharsToLines rehydrates the text in a diff from a string of line hashes to real lines of text.
420
466
func (dmp * DiffMatchPatch ) DiffCharsToLines (diffs []Diff , lineArray []string ) []Diff {
421
467
hydrated := make ([]Diff , 0 , len (diffs ))
422
468
for _ , aDiff := range diffs {
423
- chars := strings . Split ( aDiff .Text , IndexSeparator )
469
+ chars := aDiff .Text
424
470
text := make ([]string , len (chars ))
425
471
426
472
for i , r := range chars {
427
- i1 , err := strconv .Atoi (r )
428
- if err == nil {
429
- text [i ] = lineArray [i1 ]
430
- }
473
+ text [i ] = lineArray [r ]
431
474
}
432
475
433
476
aDiff .Text = strings .Join (text , "" )
@@ -1331,49 +1374,3 @@ func (dmp *DiffMatchPatch) DiffFromDelta(text1, delta string) (diffs []Diff, err
1331
1374
1332
1375
return diffs , nil
1333
1376
}
1334
-
1335
- // diffLinesToStrings splits two texts into a list of strings. Each string represents one line.
1336
- func (dmp * DiffMatchPatch ) diffLinesToStrings (text1 , text2 string ) (string , string , []string ) {
1337
- // '\x00' is a valid character, but various debuggers don't like it.
1338
- // So we'll insert a junk entry to avoid generating a null character.
1339
- lineArray := []string {"" } // e.g. lineArray[4] == 'Hello\n'
1340
-
1341
- // Each string has the index of lineArray which it points to
1342
- strIndexArray1 := dmp .diffLinesToStringsMunge (text1 , & lineArray )
1343
- strIndexArray2 := dmp .diffLinesToStringsMunge (text2 , & lineArray )
1344
-
1345
- return intArrayToString (strIndexArray1 ), intArrayToString (strIndexArray2 ), lineArray
1346
- }
1347
-
1348
- // diffLinesToStringsMunge splits a text into an array of strings, and reduces the texts to a []string.
1349
- func (dmp * DiffMatchPatch ) diffLinesToStringsMunge (text string , lineArray * []string ) []uint32 {
1350
- // Walk the text, pulling out a substring for each line. text.split('\n')
1351
- // would would temporarily double our memory footprint.
1352
- // Modifying text would create many large strings to garbage collect.
1353
- lineHash := map [string ]int {} // e.g. lineHash['Hello\n'] == 4
1354
- lineStart := 0
1355
- lineEnd := - 1
1356
- strs := []uint32 {}
1357
-
1358
- for lineEnd < len (text )- 1 {
1359
- lineEnd = indexOf (text , "\n " , lineStart )
1360
-
1361
- if lineEnd == - 1 {
1362
- lineEnd = len (text ) - 1
1363
- }
1364
-
1365
- line := text [lineStart : lineEnd + 1 ]
1366
- lineStart = lineEnd + 1
1367
- lineValue , ok := lineHash [line ]
1368
-
1369
- if ok {
1370
- strs = append (strs , uint32 (lineValue ))
1371
- } else {
1372
- * lineArray = append (* lineArray , line )
1373
- lineHash [line ] = len (* lineArray ) - 1
1374
- strs = append (strs , uint32 (len (* lineArray )- 1 ))
1375
- }
1376
- }
1377
-
1378
- return strs
1379
- }
0 commit comments