]> gitweb @ CieloNegro.org - Lucu.git/blob - Network/HTTP/Lucu/Utils.hs
data/mime.types
[Lucu.git] / Network / HTTP / Lucu / Utils.hs
1 module Network.HTTP.Lucu.Utils
2     ( splitBy      -- (a -> Bool) -> [a] -> [[a]]
3     , joinWith     -- [a] -> [[a]] -> [a]
4     , trim         -- (a -> Bool) -> [a] -> [a]
5     , noCaseEq     -- String -> String -> Bool
6     , isWhiteSpace -- Char -> Bool
7     , quoteStr     -- String -> String
8     )
9     where
10
11 import Control.Monad.Trans
12 import Data.Char
13 import Data.List
14 import Foreign
15 import Foreign.C
16
17
18 splitBy :: (a -> Bool) -> [a] -> [[a]]
19 splitBy isSeparator src
20     = case break isSeparator src
21       of (last , []      ) -> last  : []
22          (first, sep:rest) -> first : splitBy isSeparator rest
23
24
25 joinWith :: [a] -> [[a]] -> [a]
26 joinWith separator xs
27     = foldr (++) [] $ intersperse separator xs
28
29
30 trim :: (a -> Bool) -> [a] -> [a]
31 trim p = trimTail . trimHead
32     where
33       trimHead = dropWhile p
34       trimTail = reverse . trimHead . reverse
35
36
37 noCaseEq :: String -> String -> Bool
38 noCaseEq a b
39     = (map toLower a) == (map toLower b)
40
41
42 isWhiteSpace :: Char -> Bool
43 isWhiteSpace = flip elem " \t\r\n"
44
45
46 quoteStr :: String -> String
47 quoteStr str = foldr (++) "" (["\""] ++ map quote str ++ ["\""])
48     where
49       quote :: Char -> String
50       quote '"' = "\\\""
51       quote c   = [c]