Skip to content

Commit ae1dd7d

Browse files
committed
Add function stripFilePath
1 parent 001845d commit ae1dd7d

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

System/FilePath/Internal.hs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ module System.FilePath.MODULE_NAME
8585
takeDirectory, replaceDirectory,
8686
combine, (</>),
8787
splitPath, joinPath, splitDirectories,
88+
stripFilePath,
8889

8990
-- * Drive functions
9091
splitDrive, joinDrive,
@@ -827,6 +828,49 @@ makeRelative root path
827828
takeAbs x | hasLeadingPathSeparator x && not (hasDrive x) = [pathSeparator]
828829
takeAbs x = map (\y -> if isPathSeparator y then pathSeparator else toLower y) $ takeDrive x
829830

831+
-- | Strip the given directory from the filepath if and only if
832+
-- the given directory is a prefix of the filepath.
833+
--
834+
-- >>> stripFilePath "app" "app/File.hs"
835+
-- Just "File.hs"
836+
837+
-- >>> stripFilePath "src" "app/File.hs"
838+
-- Nothing
839+
840+
-- >>> stripFilePath "src" "src-dir/File.hs"
841+
-- Nothing
842+
843+
-- >>> stripFilePath "." "src/File.hs"
844+
-- Just "src/File.hs"
845+
846+
-- >>> stripFilePath "app/" "./app/Lib/File.hs"
847+
-- Just "Lib/File.hs"
848+
849+
-- >>> stripFilePath "/app/" "./app/Lib/File.hs"
850+
-- Nothing -- Nothing since '/app/' is absolute
851+
852+
-- >>> stripFilePath "/app" "/app/Lib/File.hs"
853+
-- Just "Lib/File.hs"
854+
stripFilePath :: FilePath -> FilePath -> Maybe FilePath
855+
stripFilePath "." fp
856+
| isRelative fp = Just fp
857+
| otherwise = Nothing
858+
stripFilePath dir' fp'
859+
| Just relativeFpParts <- splitDir `stripPrefix` splitFp = Just (joinPath relativeFpParts)
860+
| otherwise = Nothing
861+
where
862+
dir = normalise dir'
863+
fp = normalise fp'
864+
865+
splitFp = splitPath fp
866+
splitDir = splitPath dir
867+
868+
stripFilePath' (x:xs) (y:ys)
869+
| x `equalFilePath` y = stripFilePath' xs ys
870+
| otherwise = Nothing
871+
stripFilePath' [] ys = Just ys
872+
stripFilePath' _ [] = Nothing
873+
830874
-- | Normalise a file
831875
--
832876
-- * \/\/ outside of the drive can be made blank

tests/TestGen.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,13 @@ tests =
384384
,("P.makeRelative \"/file/test\" \"/file/test/fred\" == \"fred\"", property $ P.makeRelative "/file/test" "/file/test/fred" == "fred")
385385
,("P.makeRelative \"/file/test\" \"/file/test/fred/\" == \"fred/\"", property $ P.makeRelative "/file/test" "/file/test/fred/" == "fred/")
386386
,("P.makeRelative \"some/path\" \"some/path/a/b/c\" == \"a/b/c\"", property $ P.makeRelative "some/path" "some/path/a/b/c" == "a/b/c")
387+
,("P.stripFilePath \"app\" \"app/File.hs\" == Just \"File.hs\"", property $ P.stripFilePath "app" "app/File.hs" == Just "File.hs")
388+
,("P.stripFilePath \"src\" \"app/File.hs\" == Nothing", property $ P.stripFilePath "src" "app/File.hs" == Nothing)
389+
,("P.stripFilePath \"src\" \"src-dir/File.hs\" == Nothing", property $ P.stripFilePath "src" "src-dir/File.hs" == Nothing)
390+
,("P.stripFilePath \".\" \"src/File.hs\" == Just \"src/File.hs\"", property $ P.stripFilePath "." "src/File.hs" == Just "src/File.hs")
391+
,("P.stripFilePath \"app/\" \"./app/Lib/File.hs\" == Just \"Lib/File.hs\"", property $ P.stripFilePath "app/" "./app/Lib/File.hs" == Just "Lib/File.hs")
392+
,("P.stripFilePath \"/app/\" \"./app/Lib/File.hs\" == Nothing", property $ P.stripFilePath "/app/" "./app/Lib/File.hs" == Nothing)
393+
,("P.stripFilePath \"/app\" \"/app/Lib/File.hs\" == Just \"Lib/File.hs\"", property $ P.stripFilePath "/app" "/app/Lib/File.hs" == Just "Lib/File.hs")
387394
,("P.normalise \"/file/\\\\test////\" == \"/file/\\\\test/\"", property $ P.normalise "/file/\\test////" == "/file/\\test/")
388395
,("P.normalise \"/file/./test\" == \"/file/test\"", property $ P.normalise "/file/./test" == "/file/test")
389396
,("P.normalise \"/test/file/../bob/fred/\" == \"/test/file/../bob/fred/\"", property $ P.normalise "/test/file/../bob/fred/" == "/test/file/../bob/fred/")

0 commit comments

Comments
 (0)