@@ -75,7 +75,7 @@ module System.FilePath.MODULE_NAME
75
75
-- * Extension functions
76
76
splitExtension ,
77
77
takeExtension , replaceExtension , (-<.>) , dropExtension , addExtension , hasExtension , (<.>) ,
78
- splitExtensions , dropExtensions , takeExtensions , replaceExtensions ,
78
+ splitExtensions , dropExtensions , takeExtensions , replaceExtensions , isExtensionOf ,
79
79
stripExtension ,
80
80
81
81
-- * Filename\/directory functions
@@ -105,7 +105,7 @@ module System.FilePath.MODULE_NAME
105
105
106
106
import Data.Char (toLower , toUpper , isAsciiLower , isAsciiUpper )
107
107
import Data.Maybe (isJust )
108
- import Data.List (stripPrefix )
108
+ import Data.List (stripPrefix , isSuffixOf )
109
109
110
110
import System.Environment (getEnv )
111
111
@@ -313,6 +313,18 @@ hasExtension :: FilePath -> Bool
313
313
hasExtension = any isExtSeparator . takeFileName
314
314
315
315
316
+ -- | Does the given filename have the specified extension?
317
+ --
318
+ -- > "png" `isExtensionOf` "/directory/file.png" == True
319
+ -- > ".png" `isExtensionOf` "/directory/file.png" == True
320
+ -- > ".tar.gz" `isExtensionOf` "bar/foo.tar.gz" == True
321
+ -- > "ar.gz" `isExtensionOf` "bar/foo.tar.gz" == False
322
+ -- > "png" `isExtensionOf` "/directory/file.png.jpg" == False
323
+ -- > "csv/table.csv" `isExtensionOf` "/data/csv/table.csv" == False
324
+ isExtensionOf :: String -> FilePath -> Bool
325
+ isExtensionOf ext@ (' .' : _) = isSuffixOf ext . takeExtensions
326
+ isExtensionOf ext = isSuffixOf (' .' : ext) . takeExtensions
327
+
316
328
-- | Drop the given extension from a FilePath, and the @\".\"@ preceding it.
317
329
-- Returns 'Nothing' if the FilePath does not have the given extension, or
318
330
-- 'Just' and the part before the extension if it does.
0 commit comments