on master help sign in
Do not force the entire haystack before searching it.

--HG-- extra : convert_revision : 28f0f439b23997945fd8c73cd2932e6c6666c82c

commit51fc7e0f8f8c6fe6dcb701646d444a128a8077c6 authorBryan O'Sullivan <bos@serpentine.com> dateWed 7 Oct 2009, 04:56 (17 years ago) parent77d1877
2 files changed+28−16
Data/Text/Lazy/Internal.hs+1 Data/Text/Lazy/Search.hs+27−16
Data/Text/Lazy/Internal.hs+1
@@ -91,6 +91,7 @@ foldlChunks f z = go z
9191defaultChunkSize :: Int
9292defaultChunkSize = 32 * k - chunkOverhead
9393 where k = 1024 `div` sizeOf (undefined :: Word16)
94+{-# INLINE defaultChunkSize #-}
9495
9596-- | Currently set to 4k, less the memory management overhead.
9697smallChunkSize :: Int
Data/Text/Lazy/Search.hs+27−16
@@ -13,8 +13,6 @@
1313-- Fast substring search for lazy 'Text', based on work by Boyer,
1414-- Moore, Horspool, Sunday, and Lundh. Adapted from the strict
1515-- implementation.
16−--
17−-- /Note/: this is currently too strict!
1816
1917module Data.Text.Lazy.Search
2018 (
@@ -34,23 +32,26 @@ import Data.Text.UnsafeShift (shiftL)
3432-- | /O(n+m)/ Find the offsets of all non-overlapping indices of
3533-- @needle@ within @haystack@.
3634--
35+-- This function is strict in @needle@, and lazy (as far as possible)
36+-- in the chunks of @haystack@.
37+--
3738-- In (unlikely) bad cases, this algorithm's complexity degrades
3839-- towards /O(n*m)/.
3940indices :: Text -- ^ Substring to search for (@needle@)
4041 -> Text -- ^ Text to search in (@haystack@)
4142 -> [Int64]
42−indices needle@(Chunk n ns) haystack@(Chunk k ks)
43− | nlen <= 0 || ldiff < 0 = []
44− | nlen == 1 = scanOne (nindex 0) 0 k ks
45− | otherwise = scan 0 0 k ks
43+indices needle@(Chunk n ns) _haystack@(Chunk k ks)
44+ | nlen <= 0 = []
45+ | nlen == 1 = scanOne (nindex 0) 0 k ks
46+ | otherwise = scan 0 0 k ks
4647 where
4748 scan !g !i x@(T.Text _ _ l) xs
48− | g > ldiff = []
49− | i >= m = case xs of
50− Empty -> []
51− Chunk y ys -> scan g (i-m) y ys
52− | c == z && candidateMatch 0 = g : scan (g+nlen) (i+nlen) x xs
53− | otherwise = scan (g+delta) (i+delta) x xs
49+ | i >= m = case xs of
50+ Empty -> []
51+ Chunk y ys -> scan g (i-m) y ys
52+ | lackingHay (i + nlen) x xs = []
53+ | c == z && candidateMatch 0 = g : scan (g+nlen) (i+nlen) x xs
54+ | otherwise = scan (g+delta) (i+delta) x xs
5455 where
5556 m = fromIntegral l
5657 c = hindex (i + nlast)
@@ -62,8 +63,7 @@ indices needle@(Chunk n ns) haystack@(Chunk k ks)
6263 | j >= nlast = True
6364 | hindex (i+j) /= nindex j = False
6465 | otherwise = candidateMatch (j+1)
65− hindex = index x xs
66− ldiff = wordLength haystack - nlen
66+ hindex = index x xs
6767 nlen = wordLength needle
6868 nlast = nlen - 1
6969 nindex = index n ns
@@ -90,6 +90,14 @@ indices needle@(Chunk n ns) haystack@(Chunk k ks)
9090 | on == c = i + fromIntegral h : go (h+1)
9191 | otherwise = go (h+1)
9292 where on = A.unsafeIndex oarr (ooff+h)
93+ -- | Check whether an attempt to index into the haystack at the
94+ -- given offset will fail.
95+ lackingHay q = go 0
96+ where
97+ go p (T.Text _ _ l) ps = p' < q && case ps of
98+ Empty -> True
99+ Chunk r rs -> go p' r rs
100+ where p' = p + fromIntegral l
93101indices _ _ = []
94102
95103-- | Fast index into a partly unpacked 'Text'. We take into account
@@ -99,8 +107,11 @@ index :: T.Text -> Text -> Int64 -> Word16
99107index (T.Text arr off len) xs i
100108 | j < len = A.unsafeIndex arr (off+j)
101109 | otherwise = case xs of
102− Empty | j == len -> 0 -- out of bounds, but legal
103− | otherwise -> emptyError "index"
110+ Empty
111+ -- out of bounds, but legal
112+ | j == len -> 0
113+ -- should never happen, due to lackingHay above
114+ | otherwise -> emptyError "index"
104115 Chunk c cs -> index c cs (i-fromIntegral len)
105116 where j = fromIntegral i
106117