Skip to content
This repository has been archived by the owner on Oct 26, 2023. It is now read-only.

Commit

Permalink
Optimize computeRoutePrefix (prometheus#584)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

---------

Signed-off-by: donuts-are-good <[email protected]>
  • Loading branch information
donuts-are-good authored Oct 21, 2023
1 parent 53697b6 commit ec7afda
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit ec7afda

Please sign in to comment.