Skip to content

Commit

Permalink
Fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rnjtranjan committed Jul 25, 2022
1 parent 67fbde4 commit 2353ae4
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/Streamly/Coreutils/Basename.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2353ae4

Please sign in to comment.