@@ -1474,43 +1474,38 @@ func (dmp *DiffMatchPatch) DiffFromDelta(text1, delta string) (diffs []Diff, err
1474
1474
// operation of this token (delete, insert, equality).
1475
1475
param := token [1 :]
1476
1476
1477
- switch token [0 ] {
1477
+ switch op := token [0 ]; op {
1478
1478
case '+' :
1479
1479
// decode would Diff all "+" to " "
1480
1480
param = strings .Replace (param , "+" , "%2b" , - 1 )
1481
1481
param , _ = url .QueryUnescape (param )
1482
1482
diffs = append (diffs , Diff {DiffInsert , param })
1483
- break
1484
1483
case '=' , '-' :
1485
1484
n , err := strconv .ParseInt (param , 10 , 0 )
1486
1485
if err != nil {
1487
1486
return diffs , err
1488
- }
1489
-
1490
- if n < 0 {
1487
+ } else if n < 0 {
1491
1488
return diffs , errors .New ("Negative number in DiffFromDelta: " + param )
1492
1489
}
1493
1490
1494
- text := text1 [pointer : pointer + int (n )]
1491
+ // remember that string slicing is by byte - we want by rune here.
1492
+ text := string ([]rune (text1 )[pointer : pointer + int (n )])
1495
1493
pointer += int (n )
1496
1494
1497
- if token [ 0 ] == '=' {
1495
+ if op == '=' {
1498
1496
diffs = append (diffs , Diff {DiffEqual , text })
1499
1497
} else {
1500
1498
diffs = append (diffs , Diff {DiffDelete , text })
1501
1499
}
1502
- break
1503
1500
default :
1504
1501
// Anything else is an error.
1505
1502
return diffs , errors .New ("Invalid diff operation in DiffFromDelta: " + string (token [0 ]))
1506
1503
}
1507
1504
}
1508
1505
1509
- if pointer != len (text1 ) {
1510
- return diffs , errors . New ("Delta length (" + string ( pointer ) + ") smaller than source text length (" + string ( len (text1 )) + ")." )
1506
+ if pointer != len ([] rune ( text1 ) ) {
1507
+ return diffs , fmt . Errorf ("Delta length (%v) smaller than source text length (%v)" , pointer , len (text1 ))
1511
1508
}
1512
- fmt .Println ("" ) //text1, pointer, n)
1513
-
1514
1509
return diffs , err
1515
1510
}
1516
1511
0 commit comments