patchTextValidUTF8 reports whether every text line of a patch decodes to valid UTF-8. A line is escaped, so "%FF" is the byte 0xFF once parsed; this decodes each line the way PatchFromText does, whose result is unexported.
(patchText string)
| 71 | // valid UTF-8. A line is escaped, so "%FF" is the byte 0xFF once parsed; this |
| 72 | // decodes each line the way PatchFromText does, whose result is unexported. |
| 73 | func patchTextValidUTF8(patchText string) bool { |
| 74 | for _, line := range strings.Split(patchText, "\n") { |
| 75 | if line == "" { |
| 76 | continue |
| 77 | } |
| 78 | switch line[0] { |
| 79 | case ' ', '-', '+': |
| 80 | // PatchFromText ignores the error too: a malformed line decodes |
| 81 | // to "", which is valid. |
| 82 | text, _ := url.QueryUnescape(strings.ReplaceAll(line[1:], "+", "%2b")) |
| 83 | if !utf8.ValidString(text) { |
| 84 | return false |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | return true |
| 89 | } |
| 90 | |
| 91 | // patchApply is go-diff's PatchApply with its panic returned as an error. |
| 92 | func patchApply(patches []difflib.Patch, base string) (result string, applied []bool, err error) { |