mirror of
https://github.com/vbatts/go-mtree.git
synced 2025-10-04 04:31:00 +00:00
vis: switch to 'switch' for non-escaped logic
There was a TODO to make this code more legible. I still think it's somewhat ugly, but it does read _slightly_ better as a switch statement. Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
This commit is contained in:
parent
47086b0654
commit
38fd14f297
1 changed files with 18 additions and 22 deletions
|
@ -65,32 +65,28 @@ func vis(output *strings.Builder, b byte, flag VisFlag) {
|
||||||
ch := rune(b)
|
ch := rune(b)
|
||||||
|
|
||||||
// XXX: This is quite a horrible thing to support.
|
// XXX: This is quite a horrible thing to support.
|
||||||
if flag&VisHTTPStyle == VisHTTPStyle {
|
if flag&VisHTTPStyle == VisHTTPStyle && !ishttp(ch) {
|
||||||
if !ishttp(ch) {
|
_, _ = fmt.Fprintf(output, "%%%.2X", ch)
|
||||||
_, _ = fmt.Fprintf(output, "%%%.2X", ch)
|
return
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Figure out if the character doesn't need to be encoded. Effectively, we
|
// Figure out if the character doesn't need to be encoded. Effectively, we
|
||||||
// encode most "normal" (graphical) characters as themselves unless we have
|
// encode most "normal" (graphical) characters as themselves unless we have
|
||||||
// been specifically asked not to. Note though that we *ALWAYS* encode
|
// been specifically asked not to.
|
||||||
// everything outside ASCII.
|
switch {
|
||||||
// TODO: Switch this to much more logical code.
|
case ch > unicode.MaxASCII:
|
||||||
|
// We must *always* encode stuff characters not in ASCII.
|
||||||
if ch > unicode.MaxASCII {
|
case flag&VisGlob == VisGlob && isglob(ch):
|
||||||
/* ... */
|
// Glob characters are graphical but can be forced to be encoded.
|
||||||
} else if flag&VisGlob == VisGlob && isglob(ch) {
|
case flag&VisNoSlash == 0 && ch == '\\':
|
||||||
/* ... */
|
// Prefix \ if applicable.
|
||||||
} else if isgraph(ch) ||
|
_ = output.WriteByte('\\')
|
||||||
(flag&VisSpace != VisSpace && ch == ' ') ||
|
fallthrough
|
||||||
(flag&VisTab != VisTab && ch == '\t') ||
|
case isgraph(ch),
|
||||||
(flag&VisNewline != VisNewline && ch == '\n') ||
|
flag&VisSpace != VisSpace && ch == ' ',
|
||||||
(flag&VisSafe != 0 && isunsafe(ch)) {
|
flag&VisTab != VisTab && ch == '\t',
|
||||||
|
flag&VisNewline != VisNewline && ch == '\n',
|
||||||
if ch == '\\' && flag&VisNoSlash == 0 {
|
flag&VisSafe != 0 && isunsafe(ch):
|
||||||
_ = output.WriteByte('\\')
|
|
||||||
}
|
|
||||||
_ = output.WriteByte(b)
|
_ = output.WriteByte(b)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue