on master help sign in
Search performance and correctness improvements.
b58347bBryan O'Sullivan
Data/Text/Search.hs+13−14
@@ -47,9 +47,9 @@ indices :: Text -- ^ Substring to search for (@needle@)
47 -> Text -- ^ Text to search in (@haystack@)
47 -> Text -- ^ Text to search in (@haystack@)
48 -> [Int]
48 -> [Int]
49indices _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)
50+ | nlen == 1 = scanOne (nindex 0)
51− | nlen <= 0 || ldiff < 0 = []
51+ | nlen <= 0 || ldiff < 0 = []
52− | otherwise = scan 0
52+ | otherwise = scan 0
53 where
53 where
54 ldiff = hlen - nlen
54 ldiff = hlen - nlen
55 nlast = nlen - 1
55 nlast = nlen - 1
@@ -63,21 +63,20 @@ indices _needle@(Text narr noff nlen) _haystack@(Text harr hoff hlen)
63 where c = nindex i
63 where c = nindex i
64 skp' | c == z = nlen - i - 2
64 skp' | c == z = nlen - i - 2
65 | otherwise = skp
65 | otherwise = skp
66− swizzle k = 1 `shiftL` (fromEnum k .&. 0x3f)
66+ swizzle k = 1 `shiftL` (fromIntegral k .&. 0x3f)
67 scan !i
67 scan !i
68 | i > ldiff = []
68 | i > ldiff = []
69 | c == z && candidateMatch 0 = i : scan (i + nlast)
69 | c == z && candidateMatch 0 = i : scan (i + nlast)
70 | otherwise = scan (i + delta)
70 | otherwise = scan (i + delta)
71− where c = hindex (i + nlast)
71+ where c = hindex (i + nlast)
72− candidateMatch j
72+ candidateMatch !j
73− | j >= nlast = True
73+ | j >= nlast = True
74− | hindex (i+j) /= nindex j = False
74+ | hindex (i+j) /= nindex j = False
75− | otherwise = candidateMatch (j+1)
75+ | otherwise = candidateMatch (j+1)
76− delta
77− | nextInPattern = nlen + 1
76+ delta | nextInPattern = nlen + 1
78− | c == z = skip + 1
77+ | c == z = skip + 1
79− | otherwise = 1
78+ | otherwise = 1
80− nextInPattern = (mask .&. swizzle (hindex (i+nlen))) == 0
79+ nextInPattern = mask .&. swizzle (hindex (i+nlen)) == 0
81 scanOne c = loop 0
80 scanOne c = loop 0
82 where loop !i | i >= hlen = []
81 where loop !i | i >= hlen = []
83 | hindex i == c = i : loop (i+1)
82 | hindex i == c = i : loop (i+1)