From 6380eabb29f9ca2f490e2bbe64ca5a1744975b4f Mon Sep 17 00:00:00 2001 From: Ranjeet Kumar Ranjan Date: Tue, 19 Jul 2022 16:25:03 +0530 Subject: [PATCH 1/3] Add Basename module --- src/Streamly/Coreutils/Basename.hs | 38 ++++++++++++++++++++++++++++++ streamly-coreutils.cabal | 1 + 2 files changed, 39 insertions(+) create mode 100644 src/Streamly/Coreutils/Basename.hs diff --git a/src/Streamly/Coreutils/Basename.hs b/src/Streamly/Coreutils/Basename.hs new file mode 100644 index 0000000..103bad3 --- /dev/null +++ b/src/Streamly/Coreutils/Basename.hs @@ -0,0 +1,38 @@ +-- | +-- Module : Streamly.Coreutils.Basename +-- Copyright : (c) 2022 Composewell Technologies +-- License : BSD-3-Clause +-- Maintainer : streamly@composewell.com +-- Stability : experimental +-- Portability : GHC +-- +-- Return pathe with any leading directory components removed. +-- If specified, also remove a trailing suffix (.extension). + +module Streamly.Coreutils.Basename + ( basename + + -- * Options + , Basename + , suffix + ) +where + +import System.FilePath (takeBaseName, takeFileName) +import Streamly.Coreutils.Common (Switch(..)) + +newtype Basename = Basename {keepSuffix :: Switch} + +suffix :: Switch -> Basename -> Basename +suffix opt cfg = cfg {keepSuffix = opt} + +defaultConfig :: Basename +defaultConfig = Basename On + +basename :: (Basename -> Basename) -> FilePath -> IO String +basename f path = do + let opt = f defaultConfig + return $ + case keepSuffix opt of + Off -> takeBaseName path + On -> takeFileName path diff --git a/streamly-coreutils.cabal b/streamly-coreutils.cabal index 7ac11b4..b7727b0 100644 --- a/streamly-coreutils.cabal +++ b/streamly-coreutils.cabal @@ -108,6 +108,7 @@ library hs-source-dirs: src exposed-modules: Streamly.Coreutils + , Streamly.Coreutils.Basename , Streamly.Coreutils.Common , Streamly.Coreutils.Cp , Streamly.Coreutils.Directory From 67fbde42309c1b1df0e062340eb4f4692301b538 Mon Sep 17 00:00:00 2001 From: Ranjeet Kumar Ranjan Date: Tue, 19 Jul 2022 16:41:29 +0530 Subject: [PATCH 2/3] Change the signature of basename --- src/Streamly/Coreutils/Basename.hs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Streamly/Coreutils/Basename.hs b/src/Streamly/Coreutils/Basename.hs index 103bad3..4c542d6 100644 --- a/src/Streamly/Coreutils/Basename.hs +++ b/src/Streamly/Coreutils/Basename.hs @@ -29,10 +29,9 @@ suffix opt cfg = cfg {keepSuffix = opt} defaultConfig :: Basename defaultConfig = Basename On -basename :: (Basename -> Basename) -> FilePath -> IO String -basename f path = do +basename :: (Basename -> Basename) -> FilePath -> String +basename f path = let opt = f defaultConfig - return $ - case keepSuffix opt of + in case keepSuffix opt of Off -> takeBaseName path On -> takeFileName path From 2353ae4c4086d74d639b17cd660b81034f449015 Mon Sep 17 00:00:00 2001 From: Ranjeet Kumar Ranjan Date: Mon, 25 Jul 2022 17:30:15 +0530 Subject: [PATCH 3/3] Fix review comments --- src/Streamly/Coreutils/Basename.hs | 35 ++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/Streamly/Coreutils/Basename.hs b/src/Streamly/Coreutils/Basename.hs index 4c542d6..e8ea7eb 100644 --- a/src/Streamly/Coreutils/Basename.hs +++ b/src/Streamly/Coreutils/Basename.hs @@ -7,31 +7,42 @@ -- Portability : GHC -- -- Return pathe with any leading directory components removed. --- If specified, also remove a trailing suffix (.extension). +-- If specified, also remove a trailing suffix. module Streamly.Coreutils.Basename ( basename + , basenameWith -- * Options , Basename + , Suffix(..) , suffix ) where -import System.FilePath (takeBaseName, takeFileName) -import Streamly.Coreutils.Common (Switch(..)) +import Data.List (stripPrefix) -newtype Basename = Basename {keepSuffix :: Switch} +data Suffix = None | Suffix [Char] -suffix :: Switch -> Basename -> Basename -suffix opt cfg = cfg {keepSuffix = opt} +newtype Basename = Basename {removeSuffix :: Suffix} + +suffix :: Suffix -> Basename -> Basename +suffix opt cfg = cfg {removeSuffix = opt} defaultConfig :: Basename -defaultConfig = Basename On +defaultConfig = Basename None -basename :: (Basename -> Basename) -> FilePath -> String -basename f path = +basenameWith :: (Basename -> Basename) -> FilePath -> String +basenameWith f path = let opt = f defaultConfig - in case keepSuffix opt of - Off -> takeBaseName path - On -> takeFileName path + base = reverse $ takeWhile (/= '/') $ reverse path + in case removeSuffix opt of + None -> base + Suffix x -> + let suf = reverse x + val0 = stripPrefix suf $ takeWhile (/= '/') $ reverse path + val = maybe base reverse val0 + in val + +basename :: FilePath -> String +basename = basenameWith id