1
0
mirror of https://github.com/golang/go synced 2024-11-25 06:57:58 -07:00

Update exec_windows.go

change `hasSpace ` to  `needsQuote` for better understanding
This commit is contained in:
pzx521521 2022-03-16 09:42:28 +08:00 committed by GitHub
parent 3930f1b536
commit e20353eeba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -48,7 +48,7 @@ func appendEscapeArg(b []byte, s string) []byte {
}
needsBackslash := false
hasSpace := false
needsQuote := false
for i := 0; i < len(s); i++ {
if !needsBackslash{
switch s[i] {
@ -56,26 +56,26 @@ func appendEscapeArg(b []byte, s string) []byte {
needsBackslash = true
}
}
if !hasSpace{
if !needsQuote{
switch s[i] {
case ' ', '\t', '!','&', '(', ')', '[', ']', '{', '}', ';', '<', '>', '|':
hasSpace = true
needsQuote = true
}
}
}
if !needsBackslash && !hasSpace {
if !needsBackslash && !needsQuote {
// No special handling required; normal case.
return append(b, s...)
}
if !needsBackslash {
// hasSpace is true, so we need to quote the string.
// needsQuote is true, so we need to quote the string.
b = append(b, '"')
b = append(b, s...)
return append(b, '"')
}
if hasSpace {
if needsQuote {
b = append(b, '"')
}
slashes := 0
@ -94,7 +94,7 @@ func appendEscapeArg(b []byte, s string) []byte {
}
b = append(b, c)
}
if hasSpace {
if needsQuote {
for ; slashes > 0; slashes-- {
b = append(b, '\\')
}