[commit: packages/filepath] master: #42, add replaceExtensions (10318a7)

git at git.haskell.org git at git.haskell.org
Mon Dec 28 20:40:16 UTC 2015


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

On branch  : master
Link       : http://git.haskell.org/packages/filepath.git/commitdiff/10318a79d2028e8178057c0136bb08c52768c049

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

commit 10318a79d2028e8178057c0136bb08c52768c049
Author: Neil Mitchell <ndmitchell at gmail.com>
Date:   Tue Dec 22 13:21:02 2015 +0000

    #42, add replaceExtensions


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

10318a79d2028e8178057c0136bb08c52768c049
 System/FilePath/Internal.hs | 13 ++++++++++++-
 changelog.md                |  2 ++
 tests/TestGen.hs            |  4 ++++
 3 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/System/FilePath/Internal.hs b/System/FilePath/Internal.hs
index 27f2af8..2757c10 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,
+    splitExtensions, dropExtensions, takeExtensions, replaceExtensions,
 
     -- * Filename\/directory functions
     splitFileName,
@@ -340,6 +340,17 @@ takeExtensions :: FilePath -> String
 takeExtensions = snd . splitExtensions
 
 
+-- | Replace all extensions of a file with a new extension. Note
+--   that 'replaceExtension' and 'addExtension' both work for adding
+--   multiple extensions, so only required when you need to drop
+--   all extensions first.
+--
+-- > replaceExtensions "file.fred.bob" "txt" == "file.txt"
+-- > replaceExtensions "file.fred.bob" "tar.gz" == "file.tar.gz"
+replaceExtensions :: FilePath -> String -> FilePath
+replaceExtensions x y = dropExtensions x <.> y
+
+
 
 ---------------------------------------------------------------------
 -- Drive methods
diff --git a/changelog.md b/changelog.md
index e749e3f..976d006 100644
--- a/changelog.md
+++ b/changelog.md
@@ -4,6 +4,8 @@ _Note: below all `FilePath` values are unquoted, so `\\` really means two backsl
 
 ## 1.4.1.0  *Unreleased*
 
+ * Add `replaceExtensions` function.
+
  * Make `isValid` detect more invalid Windows paths, e.g. `nul .txt` and `foo\nbar`.
 
  * Improve the documentation.
diff --git a/tests/TestGen.hs b/tests/TestGen.hs
index ead85d8..93733e6 100755
--- a/tests/TestGen.hs
+++ b/tests/TestGen.hs
@@ -127,6 +127,10 @@ tests =
     ,("W.takeExtensions \"/directory/path.ext\" == \".ext\"", test $ W.takeExtensions "/directory/path.ext" == ".ext")
     ,("P.takeExtensions \"file.tar.gz\" == \".tar.gz\"", test $ P.takeExtensions "file.tar.gz" == ".tar.gz")
     ,("W.takeExtensions \"file.tar.gz\" == \".tar.gz\"", test $ W.takeExtensions "file.tar.gz" == ".tar.gz")
+    ,("P.replaceExtensions \"file.fred.bob\" \"txt\" == \"file.txt\"", test $ P.replaceExtensions "file.fred.bob" "txt" == "file.txt")
+    ,("W.replaceExtensions \"file.fred.bob\" \"txt\" == \"file.txt\"", test $ W.replaceExtensions "file.fred.bob" "txt" == "file.txt")
+    ,("P.replaceExtensions \"file.fred.bob\" \"tar.gz\" == \"file.tar.gz\"", test $ P.replaceExtensions "file.fred.bob" "tar.gz" == "file.tar.gz")
+    ,("W.replaceExtensions \"file.fred.bob\" \"tar.gz\" == \"file.tar.gz\"", test $ W.replaceExtensions "file.fred.bob" "tar.gz" == "file.tar.gz")
     ,("uncurry (++) (P.splitDrive x) == x", test $ \(QFilePath x) -> uncurry (++) (P.splitDrive x) == x)
     ,("uncurry (++) (W.splitDrive x) == x", test $ \(QFilePath x) -> uncurry (++) (W.splitDrive x) == x)
     ,("W.splitDrive \"file\" == (\"\", \"file\")", test $ W.splitDrive "file" == ("", "file"))



More information about the ghc-commits mailing list