ReversePatch is CreateReversePatch with UTF-8 and round-trip checks. If either text is invalid UTF-8 (BUG-3839), or applying the patch does not reproduce oldText, ok is false and the caller stores the full body instead. go-diff 1.4.0 built unusable patches for about half of valid multi-byte pairs in
(oldText, newText string)
| 127 | // in a fuzz; verification keeps a future regression from storing history that |
| 128 | // cannot be reconstructed. Never sanitize and diff: that records another body. |
| 129 | func ReversePatch(oldText, newText string) (patch string, ok bool) { |
| 130 | if !utf8.ValidString(oldText) || !utf8.ValidString(newText) { |
| 131 | return "", false |
| 132 | } |
| 133 | patch = CreatePatch(newText, oldText) |
| 134 | got, err := ApplyPatch(newText, patch) |
| 135 | if err != nil || got != oldText { |
| 136 | return "", false |
| 137 | } |
| 138 | return patch, true |
| 139 | } |
| 140 | |
| 141 | // FormatDiffSummary returns a human-readable summary of changes. Its "lines" |
| 142 | // are character-diff CHUNKS (a one-character edit reads +1/-1); LineCounts is |