on master help sign in
Implement findAll.

--HG-- extra : convert_revision : 5aa8eece74fb791dfd45b906bc75adafd8b7b6de

commit709916e294c7f79829905e25de8a6294f2530273 authorBryan O'Sullivan <bos@serpentine.com> dateMon 17 Aug 2009, 01:47 (17 years ago) parent8f8701d
1 file changed+25−8
Data/Text.hs+25−8
Data/Text.hs+25−8
@@ -1052,21 +1052,38 @@ filter :: (Char -> Bool) -> Text -> Text
10521052filter p t = unstream (S.filter p (stream t))
10531053{-# INLINE filter #-}
10541054
1055+-- /O(n*m)/ Find all non-overlapping instances of @needle@ in
1056+-- @haystack@. The first element of the returned tuple is the prefix
1057+-- of @haystack@ before any matches of @needle@. The second element
1058+-- of the tuple is a list of non-overlapping substrings from
1059+-- @haystack@. Each begins with @needle@ and ends just before the
1060+-- following match. If @needle@ is not present in @haystack@, the
1061+-- list will be empty.
1062+--
1063+-- Examples:
1064+--
1065+-- > findAll "::" "a::b::c" ==> ("a", ["::b", "::c"])
1066+-- > findAll "/" "foobar" ==> ("foobar", [])
1067+--
1068+-- Laws:
1069+--
1070+-- > concat (prefix : matches) == haystack
1071+-- > where (prefix, matches) = findAll needle haystack
10551072findAll :: Text -> Text -> (Text, [Text])
10561073findAll pat@(Text _ _ plen) src@(Text sarr soff slen)
10571074 | plen <= 0 = emptyError "findAll"
10581075 | otherwise = (h,t)
10591076 where
1060− (h:t) = go 0 (search 0)
1061− go !k (x:xs) = Text sarr k (x-k) : go x xs
1062− go _ [] = []
1063− search !i
1077+ (h:t) = go 0 (search 0)
1078+ go k (x:xs) = Text sarr k (x-k) : go x xs
1079+ go _ [] = []
1080+ search i
10641081 | i >= slen = [slen] -- not found
1065− | pat `isPrefixOf` s = i : search (i+d+plen)
1082+ | pat `isPrefixOf` s = i : search (i+plen)
10661083 | otherwise = search (i+d)
1067− where s = Text sarr (soff+i) (slen-i)
1068− d = iter_ src i
1069−
1084+ where d = iter_ src i
1085+ s = Text sarr (soff+i) (slen-i)
1086+{-# INLINE findAll #-}
10701087
10711088-------------------------------------------------------------------------------
10721089-- ** Indexing 'Text's