Verified Commit 7ba2a7c6 authored by Camil Staps's avatar Camil Staps 🚀

Reduce heap usage of System.FilePath; add concatPaths to avoid many </> concatenations

parent 916cbcb8
Pipeline #40687 passed with stage
in 1 minute and 53 seconds
......@@ -49,6 +49,9 @@ from System.OSError import :: OSError, :: OSErrorCode, :: OSErrorMessage, :: May
*/
pathSeparator :: Char
//* `pathSeparator` as a String.
pathSeparatorString :: String
/**
* Returns a list of all allowed platform path separators
*/
......@@ -59,11 +62,17 @@ pathSeparators :: [Char]
*/
extSeparator :: Char
//* `extSeparator` as a String.
extSeparatorString :: String
/**
* Concatenates two paths
*/
(</>) infixr 5 :: !FilePath !FilePath -> FilePath
//* Concatenate a list of paths.
concatPaths :: ![FilePath] -> FilePath
/**
* Split a FilePath into filename and extension. The result does not include the extension separator (.).
*
......
......@@ -14,14 +14,32 @@ import qualified System._FilePath
pathSeparator :: Char
pathSeparator = OS_PATH_SEPARATOR
pathSeparatorString :: String
pathSeparatorString =: {#pathSeparator}
pathSeparators :: [Char]
pathSeparators = ['\\', '/']
extSeparator :: Char
extSeparator = '.'
extSeparatorString :: String
extSeparatorString =: {#extSeparator}
(</>) infixr 5 :: !FilePath !FilePath -> FilePath
(</>) x y = (addTrailingPathSeparator x) +++ y
(</>) x y
| hasTrailingPathSeparator x
= x +++ y
= concat [x,pathSeparatorString,y]
concatPaths :: ![FilePath] -> FilePath
concatPaths paths = concat (addSeparators paths)
where
addSeparators [] = []
addSeparators [p:ps]
| hasTrailingPathSeparator p
= [p:addSeparators ps]
= [p,pathSeparatorString:ps]
splitExtension :: !FilePath -> (String, String)
splitExtension path = split sz
......@@ -46,23 +64,29 @@ dropExtension :: !FilePath -> String
dropExtension path = fst (splitExtension path)
addExtension :: !FilePath !String -> FilePath
addExtension path "" = path
addExtension path ext | path.[size path - 1] == extSeparator = path +++ ext
addExtension path ext = path +++ {extSeparator} +++ ext
addExtension path ext
| size ext == 0
= path
# sz = size path
| sz == 0
= ext
| path.[sz-1] == extSeparator
= path +++ ext
= concat [path,extSeparatorString,ext]
replaceExtension :: !FilePath !String -> FilePath
replaceExtension path ext = addExtension (dropExtension path) ext
hasTrailingPathSeparator :: !FilePath -> Bool
hasTrailingPathSeparator "" = False
hasTrailingPathSeparator path = path.[size path - 1] == pathSeparator
addTrailingPathSeparator :: !FilePath -> FilePath
addTrailingPathSeparator path = if (hasTrailingPathSeparator path) path (path +++ {pathSeparator})
hasTrailingPathSeparator path
# sz = size path
| sz == 0
= False
= path.[sz-1] == pathSeparator
splitFileName :: !FilePath -> (String, String)
splitFileName path =
case lastIndexOf {pathSeparator} path of
case lastIndexOf pathSeparatorString path of
-1 -> ("", path)
i -> (subString 0 i path, subString (i+1) (size path - i - 1) path)
......@@ -70,7 +94,7 @@ takeDirectory :: !FilePath -> FilePath
takeDirectory path = fst (splitFileName path)
dropDirectory :: !FilePath -> String
dropDirectory path = case lastIndexOf {pathSeparator} path of
dropDirectory path = case lastIndexOf pathSeparatorString path of
-1 = path
i | i == sizePath - 1 = dropDirectory $ subString 0 (sizePath - 1) path // drop file separator at end of path
| otherwise = subString (i+1) (sizePath - i - 1) path
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment