[commit: packages/filepath] master: Refactor normalise: simplify propSep (eca255a)

git at git.haskell.org git at git.haskell.org
Thu Mar 19 11:34:32 UTC 2015


Repository : ssh://git@git.haskell.org/filepath

On branch  : master
Link       : http://git.haskell.org/packages/filepath.git/commitdiff/eca255a41c6e6cf427f22f468f5c051ecad3d4e3

>---------------------------------------------------------------

commit eca255a41c6e6cf427f22f468f5c051ecad3d4e3
Author: Thomas Miedema <thomasmiedema at gmail.com>
Date:   Mon Sep 15 00:20:52 2014 +0200

    Refactor normalise: simplify propSep
    
    Only when a path on Windows starts with a leading forward slash does propSep
    need to do something, all others path separators are properly taken care of by
    joinPath.
    
    Add 2 tests to show that splitDirectories can handle superfluous
    pathSeparators, and a test to show that (this new version of) propSep is
    indeed needed to normalise relative-to-root paths on Windows.


>---------------------------------------------------------------

eca255a41c6e6cf427f22f468f5c051ecad3d4e3
 System/FilePath/Internal.hs | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/System/FilePath/Internal.hs b/System/FilePath/Internal.hs
index 7256991..8b67fb2 100644
--- a/System/FilePath/Internal.hs
+++ b/System/FilePath/Internal.hs
@@ -642,6 +642,8 @@ splitPath x = [drive | drive /= ""] ++ f path
 -- > Windows: splitDirectories "C:\\test\\file" == ["C:\\", "test", "file"]
 -- > Valid x => joinPath (splitDirectories x) `equalFilePath` x
 -- > splitDirectories "" == []
+-- > Windows: splitDirectories "C:\\test\\\\\\file" == ["C:\\", "test", "file"]
+-- >          splitDirectories "/test///file" == ["/","test","file"]
 splitDirectories :: FilePath -> [FilePath]
 splitDirectories path =
         if hasDrive path then head pathComponents : f (tail pathComponents)
@@ -751,6 +753,7 @@ makeRelative root path
 -- > Windows: normalise "\\\\server\\test" == "\\\\server\\test"
 -- > Windows: normalise "//server/test" == "\\\\server\\test"
 -- > Windows: normalise "c:/file" == "C:\\file"
+-- > Windows: normalise "/file" == "\\file"
 -- > Windows: normalise "\\" == "\\"
 -- >          normalise "." == "."
 -- > Posix:   normalise "./" == "./"
@@ -769,13 +772,10 @@ normalise path = joinDrive' (normaliseDrive drv) (f pth)
         isDirPath xs = hasTrailingPathSeparator xs
             || not (null xs) && last xs == '.' && hasTrailingPathSeparator (init xs)
 
-        f = joinPath . dropDots . splitDirectories . propSep
+        f = joinPath . dropDots . propSep . splitDirectories
 
-        propSep (a:b:xs)
-         | isPathSeparator a && isPathSeparator b = propSep (a:xs)
-        propSep (a:xs)
-         | isPathSeparator a = pathSeparator : propSep xs
-        propSep (x:xs) = x : propSep xs
+        propSep (x:xs) | all isPathSeparator x = [pathSeparator] : xs
+                       | otherwise = x : xs
         propSep [] = []
 
         dropDots = filter ("." /=)



More information about the ghc-commits mailing list