From 04d6592df1d2a8ceb98bfb8423b11294eb13855c Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 9 Nov 2022 19:05:01 +0100 Subject: [PATCH] reference: remove "literal()" utility With the exception of ".", none of the literals used required escaping, which made the function rather redundant (and the extra abstraction made it harder to read). Signed-off-by: Sebastiaan van Stijn --- reference/regexp.go | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/reference/regexp.go b/reference/regexp.go index 9875d4ae..f6a3d877 100644 --- a/reference/regexp.go +++ b/reference/regexp.go @@ -55,7 +55,7 @@ var ( // 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 = expression(domainNameComponent, optional(repeated(literal(`.`), domainNameComponent))) + domainName = expression(domainNameComponent, optional(repeated(`\.`+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, @@ -66,7 +66,7 @@ var ( // allowed by the URI Host subcomponent on rfc3986 to ensure backwards // compatibility with Docker image names. - domain = expression(host, optional(literal(`:`), `[0-9]+`)) + domain = expression(host, optional(`:[0-9]+`)) // DomainRegexp matches hostname or IP-addresses, optionally including a port // number. It defines the structure of potential domain components that may be @@ -93,11 +93,11 @@ var ( // end of the matched string. anchoredDigestRegexp = regexp.MustCompile(anchored(digestPat)) - // pathComponent restricts registry path-components to start with at least - // one letter or number, with following parts able to be separated by one - // period, one or two underscore and multiple dashes. + // pathComponent restricts path-components to start with an alphanumeric + // character, with following parts able to be separated by a separator + // (one period, one or two underscore and multiple dashes). pathComponent = expression(alphanumeric, optional(repeated(separator, alphanumeric))) - namePat = expression(optional(domain, literal(`/`)), pathComponent, optional(repeated(literal(`/`), pathComponent))) + namePat = expression(optional(domain+`/`), pathComponent, optional(repeated(`/`+pathComponent))) // NameRegexp is the format for the name component of references, including // an optional domain and port, but without tag or digest suffix. @@ -105,9 +105,9 @@ var ( // anchoredNameRegexp is used to parse a name value, capturing the // domain and trailing components. - anchoredNameRegexp = regexp.MustCompile(anchored(optional(capture(domain), literal(`/`)), capture(pathComponent, optional(repeated(literal(`/`), pathComponent))))) + anchoredNameRegexp = regexp.MustCompile(anchored(optional(capture(domain), `/`), capture(pathComponent, optional(repeated(`/`+pathComponent))))) - referencePat = anchored(capture(namePat), optional(literal(":"), capture(tag)), optional(literal("@"), capture(digestPat))) + referencePat = anchored(capture(namePat), optional(`:`, capture(tag)), optional(`@`, capture(digestPat))) // ReferenceRegexp is the full supported format of a reference. The regexp // is anchored and has capturing groups for name, tag, and digest @@ -124,18 +124,6 @@ var ( anchoredIdentifierRegexp = regexp.MustCompile(anchored(identifier)) ) -// literal compiles s into a literal regular expression, escaping any regexp -// reserved characters. -func literal(s string) string { - re := regexp.MustCompile(regexp.QuoteMeta(s)) - - if _, complete := re.LiteralPrefix(); !complete { - panic("must be a literal") - } - - return re.String() -} - // expression defines a full expression, where each regular expression must // follow the previous. func expression(res ...string) string {