Commits

Bryan O'Sullivan committed 1179a89

Fix up splitWith.

Comments (0)

Files changed (2)

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

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),
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.