@@ -10,22 +10,9 @@
1010-- Stability : experimental
1111-- Portability : GHC
1212--
13−-- Fast substring search for 'Text', based on work by Boyer, Moore,
14−-- Horspool, Sunday, and Lundh.
15−--
16−-- References:
17−--
18−-- * R. S. Boyer, J. S. Moore: A Fast String Searching Algorithm.
19−-- Communications of the ACM, 20, 10, 762-772 (1977)
20−--
21−-- * R. N. Horspool: Practical Fast Searching in Strings. Software -
22−-- Practice and Experience 10, 501-506 (1980)
23−--
24−-- * D. M. Sunday: A Very Fast Substring Search Algorithm.
25−-- Communications of the ACM, 33, 8, 132-142 (1990)
26−--
27−-- * F. Lundh: The Fast Search Algorithm.
28−-- <http://effbot.org/zone/stringlib.htm> (2006)
13+-- Fast substring search for lazy 'Text', based on work by Boyer,
14+-- Moore, Horspool, Sunday, and Lundh. Adapted from the strict
15+-- implementation.
2916
3017module Data.Text.Lazy.Search
3118 (
@@ -74,8 +61,7 @@ indices needle@(Chunk n ns) haystack@(Chunk k ks)
7461 | hindex (i+j) /= nindex j = False
7562 | otherwise = candidateMatch (j+1)
7663 hindex = index x xs
77− ldiff = hlen - nlen
78− hlen = wordLength haystack
64+ ldiff = wordLength haystack - nlen
7965 nlen = wordLength needle
8066 nlast = nlen - 1
8167 nindex = index n ns
@@ -104,15 +90,19 @@ indices needle@(Chunk n ns) haystack@(Chunk k ks)
10490 where on = A.unsafeIndex oarr (ooff+h)
10591indices _ _ = []
10692
93+-- | Fast index into a partly unpacked 'Text'. We take into account
94+-- the possibility that the caller might try to access one element
95+-- past the end.
10796index :: T.Text -> Text -> Int64 -> Word16
10897index (T.Text arr off len) xs i
10998 | j < len = A.unsafeIndex arr (off+j)
11099 | otherwise = case xs of
111− Empty | j == len -> 0
100+ Empty | j == len -> 0 -- out of bounds, but legal
112101 | otherwise -> emptyError "index"
113102 Chunk c cs -> index c cs (i-fromIntegral len)
114103 where j = fromIntegral i
115104
105+-- | The number of 'Word16' values in a 'Text'.
116106wordLength :: Text -> Int64
117107wordLength = foldlChunks sumLength 0
118108 where sumLength i (T.Text _ _ l) = i + fromIntegral l