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
20132a83
Commit
20132a83
authored
Jan 14, 2021
by
gammazero
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
code review changes
parent
267d6fc4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
3 deletions
+52
-3
core/commands/cmdenv/env.go
core/commands/cmdenv/env.go
+4
-3
core/commands/cmdenv/env_test.go
core/commands/cmdenv/env_test.go
+48
-0
No files found.
core/commands/cmdenv/env.go
View file @
20132a83
...
...
@@ -72,10 +72,11 @@ func GetConfigRoot(env cmds.Environment) (string, error) {
return
ctx
.
ConfigRoot
,
nil
}
// EscNonPrint converts non-printable characters and backslash into Go
// escape sequences, if the given string contains any.
// EscNonPrint converts non-printable characters and backslash into Go escape
// sequences. This is done to display all characters in a string, including
// those that would otherwise not be displayed or have an undesirable effect on
// the display.
func
EscNonPrint
(
s
string
)
string
{
// First see if escaping is needed, to avoid creating garbage.
if
!
needEscape
(
s
)
{
return
s
}
...
...
core/commands/cmdenv/env_test.go
0 → 100644
View file @
20132a83
package
cmdenv
import
(
"strconv"
"testing"
)
func
TestEscNonPrint
(
t
*
testing
.
T
)
{
b
:=
[]
byte
(
"hello"
)
b
[
2
]
=
0x7f
s
:=
string
(
b
)
if
!
needEscape
(
s
)
{
t
.
Fatal
(
"string needs escaping"
)
}
if
!
hasNonPrintable
(
s
)
{
t
.
Fatal
(
"expected non-printable"
)
}
if
hasNonPrintable
(
EscNonPrint
(
s
))
{
t
.
Fatal
(
"escaped string has non-printable"
)
}
if
EscNonPrint
(
`hel\lo`
)
!=
`hel\\lo`
{
t
.
Fatal
(
"backslash not escaped"
)
}
s
=
`hello`
if
needEscape
(
s
)
{
t
.
Fatal
(
"string does not need escaping"
)
}
if
EscNonPrint
(
s
)
!=
s
{
t
.
Fatal
(
"string should not have changed"
)
}
s
=
`"hello"`
if
EscNonPrint
(
s
)
!=
s
{
t
.
Fatal
(
"string should not have changed"
)
}
if
EscNonPrint
(
`"hel\"lo"`
)
!=
`"hel\\"lo"`
{
t
.
Fatal
(
"did not get expected escaped string"
)
}
}
func
hasNonPrintable
(
s
string
)
bool
{
for
_
,
r
:=
range
s
{
if
!
strconv
.
IsPrint
(
r
)
{
return
true
}
}
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