Commits

Bryan O'Sullivan  committed a1fd1ed

Implement findAll.

  • Participants
  • Parent commits 321e61f

Comments (0)

Files changed (1)

File Data/Text.hs

     -- * Searching
     , elem
     , filter
+    , findAll
     , find
     , partition
 
 filter p t = unstream (S.filter p (stream t))
 {-# INLINE filter #-}
 
+findAll :: Text -> Text -> (Text, [Text])
+findAll pat@(Text _ _ plen) src@(Text sarr soff slen)
+    | plen == 0 = emptyError "findAll"
+    | otherwise = (h,t)
+  where
+    (h:t) = go 0 (search 0)
+    go !k (x:xs) = Text sarr k (x-k) : go x xs
+    go _ []      = []
+    search !i
+      | i >= slen          = [slen]      -- not found
+      | pat `isPrefixOf` s = i : search (i+d+plen)
+      | otherwise          = search (i+d)
+            where s = Text sarr (soff+i) (slen-i)
+                  d = iter_ src i
+
 
 -------------------------------------------------------------------------------
 -- ** Indexing 'Text's