]> gitweb @ CieloNegro.org - Lucu.git/blob - Network/HTTP/Lucu/Utils.hs
staticDir
[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 import Network.URI
17
18
19 splitBy :: (a -> Bool) -> [a] -> [[a]]
20 splitBy isSeparator src
21     = case break isSeparator src
22       of (last , []      ) -> last  : []
23          (first, sep:rest) -> first : splitBy isSeparator rest
24
25
26 joinWith :: [a] -> [[a]] -> [a]
27 joinWith separator xs
28     = foldr (++) [] $ intersperse separator xs
29
30
31 trim :: (a -> Bool) -> [a] -> [a]
32 trim p = trimTail . trimHead
33     where
34       trimHead = dropWhile p
35       trimTail = reverse . trimHead . reverse
36
37
38 noCaseEq :: String -> String -> Bool
39 noCaseEq a b
40     = (map toLower a) == (map toLower b)
41
42
43 isWhiteSpace :: Char -> Bool
44 isWhiteSpace = flip elem " \t\r\n"
45
46
47 quoteStr :: String -> String
48 quoteStr str = foldr (++) "" (["\""] ++ map quote str ++ ["\""])
49     where
50       quote :: Char -> String
51       quote '"' = "\\\""
52       quote c   = [c]