on master help sign in
Fix up splitWith.

--HG-- extra : convert_revision : d4963f280a9ae0779cf0e4bc13fb00daf6166acd

commit39cfef60c5fff22f642e440e487f8bf34abd153a authorBryan O'Sullivan <bos@serpentine.com> dateMon 23 Feb 2009, 01:53 (18 years ago) parente99decf
2 files changed+11−14
Data/Text.hs+5−7 tests/Properties.hs+6−7
Data/Text.hs+5−7
@@ -755,13 +755,11 @@ split c = splitWith (==c)
755755-- > splitWith (=='a') [] == []
756756splitWith :: (Char -> Bool) -> Text -> [Text]
757757splitWith p = loop
758− where loop t@(Text arr off len)
759− | null t = [empty]
760− | otherwise = text arr off n : rest
761− where n = findAIndexOrEnd p t
762− m = n + iter_ t n
763− rest | n >= len = []
764− | otherwise = loop (text arr (off+m) (len-m))
758+ where loop s | null s = []
759+ | otherwise = if null s'
760+ then [s]
761+ else l : loop (unsafeTail s')
762+ where (l, s') = break p s
765763{-# INLINE splitWith #-}
766764
767765-- ----------------------------------------------------------------------------
tests/Properties.hs+6−7
@@ -138,12 +138,11 @@ prop_split_i c = id `eq` (T.intercalate (T.singleton c) . T.split c)
138138{-
139139prop_splitWith p = splitWith p `eqP` (map unpack . T.splitWith p)
140140
141−splitWith _ [] = [""]
142−splitWith p xs = h : splitWith p (whale t)
143− where (h,t) = break p xs
144− whale (_:xs) = xs
145− whale xs = xs
146−-}
141+splitWith _ "" = []
142+splitWith p s = if null s'
143+ then [s]
144+ else l : splitWith p (tail s')
145+ where (l, s') = break p s
147146
148147prop_breakSubstring_isInfixOf s l
149148 = T.isInfixOf s l ==
@@ -275,7 +274,7 @@ tests = [
275274 ("prop_tails", mytest prop_tails),
276275
277276 ("prop_split_i", mytest prop_split_i),
278−--("prop_splitWith", mytest prop_splitWith),
277+ ("prop_splitWith", mytest prop_splitWith),
279278 ("prop_breakSubstringC", mytest prop_breakSubstringC),
280279 ("prop_breakSubstring_isInfixOf", mytest prop_breakSubstring_isInfixOf),
281280