summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2019-10-04 13:48:36 -0400
committerAndrew Cady <d@jerkface.net>2019-10-04 13:51:57 -0400
commitcd7dc6266b415cf3c937fc3b3aa14fce999c6f0d (patch)
treefe89beb18d89d29cee49f2e04326fc9152638bbf /src
parenta9aab3eddf957e1ac4ef29a1dc6374567827c343 (diff)
factor out string functions
most of these were copied from MissingH
Diffstat (limited to 'src')
-rw-r--r--src/String.hs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/String.hs b/src/String.hs
new file mode 100644
index 0000000..633c920
--- /dev/null
+++ b/src/String.hs
@@ -0,0 +1,44 @@
1{-# LANGUAGE NoImplicitPrelude #-}
2
3module String where
4
5import Rebase.Prelude hiding (bool, hash, join, o, (<.>))
6
7split :: Eq a => [a] -> [a] -> [[a]]
8split _ [] = []
9split delim str =
10 let (firstline, remainder) = breakList (startswith delim) str
11 in
12 firstline : case remainder of
13 [] -> []
14 x -> if x == delim
15 then [] : []
16 else split delim
17 (drop (length delim) x)
18
19spanList :: ([a] -> Bool) -> [a] -> ([a], [a])
20spanList _ [] = ([],[])
21spanList func list@(x:xs) =
22 if func list
23 then (x:ys,zs)
24 else ([],list)
25 where (ys,zs) = spanList func xs
26
27breakList :: ([a] -> Bool) -> [a] -> ([a], [a])
28breakList func = spanList (not . func)
29
30startswith :: Eq a => [a] -> [a] -> Bool
31startswith = isPrefixOf
32
33join :: [a] -> [[a]] -> [a]
34join delim l = concat (intersperse delim l)
35
36replace :: Eq a => [a] -> [a] -> [a] -> [a]
37replace old new l = join new . split old $ l
38
39wordsBy :: Char -> [Char] -> [String]
40wordsBy c s = words (rep <$> s)
41 where
42 rep x | x == c = ' '
43 | otherwise = x
44