{-# 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