From ec7afda4eef288bd9b9c43d063e4df54c8961272 Mon Sep 17 00:00:00 2001 From: donuts-are-good <96031819+donuts-are-good@users.noreply.github.com> Date: Sat, 21 Oct 2023 09:39:46 -0500 Subject: [PATCH] Optimize computeRoutePrefix (#584) * Optimize computeRoutePrefix Previously, computeRoutePrefix was adding a '/' to the start of the prefix and then removing it from the end, regardless of whether it was there or not. This resulted in unnecessary string operations. The updated function only adds or removes the '/' when necessary, improving efficiency. Additionally, the function now uses strings.HasPrefix and strings.HasSuffix for clearer intent and readability. Signed-off-by: donuts-are-good <96031819+donuts-are-good@users.noreply.github.com> --------- Signed-off-by: donuts-are-good <96031819+donuts-are-good@users.noreply.github.com> --- main.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 8766c86d..dd6fdd70 100644 --- a/main.go +++ b/main.go @@ -251,13 +251,16 @@ func computeRoutePrefix(prefix string, externalURL *url.URL) string { } if prefix == "/" { - prefix = "" + return "" } - if prefix != "" { - prefix = "/" + strings.Trim(prefix, "/") + // Ensure prefix starts with "/". + if !strings.HasPrefix(prefix, "/") { + prefix = "/" + prefix } + prefix = strings.TrimSuffix(prefix, "/") + return prefix }