From c577ba6addb2513df8704d4663b26c60eb0ac3f6 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 11 Apr 2025 10:28:43 +0200 Subject: [PATCH] make more expressions a const Signed-off-by: Sebastiaan van Stijn --- regexp.go | 6 +++--- regexp_test.go | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/regexp.go b/regexp.go index 65bc49d..e73c53d 100644 --- a/regexp.go +++ b/regexp.go @@ -89,14 +89,12 @@ const ( // are allowed, IPv6 zone identifiers (rfc6874) or Special addresses such as // IPv4-Mapped are deliberately excluded. ipv6address = `\[(?:[a-fA-F0-9:]+)\]` -) -var ( // domainName defines the structure of potential domain components // that may be part of image names. This is purposely a subset of what is // allowed by DNS to ensure backwards compatibility with Docker image // names. This includes IPv4 addresses on decimal format. - domainName = domainNameComponent + anyTimes(`\.`+domainNameComponent) + domainName = domainNameComponent + `(?:\.` + domainNameComponent + `)*` // host defines the structure of potential domains based on the URI // Host subcomponent on rfc3986. It may be a subset of DNS domain name, @@ -108,7 +106,9 @@ var ( // allowed by the URI Host subcomponent on rfc3986 to ensure backwards // compatibility with Docker image names. domainAndPort = host + optionalPort +) +var ( // anchoredTagRegexp matches valid tag names, anchored at the start and // end of the matched string. anchoredTagRegexp = regexp.MustCompile(anchored(tag)) diff --git a/regexp_test.go b/regexp_test.go index ca4680d..3ee30cb 100644 --- a/regexp_test.go +++ b/regexp_test.go @@ -161,7 +161,10 @@ func TestDomainRegexp(t *testing.T) { match: false, }, } - r := regexp.MustCompile(`^` + DomainRegexp.String() + `$`) + r, err := regexp.Compile(`^` + domainAndPort + `$`) + if err != nil { + t.Fatal(err) + } for _, tc := range tests { tc := tc t.Run(tc.input, func(t *testing.T) {