Skip to content

Commit 1230614

Browse files
authored
Merge pull request #61 from SwiftsNamesake/master
The long lost cousin of takeExtension has found its way home
2 parents b67ec85 + 2145c08 commit 1230614

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

System/FilePath/Internal.hs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module System.FilePath.MODULE_NAME
7575
-- * Extension functions
7676
splitExtension,
7777
takeExtension, replaceExtension, (-<.>), dropExtension, addExtension, hasExtension, (<.>),
78-
splitExtensions, dropExtensions, takeExtensions, replaceExtensions,
78+
splitExtensions, dropExtensions, takeExtensions, replaceExtensions, isExtensionOf,
7979
stripExtension,
8080

8181
-- * Filename\/directory functions
@@ -105,7 +105,7 @@ module System.FilePath.MODULE_NAME
105105

106106
import Data.Char(toLower, toUpper, isAsciiLower, isAsciiUpper)
107107
import Data.Maybe(isJust)
108-
import Data.List(stripPrefix)
108+
import Data.List(stripPrefix, isSuffixOf)
109109

110110
import System.Environment(getEnv)
111111

@@ -313,6 +313,18 @@ hasExtension :: FilePath -> Bool
313313
hasExtension = any isExtSeparator . takeFileName
314314

315315

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+
316328
-- | Drop the given extension from a FilePath, and the @\".\"@ preceding it.
317329
-- Returns 'Nothing' if the FilePath does not have the given extension, or
318330
-- 'Just' and the part before the extension if it does.

0 commit comments

Comments
 (0)