parseHunkHeader parses a unified-diff hunk header of the form @@ - [, ] + [, ] @@ returning the four line numbers. When count is omitted it defaults to 1.
(header string)
| 180 | // |
| 181 | // returning the four line numbers. When count is omitted it defaults to 1. |
| 182 | func parseHunkHeader(header string) (oldStart, oldCount, newStart, newCount int, ok bool) { |
| 183 | if !strings.HasPrefix(header, "@@") { |
| 184 | return 0, 0, 0, 0, false |
| 185 | } |
| 186 | body := strings.TrimPrefix(header, "@@") |
| 187 | body = strings.TrimSpace(body) |
| 188 | end := strings.Index(body, "@@") |
| 189 | if end == -1 { |
| 190 | return 0, 0, 0, 0, false |
| 191 | } |
| 192 | parts := strings.Fields(strings.TrimSpace(body[:end])) |
| 193 | if len(parts) < 2 { |
| 194 | return 0, 0, 0, 0, false |
| 195 | } |
| 196 | oldStart, oldCount, ok = parseHunkSide(parts[0], '-') |
| 197 | if !ok { |
| 198 | return 0, 0, 0, 0, false |
| 199 | } |
| 200 | newStart, newCount, ok = parseHunkSide(parts[1], '+') |
| 201 | if !ok { |
| 202 | return 0, 0, 0, 0, false |
| 203 | } |
| 204 | return oldStart, oldCount, newStart, newCount, true |
| 205 | } |
| 206 | |
| 207 | func parseHunkSide(s string, sign byte) (start, count int, ok bool) { |
| 208 | if len(s) == 0 || s[0] != sign { |