From 1d4917d4fb8f4445232e05d58ae38e653b5509f5 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 9 Nov 2022 18:59:49 +0100 Subject: [PATCH] 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 --- reference/regexp.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/reference/regexp.go b/reference/regexp.go index bf8d340ce..916c4e609 100644 --- a/reference/regexp.go +++ b/reference/regexp.go @@ -1,6 +1,9 @@ package reference -import "regexp" +import ( + "regexp" + "strings" +) const ( // 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 // follow the previous. func expression(res ...string) string { - var s string - for _, re := range res { - s += re - } - - return s + return strings.Join(res, "") } // optional wraps the expression in a non-capturing group and makes the // production optional. 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 // matches. func repeated(res ...string) string { - return group(expression(res...)) + `+` + return group(strings.Join(res, "")) + `+` } // group wraps the regexp in a non-capturing group. func group(res ...string) string { - return `(?:` + expression(res...) + `)` + return `(?:` + strings.Join(res, "") + `)` } // capture wraps the expression in a capturing group. func capture(res ...string) string { - return `(` + expression(res...) + `)` + return `(` + strings.Join(res, "") + `)` } // anchored anchors the regular expression by adding start and end delimiters. func anchored(res ...string) string { - return `^` + expression(res...) + `$` + return `^` + strings.Join(res, "") + `$` }