Bryan O'Sullivan avatar Bryan O'Sullivan committed dd34e84

Move the definition of buildTable inline, to save repeated work.

Comments (0)

Files changed (1)

Data/Text/Lazy/Search.hs

     nindex    = index n ns
     z         = foldlChunks fin 0 needle
         where fin _ (T.Text farr foff flen) = A.unsafeIndex farr (foff+flen-1)
-    mask :*: skip = buildTable needle
+    (mask :: Word64) :*: skip = buildTable n ns 0 0 0 (nlen-2)
+    swizzle w = 1 `shiftL` (fromIntegral w .&. 0x3f)
+    buildTable (T.Text xarr xoff xlen) xs = go
+      where
+        go !(g::Int64) !i !msk !skp
+            | i >= xlast = case xs of
+                             Empty      -> (msk .|. swizzle z) :*: skp
+                             Chunk y ys -> buildTable y ys g 0 msk skp
+            | otherwise = go (g+1) (i+1) (msk .|. swizzle c) skp'
+            where c                = A.unsafeIndex xarr (xoff+i)
+                  skp' | c == z    = nlen - fromIntegral g - 2
+                       | otherwise = skp
+                  xlast = xlen - 1
     scanOne c i (T.Text oarr ooff olen) os = go 0
       where
         go h | h >= olen = case os of
     | j < len   = A.unsafeIndex arr (off+j)
     | otherwise = case xs of
                     Empty | j == len  -> 0
-                          | otherwise -> error "empty"
+                          | otherwise -> emptyError "index"
                     Chunk c cs -> index c cs (i-fromIntegral len)
     where j = fromIntegral i
 
-swizzle :: Word16 -> Word64
-swizzle k = 1 `shiftL` (fromIntegral k .&. 0x3f)
-
 wordLength :: Text -> Int64
 wordLength = foldlChunks sumLength 0
     where sumLength i (T.Text _ _ l) = i + fromIntegral l
 
-buildTable :: Text -> PairS Word64 Int64
-buildTable Empty = 0 :*: 0
-buildTable needle@(Chunk k ks) = outer k ks 0 0 0 (nlen-2)
-  where
-    outer (T.Text xarr xoff xlen) xs = go
-      where
-        go !(g::Int64) !i !mask !skip
-            | i >= xlast = case xs of
-                             Empty      -> (mask .|. swizzle z) :*: skip
-                             Chunk y ys -> outer y ys g 0 mask skip
-            | otherwise = go (g+1) (i+1) (mask .|. swizzle c) skip'
-            where c                 = A.unsafeIndex xarr (xoff+i)
-                  skip' | c == z    = nlen - fromIntegral g - 2
-                        | otherwise = skip
-                  xlast = xlen - 1
-    nlen      = wordLength needle
-    z         = foldlChunks fin 0 needle
-        where fin _ (T.Text farr foff flen) = A.unsafeIndex farr (foff+flen-1)
+emptyError :: String -> a
+emptyError fun = error ("Data.Text.Lazy.Search." ++ fun ++ ": empty input")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.