summaryrefslogtreecommitdiff
path: root/src/String.hs
blob: 633c92000c2208f65a999d4da794027d7005a525 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{-# LANGUAGE NoImplicitPrelude #-}

module String where

import           Rebase.Prelude hiding (bool, hash, join, o, (<.>))

split :: Eq a => [a] -> [a] -> [[a]]
split _ [] = []
split delim str =
    let (firstline, remainder) = breakList (startswith delim) str
        in
        firstline : case remainder of
                                   [] -> []
                                   x -> if x == delim
                                        then [] : []
                                        else split delim
                                                 (drop (length delim) x)

spanList :: ([a] -> Bool) -> [a] -> ([a], [a])
spanList _ [] = ([],[])
spanList func list@(x:xs) =
    if func list
       then (x:ys,zs)
       else ([],list)
    where (ys,zs) = spanList func xs

breakList :: ([a] -> Bool) -> [a] -> ([a], [a])
breakList func = spanList (not . func)

startswith :: Eq a => [a] -> [a] -> Bool
startswith = isPrefixOf

join :: [a] -> [[a]] -> [a]
join delim l = concat (intersperse delim l)

replace :: Eq a => [a] -> [a] -> [a] -> [a]
replace old new l = join new . split old $ l

wordsBy :: Char -> [Char] -> [String]
wordsBy c s = words (rep <$> s)
  where
    rep x | x == c    = ' '
          | otherwise = x