[commit: packages/filepath] master: #47, make isValid detect more invalid characters (dd13bb3)

git at git.haskell.org git at git.haskell.org
Mon Dec 28 20:40:06 UTC 2015


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

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

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

commit dd13bb32b7b563248fa2bfa232891e05759d6506
Author: Neil Mitchell <ndmitchell at gmail.com>
Date:   Tue Dec 22 11:41:53 2015 +0000

    #47, make isValid detect more invalid characters


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

dd13bb32b7b563248fa2bfa232891e05759d6506
 System/FilePath/Internal.hs | 10 +++++-----
 tests/TestGen.hs            |  1 +
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/System/FilePath/Internal.hs b/System/FilePath/Internal.hs
index 4228376..30ee372 100644
--- a/System/FilePath/Internal.hs
+++ b/System/FilePath/Internal.hs
@@ -844,8 +844,8 @@ normaliseDrive drive = if isJust $ readDriveLetter x2
         repSlash x = if isPathSeparator x then pathSeparator else x
 
 -- Information for validity functions on Windows. See [1].
-badCharacters :: [Char]
-badCharacters = ":*?><|\""
+isBadCharacter :: Char -> Bool
+isBadCharacter x = x >= '\0' && x <= '\31' || x `elem` ":*?><|\""
 
 badElements :: [FilePath]
 badElements =
@@ -871,12 +871,13 @@ badElements =
 -- > Windows: isValid "\\\\" == False
 -- > Windows: isValid "\\\\\\foo" == False
 -- > Windows: isValid "\\\\?\\D:file" == False
+-- > Windows: isValid "foo\tbar" == False
 isValid :: FilePath -> Bool
 isValid "" = False
 isValid x | '\0' `elem` x = False
 isValid _ | isPosix = True
 isValid path =
-        not (any (`elem` badCharacters) x2) &&
+        not (any isBadCharacter x2) &&
         not (any f $ splitDirectories x2) &&
         not (isJust (readDriveShare x1) && all isPathSeparator x1) &&
         not (isJust (readDriveUNC x1) && not (hasTrailingPathSeparator x1))
@@ -912,8 +913,7 @@ makeValid path
         (drv,pth) = splitDrive path
 
         validChars = map f
-        f x | x `elem` badCharacters || x == '\0' = '_'
-            | otherwise = x
+        f x = if isBadCharacter x then '_' else x
 
         validElements x = joinPath $ map g $ splitPath x
         g x = h a ++ b
diff --git a/tests/TestGen.hs b/tests/TestGen.hs
index 7dfaffc..c3b1acd 100755
--- a/tests/TestGen.hs
+++ b/tests/TestGen.hs
@@ -384,6 +384,7 @@ tests =
     ,("W.isValid \"\\\\\\\\\" == False", test $ W.isValid "\\\\" == False)
     ,("W.isValid \"\\\\\\\\\\\\foo\" == False", test $ W.isValid "\\\\\\foo" == False)
     ,("W.isValid \"\\\\\\\\?\\\\D:file\" == False", test $ W.isValid "\\\\?\\D:file" == False)
+    ,("W.isValid \"foo\\tbar\" == False", test $ W.isValid "foo\tbar" == False)
     ,("P.isValid (P.makeValid x)", test $ \(QFilePath x) -> P.isValid (P.makeValid x))
     ,("W.isValid (W.makeValid x)", test $ \(QFilePath x) -> W.isValid (W.makeValid x))
     ,("P.isValid x ==> P.makeValid x == x", test $ \(QFilePath x) -> P.isValid x ==> P.makeValid x == x)



More information about the ghc-commits mailing list