nolint:gocognit // refactor if needed
()
| 357 | |
| 358 | //nolint:gocognit // refactor if needed |
| 359 | func (p *Parser) parseSection() (*Section, error) { |
| 360 | line := string(p.buffer) |
| 361 | p.buffer = nil |
| 362 | |
| 363 | section := &Section{ |
| 364 | Lines: []*Line{ |
| 365 | { |
| 366 | Type: DiffLineSection, |
| 367 | Content: line, |
| 368 | }, |
| 369 | }, |
| 370 | } |
| 371 | |
| 372 | // Parse line number, e.g. @@ -0,0 +1,3 @@ |
| 373 | var leftLine, rightLine int |
| 374 | ss := strings.Split(line, "@@") |
| 375 | ranges := strings.Split(ss[1][1:], " ") |
| 376 | leftLine, _ = strconv.Atoi(strings.Split(ranges[0], ",")[0][1:]) |
| 377 | if len(ranges) > 1 { |
| 378 | rightLine, _ = strconv.Atoi(strings.Split(ranges[1], ",")[0]) |
| 379 | } else { |
| 380 | rightLine = leftLine |
| 381 | } |
| 382 | |
| 383 | for !p.isEOF { |
| 384 | newLine, err := p.readLine() |
| 385 | if err != nil { |
| 386 | return nil, err |
| 387 | } |
| 388 | |
| 389 | if len(p.buffer) == 0 { |
| 390 | p.buffer = nil |
| 391 | continue |
| 392 | } |
| 393 | |
| 394 | // Make sure we're still in the section. If not, we're done with this section. |
| 395 | if p.buffer[0] != ' ' && |
| 396 | p.buffer[0] != '+' && |
| 397 | p.buffer[0] != '-' { |
| 398 | // No new line indicator |
| 399 | if p.buffer[0] == '\\' && |
| 400 | bytes.HasPrefix(p.buffer, []byte(`\ No newline at end of file`)) { |
| 401 | p.buffer = nil |
| 402 | continue |
| 403 | } |
| 404 | return section, nil |
| 405 | } |
| 406 | |
| 407 | if p.IncludePatch && len(p.buffer) > 0 { |
| 408 | p.Patch.Write(p.buffer) |
| 409 | if newLine { |
| 410 | p.Patch.Write([]byte{'\n'}) |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | subLine := string(p.buffer) |
| 415 | p.buffer = nil |
| 416 |