reference: expression(): use strings.Join()

It's easier to read, and more performant:

    pkg: github.com/distribution/distribution/v3/reference
    BenchmarkExpression
    BenchmarkExpression-10    	10474380	        97.32 ns/op	      64 B/op	       4 allocs/op
    BenchmarkJoin
    BenchmarkJoin-10          	27722588	        42.71 ns/op	      24 B/op	       1 allocs/op
    PASS

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-11-09 18:59:49 +01:00
parent a7e7ff933c
commit 1d4917d4fb
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -1,6 +1,9 @@
package reference package reference
import "regexp" import (
"regexp"
"strings"
)
const ( const (
// alphanumeric defines the alphanumeric atom, typically a // alphanumeric defines the alphanumeric atom, typically a
@ -136,37 +139,32 @@ func literal(s string) string {
// expression defines a full expression, where each regular expression must // expression defines a full expression, where each regular expression must
// follow the previous. // follow the previous.
func expression(res ...string) string { func expression(res ...string) string {
var s string return strings.Join(res, "")
for _, re := range res {
s += re
}
return s
} }
// optional wraps the expression in a non-capturing group and makes the // optional wraps the expression in a non-capturing group and makes the
// production optional. // production optional.
func optional(res ...string) string { func optional(res ...string) string {
return group(expression(res...)) + `?` return group(strings.Join(res, "")) + `?`
} }
// repeated wraps the regexp in a non-capturing group to get one or more // repeated wraps the regexp in a non-capturing group to get one or more
// matches. // matches.
func repeated(res ...string) string { func repeated(res ...string) string {
return group(expression(res...)) + `+` return group(strings.Join(res, "")) + `+`
} }
// group wraps the regexp in a non-capturing group. // group wraps the regexp in a non-capturing group.
func group(res ...string) string { func group(res ...string) string {
return `(?:` + expression(res...) + `)` return `(?:` + strings.Join(res, "") + `)`
} }
// capture wraps the expression in a capturing group. // capture wraps the expression in a capturing group.
func capture(res ...string) string { func capture(res ...string) string {
return `(` + expression(res...) + `)` return `(` + strings.Join(res, "") + `)`
} }
// anchored anchors the regular expression by adding start and end delimiters. // anchored anchors the regular expression by adding start and end delimiters.
func anchored(res ...string) string { func anchored(res ...string) string {
return `^` + expression(res...) + `$` return `^` + strings.Join(res, "") + `$`
} }