From 40befe38b26af0963298eefc9c22be8dea9ddb28 Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Wed, 23 Feb 2022 11:39:19 +0100 Subject: [PATCH] docker/referece: add IsFullIdentifier containers/common/libimage is in need to check whether a given input looks like a full image ID (i.e., a 64-byte hex string). Since compiling regexes has a negative impact on init- and run-time, let's expose a function to check whether a given string matches the already compiled regex in docker/reference. Signed-off-by: Valentin Rothberg --- docker/reference/regexp-additions.go | 6 ++++++ docker/reference/regexp_test.go | 3 +++ 2 files changed, 9 insertions(+) create mode 100644 docker/reference/regexp-additions.go diff --git a/docker/reference/regexp-additions.go b/docker/reference/regexp-additions.go new file mode 100644 index 000000000..7b15871f7 --- /dev/null +++ b/docker/reference/regexp-additions.go @@ -0,0 +1,6 @@ +package reference + +// Return true if the specified string fully matches `IdentifierRegexp`. +func IsFullIdentifier(s string) bool { + return anchoredIdentifierRegexp.MatchString(s) +} diff --git a/docker/reference/regexp_test.go b/docker/reference/regexp_test.go index 09bc81927..48f721943 100644 --- a/docker/reference/regexp_test.go +++ b/docker/reference/regexp_test.go @@ -545,6 +545,9 @@ func TestIdentifierRegexp(t *testing.T) { for i := range fullCases { checkRegexp(t, anchoredIdentifierRegexp, fullCases[i]) + if IsFullIdentifier(fullCases[i].input) != fullCases[i].match { + t.Errorf("Expected match for %q to be %v", fullCases[i].input, fullCases[i].match) + } } for i := range shortCases {