Commits

Bryan O'Sullivan committed 1179a89

Fix up splitWith.

  • Participants
  • Parent commits 2f4bcde

Comments (0)

Files changed (2)

File Data/Text.hs

 -- > splitWith (=='a') []        == []
 splitWith :: (Char -> Bool) -> Text -> [Text]
 splitWith p = loop
-  where loop t@(Text arr off len)
-            | null t    = [empty]
-            | otherwise = text arr off n : rest
-            where n = findAIndexOrEnd p t
-                  m = n + iter_ t n
-                  rest | n >= len   = []
-                       | otherwise = loop (text arr (off+m) (len-m))
+    where loop s | null s    = []
+                 | otherwise = if null s'
+                               then [s]
+                               else l : loop (unsafeTail s')
+              where (l, s') = break p s
 {-# INLINE splitWith #-}
 
 -- ----------------------------------------------------------------------------

File tests/Properties.hs

 {-
 prop_splitWith p     = splitWith p `eqP` (map unpack . T.splitWith p)
 
-splitWith _ [] = [""]
-splitWith p xs = h : splitWith p (whale t)
-    where (h,t) = break p xs
-          whale (_:xs) = xs
-          whale xs = xs
--}
+splitWith _ "" =  []
+splitWith p s  = if null s'
+                 then [s]
+                 else l : splitWith p (tail s')
+    where (l, s') = break p s
 
 prop_breakSubstring_isInfixOf s l
                      = T.isInfixOf s l ==
   ("prop_tails", mytest prop_tails),
 
   ("prop_split_i", mytest prop_split_i),
---("prop_splitWith", mytest prop_splitWith),
+  ("prop_splitWith", mytest prop_splitWith),
   ("prop_breakSubstringC", mytest prop_breakSubstringC),
   ("prop_breakSubstring_isInfixOf", mytest prop_breakSubstring_isInfixOf),