util: safepath: Add a new Dir method and tests
This adds a new helper method. It should be considered for other types as well.
This commit is contained in:
@@ -176,7 +176,6 @@ func (obj AbsFile) Cmp(absFile AbsFile) error {
|
||||
}
|
||||
|
||||
// Base returns the last component of the AbsFile, in this case, the filename.
|
||||
// TODO: add tests
|
||||
func (obj AbsFile) Base() RelFile {
|
||||
obj.PanicValidate()
|
||||
ix := strings.LastIndex(obj.path, "/")
|
||||
@@ -185,6 +184,20 @@ func (obj AbsFile) Base() RelFile {
|
||||
}
|
||||
}
|
||||
|
||||
// Dir returns the head component of the AbsFile, in this case, the directory.
|
||||
func (obj AbsFile) Dir() AbsDir {
|
||||
obj.PanicValidate()
|
||||
ix := strings.LastIndex(obj.path, "/")
|
||||
if ix == 0 {
|
||||
return AbsDir{
|
||||
path: "/",
|
||||
}
|
||||
}
|
||||
return AbsDir{
|
||||
path: obj.path[0:ix],
|
||||
}
|
||||
}
|
||||
|
||||
// HasDir returns true if the input relative dir is present in the path.
|
||||
func (obj AbsFile) HasDir(relDir RelDir) bool {
|
||||
obj.PanicValidate()
|
||||
|
||||
Reference in New Issue
Block a user