Replace references to `re` with `regexp.MustCompile`

Signed-off-by: Paul Cacheux <paul.cacheux@datadoghq.com>
This commit is contained in:
Paul Cacheux 2022-01-13 22:06:06 +01:00
parent 1c89ce5fc1
commit 89622d99a1
1 changed files with 13 additions and 16 deletions

View File

@ -36,25 +36,25 @@ var (
// that may be part of image names. This is purposely a subset of what is // 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 // allowed by DNS to ensure backwards compatibility with Docker image
// names. // names.
DomainRegexp = re(domain) DomainRegexp = regexp.MustCompile(domain)
tag = `[\w][\w.-]{0,127}` tag = `[\w][\w.-]{0,127}`
// TagRegexp matches valid tag names. From docker/docker:graph/tags.go. // TagRegexp matches valid tag names. From docker/docker:graph/tags.go.
TagRegexp = re(tag) TagRegexp = regexp.MustCompile(tag)
anchoredTag = anchored(tag) anchoredTag = anchored(tag)
// anchoredTagRegexp matches valid tag names, anchored at the start and // anchoredTagRegexp matches valid tag names, anchored at the start and
// end of the matched string. // end of the matched string.
anchoredTagRegexp = re(anchoredTag) anchoredTagRegexp = regexp.MustCompile(anchoredTag)
digestPat = `[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,}` digestPat = `[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,}`
// DigestRegexp matches valid digests. // DigestRegexp matches valid digests.
DigestRegexp = re(digestPat) DigestRegexp = regexp.MustCompile(digestPat)
anchoredDigest = anchored(digestPat) anchoredDigest = anchored(digestPat)
// anchoredDigestRegexp matches valid digests, anchored at the start and // anchoredDigestRegexp matches valid digests, anchored at the start and
// end of the matched string. // end of the matched string.
anchoredDigestRegexp = re(anchoredDigest) anchoredDigestRegexp = regexp.MustCompile(anchoredDigest)
namePat = expression( namePat = expression(
optional(domain, literal(`/`)), optional(domain, literal(`/`)),
@ -63,7 +63,7 @@ var (
// NameRegexp is the format for the name component of references. The // NameRegexp is the format for the name component of references. The
// regexp has capturing groups for the domain and name part omitting // regexp has capturing groups for the domain and name part omitting
// the separating forward slash from either. // the separating forward slash from either.
NameRegexp = re(namePat) NameRegexp = regexp.MustCompile(namePat)
anchoredName = anchored( anchoredName = anchored(
optional(capture(domain), literal(`/`)), optional(capture(domain), literal(`/`)),
@ -71,7 +71,7 @@ var (
optional(repeated(literal(`/`), nameComponent)))) optional(repeated(literal(`/`), nameComponent))))
// anchoredNameRegexp is used to parse a name value, capturing the // anchoredNameRegexp is used to parse a name value, capturing the
// domain and trailing components. // domain and trailing components.
anchoredNameRegexp = re(anchoredName) anchoredNameRegexp = regexp.MustCompile(anchoredName)
referencePat = anchored(capture(namePat), referencePat = anchored(capture(namePat),
optional(literal(":"), capture(tag)), optional(literal(":"), capture(tag)),
@ -79,39 +79,36 @@ var (
// ReferenceRegexp is the full supported format of a reference. The regexp // ReferenceRegexp is the full supported format of a reference. The regexp
// is anchored and has capturing groups for name, tag, and digest // is anchored and has capturing groups for name, tag, and digest
// components. // components.
ReferenceRegexp = re(referencePat) ReferenceRegexp = regexp.MustCompile(referencePat)
identifier = `([a-f0-9]{64})` identifier = `([a-f0-9]{64})`
// IdentifierRegexp is the format for string identifier used as a // IdentifierRegexp is the format for string identifier used as a
// content addressable identifier using sha256. These identifiers // content addressable identifier using sha256. These identifiers
// are like digests without the algorithm, since sha256 is used. // are like digests without the algorithm, since sha256 is used.
IdentifierRegexp = re(identifier) IdentifierRegexp = regexp.MustCompile(identifier)
shortIdentifier = `([a-f0-9]{6,64})` shortIdentifier = `([a-f0-9]{6,64})`
// ShortIdentifierRegexp is the format used to represent a prefix // ShortIdentifierRegexp is the format used to represent a prefix
// of an identifier. A prefix may be used to match a sha256 identifier // of an identifier. A prefix may be used to match a sha256 identifier
// within a list of trusted identifiers. // within a list of trusted identifiers.
ShortIdentifierRegexp = re(shortIdentifier) ShortIdentifierRegexp = regexp.MustCompile(shortIdentifier)
anchoredIdentifier = anchored(identifier) anchoredIdentifier = anchored(identifier)
// anchoredIdentifierRegexp is used to check or match an // anchoredIdentifierRegexp is used to check or match an
// identifier value, anchored at start and end of string. // identifier value, anchored at start and end of string.
anchoredIdentifierRegexp = re(anchoredIdentifier) anchoredIdentifierRegexp = regexp.MustCompile(anchoredIdentifier)
anchoredShortIdentifier = anchored(shortIdentifier) anchoredShortIdentifier = anchored(shortIdentifier)
// anchoredShortIdentifierRegexp is used to check if a value // anchoredShortIdentifierRegexp is used to check if a value
// is a possible identifier prefix, anchored at start and end // is a possible identifier prefix, anchored at start and end
// of string. // of string.
anchoredShortIdentifierRegexp = re(anchoredShortIdentifier) anchoredShortIdentifierRegexp = regexp.MustCompile(anchoredShortIdentifier)
) )
// re compiles the string to a regular expression.
var re = regexp.MustCompile
// literal compiles s into a literal regular expression, escaping any regexp // literal compiles s into a literal regular expression, escaping any regexp
// reserved characters. // reserved characters.
func literal(s string) string { func literal(s string) string {
re := re(regexp.QuoteMeta(s)) re := regexp.MustCompile(regexp.QuoteMeta(s))
if _, complete := re.LiteralPrefix(); !complete { if _, complete := re.LiteralPrefix(); !complete {
panic("must be a literal") panic("must be a literal")