Skip to content

Commit 9488e86

Browse files
committed
Fix review comments
1 parent 95328d6 commit 9488e86

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

src/Streamly/Coreutils/Basename.hs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,42 @@
77
-- Portability : GHC
88
--
99
-- Return pathe with any leading directory components removed.
10-
-- If specified, also remove a trailing suffix (.extension).
10+
-- If specified, also remove a trailing suffix.
1111

1212
module Streamly.Coreutils.Basename
1313
( basename
14+
, basenameWith
1415

1516
-- * Options
1617
, Basename
18+
, Suffix(..)
1719
, suffix
1820
)
1921
where
2022

21-
import System.FilePath (takeBaseName, takeFileName)
22-
import Streamly.Coreutils.Common (Switch(..))
23+
import Data.List (stripPrefix)
2324

24-
newtype Basename = Basename {keepSuffix :: Switch}
25+
data Suffix = None | Suffix [Char]
2526

26-
suffix :: Switch -> Basename -> Basename
27-
suffix opt cfg = cfg {keepSuffix = opt}
27+
newtype Basename = Basename {removeSuffix :: Suffix}
28+
29+
suffix :: Suffix -> Basename -> Basename
30+
suffix opt cfg = cfg {removeSuffix = opt}
2831

2932
defaultConfig :: Basename
30-
defaultConfig = Basename On
33+
defaultConfig = Basename None
3134

32-
basename :: (Basename -> Basename) -> FilePath -> String
33-
basename f path =
35+
basenameWith :: (Basename -> Basename) -> FilePath -> String
36+
basenameWith f path =
3437
let opt = f defaultConfig
35-
in case keepSuffix opt of
36-
Off -> takeBaseName path
37-
On -> takeFileName path
38+
base = reverse $ takeWhile (/= '/') $ reverse path
39+
in case removeSuffix opt of
40+
None -> base
41+
Suffix x ->
42+
let suf = reverse x
43+
val0 = stripPrefix suf $ takeWhile (/= '/') $ reverse path
44+
val = maybe base reverse val0
45+
in val
46+
47+
basename :: FilePath -> String
48+
basename = basenameWith id

0 commit comments

Comments
 (0)