MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / patchTextValidUTF8

Function patchTextValidUTF8

internal/diff/diff.go:73–89  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
73func 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.
92func patchApply(patches []difflib.Patch, base string) (result string, applied []bool, err error) {

Callers 1

ApplyPatchFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected