@@ -1280,19 +1280,10 @@ func (dmp *DiffMatchPatch) DiffToDelta(diffs []Diff) string {
1280
1280
}
1281
1281
1282
1282
// DiffFromDelta given the original text1, and an encoded string which describes the operations required to transform text1 into text2, comAdde the full diff.
1283
- func (dmp * DiffMatchPatch ) DiffFromDelta (text1 , delta string ) (diffs []Diff , err error ) {
1284
- diffs = [] Diff {}
1283
+ func (dmp * DiffMatchPatch ) DiffFromDelta (text1 string , delta string ) (diffs []Diff , err error ) {
1284
+ i := 0
1285
1285
1286
- defer func () {
1287
- if r := recover (); r != nil {
1288
- err = r .(error )
1289
- }
1290
- }()
1291
-
1292
- pointer := 0 // Cursor in text1
1293
- tokens := strings .Split (delta , "\t " )
1294
-
1295
- for _ , token := range tokens {
1286
+ for _ , token := range strings .Split (delta , "\t " ) {
1296
1287
if len (token ) == 0 {
1297
1288
// Blank tokens are ok (from a trailing \t).
1298
1289
continue
@@ -1312,18 +1303,19 @@ func (dmp *DiffMatchPatch) DiffFromDelta(text1, delta string) (diffs []Diff, err
1312
1303
if ! utf8 .ValidString (param ) {
1313
1304
return nil , fmt .Errorf ("invalid UTF-8 token: %q" , param )
1314
1305
}
1306
+
1315
1307
diffs = append (diffs , Diff {DiffInsert , param })
1316
1308
case '=' , '-' :
1317
1309
n , err := strconv .ParseInt (param , 10 , 0 )
1318
1310
if err != nil {
1319
- return diffs , err
1311
+ return nil , err
1320
1312
} else if n < 0 {
1321
- return diffs , errors .New ("Negative number in DiffFromDelta: " + param )
1313
+ return nil , errors .New ("Negative number in DiffFromDelta: " + param )
1322
1314
}
1323
1315
1324
1316
// Remember that string slicing is by byte - we want by rune here.
1325
- text := string ([]rune (text1 )[pointer : pointer + int (n )])
1326
- pointer += int (n )
1317
+ text := string ([]rune (text1 )[i : i + int (n )])
1318
+ i += int (n )
1327
1319
1328
1320
if op == '=' {
1329
1321
diffs = append (diffs , Diff {DiffEqual , text })
@@ -1332,12 +1324,13 @@ func (dmp *DiffMatchPatch) DiffFromDelta(text1, delta string) (diffs []Diff, err
1332
1324
}
1333
1325
default :
1334
1326
// Anything else is an error.
1335
- return diffs , errors .New ("Invalid diff operation in DiffFromDelta: " + string (token [0 ]))
1327
+ return nil , errors .New ("Invalid diff operation in DiffFromDelta: " + string (token [0 ]))
1336
1328
}
1337
1329
}
1338
1330
1339
- if pointer != len ([]rune (text1 )) {
1340
- return diffs , fmt .Errorf ("Delta length (%v) smaller than source text length (%v)" , pointer , len (text1 ))
1331
+ if i != len ([]rune (text1 )) {
1332
+ return nil , fmt .Errorf ("Delta length (%v) smaller than source text length (%v)" , i , len (text1 ))
1341
1333
}
1342
- return diffs , err
1334
+
1335
+ return diffs , nil
1343
1336
}
0 commit comments