@@ -1,25 +1,62 @@
11{-# LANGUAGE BangPatterns, ScopedTypeVariables #-}
22
3−module Data.Text.Search where
3+-- |
4+-- Module : Data.Text.Search
5+-- Copyright : (c) Bryan O'Sullivan 2009
6+--
7+-- License : BSD-style
8+-- Maintainer : bos@serpentine.com, rtharper@aftereternity.co.uk,
9+-- duncan@haskell.org
10+-- Stability : experimental
11+-- Portability : GHC
12+--
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)
29+module Data.Text.Search
30+ (
31+ indices
32+ , slowIndices
33+ ) where
434
5−import qualified Data.Text as T ()
35+import qualified Data.Text as T
636import qualified Data.Text.Array as A
737import Data.Word (Word64)
838import Data.Text.Internal (Text(..))
939import Data.Bits ((.|.), (.&.))
1040import Data.Text.UnsafeShift (shiftL)
41+import Data.Text.Unsafe (iter_)
1142
12−indices :: Text -> Text -> [Int]
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)/.
46+indices :: Text -- ^ Substring to search for (@needle@)
47+ -> Text -- ^ Text to search in (@haystack@)
48+ -> [Int]
1349indices _needle@(Text narr noff nlen) _haystack@(Text harr hoff hlen)
14− | ldiff < 0 = []
15− | otherwise = outer 0
50+ | nlen == 1 = scanOne (nindex 0)
51+ | nlen <= 0 || ldiff < 0 = []
52+ | otherwise = scan 0
1653 where
17− ldiff = hlen - nlen
18− nlast = nlen - 1
19− z = nindex nlast
54+ ldiff = hlen - nlen
55+ nlast = nlen - 1
56+ z = nindex nlast
2057 nindex k = A.unsafeIndex narr (noff+k)
2158 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)
2360 buildTable !i !msk !skp
2461 | i >= nlast = (msk .|. swizzle z, skp)
2562 | otherwise = buildTable (i+1) (msk .|. swizzle c) skp'
@@ -27,18 +64,38 @@ indices _needle@(Text narr noff nlen) _haystack@(Text harr hoff hlen)
2764 skp' | c == z = nlen - i - 2
2865 | otherwise = skp
2966 swizzle k = 1 `shiftL` (fromEnum k .&. 0x3f)
30− outer !i
31− | i > ldiff = []
32− | c == z = if candidateMatch 0
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)
39− where c = hindex (i+nlast)
67+ scan !i
68+ | i > ldiff = []
69+ | c == z && candidateMatch 0 = i : scan (i + nlast)
70+ | otherwise = scan (i + delta)
71+ where c = hindex (i + nlast)
4072 candidateMatch j
4173 | j >= nlast = True
4274 | hindex (i+j) /= nindex j = False
4375 | otherwise = candidateMatch (j+1)
44− nextInPattern = (mask .&. swizzle (hindex (i+nlen))) == 0
76+ delta
77+ | nextInPattern = nlen + 1
78+ | c == z = skip + 1
79+ | otherwise = 1
80+ nextInPattern = (mask .&. swizzle (hindex (i+nlen))) == 0
81+ scanOne c = loop 0
82+ where loop !i | i >= hlen = []
83+ | hindex i == c = i : loop (i+1)
84+ | otherwise = loop (i+1)
85+{-# INLINE indices #-}
86+
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@)
92+ -> [Int]
93+slowIndices needle@(Text _narr _noff nlen) haystack@(Text harr hoff hlen)
94+ | T.null needle = []
95+ | otherwise = scan 0
96+ where
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