@@ -273,7 +273,7 @@ func (diff *Diff) NumFiles() int {
273
273
}
274
274
275
275
// Example: @@ -1,8 +1,9 @@ => [..., 1, 8, 1, 9]
276
- var hunkRegex = regexp .MustCompile (`^@@ -([0-9]+),( [0-9]+) \+([0-9]+)(,([0-9]+))? @@` )
276
+ var hunkRegex = regexp .MustCompile (`^@@ -(?P<beginOld> [0-9]+)(,(?P<endOld> [0-9]+))? \+(?P<beginNew> [0-9]+)(,(?P<endNew> [0-9]+))? @@` )
277
277
278
278
func isHeader (lof string ) bool {
279
279
return strings .HasPrefix (lof , cmdDiffHead ) || strings .HasPrefix (lof , "---" ) || strings .HasPrefix (lof , "+++" )
@@ -311,21 +311,28 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi
311
311
if len (hunk ) > headerLines {
312
312
break
313
313
}
314
- groups := hunkRegex .FindStringSubmatch (lof )
314
+ // A map with named groups of our regex to recognize them later more easily
315
+ submatches := hunkRegex .FindStringSubmatch (lof )
316
+ groups := make (map [string ]string )
317
+ for i , name := range hunkRegex .SubexpNames () {
318
+ if i != 0 && name != "" {
319
+ groups [name ] = submatches [i ]
320
+ }
321
+ }
315
322
if old {
316
- begin = com .StrTo (groups [1 ]).MustInt64 ()
317
- end = com .StrTo (groups [2 ]).MustInt64 ()
323
+ begin = com .StrTo (groups ["beginOld" ]).MustInt64 ()
324
+ end = com .StrTo (groups ["endOld" ]).MustInt64 ()
318
325
// init otherLine with begin of opposite side
319
- otherLine = com .StrTo (groups [3 ]).MustInt64 ()
326
+ otherLine = com .StrTo (groups ["beginNew" ]).MustInt64 ()
320
327
} else {
321
- begin = com .StrTo (groups [3 ]).MustInt64 ()
322
- if groups [5 ] != "" {
323
- end = com .StrTo (groups [5 ]).MustInt64 ()
328
+ begin = com .StrTo (groups ["beginNew" ]).MustInt64 ()
329
+ if groups ["endNew" ] != "" {
330
+ end = com .StrTo (groups ["endNew" ]).MustInt64 ()
324
331
} else {
325
332
end = 0
326
333
}
327
334
// init otherLine with begin of opposite side
328
- otherLine = com .StrTo (groups [1 ]).MustInt64 ()
335
+ otherLine = com .StrTo (groups ["beginOld" ]).MustInt64 ()
329
336
}
330
337
end += begin // end is for real only the number of lines in hunk
331
338
// lof is between begin and end
0 commit comments