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. -- |Compare the extension of a file with the given String.
cmpExt :: String -> FilePath -> Bool cmpExt :: String -> FilePath -> Bool
cmpExt _ [] = False cmpExt checkExt = (==) checkExt . getExt
cmpExt checkExt fp
| actualExt == fp = False
| otherwise = checkExt == actualExt
where
actualExt = getExt fp
-- |Get the extension of a file. -- |Get the extension of a file.
getExt :: FilePath -> String getExt :: FilePath -> String
getExt = last . getExt fp
splitBy (== '.') . | hasExt fp = last .
last . splitBy (== '.') .
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. -- |Split an array into subarrays depending on a given condition.