From 877bfeb9b4178aad49c4ddc98af9b371850716f9 Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Mon, 19 Feb 2024 13:54:28 +0200 Subject: [PATCH] Remove spaces from runtime.Version --- commands.go | 2 +- internal/util.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/commands.go b/commands.go index 546ebafb2..db5959446 100644 --- a/commands.go +++ b/commands.go @@ -309,7 +309,7 @@ func (c statefulCmdable) ClientSetInfo(ctx context.Context, info LibraryInfo) *S var cmd *StatusCmd if info.LibName != nil { - libName := fmt.Sprintf("go-redis(%s,%s)", *info.LibName, runtime.Version()) + libName := fmt.Sprintf("go-redis(%s,%s)", *info.LibName, internal.ReplaceSpaces(runtime.Version())) cmd = NewStatusCmd(ctx, "client", "setinfo", "LIB-NAME", libName) } else { cmd = NewStatusCmd(ctx, "client", "setinfo", "LIB-VER", *info.LibVer) diff --git a/internal/util.go b/internal/util.go index 77ca4ee11..ed81ad7aa 100644 --- a/internal/util.go +++ b/internal/util.go @@ -2,6 +2,7 @@ package internal import ( "context" + "strings" "time" "github.com/redis/go-redis/v9/internal/util" @@ -44,3 +45,22 @@ func isLower(s string) bool { } return true } + +func ReplaceSpaces(s string) string { + // Pre-allocate a builder with the same length as s to minimize allocations. + // This is a basic optimization; adjust the initial size based on your use case. + var builder strings.Builder + builder.Grow(len(s)) + + for _, char := range s { + if char == ' ' { + // Replace space with a hyphen. + builder.WriteRune('-') + } else { + // Copy the character as-is. + builder.WriteRune(char) + } + } + + return builder.String() +}