Skip to content

Commit

Permalink
haskell#47, make isValid detect more invalid characters
Browse files Browse the repository at this point in the history
  • Loading branch information
ndmitchell committed Dec 22, 2015
1 parent 63eaad2 commit dd13bb3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
10 changes: 5 additions & 5 deletions System/FilePath/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/TestGen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit dd13bb3

Please sign in to comment.