Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
267d6fc4
Commit
267d6fc4
authored
Jan 12, 2021
by
gammazero
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Modify escaping
parent
306dfb34
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
3 deletions
+17
-3
core/commands/cmdenv/env.go
core/commands/cmdenv/env.go
+17
-3
No files found.
core/commands/cmdenv/env.go
View file @
267d6fc4
...
...
@@ -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
s
tr
ings
.
Trim
(
strconv
.
Quote
(
s
),
"
\"
"
)
return
tr
ue
}
}
return
s
return
false
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment