Improve splitPath

This commit is contained in:
Andy Lok 2020-12-07 05:18:58 +08:00
parent d3a74e0199
commit 5578761bdb
2 changed files with 47 additions and 24 deletions

View File

@ -298,10 +298,28 @@ append' left right =
else else
record { body = left.body ++ right.body, hasTrailSep = right.hasTrailSep } left record { body = left.body ++ right.body, hasTrailSep = right.hasTrailSep } left
splitPath' : Path -> List Path
splitPath' path =
case splitRoot path of
(Just root, other) => root :: iterateBody (path.body) (path.hasTrailSep)
(Nothing, other) => iterateBody (path.body) (path.hasTrailSep)
where
splitRoot : Path -> (Maybe Path, Path)
splitRoot path@(MkPath Nothing False _ _) = (Nothing, path)
splitRoot (MkPath vol root xs trailSep) =
(Just $ MkPath vol root [] False, MkPath Nothing False xs trailSep)
iterateBody : List Body -> (trailSep : Bool) -> List Path
iterateBody [] _ = []
iterateBody [x] trailSep = [MkPath Nothing False [x] trailSep]
iterateBody (x::y::xs) trailSep =
(MkPath Nothing False [x] False) :: iterateBody (y::xs) trailSep
splitParent' : Path -> Maybe (Path, Path) splitParent' : Path -> Maybe (Path, Path)
splitParent' path with (path.body) splitParent' path =
splitParent' path | [] = Nothing case path.body of
splitParent' path | (x::xs) = [] => Nothing
(x::xs) =>
let let
parent = record { body = init (x::xs), hasTrailSep = False } path parent = record { body = init (x::xs), hasTrailSep = False } path
child = MkPath Nothing False [last (x::xs)] path.hasTrailSep child = MkPath Nothing False [last (x::xs)] path.hasTrailSep
@ -386,17 +404,11 @@ joinPath xs = foldl (</>) "" xs
||| |||
||| ```idris example ||| ```idris example
||| splitPath "/usr/local/etc" == ["/", "usr", "local", "etc"] ||| splitPath "/usr/local/etc" == ["/", "usr", "local", "etc"]
||| splitPath "/tmp/Foo.idr" == ["/", "tmp", "Foo.idr"] ||| splitPath "tmp/Foo.idr" == ["tmp", "Foo.idr"]
||| ``` ||| ```
export export
splitPath : String -> List String splitPath : String -> List String
splitPath path = assert_total $ map show $ reverse $ iterate (parse path) splitPath path = map show $ splitPath' (parse path)
where
iterate : Path -> List Path
iterate path =
case splitParent' path of
Just (parent, child) => child :: iterate parent
Nothing => []
||| Split the path into parent and child. ||| Split the path into parent and child.
||| |||

View File

@ -296,6 +296,23 @@ append' left right =
else else
record { body = left.body ++ right.body, hasTrailSep = right.hasTrailSep } left record { body = left.body ++ right.body, hasTrailSep = right.hasTrailSep } left
splitPath' : Path -> List Path
splitPath' path =
case splitRoot path of
(Just root, other) => root :: iterateBody (path.body) (path.hasTrailSep)
(Nothing, other) => iterateBody (path.body) (path.hasTrailSep)
where
splitRoot : Path -> (Maybe Path, Path)
splitRoot path@(MkPath Nothing False _ _) = (Nothing, path)
splitRoot (MkPath vol root xs trailSep) =
(Just $ MkPath vol root [] False, MkPath Nothing False xs trailSep)
iterateBody : List Body -> (trailSep : Bool) -> List Path
iterateBody [] _ = []
iterateBody [x] trailSep = [MkPath Nothing False [x] trailSep]
iterateBody (x::y::xs) trailSep =
(MkPath Nothing False [x] False) :: iterateBody (y::xs) trailSep
splitParent' : Path -> Maybe (Path, Path) splitParent' : Path -> Maybe (Path, Path)
splitParent' path = splitParent' path =
case path.body of case path.body of
@ -385,17 +402,11 @@ joinPath xs = foldl (</>) "" xs
||| |||
||| ```idris example ||| ```idris example
||| splitPath "/usr/local/etc" == ["/", "usr", "local", "etc"] ||| splitPath "/usr/local/etc" == ["/", "usr", "local", "etc"]
||| splitPath "/tmp/Foo.idr" == ["/", "tmp", "Foo.idr"] ||| splitPath "tmp/Foo.idr" == ["tmp", "Foo.idr"]
||| ``` ||| ```
export export
splitPath : String -> List String splitPath : String -> List String
splitPath path = assert_total $ map show $ reverse $ iterate (parse path) splitPath path = map show $ splitPath' (parse path)
where
iterate : Path -> List Path
iterate path =
case splitParent' path of
Just (parent, child) => child :: iterate parent
Nothing => []
||| Split the path into parent and child. ||| Split the path into parent and child.
||| |||