module Network.HTTP.Utils
( trim
, trimL
, trimR
, crlf
, lf
, sp
, split
, splitBy
, readsOne
, dropWhileTail
, chopAtDelim
) where
import Data.Char
import Data.List ( elemIndex )
import Data.Maybe ( fromMaybe )
crlf :: String
crlf = "\r\n"
lf :: String
lf = "\n"
sp :: String
sp = " "
split :: Eq a => a -> [a] -> Maybe ([a],[a])
split delim list = case delim `elemIndex` list of
Nothing -> Nothing
Just x -> Just $ splitAt x list
trim :: String -> String
trim xs = trimR (trimL xs)
trimL :: String -> String
trimL xs = dropWhile isSpace xs
trimR :: String -> String
trimR str = fromMaybe "" $ foldr trimIt Nothing str
where
trimIt x (Just xs) = Just (x:xs)
trimIt x Nothing
| isSpace x = Nothing
| otherwise = Just [x]
splitBy :: Eq a => a -> [a] -> [[a]]
splitBy _ [] = []
splitBy c xs =
case break (==c) xs of
(_,[]) -> [xs]
(as,_:bs) -> as : splitBy c bs
readsOne :: Read a => (a -> b) -> b -> String -> b
readsOne f n str =
case reads str of
((v,_):_) -> f v
_ -> n
dropWhileTail :: (a -> Bool) -> [a] -> [a]
dropWhileTail f ls =
case foldr chop Nothing ls of { Just xs -> xs; Nothing -> [] }
where
chop x (Just xs) = Just (x:xs)
chop x _
| f x = Nothing
| otherwise = Just [x]
chopAtDelim :: Eq a => a -> [a] -> ([a],[a])
chopAtDelim elt xs =
case break (==elt) xs of
(_,[]) -> (xs,[])
(as,_:bs) -> (as,bs)