PatchFromText parses a textual representation of patches and returns a List of Patch objects.
(textline string)
| 590 | |
| 591 | // PatchFromText parses a textual representation of patches and returns a List of Patch objects. |
| 592 | func (dmp *DiffMatchPatch) PatchFromText(textline string) ([]Patch, error) { |
| 593 | patches := []Patch{} |
| 594 | if len(textline) == 0 { |
| 595 | return patches, nil |
| 596 | } |
| 597 | text := strings.Split(textline, "\n") |
| 598 | textPointer := 0 |
| 599 | |
| 600 | var patch Patch |
| 601 | var sign uint8 |
| 602 | var line string |
| 603 | for textPointer < len(text) { |
| 604 | m := patchHeaderRegex.FindStringSubmatch(text[textPointer]) |
| 605 | if m == nil { |
| 606 | return patches, errors.New("Invalid patch string: " + text[textPointer]) |
| 607 | } |
| 608 | |
| 609 | patch = Patch{} |
| 610 | |
| 611 | // Coordinates are parsed as 32-bit integers, like the reference Java |
| 612 | // and C# implementations. Out-of-range values are invalid rather |
| 613 | // than clamped: accepting them made hostile patches apply at wrong |
| 614 | // locations and leak internal padding into the result. |
| 615 | start1, err := strconv.ParseInt(m[1], 10, 32) |
| 616 | if err != nil { |
| 617 | return patches, errors.New("Invalid patch string: " + text[textPointer]) |
| 618 | } |
| 619 | patch.Start1 = int(start1) |
| 620 | if len(m[2]) == 0 { |
| 621 | patch.Start1-- |
| 622 | patch.Length1 = 1 |
| 623 | } else if m[2] == "0" { |
| 624 | patch.Length1 = 0 |
| 625 | } else { |
| 626 | length1, err := strconv.ParseInt(m[2], 10, 32) |
| 627 | if err != nil { |
| 628 | return patches, errors.New("Invalid patch string: " + text[textPointer]) |
| 629 | } |
| 630 | patch.Start1-- |
| 631 | patch.Length1 = int(length1) |
| 632 | } |
| 633 | |
| 634 | start2, err := strconv.ParseInt(m[3], 10, 32) |
| 635 | if err != nil { |
| 636 | return patches, errors.New("Invalid patch string: " + text[textPointer]) |
| 637 | } |
| 638 | patch.Start2 = int(start2) |
| 639 | if len(m[4]) == 0 { |
| 640 | patch.Start2-- |
| 641 | patch.Length2 = 1 |
| 642 | } else if m[4] == "0" { |
| 643 | patch.Length2 = 0 |
| 644 | } else { |
| 645 | length2, err := strconv.ParseInt(m[4], 10, 32) |
| 646 | if err != nil { |
| 647 | return patches, errors.New("Invalid patch string: " + text[textPointer]) |
| 648 | } |
| 649 | patch.Start2-- |
no outgoing calls