From a3512c1cfa6925429008bd51282947311104658f Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 4 May 2026 12:02:08 +0200 Subject: [PATCH] util/ocilayout: replace regex for matching windows drive-letters follow-up to bf34a4cbed33d90faae9add5f74170cd13d172cd, which changed the isWindowsDrivePath function to use a regex for matching the trailing slash. This reverts it back to te previous implementation to avoid a regex, but adds a check for the colon to be followed by a forward-slash, which is faster, and avoids the overhead of `regex.MustCompile` during init. BenchmarkRegex-11 39056917 32.45 ns/op 0 B/op 0 allocs/op BenchmarkManual-11 1000000000 0.2476 ns/op 0 B/op 0 allocs/op Signed-off-by: Sebastiaan van Stijn --- util/ocilayout/parse.go | 12 ++++++++---- util/ocilayout/parse_test.go | 14 ++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/util/ocilayout/parse.go b/util/ocilayout/parse.go index 5c7dd8ed7..9e0b6ee42 100644 --- a/util/ocilayout/parse.go +++ b/util/ocilayout/parse.go @@ -1,7 +1,6 @@ package ocilayout import ( - "regexp" "strings" "github.com/distribution/reference" @@ -60,8 +59,13 @@ func (r Ref) String() string { return s } -var windowsDrivePath = regexp.MustCompile(`^[A-Za-z]:[\\/]`) - func isWindowsDrivePath(path string, colon int) bool { - return colon == 1 && windowsDrivePath.MatchString(path) + if colon != 1 || len(path) < 3 { + return false + } + if path[2] != '/' && path[2] != '\\' { + return false + } + c := path[0] + return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') } diff --git a/util/ocilayout/parse_test.go b/util/ocilayout/parse_test.go index 3b3e5e966..3a4fec0f2 100644 --- a/util/ocilayout/parse_test.go +++ b/util/ocilayout/parse_test.go @@ -72,12 +72,14 @@ func TestParse(t *testing.T) { dgst: "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", }, } { - ref, ok, err := Parse(tt.s) - require.True(t, ok) - require.NoError(t, err) - assert.Equal(t, tt.path, ref.Path, "comparing path: %s", tt.s) - assert.Equal(t, tt.dgst, ref.Digest.String(), "comparing digest: %s", tt.s) - assert.Equal(t, tt.tag, ref.Tag, "comparing tag: %s", tt.s) + t.Run(tt.s, func(t *testing.T) { + ref, ok, err := Parse(tt.s) + require.True(t, ok) + require.NoError(t, err) + assert.Equal(t, tt.path, ref.Path, "comparing path: %s", tt.s) + assert.Equal(t, tt.dgst, ref.Digest.String(), "comparing digest: %s", tt.s) + assert.Equal(t, tt.tag, ref.Tag, "comparing tag: %s", tt.s) + }) } }