MCPcopy Create free account
hub / github.com/dim-an/cod / quoteArg

Function quoteArg

shells/quote.go:27–63  ·  view source on GitHub ↗
(arg string)

Source from the content-addressed store, hash-verified

25}
26
27func quoteArg(arg string) string {
28 argBytes := []byte(arg)
29
30 needQuoting := false
31 hasSingleQuote := false
32 for _, c := range argBytes {
33 switch {
34 case c == '\'':
35 needQuoting = true
36 hasSingleQuote = true
37 case checkNeedQuoting(c):
38 needQuoting = true
39 }
40 }
41
42 if !needQuoting {
43 return arg
44 }
45
46 sb := strings.Builder{}
47 if hasSingleQuote {
48 sb.WriteByte('"')
49 for _, c := range argBytes {
50 if checkNeedQuoting(c) {
51 sb.WriteByte('\\')
52 }
53 sb.WriteByte(c)
54 }
55 sb.WriteByte('"')
56 } else {
57 sb.WriteByte('\'')
58 sb.WriteString(arg)
59 sb.WriteByte('\'')
60 }
61
62 return sb.String()
63}
64
65func checkNeedQuoting(c byte) bool {
66 switch c {

Callers 11

QuoteFunction · 0.85
BashRemoveCompletionsFunction · 0.85
GetPreambleMethod · 0.85
GenerateCompletionsMethod · 0.85
ResetCommandMethod · 0.85
GenerateCompletionsMethod · 0.85
ResetCommandMethod · 0.85
GetPreambleMethod · 0.85
GenerateCompletionsMethod · 0.85
ResetCommandMethod · 0.85
GetPreambleMethod · 0.85

Calls 1

checkNeedQuotingFunction · 0.85

Tested by

no test coverage detected