[commit: packages/filepath] RyanGlScott-patch-1, master: The long lost cousin of takeExtension has found its way home (64f23c5)

git at git.haskell.org git at git.haskell.org
Thu Sep 13 14:51:02 UTC 2018


Repository : ssh://git@git.haskell.org/filepath

On branches: RyanGlScott-patch-1,master
Link       : http://git.haskell.org/packages/filepath.git/commitdiff/64f23c5a5b8518bc8517f80b46e0d0b20de1b96d

>---------------------------------------------------------------

commit 64f23c5a5b8518bc8517f80b46e0d0b20de1b96d
Author: Jonatan H Sundqvist <jonatanhsundqvist at gmail.com>
Date:   Tue Aug 15 08:45:09 2017 +0200

    The long lost cousin of takeExtension has found its way home


>---------------------------------------------------------------

64f23c5a5b8518bc8517f80b46e0d0b20de1b96d
 System/FilePath/Internal.hs | 11 ++++++++++-
 tests/TestGen.hs            |  6 ++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/System/FilePath/Internal.hs b/System/FilePath/Internal.hs
index 4a376b3..1ad7e31 100644
--- a/System/FilePath/Internal.hs
+++ b/System/FilePath/Internal.hs
@@ -75,7 +75,7 @@ module System.FilePath.MODULE_NAME
     -- * Extension functions
     splitExtension,
     takeExtension, replaceExtension, (-<.>), dropExtension, addExtension, hasExtension, (<.>),
-    splitExtensions, dropExtensions, takeExtensions, replaceExtensions,
+    splitExtensions, dropExtensions, takeExtensions, replaceExtensions, isExtensionOf,
     stripExtension,
 
     -- * Filename\/directory functions
@@ -313,6 +313,15 @@ hasExtension :: FilePath -> Bool
 hasExtension = any isExtSeparator . takeFileName
 
 
+-- | Is the given string the final extension of the filename?
+--   The extension should not include the separator.
+--
+-- > "png" `isExtensionOf` "/directory/file.png"     == True
+-- > "png" `isExtensionOf` "/directory/file.png.jpg" == False
+-- > "csv" `isExtensionOf` "/directory/data.csv"     == True
+isExtensionOf :: String -> FilePath -> Bool
+isExtensionOf ext = (== ext) . drop 1 . takeExtension
+
 -- | Drop the given extension from a FilePath, and the @\".\"@ preceding it.
 --   Returns 'Nothing' if the FilePath does not have the given extension, or
 --   'Just' and the part before the extension if it does.
diff --git a/tests/TestGen.hs b/tests/TestGen.hs
index 7bc914e..cdff89c 100755
--- a/tests/TestGen.hs
+++ b/tests/TestGen.hs
@@ -106,6 +106,12 @@ tests =
     ,("W.hasExtension \"/directory/path.ext\" == True", property $ W.hasExtension "/directory/path.ext" == True)
     ,("P.hasExtension \"/directory/path\" == False", property $ P.hasExtension "/directory/path" == False)
     ,("W.hasExtension \"/directory/path\" == False", property $ W.hasExtension "/directory/path" == False)
+    ,("W.isExtensionOf \"png\" \"/directory/file.png\"     == True",  property $ W.isExtensionOf "png" "/directory/file.png"     == True)
+    ,("P.isExtensionOf \"png\" \"/directory/file.png\"     == True",  property $ P.isExtensionOf "png" "/directory/file.png"     == True)
+    ,("W.isExtensionOf \"png\" \"/directory/file.png.jpg\" == False", property $ W.isExtensionOf "png" "/directory/file.png.jpg" == False)
+    ,("P.isExtensionOf \"png\" \"/directory/file.png.jpg\" == False", property $ P.isExtensionOf "png" "/directory/file.png.jpg" == False)
+    ,("W.isExtensionOf \"csv\" \"/directory/data.csv\"     == True",  property $ W.isExtensionOf "csv" "/directory/data.csv"     == True)
+    ,("P.isExtensionOf \"csv\" \"/directory/data.csv\"     == True",  property $ P.isExtensionOf "csv" "/directory/data.csv"     == True)
     ,("null (P.takeExtension x) == not (P.hasExtension x)", property $ \(QFilePath x) -> null (P.takeExtension x) == not (P.hasExtension x))
     ,("null (W.takeExtension x) == not (W.hasExtension x)", property $ \(QFilePath x) -> null (W.takeExtension x) == not (W.hasExtension x))
     ,("P.stripExtension \"hs.o\" \"foo.x.hs.o\" == Just \"foo.x\"", property $ P.stripExtension "hs.o" "foo.x.hs.o" == Just "foo.x")



More information about the ghc-commits mailing list