Skip to content

Commit f189e68

Browse files
committed
remove internal uses of ReferenceRegexp
Split into a non-exported and exported regex, and use the non-exported regex internally. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 56cff07 commit f189e68

3 files changed

Lines changed: 15 additions & 11 deletions

File tree

reference.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,9 @@ func Parse(s string) (Reference, error) {
232232
return nil, ErrNameEmpty
233233
}
234234

235-
matches := ReferenceRegexp.FindStringSubmatch(s)
235+
matches := referenceRegexp.FindStringSubmatch(s)
236236
if matches == nil {
237-
if sl := strings.ToLower(s); sl != s && ReferenceRegexp.FindStringSubmatch(sl) != nil {
237+
if sl := strings.ToLower(s); sl != s && referenceRegexp.FindStringSubmatch(sl) != nil {
238238
// Succeeds when lower-casing, so input contains an invalid repository name.
239239
return nil, ErrNameContainsUppercase
240240
}

regexp.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var NameRegexp = regexp.MustCompile(namePat)
3131
// ReferenceRegexp is the full supported format of a reference. The regexp
3232
// is anchored and has capturing groups for name, tag, and digest
3333
// components.
34-
var ReferenceRegexp = regexp.MustCompile(referencePat)
34+
var ReferenceRegexp = referenceRegexp
3535

3636
// TagRegexp matches valid tag names. From [docker/docker:graph/tags.go].
3737
//
@@ -109,6 +109,11 @@ const (
109109
)
110110

111111
var (
112+
// referenceRegexp is the full supported format of a reference. The regexp
113+
// is anchored and has capturing groups for name, tag, and digest
114+
// components.
115+
referenceRegexp = regexp.MustCompile(referencePat)
116+
112117
// anchoredTagRegexp matches valid tag names, anchored at the start and
113118
// end of the matched string.
114119
anchoredTagRegexp = regexp.MustCompile(anchored(tag))
@@ -131,7 +136,8 @@ var (
131136

132137
// anchoredNameRegexp is used to parse a name value, capturing the
133138
// domain and trailing components.
134-
anchoredNameRegexp = regexp.MustCompile(anchored(optional(capture(domainAndPort), `/`), capture(remoteName)))
139+
anchoredNameRegexp = regexp.MustCompile(anchoredNamePat)
140+
anchoredNamePat = anchored(optional(capture(domainAndPort), `/`), capture(remoteName))
135141

136142
referencePat = anchored(capture(namePat), optional(`:`, capture(tag)), optional(`@`, capture(digestPat)))
137143

regexp_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,8 @@ func TestDomainRegexp(t *testing.T) {
179179

180180
func TestFullNameRegexp(t *testing.T) {
181181
t.Parallel()
182-
if anchoredNameRegexp.NumSubexp() != 2 {
183-
t.Fatalf("anchored name regexp should have two submatches: %v, %v != 2",
184-
anchoredNameRegexp, anchoredNameRegexp.NumSubexp())
182+
if n := anchoredNameRegexp.NumSubexp(); n != 2 {
183+
t.Fatalf("anchored name regexp should have two submatches: %v, %v != 2", anchoredNamePat, n)
185184
}
186185

187186
tests := []regexpMatch{
@@ -479,9 +478,8 @@ func TestFullNameRegexp(t *testing.T) {
479478

480479
func TestReferenceRegexp(t *testing.T) {
481480
t.Parallel()
482-
if ReferenceRegexp.NumSubexp() != 3 {
483-
t.Fatalf("anchored name regexp should have three submatches: %v, %v != 3",
484-
ReferenceRegexp, ReferenceRegexp.NumSubexp())
481+
if n := referenceRegexp.NumSubexp(); n != 3 {
482+
t.Fatalf("anchored name regexp should have three submatches: %v, %v != 3", referencePat, n)
485483
}
486484

487485
tests := []regexpMatch{
@@ -547,7 +545,7 @@ func TestReferenceRegexp(t *testing.T) {
547545
tc := tc
548546
t.Run(tc.input, func(t *testing.T) {
549547
t.Parallel()
550-
checkRegexp(t, ReferenceRegexp, tc)
548+
checkRegexp(t, referenceRegexp, tc)
551549
})
552550
}
553551
}

0 commit comments

Comments
 (0)