-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasename.hs
More file actions
48 lines (39 loc) · 1.23 KB
/
Basename.hs
File metadata and controls
48 lines (39 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
-- |
-- 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.
module Streamly.Coreutils.Basename
( basename
, basenameWith
-- * Options
, Basename
, Suffix(..)
, suffix
)
where
import Data.List (stripPrefix)
data Suffix = None | Suffix [Char]
newtype Basename = Basename {removeSuffix :: Suffix}
suffix :: Suffix -> Basename -> Basename
suffix opt cfg = cfg {removeSuffix = opt}
defaultConfig :: Basename
defaultConfig = Basename None
basenameWith :: (Basename -> Basename) -> FilePath -> String
basenameWith f path =
let opt = f defaultConfig
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