diff options
| author | Zygmunt Krynicki <zygmunt.krynicki@canonical.com> | 2018-06-13 08:20:45 -0700 |
|---|---|---|
| committer | Zygmunt Krynicki <zygmunt.krynicki@canonical.com> | 2018-06-13 08:58:08 -0700 |
| commit | 7e30792992cf78790bf9f7a8e9fb46cbbe6b2c10 (patch) | |
| tree | e1b935954c29cdd9b2cae80f564a6024ead30dbc /strutil | |
| parent | 3922541ac5b0a6356a67310fc54f8b3f9d51d9ea (diff) | |
strutil: add PathIterator.Depth
This patch adds simple "depth" tracking to the path iterator. This allows to ask the iterator for a reliable directory count rather than having to keep track of that separately. Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Diffstat (limited to 'strutil')
| -rw-r--r-- | strutil/pathiter.go | 10 | ||||
| -rw-r--r-- | strutil/pathiter_test.go | 22 |
2 files changed, 32 insertions, 0 deletions
diff --git a/strutil/pathiter.go b/strutil/pathiter.go index 460ca5356d..7f88c50f51 100644 --- a/strutil/pathiter.go +++ b/strutil/pathiter.go @@ -43,6 +43,7 @@ import ( type PathIterator struct { path string left, right int + depth int } // NewPathIterator returns an iterator for traversing the given path. @@ -84,6 +85,14 @@ func (iter *PathIterator) CurrentBase() string { return iter.path[:iter.left] } +// Depth returns the directory depth of the current path. +// +// This is equal to the number of traversed directories, including that of the +// root directory. +func (iter *PathIterator) Depth() int { + return iter.depth +} + // Next advances the iterator to the next name, returning true if one is found. // // If this method returns false then no change is made and all helper methods @@ -117,5 +126,6 @@ func (iter *PathIterator) Next() bool { } else { iter.right = len(iter.path) } + iter.depth++ return true } diff --git a/strutil/pathiter_test.go b/strutil/pathiter_test.go index 7f5ab2c1f8..74577b8c31 100644 --- a/strutil/pathiter_test.go +++ b/strutil/pathiter_test.go @@ -39,60 +39,72 @@ func (s *pathIterSuite) TestPathIteratorFilename(c *C) { iter, err := strutil.NewPathIterator("foo") c.Assert(err, IsNil) c.Assert(iter.Path(), Equals, "foo") + c.Assert(iter.Depth(), Equals, 0) c.Assert(iter.Next(), Equals, true) c.Assert(iter.CurrentBase(), Equals, "") c.Assert(iter.CurrentPath(), Equals, "foo") c.Assert(iter.CurrentName(), Equals, "foo") c.Assert(iter.CurrentCleanName(), Equals, "foo") + c.Assert(iter.Depth(), Equals, 1) c.Assert(iter.Next(), Equals, false) + c.Assert(iter.Depth(), Equals, 1) } func (s *pathIterSuite) TestPathIteratorRelative(c *C) { iter, err := strutil.NewPathIterator("foo/bar") c.Assert(err, IsNil) c.Assert(iter.Path(), Equals, "foo/bar") + c.Assert(iter.Depth(), Equals, 0) c.Assert(iter.Next(), Equals, true) c.Assert(iter.CurrentBase(), Equals, "") c.Assert(iter.CurrentPath(), Equals, "foo/") c.Assert(iter.CurrentName(), Equals, "foo/") c.Assert(iter.CurrentCleanName(), Equals, "foo") + c.Assert(iter.Depth(), Equals, 1) c.Assert(iter.Next(), Equals, true) c.Assert(iter.CurrentBase(), Equals, "foo/") c.Assert(iter.CurrentPath(), Equals, "foo/bar") c.Assert(iter.CurrentName(), Equals, "bar") c.Assert(iter.CurrentCleanName(), Equals, "bar") + c.Assert(iter.Depth(), Equals, 2) c.Assert(iter.Next(), Equals, false) + c.Assert(iter.Depth(), Equals, 2) } func (s *pathIterSuite) TestPathIteratorAbsoluteClean(c *C) { iter, err := strutil.NewPathIterator("/foo/bar") c.Assert(err, IsNil) c.Assert(iter.Path(), Equals, "/foo/bar") + c.Assert(iter.Depth(), Equals, 0) c.Assert(iter.Next(), Equals, true) c.Assert(iter.CurrentBase(), Equals, "") c.Assert(iter.CurrentPath(), Equals, "/") c.Assert(iter.CurrentName(), Equals, "/") c.Assert(iter.CurrentCleanName(), Equals, "") + c.Assert(iter.Depth(), Equals, 1) c.Assert(iter.Next(), Equals, true) c.Assert(iter.CurrentBase(), Equals, "/") c.Assert(iter.CurrentPath(), Equals, "/foo/") c.Assert(iter.CurrentName(), Equals, "foo/") c.Assert(iter.CurrentCleanName(), Equals, "foo") + c.Assert(iter.Depth(), Equals, 2) c.Assert(iter.Next(), Equals, true) c.Assert(iter.CurrentBase(), Equals, "/foo/") c.Assert(iter.CurrentPath(), Equals, "/foo/bar") c.Assert(iter.CurrentName(), Equals, "bar") c.Assert(iter.CurrentCleanName(), Equals, "bar") + c.Assert(iter.Depth(), Equals, 3) c.Assert(iter.Next(), Equals, false) + c.Assert(iter.Depth(), Equals, 3) } func (s *pathIterSuite) TestPathIteratorAbsoluteUnclean(c *C) { @@ -105,14 +117,17 @@ func (s *pathIterSuite) TestPathIteratorRootDir(c *C) { iter, err := strutil.NewPathIterator("/") c.Assert(err, IsNil) c.Assert(iter.Path(), Equals, "/") + c.Assert(iter.Depth(), Equals, 0) c.Assert(iter.Next(), Equals, true) c.Assert(iter.CurrentBase(), Equals, "") c.Assert(iter.CurrentPath(), Equals, "/") c.Assert(iter.CurrentName(), Equals, "/") c.Assert(iter.CurrentCleanName(), Equals, "") + c.Assert(iter.Depth(), Equals, 1) c.Assert(iter.Next(), Equals, false) + c.Assert(iter.Depth(), Equals, 1) } func (s *pathIterSuite) TestPathIteratorUncleanPath(c *C) { @@ -125,32 +140,38 @@ func (s *pathIterSuite) TestPathIteratorUnicode(c *C) { iter, err := strutil.NewPathIterator("/zażółć/gęślą/jaźń") c.Assert(err, IsNil) c.Assert(iter.Path(), Equals, "/zażółć/gęślą/jaźń") + c.Assert(iter.Depth(), Equals, 0) c.Assert(iter.Next(), Equals, true) c.Assert(iter.CurrentBase(), Equals, "") c.Assert(iter.CurrentPath(), Equals, "/") c.Assert(iter.CurrentName(), Equals, "/") c.Assert(iter.CurrentCleanName(), Equals, "") + c.Assert(iter.Depth(), Equals, 1) c.Assert(iter.Next(), Equals, true) c.Assert(iter.CurrentBase(), Equals, "/") c.Assert(iter.CurrentPath(), Equals, "/zażółć/") c.Assert(iter.CurrentName(), Equals, "zażółć/") c.Assert(iter.CurrentCleanName(), Equals, "zażółć") + c.Assert(iter.Depth(), Equals, 2) c.Assert(iter.Next(), Equals, true) c.Assert(iter.CurrentBase(), Equals, "/zażółć/") c.Assert(iter.CurrentPath(), Equals, "/zażółć/gęślą/") c.Assert(iter.CurrentName(), Equals, "gęślą/") c.Assert(iter.CurrentCleanName(), Equals, "gęślą") + c.Assert(iter.Depth(), Equals, 3) c.Assert(iter.Next(), Equals, true) c.Assert(iter.CurrentBase(), Equals, "/zażółć/gęślą/") c.Assert(iter.CurrentPath(), Equals, "/zażółć/gęślą/jaźń") c.Assert(iter.CurrentName(), Equals, "jaźń") c.Assert(iter.CurrentCleanName(), Equals, "jaźń") + c.Assert(iter.Depth(), Equals, 4) c.Assert(iter.Next(), Equals, false) + c.Assert(iter.Depth(), Equals, 4) } func (s *pathIterSuite) TestPathIteratorExample(c *C) { @@ -161,5 +182,6 @@ func (s *pathIterSuite) TestPathIteratorExample(c *C) { _ = iter.CurrentPath() _ = iter.CurrentName() _ = iter.CurrentCleanName() + _ = iter.Depth() } } |
