@@ -273,7 +273,7 @@ func (diff *Diff) NumFiles() int {
273273}
274274
275275// 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]+))? @@` )
277277
278278func isHeader (lof string ) bool {
279279 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
311311 if len (hunk ) > headerLines {
312312 break
313313 }
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+ }
315322 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 ()
318325 // init otherLine with begin of opposite side
319- otherLine = com .StrTo (groups [3 ]).MustInt64 ()
326+ otherLine = com .StrTo (groups ["beginNew" ]).MustInt64 ()
320327 } 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 ()
324331 } else {
325332 end = 0
326333 }
327334 // init otherLine with begin of opposite side
328- otherLine = com .StrTo (groups [1 ]).MustInt64 ()
335+ otherLine = com .StrTo (groups ["beginOld" ]).MustInt64 ()
329336 }
330337 end += begin // end is for real only the number of lines in hunk
331338 // lof is between begin and end
0 commit comments