on master help sign in
Refactor lazy search code to make it easier to read
e33c73cBryan O'Sullivan
Data/Text/Lazy/Search.hs+14−9
@@ -41,7 +41,7 @@ indices :: Text -- ^ Substring to search for (@needle@)
41 -> [Int64]
41 -> [Int64]
42indices needle@(Chunk n ns) _haystack@(Chunk k ks)
42indices needle@(Chunk n ns) _haystack@(Chunk k ks)
43 | nlen <= 0 = []
43 | nlen <= 0 = []
44− | nlen == 1 = scanOne (nindex 0) 0 k ks
44+ | nlen == 1 = indicesOne (nindex 0) 0 k ks
45 | otherwise = scan 0 0 k ks
45 | otherwise = scan 0 0 k ks
46 where
46 where
47 scan !g !i x@(T.Text _ _ l) xs
47 scan !g !i x@(T.Text _ _ l) xs
@@ -81,14 +81,6 @@ indices needle@(Chunk n ns) _haystack@(Chunk k ks)
81 skp' | c == z = nlen - fromIntegral g - 2
81 skp' | c == z = nlen - fromIntegral g - 2
82 | otherwise = skp
82 | otherwise = skp
83 xlast = xlen - 1
83 xlast = xlen - 1
84− scanOne c i (T.Text oarr ooff olen) os = go 0
85− where
86− go h | h >= olen = case os of
87− Empty -> []
88− Chunk y ys -> scanOne c (i+fromIntegral olen) y ys
89− | on == c = i + fromIntegral h : go (h+1)
90− | otherwise = go (h+1)
91− where on = A.unsafeIndex oarr (ooff+h)
92 -- | Check whether an attempt to index into the haystack at the
84 -- | Check whether an attempt to index into the haystack at the
93 -- given offset would fail.
85 -- given offset would fail.
94 lackingHay q = go 0
86 lackingHay q = go 0
@@ -114,6 +106,19 @@ index (T.Text arr off len) xs !i
114 Chunk c cs -> index c cs (i-fromIntegral len)
106 Chunk c cs -> index c cs (i-fromIntegral len)
115 where j = fromIntegral i
107 where j = fromIntegral i
116
108
109+-- | A variant of 'indices' that scans linearly for a single 'Word16'.
110+indicesOne :: Word16 -> Int64 -> T.Text -> Text -> [Int64]
111+indicesOne c = chunk
112+ where
113+ chunk !i (T.Text oarr ooff olen) os = go 0
114+ where
115+ go h | h >= olen = case os of
116+ Empty -> []
117+ Chunk y ys -> chunk (i+fromIntegral olen) y ys
118+ | on == c = i + fromIntegral h : go (h+1)
119+ | otherwise = go (h+1)
120+ where on = A.unsafeIndex oarr (ooff+h)
121+
117-- | The number of 'Word16' values in a 'Text'.
122-- | The number of 'Word16' values in a 'Text'.
118wordLength :: Text -> Int64
123wordLength :: Text -> Int64
119wordLength = foldlChunks sumLength 0
124wordLength = foldlChunks sumLength 0