imp: lib: Hledger.Utils.String: more string strippers
added: strip1Char stripBy strip1By Not used in hledger right now, but useful to offer in our scripting prelude.
This commit is contained in:
parent
521e0adb5d
commit
d3e4f8547c
@ -20,6 +20,9 @@ module Hledger.Utils.String (
|
||||
strip,
|
||||
lstrip,
|
||||
rstrip,
|
||||
strip1Char,
|
||||
stripBy,
|
||||
strip1By,
|
||||
chomp,
|
||||
chomp1,
|
||||
singleline,
|
||||
@ -69,6 +72,25 @@ lstrip = dropWhile isSpace
|
||||
rstrip :: String -> String
|
||||
rstrip = reverse . lstrip . reverse
|
||||
|
||||
-- | Strip the given starting and ending character
|
||||
-- from the start and end of a string if both are present.
|
||||
strip1Char :: Char -> Char -> String -> String
|
||||
strip1Char b e s = case s of
|
||||
(c:cs) | c==b, not $ null cs, last cs==e -> init cs
|
||||
_ -> s
|
||||
|
||||
-- | Strip a run of zero or more characters matching the predicate
|
||||
-- from the start and end of a string.
|
||||
stripBy :: (Char -> Bool) -> String -> String
|
||||
stripBy f = dropWhileEnd f . dropWhile f
|
||||
|
||||
-- | Strip a single balanced enclosing pair of a character matching the predicate
|
||||
-- from the start and end of a string.
|
||||
strip1By :: (Char -> Bool) -> String -> String
|
||||
strip1By f s = case s of
|
||||
(c:cs) | f c, not $ null cs, last cs==c -> init cs
|
||||
_ -> s
|
||||
|
||||
-- | Remove all trailing newlines/carriage returns.
|
||||
chomp :: String -> String
|
||||
chomp = reverse . dropWhile (`elem` "\r\n") . reverse
|
||||
|
||||
Loading…
Reference in New Issue
Block a user