@@ -1,25 +1,62 @@
1{-# LANGUAGE BangPatterns, ScopedTypeVariables #-}
1{-# LANGUAGE BangPatterns, ScopedTypeVariables #-}
4+-- Module : Data.Text.Search
5+-- Copyright : (c) Bryan O'Sullivan 2009
8+-- Maintainer : bos@serpentine.com, rtharper@aftereternity.co.uk,
10+-- Stability : experimental
13+-- Fast substring search for 'Text', based on work by Boyer, Moore,
14+-- Horspool, Sunday, and Lundh.
18+-- * R. S. Boyer, J. S. Moore: A Fast String Searching Algorithm.
19+-- Communications of the ACM, 20, 10, 762-772 (1977)
21+-- * R. N. Horspool: Practical Fast Searching in Strings. Software -
22+-- Practice and Experience 10, 501-506 (1980)
24+-- * D. M. Sunday: A Very Fast Substring Search Algorithm.
25+-- Communications of the ACM, 33, 8, 132-142 (1990)
27+-- * F. Lundh: The Fast Search Algorithm.
28+-- <http://effbot.org/zone/stringlib.htm> (2006)
3−module Data.Text.Search where
29+module Data.Text.Search
5−import qualified Data.Text as T ()
35+import qualified Data.Text as T
6import qualified Data.Text.Array as A
36import qualified Data.Text.Array as A
7import Data.Word (Word64)
37import Data.Word (Word64)
8import Data.Text.Internal (Text(..))
38import Data.Text.Internal (Text(..))
9import Data.Bits ((.|.), (.&.))
39import Data.Bits ((.|.), (.&.))
10import Data.Text.UnsafeShift (shiftL)
40import Data.Text.UnsafeShift (shiftL)
41+import Data.Text.Unsafe (iter_)
43+-- | /O(n+m)/ Find the offsets of all non-overlapping indices of
44+-- @needle@ within @haystack@. In (unlikely) bad cases, this
45+-- algorithm's complexity degenerates towards /O(n*m)/.
12−indices :: Text -> Text -> [Int]
46+indices :: Text -- ^ Substring to search for (@needle@)
47+ -> Text -- ^ Text to search in (@haystack@)
13indices _needle@(Text narr noff nlen) _haystack@(Text harr hoff hlen)
49indices _needle@(Text narr noff nlen) _haystack@(Text harr hoff hlen)
50+ | nlen == 1 = scanOne (nindex 0)
14− | ldiff < 0 = []
51+ | nlen <= 0 || ldiff < 0 = []
15− | otherwise = outer 0
52+ | otherwise = scan 0
17− ldiff = hlen - nlen
54+ ldiff = hlen - nlen
18− nlast = nlen - 1
55+ nlast = nlen - 1
19− z = nindex nlast
56+ z = nindex nlast
20 nindex k = A.unsafeIndex narr (noff+k)
57 nindex k = A.unsafeIndex narr (noff+k)
21 hindex k = A.unsafeIndex harr (hoff+k)
58 hindex k = A.unsafeIndex harr (hoff+k)
22− (mask :: Word64, skip :: Int) = buildTable 0 0 (nlen-2)
59+ (mask :: Word64, skip) = buildTable 0 0 (nlen-2)
23 buildTable !i !msk !skp
60 buildTable !i !msk !skp
24 | i >= nlast = (msk .|. swizzle z, skp)
61 | i >= nlast = (msk .|. swizzle z, skp)
25 | otherwise = buildTable (i+1) (msk .|. swizzle c) skp'
62 | otherwise = buildTable (i+1) (msk .|. swizzle c) skp'
@@ -27,18 +64,38 @@ indices _needle@(Text narr noff nlen) _haystack@(Text harr hoff hlen)
27 skp' | c == z = nlen - i - 2
64 skp' | c == z = nlen - i - 2
28 | otherwise = skp
65 | otherwise = skp
29 swizzle k = 1 `shiftL` (fromEnum k .&. 0x3f)
66 swizzle k = 1 `shiftL` (fromEnum k .&. 0x3f)
31− | i > ldiff = []
68+ | i > ldiff = []
32− | c == z = if candidateMatch 0
69+ | c == z && candidateMatch 0 = i : scan (i + nlast)
33− then i : outer (i + nlast)
34− else if nextInPattern
35− then outer (i + nlen)
36− else outer (i + skip)
37− | nextInPattern = outer (i + nlen)
38− | otherwise = outer (i + 1)
70+ | otherwise = scan (i + delta)
39− where c = hindex (i+nlast)
71+ where c = hindex (i + nlast)
40 candidateMatch j
72 candidateMatch j
41 | j >= nlast = True
73 | j >= nlast = True
42 | hindex (i+j) /= nindex j = False
74 | hindex (i+j) /= nindex j = False
43 | otherwise = candidateMatch (j+1)
75 | otherwise = candidateMatch (j+1)
77+ | nextInPattern = nlen + 1
44− nextInPattern = (mask .&. swizzle (hindex (i+nlen))) == 0
80+ nextInPattern = (mask .&. swizzle (hindex (i+nlen))) == 0
82+ where loop !i | i >= hlen = []
83+ | hindex i == c = i : loop (i+1)
84+ | otherwise = loop (i+1)
85+{-# INLINE indices #-}
87+-- | /O(n*m)/ Find the offsets of all non-overlapping indices of
88+-- @needle@ within @haystack@. Provided purely as a slower, reliable
89+-- counterpart to 'indices'.
90+slowIndices :: Text -- ^ Substring to search for (@needle@)
91+ -> Text -- ^ Text to search in (@haystack@)
93+slowIndices needle@(Text _narr _noff nlen) haystack@(Text harr hoff hlen)
97+ scan i | i >= hlen = []
98+ | needle `T.isPrefixOf` t = i : scan (i+nlen)
99+ | otherwise = scan (i+d)
100+ where t = Text harr (hoff+i) (hlen-i)
101+ d = iter_ haystack i