From 7b66379d4926f73435c9a24b5021741e657b6395 Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Mon, 20 Jan 2020 19:46:01 +0100 Subject: [PATCH] Add rootPath, isRootPath, and getAllComponents{,AfterRoot} --- hpath/src/HPath.hs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/hpath/src/HPath.hs b/hpath/src/HPath.hs index a167eb4..d68be84 100644 --- a/hpath/src/HPath.hs +++ b/hpath/src/HPath.hs @@ -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