diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2024-02-09 13:52:36 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2024-02-16 13:17:53 +0100 |
commit | 639073e4fee8fd4235c1002b076e110fad4c82f2 (patch) | |
tree | 55f4c392bc4f156f8605993d3d416bd5b105cc40 /common/paths | |
parent | 21d9057dbfe64f668d5fc6a7f458e0984fbf7e56 (diff) |
Fix rebuild with resources.Concat
Fixes #12017
Diffstat (limited to 'common/paths')
-rw-r--r-- | common/paths/pathparser.go | 71 | ||||
-rw-r--r-- | common/paths/pathparser_test.go | 6 |
2 files changed, 63 insertions, 14 deletions
diff --git a/common/paths/pathparser.go b/common/paths/pathparser.go index eceb46b3d..898eee341 100644 --- a/common/paths/pathparser.go +++ b/common/paths/pathparser.go @@ -18,9 +18,11 @@ import ( "path/filepath" "runtime" "strings" + "sync" "github.com/gohugoio/hugo/common/types" "github.com/gohugoio/hugo/hugofs/files" + "github.com/gohugoio/hugo/identity" ) var defaultPathParser PathParser @@ -50,19 +52,42 @@ func NormalizePathStringBasic(s string) string { return s } +// ParseIdentity parses component c with path s into a StringIdentity. +func (pp *PathParser) ParseIdentity(c, s string) identity.StringIdentity { + s = NormalizePathStringBasic(s) + p := getPath() + p.component = c + defer putPath(p) + p, err := pp.doParse(c, s, p) + if err != nil { + panic(err) + } + return identity.StringIdentity(p.IdentifierBase()) +} + // Parse parses component c with path s into Path using Hugo's content path rules. -func (parser PathParser) Parse(c, s string) *Path { - p, err := parser.parse(c, s) +func (pp *PathParser) Parse(c, s string) *Path { + p, err := pp.parse(c, s) if err != nil { panic(err) } return p } +func (pp *PathParser) newPath(component string) *Path { + return &Path{ + component: component, + posContainerLow: -1, + posContainerHigh: -1, + posSectionHigh: -1, + posIdentifierLanguage: -1, + } +} + func (pp *PathParser) parse(component, s string) (*Path, error) { ss := NormalizePathStringBasic(s) - p, err := pp.doParse(component, ss) + p, err := pp.doParse(component, ss, pp.newPath(component)) if err != nil { return nil, err } @@ -70,7 +95,7 @@ func (pp *PathParser) parse(component, s string) (*Path, error) { if s != ss { var err error // Preserve the original case for titles etc. - p.unnormalized, err = pp.doParse(component, s) + p.unnormalized, err = pp.doParse(component, s, pp.newPath(component)) if err != nil { return nil, err @@ -82,15 +107,7 @@ func (pp *PathParser) parse(component, s string) (*Path, error) { return p, nil } -func (pp *PathParser) doParse(component, s string) (*Path, error) { - p := &Path{ - component: component, - posContainerLow: -1, - posContainerHigh: -1, - posSectionHigh: -1, - posIdentifierLanguage: -1, - } - +func (pp *PathParser) doParse(component, s string, p *Path) (*Path, error) { hasLang := pp.LanguageIndex != nil hasLang = hasLang && (component == files.ComponentFolderContent || component == files.ComponentFolderLayouts) @@ -220,6 +237,7 @@ const ( ) type Path struct { + // Note: Any additions to this struct should also be added to the pathPool. s string posContainerLow int @@ -239,6 +257,31 @@ type Path struct { unnormalized *Path } +var pathPool = &sync.Pool{ + New: func() any { + return &Path{} + }, +} + +func getPath() *Path { + return pathPool.Get().(*Path) +} + +func putPath(p *Path) { + p.s = "" + p.posContainerLow = -1 + p.posContainerHigh = -1 + p.posSectionHigh = -1 + p.component = "" + p.bundleType = 0 + p.identifiers = p.identifiers[:0] + p.posIdentifierLanguage = -1 + p.disabled = false + p.trimLeadingSlash = false + p.unnormalized = nil + pathPool.Put(p) +} + // TrimLeadingSlash returns a copy of the Path with the leading slash removed. func (p Path) TrimLeadingSlash() *Path { p.trimLeadingSlash = true @@ -254,7 +297,7 @@ func (p *Path) norm(s string) string { // IdentifierBase satifies identity.Identity. func (p *Path) IdentifierBase() string { - return p.Base()[1:] + return p.Base() } // Component returns the component for this path (e.g. "content"). diff --git a/common/paths/pathparser_test.go b/common/paths/pathparser_test.go index 27d0b45e9..8c89ddd41 100644 --- a/common/paths/pathparser_test.go +++ b/common/paths/pathparser_test.go @@ -349,3 +349,9 @@ func TestHasExt(t *testing.T) { c.Assert(HasExt("/a/b/c"), qt.IsFalse) c.Assert(HasExt("/a/b.c/d"), qt.IsFalse) } + +func BenchmarkParseIdentity(b *testing.B) { + for i := 0; i < b.N; i++ { + testParser.ParseIdentity(files.ComponentFolderAssets, "/a/b.css") + } +} |