mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 18:37:45 +08:00
Modify escaping
This commit is contained in:
parent
306dfb34a1
commit
267d6fc4fe
@ -72,13 +72,27 @@ func GetConfigRoot(env cmds.Environment) (string, error) {
|
||||
return ctx.ConfigRoot, nil
|
||||
}
|
||||
|
||||
// EscNonPrint converts control characters and non-printable characters into Go
|
||||
// EscNonPrint converts non-printable characters and backslash into Go
|
||||
// escape sequences, if the given string contains any.
|
||||
func EscNonPrint(s string) string {
|
||||
// First see if escaping is needed, to avoid creating garbage.
|
||||
if !needEscape(s) {
|
||||
return s
|
||||
}
|
||||
|
||||
esc := strconv.Quote(s)
|
||||
// Remove first and last quote, and unescape quotes.
|
||||
return strings.ReplaceAll(esc[1:len(esc)-1], `\"`, `"`)
|
||||
}
|
||||
|
||||
func needEscape(s string) bool {
|
||||
if strings.ContainsRune(s, '\\') {
|
||||
return true
|
||||
}
|
||||
for _, r := range s {
|
||||
if !strconv.IsPrint(r) {
|
||||
return strings.Trim(strconv.Quote(s), "\"")
|
||||
return true
|
||||
}
|
||||
}
|
||||
return s
|
||||
return false
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user