diff options
Diffstat (limited to 'src/String.hs')
-rw-r--r-- | src/String.hs | 44 |
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 | |||
3 | module String where | ||
4 | |||
5 | import Rebase.Prelude hiding (bool, hash, join, o, (<.>)) | ||
6 | |||
7 | split :: Eq a => [a] -> [a] -> [[a]] | ||
8 | split _ [] = [] | ||
9 | split 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 | |||
19 | spanList :: ([a] -> Bool) -> [a] -> ([a], [a]) | ||
20 | spanList _ [] = ([],[]) | ||
21 | spanList func list@(x:xs) = | ||
22 | if func list | ||
23 | then (x:ys,zs) | ||
24 | else ([],list) | ||
25 | where (ys,zs) = spanList func xs | ||
26 | |||
27 | breakList :: ([a] -> Bool) -> [a] -> ([a], [a]) | ||
28 | breakList func = spanList (not . func) | ||
29 | |||
30 | startswith :: Eq a => [a] -> [a] -> Bool | ||
31 | startswith = isPrefixOf | ||
32 | |||
33 | join :: [a] -> [[a]] -> [a] | ||
34 | join delim l = concat (intersperse delim l) | ||
35 | |||
36 | replace :: Eq a => [a] -> [a] -> [a] -> [a] | ||
37 | replace old new l = join new . split old $ l | ||
38 | |||
39 | wordsBy :: Char -> [Char] -> [String] | ||
40 | wordsBy c s = words (rep <$> s) | ||
41 | where | ||
42 | rep x | x == c = ' ' | ||
43 | | otherwise = x | ||
44 | |||