this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Always accept newline as a name terminator

This avoids including the newline character as part of the name when it
is the last name on a line. Also split out functions for quoted and
unquoted names to clarify the logic.

+42 -27
+39 -27
gitdiff/file_header.go
··· 199 199 // slashes are collapsed. 200 200 func parseName(s string, term rune, dropPrefix int) (name string, n int, err error) { 201 201 if len(s) > 0 && s[0] == '"' { 202 - // find matching end quote and then unquote the section 203 - for n = 1; n < len(s); n++ { 204 - if s[n] == '"' && s[n-1] != '\\' { 205 - n++ 206 - break 207 - } 208 - } 209 - if n == 2 { 210 - return "", 0, fmt.Errorf("missing name") 211 - } 212 - if name, err = strconv.Unquote(s[:n]); err != nil { 213 - return "", 0, err 214 - } 202 + name, n, err = parseQuotedName(s) 215 203 } else { 216 - // find terminator and take the previous section 217 - for n = 0; n < len(s); n++ { 218 - if term >= 0 && rune(s[n]) == term { 219 - break 220 - } 221 - if term < 0 && (s[n] == ' ' || s[n] == '\t') { 222 - break 223 - } 224 - } 225 - if n == 0 { 226 - return "", 0, fmt.Errorf("missing name") 227 - } 228 - name = s[:n] 204 + name, n, err = parseUnquotedName(s, term) 205 + } 206 + if err != nil { 207 + return "", 0, err 229 208 } 230 - 231 209 if name == devNull { 232 210 return name, n, nil 233 211 } 234 212 return cleanName(name, dropPrefix), n, nil 213 + } 214 + 215 + func parseQuotedName(s string) (name string, n int, err error) { 216 + for n = 1; n < len(s); n++ { 217 + if s[n] == '"' && s[n-1] != '\\' { 218 + n++ 219 + break 220 + } 221 + } 222 + if n == 2 { 223 + return "", 0, fmt.Errorf("missing name") 224 + } 225 + if name, err = strconv.Unquote(s[:n]); err != nil { 226 + return "", 0, err 227 + } 228 + return name, n, err 229 + } 230 + 231 + func parseUnquotedName(s string, term rune) (name string, n int, err error) { 232 + for n = 0; n < len(s); n++ { 233 + if s[n] == '\n' { 234 + break 235 + } 236 + if term >= 0 && rune(s[n]) == term { 237 + break 238 + } 239 + if term < 0 && (s[n] == ' ' || s[n] == '\t') { 240 + break 241 + } 242 + } 243 + if n == 0 { 244 + return "", 0, fmt.Errorf("missing name") 245 + } 246 + return s[:n], n, nil 235 247 } 236 248 237 249 // verifyGitHeaderName checks a parsed name against state set by previous lines
+3
gitdiff/file_header_test.go
··· 72 72 "devNull": { 73 73 Input: "/dev/null", Term: '\t', Drop: 1, Output: "/dev/null", N: 9, 74 74 }, 75 + "newlineAlwaysSeparates": { 76 + Input: "dir/file.txt\n", Term: 0, Output: "dir/file.txt", N: 12, 77 + }, 75 78 "emptyString": { 76 79 Input: "", Err: true, 77 80 },