Simplify and improve extension handling

The functions should be safe now and all known corner cases
seem to work.
This commit is contained in:
hasufell 2014-10-06 02:26:04 +02:00
parent 0ee52c5bdd
commit 69026edf83
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020

25
Util.hs
View File

@ -14,20 +14,25 @@ inRange (xlD, xuD) (ylD, yuD) (x,y)
-- |Compare the extension of a file with the given String.
cmpExt :: String -> FilePath -> Bool
cmpExt _ [] = False
cmpExt checkExt fp
| actualExt == fp = False
| otherwise = checkExt == actualExt
where
actualExt = getExt fp
cmpExt checkExt = (==) checkExt . getExt
-- |Get the extension of a file.
getExt :: FilePath -> String
getExt = last .
splitBy (== '.') .
last .
splitBy (== '/')
getExt fp
| hasExt fp = last .
splitBy (== '.') .
last .
splitBy (== '/') $
fp
| otherwise = ""
-- |Check if the file has an extension.
hasExt :: FilePath -> Bool
hasExt fp
| (<= 1) . length . splitBy (== '.') $ fp = False
| otherwise = True
-- |Split an array into subarrays depending on a given condition.