From cd7dc6266b415cf3c937fc3b3aa14fce999c6f0d Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Fri, 4 Oct 2019 13:48:36 -0400 Subject: factor out string functions most of these were copied from MissingH --- src/String.hs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/String.hs (limited to 'src') 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 @@ +{-# 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 + -- cgit v1.2.3