Browse Source

Add rootPath, isRootPath, and getAllComponents{,AfterRoot}

travis
Julian Ospald 4 years ago
parent
commit
7b66379d49
No known key found for this signature in database GPG Key ID: 511B62C09D50CD28
1 changed files with 40 additions and 0 deletions
  1. +40
    -0
      hpath/src/HPath.hs

+ 40
- 0
hpath/src/HPath.hs View File

@@ -35,6 +35,7 @@ module HPath
,parseAbs
,parseRel
,parseAny
,rootPath
-- * Path Conversion
,fromAbs
,fromRel
@@ -45,9 +46,12 @@ module HPath
,basename
,dirname
,getAllParents
,getAllComponents
,getAllComponentsAfterRoot
,stripDir
-- * Path Examination
,isParentOf
,isRootPath
-- * Path IO helpers
,withAbsPath
,withRelPath
@@ -217,6 +221,10 @@ parseAny filepath = case parseAbs filepath of
Nothing -> throwM (InvalidRel filepath)


rootPath :: Path Abs
rootPath = (MkPath (BS.singleton _slash))


--------------------------------------------------------------------------------
-- Path Conversion

@@ -307,6 +315,26 @@ getAllParents (MkPath p)
np = normalise p


-- | Gets all path components.
--
-- >>> getAllComponents (MkPath "abs/def/dod")
-- ["abs","def","dod"]
-- >>> getAllComponents (MkPath "abs")
-- ["abs"]
getAllComponents :: Path Rel -> [Path Rel]
getAllComponents (MkPath p) = fmap MkPath . splitDirectories $ p


-- | Gets all path components after the "/" root directory.
--
-- >>> getAllComponentsAfterRoot (MkPath "/abs/def/dod")
-- ["abs","def","dod"]
-- >>> getAllComponentsAfterRoot (MkPath "/abs")
-- ["abs"]
getAllComponentsAfterRoot :: Path Abs -> [Path Rel]
getAllComponentsAfterRoot p = getAllComponents (fromJust $ stripDir rootPath p)


-- | Extract the directory name of a path.
--
-- >>> dirname (MkPath "/abc/def/dod")
@@ -360,6 +388,18 @@ basename (MkPath l)
-- False
isParentOf :: Path b -> Path b -> Bool
isParentOf p l = isJust (stripDir p l :: Maybe (Path Rel))


-- | Check whether the given Path is the root "/" path.
--
-- >>> isRootPath (MkPath "/lal/lad")
-- False
-- >>> isRootPath (MkPath "/")
-- True
isRootPath :: Path Abs -> Bool
isRootPath = (== rootPath)


--------------------------------------------------------------------------------
-- Path IO helpers



Loading…
Cancel
Save