@@ -34,6 +34,8 @@ const (
34
34
DiffInsert Operation = 1
35
35
// DiffEqual item represents an equal diff.
36
36
DiffEqual Operation = 0
37
+ //IndexSeperator is used to seperate the array indexes in an index string
38
+ IndexSeperator = ","
37
39
)
38
40
39
41
// Diff represents one diff operation
@@ -396,65 +398,17 @@ func (dmp *DiffMatchPatch) DiffLinesToChars(text1, text2 string) (string, string
396
398
397
399
// DiffLinesToRunes splits two texts into a list of runes. Each rune represents one line.
398
400
func (dmp * DiffMatchPatch ) DiffLinesToRunes (text1 , text2 string ) ([]rune , []rune , []string ) {
399
- // '\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.
400
- lineArray := []string {"" } // e.g. lineArray[4] == 'Hello\n'
401
- lineHash := map [string ]int {} // e.g. lineHash['Hello\n'] == 4
402
-
403
- chars1 := dmp .diffLinesToRunesMunge (text1 , & lineArray , lineHash )
404
- chars2 := dmp .diffLinesToRunesMunge (text2 , & lineArray , lineHash )
405
-
406
- return chars1 , chars2 , lineArray
401
+ chars1 , chars2 , lineArray := dmp .DiffLinesToStrings (text1 , text2 )
402
+ return []rune (chars1 ), []rune (chars2 ), lineArray
407
403
}
408
404
409
405
func (dmp * DiffMatchPatch ) diffLinesToRunes (text1 , text2 []rune ) ([]rune , []rune , []string ) {
410
406
return dmp .DiffLinesToRunes (string (text1 ), string (text2 ))
411
407
}
412
408
413
- // diffLinesToRunesMunge splits a text into an array of strings, and reduces the texts to a []rune where each Unicode character represents one line.
414
- // We use strings instead of []runes as input mainly because you can't use []rune as a map key.
415
- func (dmp * DiffMatchPatch ) diffLinesToRunesMunge (text string , lineArray * []string , lineHash map [string ]int ) []rune {
416
- // 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.
417
- lineStart := 0
418
- lineEnd := - 1
419
- runes := []rune {}
420
-
421
- for lineEnd < len (text )- 1 {
422
- lineEnd = indexOf (text , "\n " , lineStart )
423
-
424
- if lineEnd == - 1 {
425
- lineEnd = len (text ) - 1
426
- }
427
-
428
- line := text [lineStart : lineEnd + 1 ]
429
- lineStart = lineEnd + 1
430
- lineValue , ok := lineHash [line ]
431
-
432
- if ok {
433
- runes = append (runes , rune (lineValue ))
434
- } else {
435
- * lineArray = append (* lineArray , line )
436
- lineHash [line ] = len (* lineArray ) - 1
437
- runes = append (runes , rune (len (* lineArray )- 1 ))
438
- }
439
- }
440
-
441
- return runes
442
- }
443
-
444
409
// DiffCharsToLines rehydrates the text in a diff from a string of line hashes to real lines of text.
445
410
func (dmp * DiffMatchPatch ) DiffCharsToLines (diffs []Diff , lineArray []string ) []Diff {
446
- hydrated := make ([]Diff , 0 , len (diffs ))
447
- for _ , aDiff := range diffs {
448
- chars := aDiff .Text
449
- text := make ([]string , len (chars ))
450
-
451
- for i , r := range chars {
452
- text [i ] = lineArray [r ]
453
- }
454
-
455
- aDiff .Text = strings .Join (text , "" )
456
- hydrated = append (hydrated , aDiff )
457
- }
411
+ hydrated := dmp .DiffStringsToLines (diffs , lineArray )
458
412
return hydrated
459
413
}
460
414
@@ -1343,3 +1297,71 @@ func (dmp *DiffMatchPatch) DiffFromDelta(text1 string, delta string) (diffs []Di
1343
1297
1344
1298
return diffs , nil
1345
1299
}
1300
+
1301
+ // DiffLinesToStrings splits two texts into a list of strings. Each string represents one line.
1302
+ func (dmp * DiffMatchPatch ) DiffLinesToStrings (text1 , text2 string ) (string , string , []string ) {
1303
+ // '\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.
1304
+ lineArray := []string {"" } // e.g. lineArray[4] == 'Hello\n'
1305
+
1306
+ //Each string has the index of lineArray which it points to
1307
+ strIndexArray1 := dmp .diffLinesToStringsMunge (text1 , & lineArray )
1308
+ strIndexArray2 := dmp .diffLinesToStringsMunge (text2 , & lineArray )
1309
+
1310
+ //Adding a delimter to later get the strings as array
1311
+ str1 := strings .Join (strIndexArray1 [:], IndexSeperator )
1312
+ str2 := strings .Join (strIndexArray2 [:], IndexSeperator )
1313
+
1314
+ return str1 , str2 , lineArray
1315
+ }
1316
+
1317
+ // diffLinesToStringsMunge splits a text into an array of strings, and reduces the texts to a []rune where each Unicode character represents one line.
1318
+ // We use strings instead of []runes as input mainly because you can't use []rune as a map key.
1319
+ func (dmp * DiffMatchPatch ) diffLinesToStringsMunge (text string , lineArray * []string ) []string {
1320
+ // 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.
1321
+ lineHash := map [string ]int {} // e.g. lineHash['Hello\n'] == 4
1322
+ lineStart := 0
1323
+ lineEnd := - 1
1324
+ strings := []string {}
1325
+
1326
+ for lineEnd < len (text )- 1 {
1327
+ lineEnd = indexOf (text , "\n " , lineStart )
1328
+
1329
+ if lineEnd == - 1 {
1330
+ lineEnd = len (text ) - 1
1331
+ }
1332
+
1333
+ line := text [lineStart : lineEnd + 1 ]
1334
+ lineStart = lineEnd + 1
1335
+ lineValue , ok := lineHash [line ]
1336
+
1337
+ if ok {
1338
+ strings = append (strings , strconv .Itoa (lineValue ))
1339
+ } else {
1340
+ * lineArray = append (* lineArray , line )
1341
+ lineHash [line ] = len (* lineArray ) - 1
1342
+ strings = append (strings , strconv .Itoa (len (* lineArray )- 1 ))
1343
+ }
1344
+ }
1345
+
1346
+ return strings
1347
+ }
1348
+
1349
+ // DiffStringsToLines rehydrates the text in a diff from a string of line hashes to real lines of text.
1350
+ func (dmp * DiffMatchPatch ) DiffStringsToLines (diffs []Diff , lineArray []string ) []Diff {
1351
+ hydrated := make ([]Diff , 0 , len (diffs ))
1352
+ for _ , aDiff := range diffs {
1353
+ chars := strings .Split (aDiff .Text , IndexSeperator )
1354
+ text := make ([]string , len (chars ))
1355
+
1356
+ for i , r := range chars {
1357
+ i1 , err := strconv .Atoi (r )
1358
+ if err == nil {
1359
+ text [i ] = lineArray [i1 ]
1360
+ }
1361
+ }
1362
+
1363
+ aDiff .Text = strings .Join (text , "" )
1364
+ hydrated = append (hydrated , aDiff )
1365
+ }
1366
+ return hydrated
1367
+ }
0 commit comments