File 0016-vis-switch-to-switch-for-non-escaped-logic.patch of Package go-mtree

From 38fd14f297d169430a129635d91e2edfd94d58a9 Mon Sep 17 00:00:00 2001
From: Aleksa Sarai <cyphar@cyphar.com>
Date: Tue, 23 Sep 2025 04:39:11 +1000
Subject: [PATCH 16/25] 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>
---
 pkg/govis/vis.go | 40 ++++++++++++++++++----------------------
 1 file changed, 18 insertions(+), 22 deletions(-)

diff --git a/pkg/govis/vis.go b/pkg/govis/vis.go
index 99bb0920bcbe..ef6bb54e243d 100644
--- a/pkg/govis/vis.go
+++ b/pkg/govis/vis.go
@@ -65,32 +65,28 @@ func vis(output *strings.Builder, b byte, flag VisFlag) {
 	ch := rune(b)
 
 	// XXX: This is quite a horrible thing to support.
-	if flag&VisHTTPStyle == VisHTTPStyle {
-		if !ishttp(ch) {
-			_, _ = fmt.Fprintf(output, "%%%.2X", ch)
-			return
-		}
+	if flag&VisHTTPStyle == VisHTTPStyle && !ishttp(ch) {
+		_, _ = fmt.Fprintf(output, "%%%.2X", ch)
+		return
 	}
 
 	// Figure out if the character doesn't need to be encoded. Effectively, we
 	// encode most "normal" (graphical) characters as themselves unless we have
-	// been specifically asked not to. Note though that we *ALWAYS* encode
-	// everything outside ASCII.
-	// TODO: Switch this to much more logical code.
-
-	if ch > unicode.MaxASCII {
-		/* ... */
-	} else if flag&VisGlob == VisGlob && isglob(ch) {
-		/* ... */
-	} else if isgraph(ch) ||
-		(flag&VisSpace != VisSpace && ch == ' ') ||
-		(flag&VisTab != VisTab && ch == '\t') ||
-		(flag&VisNewline != VisNewline && ch == '\n') ||
-		(flag&VisSafe != 0 && isunsafe(ch)) {
-
-		if ch == '\\' && flag&VisNoSlash == 0 {
-			_ = output.WriteByte('\\')
-		}
+	// been specifically asked not to.
+	switch {
+	case ch > unicode.MaxASCII:
+		// We must *always* encode stuff characters not in ASCII.
+	case flag&VisGlob == VisGlob && isglob(ch):
+		// Glob characters are graphical but can be forced to be encoded.
+	case flag&VisNoSlash == 0 && ch == '\\':
+		// Prefix \ if applicable.
+		_ = output.WriteByte('\\')
+		fallthrough
+	case isgraph(ch),
+		flag&VisSpace != VisSpace && ch == ' ',
+		flag&VisTab != VisTab && ch == '\t',
+		flag&VisNewline != VisNewline && ch == '\n',
+		flag&VisSafe != 0 && isunsafe(ch):
 		_ = output.WriteByte(b)
 		return
 	}
-- 
2.51.0

openSUSE Build Service is sponsored by