[commit: packages/filepath] master: Refactor isValid and makeValid (2d37bc2)

git at git.haskell.org git at git.haskell.org
Thu Mar 19 11:35:43 UTC 2015


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

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

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

commit 2d37bc25735bd658358092ff2209ef3abcf35160
Author: Thomas Miedema <thomasmiedema at gmail.com>
Date:   Tue Oct 28 19:16:52 2014 +0100

    Refactor isValid and makeValid
    
    `head (splitPath path)` is not equal to `takeDrive path` in general:
    
        head (splitPath "\\foo") == "\\"
        fst (splitDrive "\\foo") == ""
    
    We can do this refactoring because they /are/ equal within the
    constraint:
    
        `length drv >= 2 && all isPathSeparator drv`


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

2d37bc25735bd658358092ff2209ef3abcf35160
 System/FilePath/Internal.hs | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/System/FilePath/Internal.hs b/System/FilePath/Internal.hs
index 323f8e3..fedb014 100644
--- a/System/FilePath/Internal.hs
+++ b/System/FilePath/Internal.hs
@@ -808,8 +808,7 @@ isValid path =
         not (any f $ splitDirectories x2) &&
         not (length x1 >= 2 && all isPathSeparator x1)
     where
-        x1 = head (splitPath path)
-        x2 = dropDrive path
+        (x1,x2) = splitDrive path
         f x = map toUpper (dropExtensions x) `elem` badElements
 
 
@@ -828,11 +827,10 @@ isValid path =
 -- > Windows: makeValid "\\\\\\foo" == "\\\\drive"
 makeValid :: FilePath -> FilePath
 makeValid "" = "_"
-makeValid path | isPosix = path
-makeValid xs | length x >= 2 && all isPathSeparator x = take 2 x ++ "drive"
-    where
-        x = head (splitPath xs)
-makeValid path = joinDrive drv $ validElements $ validChars pth
+makeValid path
+        | isPosix = path
+        | length drv >= 2 && all isPathSeparator drv = take 2 drv ++ "drive"
+        | otherwise = joinDrive drv $ validElements $ validChars pth
     where
         (drv,pth) = splitDrive path
 



More information about the ghc-commits mailing list