aboutsummaryrefslogtreecommitdiff
path: root/Utility/Misc.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2012-10-15 22:22:40 -0400
committerGravatar Joey Hess <joey@kitenet.net>2012-10-15 22:22:40 -0400
commitacd55616a7128d42ca191e2978919dfac6e2d1ba (patch)
tree6d2a6c29f275b000680d108d7b8788cdce03b9d8 /Utility/Misc.hs
parent72813114685613f72bc2d7b5d54596c14a5cf0f2 (diff)
Bug fix: A recent change caused git-annex-shell to crash.
Diffstat (limited to 'Utility/Misc.hs')
-rw-r--r--Utility/Misc.hs18
1 files changed, 16 insertions, 2 deletions
diff --git a/Utility/Misc.hs b/Utility/Misc.hs
index 88d210de6..f03504040 100644
--- a/Utility/Misc.hs
+++ b/Utility/Misc.hs
@@ -40,9 +40,23 @@ firstLine :: String -> String
firstLine = takeWhile (/= '\n')
{- Splits a list into segments that are delimited by items matching
- - a predicate. (The delimiters are not included in the segments.) -}
+ - a predicate. (The delimiters are not included in the segments.)
+ - Segments may be empty. -}
segment :: (a -> Bool) -> [a] -> [[a]]
-segment p = filter (not . all p) . segmentDelim p
+segment p l = map reverse $ go [] [] l
+ where
+ go c r [] = reverse $ c:r
+ go c r (i:is)
+ | p i = go [] (c:r) is
+ | otherwise = go (i:c) r is
+
+prop_segment_regressionTest :: Bool
+prop_segment_regressionTest = all id
+ -- Even an empty list is a segment.
+ [ segment (== "--") [] == [[]]
+ -- There are two segements in this list, even though the first is empty.
+ , segment (== "--") ["--", "foo", "bar"] == [[],["foo","bar"]]
+ ]
{- Includes the delimiters as segments of their own. -}
segmentDelim :: (a -> Bool) -> [a] -> [[a]]