From a16c3a49c85d3f1e154855afdc8d05cf09d3a363 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 10 Apr 2025 11:40:30 +0200 Subject: [PATCH] cli/command/image: use lazyregexp to compile regexes on first use Signed-off-by: Sebastiaan van Stijn --- cli/command/image/build.go | 4 ++-- internal/lazyregexp/lazyregexp.go | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/cli/command/image/build.go b/cli/command/image/build.go index 3a8d67bf81..3fa145ac30 100644 --- a/cli/command/image/build.go +++ b/cli/command/image/build.go @@ -10,7 +10,6 @@ import ( "io" "os" "path/filepath" - "regexp" "runtime" "strings" @@ -23,6 +22,7 @@ import ( "github.com/docker/cli/cli/internal/jsonstream" "github.com/docker/cli/cli/streams" "github.com/docker/cli/cli/trust" + "github.com/docker/cli/internal/lazyregexp" "github.com/docker/cli/opts" "github.com/docker/docker/api" "github.com/docker/docker/api/types" @@ -432,7 +432,7 @@ func validateTag(rawRepo string) (string, error) { return rawRepo, nil } -var dockerfileFromLinePattern = regexp.MustCompile(`(?i)^[\s]*FROM[ \f\r\t\v]+(?P[^ \f\r\t\v\n#]+)`) +var dockerfileFromLinePattern = lazyregexp.New(`(?i)^[\s]*FROM[ \f\r\t\v]+(?P[^ \f\r\t\v\n#]+)`) // resolvedTag records the repository, tag, and resolved digest reference // from a Dockerfile rewrite. diff --git a/internal/lazyregexp/lazyregexp.go b/internal/lazyregexp/lazyregexp.go index 6334edb60d..c616a0a05b 100644 --- a/internal/lazyregexp/lazyregexp.go +++ b/internal/lazyregexp/lazyregexp.go @@ -71,6 +71,10 @@ func (r *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) stri return r.re().ReplaceAllStringFunc(src, repl) } +func (r *Regexp) ReplaceAllLiteralString(src, repl string) string { + return r.re().ReplaceAllLiteralString(src, repl) +} + func (r *Regexp) SubexpNames() []string { return r.re().SubexpNames() }