Skip to content

Commit

Permalink
Remove Slice
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jan 5, 2022
1 parent 3b31042 commit 658ba38
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 97 deletions.
81 changes: 27 additions & 54 deletions common/paths/pathparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ type Path interface {
identity.Identity
Component() string
Name() string
NameNoExt() string
NameNoIdentifier() string
Base() string
Dir() string
Ext() string
Slice(bottom, top int) string
Identifiers() []string
Identifier(i int) string
IsContent() bool
Expand Down Expand Up @@ -195,65 +196,28 @@ func (p *pathBase) Name() string {
return p.s
}

func (p *pathBase) Dir() string {
if p.posBase > 0 {
return p.s[:p.posBase-1]
// Name returns the last element of path withhout any extension.
func (p *pathBase) NameNoExt() string {
if i := p.identifierIndex(0); i != -1 {
return p.s[p.posBase : p.identifiers[i].Low-1]
}
return "/"
return p.s[p.posBase:]
}

func (p *pathBase) Slice(bottom, top int) string {
if bottom == 0 && top == 0 {
return p.s
}

if bottom < 0 {
bottom = 0
}

if top < 0 {
top = 0
}

if bottom > len(p.identifiers)+1 {
bottom = len(p.identifiers) + 1
}

if top > len(p.identifiers)+1 {
top = len(p.identifiers) + 1
}

// 0 : posBase
// posBase : identifier[0].Low
// identifier[n].Low : identifier[n].High
var low, high int
if bottom == 1 {
low = p.posBase
} else if bottom > 1 {
low = p.identifiers[len(p.identifiers)-bottom+1].Low
func (p *pathBase) NameNoIdentifier() string {
if len(p.identifiers) > 0 {
return p.s[p.posBase : p.identifiers[len(p.identifiers)-1].Low-1]
}

if top == 0 {
high = len(p.s)
} else if top > 0 {
i := top
distance := len(p.identifiers) - i
if distance <= 0 {
if distance == 0 {
high = p.identifiers[len(p.identifiers)-1].Low - 1
} else {
high = p.posBase - 1
}
} else {
high = p.identifiers[i].High
}
if i := p.identifierIndex(0); i != -1 {
}
return p.s[p.posBase:]
}

if low > high {
return ""
func (p *pathBase) Dir() string {
if p.posBase > 0 {
return p.s[:p.posBase-1]
}

return p.s[low:high]
return "/"
}

// For content files, Base returns the path without any identifiers (extension, language code etc.).
Expand Down Expand Up @@ -320,9 +284,18 @@ func (p *pathBase) IsLeafBundle() bool {
}

func (p *pathBase) identifierAsString(i int) string {
if i < 0 || i >= len(p.identifiers) {
i = p.identifierIndex(i)
if i == -1 {
return ""
}

id := p.identifiers[i]
return p.s[id.Low:id.High]
}

func (p *pathBase) identifierIndex(i int) int {
if i < 0 || i >= len(p.identifiers) {
return -1
}
return i
}
45 changes: 6 additions & 39 deletions common/paths/pathparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func TestParse(t *testing.T) {
"/A/B.txt",
func(c *qt.C, p Path) {
c.Assert(p.Name(), qt.Equals, "b.txt")
c.Assert(p.NameNoExt(), qt.Equals, "b")
c.Assert(p.NameNoIdentifier(), qt.Equals, "b")
c.Assert(p.Base(), qt.Equals, "/a/b.txt")
c.Assert(p.Ext(), qt.Equals, "txt")
},
Expand All @@ -65,6 +67,7 @@ func TestParse(t *testing.T) {
"/a/b",
func(c *qt.C, p Path) {
c.Assert(p.Name(), qt.Equals, "b")
c.Assert(p.NameNoExt(), qt.Equals, "b")
c.Assert(p.Base(), qt.Equals, "/a/b")
c.Assert(p.Ext(), qt.Equals, "")
},
Expand All @@ -83,6 +86,7 @@ func TestParse(t *testing.T) {
"/a/b.a.b.c.txt",
func(c *qt.C, p Path) {
c.Assert(p.Name(), qt.Equals, "b.a.b.c.txt")
c.Assert(p.NameNoIdentifier(), qt.Equals, "b")
c.Assert(p.Identifiers(), qt.DeepEquals, []string{"txt", "c", "b", "a"})
c.Assert(p.Base(), qt.Equals, "/a/b.txt")
c.Assert(p.Ext(), qt.Equals, "txt")
Expand All @@ -95,6 +99,8 @@ func TestParse(t *testing.T) {
c.Assert(p.Base(), qt.Equals, "/a/b")
c.Assert(p.Dir(), qt.Equals, "/a/b")
c.Assert(p.Ext(), qt.Equals, "md")
c.Assert(p.NameNoExt(), qt.Equals, "index.no")
c.Assert(p.NameNoIdentifier(), qt.Equals, "index")
c.Assert(p.Identifiers(), qt.DeepEquals, []string{"md", "no"})
c.Assert(p.IsLeafBundle(), qt.IsTrue)
c.Assert(p.IsBundle(), qt.IsTrue)
Expand Down Expand Up @@ -123,46 +129,7 @@ func TestParse(t *testing.T) {
c.Assert(p.Identifiers(), qt.DeepEquals, []string{"txt", "no"})
},
},
{
"Slice",
"/a/b/index.txt",
func(c *qt.C, p Path) {
c.Assert(p.Slice(0, 0), qt.Equals, "/a/b/index.txt")
c.Assert(p.Slice(1, 0), qt.Equals, "index.txt")
c.Assert(p.Slice(2, 0), qt.Equals, "txt")
c.Assert(p.Slice(3, 0), qt.Equals, "txt")
c.Assert(p.Slice(4, 0), qt.Equals, "txt")

c.Assert(p.Slice(0, 1), qt.Equals, "/a/b/index")
c.Assert(p.Slice(0, 2), qt.Equals, "/a/b")
c.Assert(p.Slice(0, 3), qt.Equals, "/a/b")
c.Assert(p.Slice(0, 4), qt.Equals, "/a/b")

c.Assert(p.Slice(1, 1), qt.Equals, "index")
c.Assert(p.Slice(1, 2), qt.Equals, "")
c.Assert(p.Slice(1, 3), qt.Equals, "")
},
},
{
"Slice, language",
"/a/b/index.no.txt",
func(c *qt.C, p Path) {
c.Assert(p.Slice(0, 0), qt.Equals, "/a/b/index.no.txt")
c.Assert(p.Slice(1, 0), qt.Equals, "index.no.txt")
c.Assert(p.Slice(2, 0), qt.Equals, "no.txt")
c.Assert(p.Slice(3, 0), qt.Equals, "txt")
c.Assert(p.Slice(4, 0), qt.Equals, "txt")

c.Assert(p.Slice(0, 1), qt.Equals, "/a/b/index.no")
c.Assert(p.Slice(0, 2), qt.Equals, "/a/b/index")
c.Assert(p.Slice(0, 3), qt.Equals, "/a/b")
c.Assert(p.Slice(0, 4), qt.Equals, "/a/b")

c.Assert(p.Slice(1, 1), qt.Equals, "index.no")
c.Assert(p.Slice(1, 2), qt.Equals, "index")
c.Assert(p.Slice(1, 3), qt.Equals, "")
},
},
{
"Empty",
"",
Expand Down
5 changes: 1 addition & 4 deletions source/fileInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"sync"

"github.com/gohugoio/hugo/common/paths"
"github.com/gohugoio/hugo/htesting"

"github.com/gohugoio/hugo/hugofs/files"

Expand Down Expand Up @@ -168,9 +167,7 @@ func (fi *FileInfo) LogicalName() string {

// BaseFileName returns a file's name without extension (ie. "page.sv").
func (fi *FileInfo) BaseFileName() string {
htesting.Println("===>l", fi.p().Base())

return fi.baseName
return fi.p().NameNoExt()
}

// TranslationBaseName returns a file's translation base name without the
Expand Down

0 comments on commit 658ba38

Please sign in to comment.