PatchFromText parses a textual representation of patches and returns a List of Patch objects.
(textline string)
| 476 | |
| 477 | // PatchFromText parses a textual representation of patches and returns a List of Patch objects. |
| 478 | func (dmp *DiffMatchPatch) PatchFromText(textline string) ([]Patch, error) { |
| 479 | patches := []Patch{} |
| 480 | if len(textline) == 0 { |
| 481 | return patches, nil |
| 482 | } |
| 483 | text := strings.Split(textline, "\n") |
| 484 | textPointer := 0 |
| 485 | patchHeader := regexp.MustCompile("^@@ -(\\d+),?(\\d*) \\+(\\d+),?(\\d*) @@$") |
| 486 | |
| 487 | var patch Patch |
| 488 | var sign uint8 |
| 489 | var line string |
| 490 | for textPointer < len(text) { |
| 491 | |
| 492 | if !patchHeader.MatchString(text[textPointer]) { |
| 493 | return patches, errors.New("Invalid patch string: " + text[textPointer]) |
| 494 | } |
| 495 | |
| 496 | patch = Patch{} |
| 497 | m := patchHeader.FindStringSubmatch(text[textPointer]) |
| 498 | |
| 499 | patch.Start1, _ = strconv.Atoi(m[1]) |
| 500 | if len(m[2]) == 0 { |
| 501 | patch.Start1-- |
| 502 | patch.Length1 = 1 |
| 503 | } else if m[2] == "0" { |
| 504 | patch.Length1 = 0 |
| 505 | } else { |
| 506 | patch.Start1-- |
| 507 | patch.Length1, _ = strconv.Atoi(m[2]) |
| 508 | } |
| 509 | |
| 510 | patch.Start2, _ = strconv.Atoi(m[3]) |
| 511 | |
| 512 | if len(m[4]) == 0 { |
| 513 | patch.Start2-- |
| 514 | patch.Length2 = 1 |
| 515 | } else if m[4] == "0" { |
| 516 | patch.Length2 = 0 |
| 517 | } else { |
| 518 | patch.Start2-- |
| 519 | patch.Length2, _ = strconv.Atoi(m[4]) |
| 520 | } |
| 521 | textPointer++ |
| 522 | |
| 523 | for textPointer < len(text) { |
| 524 | if len(text[textPointer]) > 0 { |
| 525 | sign = text[textPointer][0] |
| 526 | } else { |
| 527 | textPointer++ |
| 528 | continue |
| 529 | } |
| 530 | |
| 531 | line = text[textPointer][1:] |
| 532 | line = strings.Replace(line, "+", "%2b", -1) |
| 533 | line, _ = url.QueryUnescape(line) |
| 534 | if sign == '-' { |
| 535 | // Deletion. |
no outgoing calls