Commits

Bryan O'Sullivan committed 95b729d

Search performance and correctness improvements.

  • Participants
  • Parent commits f53096d

Comments (0)

Files changed (1)

File Data/Text/Search.hs

         -> Text                -- ^ Text to search in (@haystack@)
         -> [Int]
 indices _needle@(Text narr noff nlen) _haystack@(Text harr hoff hlen)
-  | nlen == 1              = scanOne (nindex 0)
-  | nlen <= 0 || ldiff < 0 = []
-  | otherwise              = scan 0
+    | nlen == 1              = scanOne (nindex 0)
+    | nlen <= 0 || ldiff < 0 = []
+    | otherwise              = scan 0
   where
     ldiff    = hlen - nlen
     nlast    = nlen - 1
         where c                = nindex i
               skp' | c == z    = nlen - i - 2
                    | otherwise = skp
-    swizzle k = 1 `shiftL` (fromEnum k .&. 0x3f)
+    swizzle k = 1 `shiftL` (fromIntegral k .&. 0x3f)
     scan !i
         | i > ldiff                  = []
         | c == z && candidateMatch 0 = i : scan (i + nlast)
         | otherwise                  = scan (i + delta)
-        where c                      = hindex (i + nlast)
-              candidateMatch j
-                  | j >= nlast               = True
-                  | hindex (i+j) /= nindex j = False
-                  | otherwise                = candidateMatch (j+1)
-              delta
-                  | nextInPattern = nlen + 1
-                  | c == z        = skip + 1
-                  | otherwise     = 1
-              nextInPattern       = (mask .&. swizzle (hindex (i+nlen))) == 0
+        where c = hindex (i + nlast)
+              candidateMatch !j
+                    | j >= nlast               = True
+                    | hindex (i+j) /= nindex j = False
+                    | otherwise                = candidateMatch (j+1)
+              delta | nextInPattern = nlen + 1
+                    | c == z        = skip + 1
+                    | otherwise     = 1
+              nextInPattern         = mask .&. swizzle (hindex (i+nlen)) == 0
     scanOne c = loop 0
         where loop !i | i >= hlen     = []
                       | hindex i == c = i : loop (i+1)