{-# LANGUAGE BangPatterns #-}
module Hpp.String (
stringify
, unquote
, stripAngleBrackets
, trimSpaces
, breakOn
, cons
)
where
import Data.Char (isSpace)
import Data.List (isPrefixOf, find)
stringify :: String -> String
stringify s = '"' : concatMap aux (strip s) ++ "\""
where aux '\\' = "\\\\"
aux '"' = "\\\""
aux c = [c]
strip = trimSpaces . dropWhile isSpace
unquote :: String -> String
unquote = stripEnds '"' '"'
stripAngleBrackets :: String -> String
stripAngleBrackets = stripEnds '<' '>'
stripEnds :: Char -> Char -> String -> String
stripEnds start end s =
case s of
(start':rest)
| start == start' -> go rest
_ -> s
where go (c:[])
| c == end = []
go [] = []
go (c:cs) = c : go cs
trimSpaces :: String -> String
trimSpaces = trimEnd isSpace
trimEnd :: (a -> Bool) -> [a] -> [a]
trimEnd p = go id
where go _ [] = []
go acc (c:cs)
| p c = go (acc . (c:)) cs
| otherwise = acc (c : go id cs)
breakOn :: [(String,t)] -> String -> Maybe (t, String, String)
breakOn needles haystack = go 0 haystack
where go _ [] = Nothing
go !i xs@(_:xs') =
case find (flip isPrefixOf xs . fst) needles of
Nothing -> go (i+1) xs'
Just (n,tag) -> Just (tag, take i haystack, drop (length n) xs)
{-# INLINE breakOn #-}
cons :: a -> [a] -> [a]
cons x xs = x : xs