From 748d5d502fc71f6db1ca437ede6ea49ba304555f Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Thu, 16 Dec 2010 12:33:24 +0100 Subject: [PATCH] Generalise the sepSplit function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently it works on splitting strings by individual chars, but we can generalise it to split lists by list elements, which means we can reuse it later in the Text module for splitting both lists of chars by '|' or lists of lines by empty newlines. The change also makes the code cleaner (uses βnull xsβ instead of string-specific βxs == ""β). Note: I tried to rewrite this in a nicer, functional style using unfolds, but I failed to account for the final terminator case (e.g. ab|cd|) resulting in a valid but empty element. Signed-off-by: Iustin Pop <iustin@google.com> Reviewed-by: Balazs Lecz <leczb@google.com> --- Ganeti/HTools/Utils.hs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Ganeti/HTools/Utils.hs b/Ganeti/HTools/Utils.hs index 7ae236777..84e8152ee 100644 --- a/Ganeti/HTools/Utils.hs +++ b/Ganeti/HTools/Utils.hs @@ -73,13 +73,13 @@ debugXy a b = debug a `seq` b commaJoin :: [String] -> String commaJoin = intercalate "," --- | Split a string on a separator and return an array. -sepSplit :: Char -> String -> [String] +-- | Split a list on a separator and return an array. +sepSplit :: Eq a => a -> [a] -> [[a]] sepSplit sep s - | x == "" && xs == [] = [] - | xs == [] = [x] - | ys == [] = [x,""] - | otherwise = x:sepSplit sep ys + | null s = [] + | null xs = [x] + | null ys = [x,[]] + | otherwise = x:sepSplit sep ys where (x, xs) = break (== sep) s ys = drop 1 xs -- GitLab