[commit: packages/filepath] master: #44, isValid \0 now returns False (73b0a99)
git at git.haskell.org
git at git.haskell.org
Mon Dec 28 20:39:07 UTC 2015
Repository : ssh://git@git.haskell.org/filepath
On branch : master
Link : http://git.haskell.org/packages/filepath.git/commitdiff/73b0a99fd10918eb419dda91475b3057ccbcd4f6
>---------------------------------------------------------------
commit 73b0a99fd10918eb419dda91475b3057ccbcd4f6
Author: Neil Mitchell <ndmitchell at gmail.com>
Date: Thu Apr 23 15:19:35 2015 +0100
#44, isValid \0 now returns False
>---------------------------------------------------------------
73b0a99fd10918eb419dda91475b3057ccbcd4f6
System/FilePath/Internal.hs | 7 +++++--
changelog.md | 2 ++
tests/TestGen.hs | 4 ++++
3 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/System/FilePath/Internal.hs b/System/FilePath/Internal.hs
index 051e44c..42bc19b 100644
--- a/System/FilePath/Internal.hs
+++ b/System/FilePath/Internal.hs
@@ -843,6 +843,7 @@ badElements = ["CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5
-- | Is a FilePath valid, i.e. could you create a file like it?
--
-- > isValid "" == False
+-- > isValid "\0" == False
-- > Posix: isValid "/random_ path:*" == True
-- > Posix: isValid x == not (null x)
-- > Windows: isValid "c:\\test" == True
@@ -856,6 +857,7 @@ badElements = ["CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5
-- > Windows: isValid "\\\\?\\D:file" == False
isValid :: FilePath -> Bool
isValid "" = False
+isValid x | '\0' `elem` x = False
isValid _ | isPosix = True
isValid path =
not (any (`elem` badCharacters) x2) &&
@@ -872,6 +874,7 @@ isValid path =
-- > isValid (makeValid x)
-- > isValid x ==> makeValid x == x
-- > makeValid "" == "_"
+-- > makeValid "file\0name" == "file_name"
-- > Windows: makeValid "c:\\already\\/valid" == "c:\\already\\/valid"
-- > Windows: makeValid "c:\\test:of_test" == "c:\\test_of_test"
-- > Windows: makeValid "test*" == "test_"
@@ -884,7 +887,7 @@ isValid path =
makeValid :: FilePath -> FilePath
makeValid "" = "_"
makeValid path
- | isPosix = path
+ | isPosix = map (\x -> if x == '\0' then '_' else x) path
| isJust (readDriveShare drv) && all isPathSeparator drv = take 2 drv ++ "drive"
| isJust (readDriveUNC drv) && not (hasTrailingPathSeparator drv) =
makeValid (drv ++ [pathSeparator] ++ pth)
@@ -893,7 +896,7 @@ makeValid path
(drv,pth) = splitDrive path
validChars = map f
- f x | x `elem` badCharacters = '_'
+ f x | x `elem` badCharacters || x == '\0' = '_'
| otherwise = x
validElements x = joinPath $ map g $ splitPath x
diff --git a/changelog.md b/changelog.md
index 6c36406..902754e 100644
--- a/changelog.md
+++ b/changelog.md
@@ -2,6 +2,8 @@
_Note: below all `FilePath` values are unquoted, so `\\` really means two backslashes._
+ * Bug fix: `isValid "\0"` now returns `False`, instead of `True`
+
## 1.4.0.0 *Mar 2015*
* Bundled with GHC 7.10.1
diff --git a/tests/TestGen.hs b/tests/TestGen.hs
index a96ad54..9bb3ebe 100755
--- a/tests/TestGen.hs
+++ b/tests/TestGen.hs
@@ -369,6 +369,8 @@ tests =
,("P.normalise \"//home\" == \"/home\"", test $ P.normalise "//home" == "/home")
,("P.isValid \"\" == False", test $ P.isValid "" == False)
,("W.isValid \"\" == False", test $ W.isValid "" == False)
+ ,("P.isValid \"\\0\" == False", test $ P.isValid "\0" == False)
+ ,("W.isValid \"\\0\" == False", test $ W.isValid "\0" == False)
,("P.isValid \"/random_ path:*\" == True", test $ P.isValid "/random_ path:*" == True)
,("P.isValid x == not (null x)", test $ \(QFilePath x) -> P.isValid x == not (null x))
,("W.isValid \"c:\\\\test\" == True", test $ W.isValid "c:\\test" == True)
@@ -386,6 +388,8 @@ tests =
,("W.isValid x ==> W.makeValid x == x", test $ \(QFilePath x) -> W.isValid x ==> W.makeValid x == x)
,("P.makeValid \"\" == \"_\"", test $ P.makeValid "" == "_")
,("W.makeValid \"\" == \"_\"", test $ W.makeValid "" == "_")
+ ,("P.makeValid \"file\\0name\" == \"file_name\"", test $ P.makeValid "file\0name" == "file_name")
+ ,("W.makeValid \"file\\0name\" == \"file_name\"", test $ W.makeValid "file\0name" == "file_name")
,("W.makeValid \"c:\\\\already\\\\/valid\" == \"c:\\\\already\\\\/valid\"", test $ W.makeValid "c:\\already\\/valid" == "c:\\already\\/valid")
,("W.makeValid \"c:\\\\test:of_test\" == \"c:\\\\test_of_test\"", test $ W.makeValid "c:\\test:of_test" == "c:\\test_of_test")
,("W.makeValid \"test*\" == \"test_\"", test $ W.makeValid "test*" == "test_")
More information about the ghc-commits
mailing list