]> gitweb @ CieloNegro.org - Lucu.git/blob - Network/HTTP/Lucu/Utils.hs
Many improvements
[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     )
8     where
9
10 import Control.Monad.Trans
11 import Data.Char
12 import Data.List
13 import Foreign
14 import Foreign.C
15
16
17 splitBy :: (a -> Bool) -> [a] -> [[a]]
18 splitBy isSeparator src
19     = case break isSeparator src
20       of (last , []      ) -> last  : []
21          (first, sep:rest) -> first : splitBy isSeparator rest
22
23
24 joinWith :: [a] -> [[a]] -> [a]
25 joinWith separator xs
26     = foldr (++) [] $ intersperse separator xs
27
28
29 trim :: (a -> Bool) -> [a] -> [a]
30 trim p = trimTail . trimHead
31     where
32       trimHead = dropWhile p
33       trimTail = reverse . trimHead . reverse
34
35
36 noCaseEq :: String -> String -> Bool
37 noCaseEq a b
38     = (map toLower a) == (map toLower b)
39
40
41 isWhiteSpace :: Char -> Bool
42 isWhiteSpace = flip elem " \t\r\n"