Skip to content

Commit

Permalink
Merge pull request #445 from orijtech/triemux-speed-up-shouldRediToLo…
Browse files Browse the repository at this point in the history
…wercase-by-regexp-compilation

triemux: make shouldRedirToLowercase more efficient by regex compilation+reuse
  • Loading branch information
sengi authored Apr 3, 2024
2 parents 919496a + 1b6c75e commit 4354582
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions triemux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,16 @@ func (mux *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
handler.ServeHTTP(w, r)
}

var reShouldRedirect = regexp.MustCompile(`^\/[A-Z]+[A-Z\W\d]+$`)

// shouldRedirToLowercasePath takes a URL path string (such as "/government/guidance")
// and returns:
// - true, if path is in all caps; for example:
// "/GOVERNMENT/GUIDANCE" -> true (should redirect to "/government/guidance")
// - false, otherwise; for example:
// "/GoVeRnMeNt/gUiDaNcE" -> false (should forward "/GoVeRnMeNt/gUiDaNcE" as-is)
func shouldRedirToLowercasePath(path string) (match bool) {
match, _ = regexp.MatchString(`^\/[A-Z]+[A-Z\W\d]+$`, path)
return
return reShouldRedirect.MatchString(path)
}

// lookup finds a URL path in the Mux and returns the corresponding handler.
Expand Down

0 comments on commit 4354582

Please sign in to comment.