1 -- |Utility functions used internally in the Lucu httpd. These
2 -- functions may be useful too for something else.
3 module Network.HTTP.Lucu.Utils
13 import Control.Monad.Trans
20 -- |> splitBy (== ':') "ab:c:def"
21 -- > ==> ["ab", "c", "def"]
22 splitBy :: (a -> Bool) -> [a] -> [[a]]
23 splitBy isSeparator src
24 = case break isSeparator src
25 of (last , [] ) -> last : []
26 (first, sep:rest) -> first : splitBy isSeparator rest
28 -- |> joinWith ':' ["ab", "c", "def"]
30 joinWith :: [a] -> [[a]] -> [a]
32 = foldr (++) [] $ intersperse separator xs
34 -- |> trim (== '_') "__ab_c__def___"
36 trim :: (a -> Bool) -> [a] -> [a]
37 trim p = trimTail . trimHead
39 trimHead = dropWhile p
40 trimTail = reverse . trimHead . reverse
42 -- |@'noCaseEq' a b@ is equivalent to @(map toLower a) == (map toLower
44 noCaseEq :: String -> String -> Bool
46 = (map toLower a) == (map toLower b)
48 -- |@'isWhiteSpace' c@ is True iff c is one of SP, HT, CR and LF.
49 isWhiteSpace :: Char -> Bool
50 isWhiteSpace = flip elem " \t\r\n"
56 -- > ==> "\"ab\\\"c\""
57 quoteStr :: String -> String
58 quoteStr str = foldr (++) "" (["\""] ++ map quote str ++ ["\""])
60 quote :: Char -> String