Skip to content

Commit 4142530

Browse files
LysxiaBodigrim
authored andcommitted
Use memchr in indices
1 parent 2ef973b commit 4142530

3 files changed

Lines changed: 10 additions & 10 deletions

File tree

src/Data/Text/Internal/ArrayUtils.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import System.Posix.Types (CSsize(..))
1414
import GHC.Exts (ByteArray#)
1515
import Data.Word (Word8)
1616

17+
-- | Find index of next occurrence of given byte,
18+
-- relative to the starting offset, or @-1@ if not found.
1719
memchr :: ByteArray# -> Int -> Int -> Word8 -> Int
1820
#if defined(PURE_HASKELL)
1921
memchr arr# off len w =

src/Data/Text/Internal/Lazy/Search.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,11 @@ indicesOne c = chunk
111111
where
112112
chunk :: Int64 -> Text -> [Int64]
113113
chunk !_ Empty = []
114-
chunk !i (Chunk (T.Text oarr ooff olen) os) = go 0
114+
chunk !i (Chunk (T.Text (A.ByteArray oarr#) ooff olen) os) = go 0
115115
where
116-
go h | h >= olen = chunk (i+intToInt64 olen) os
117-
| on == c = i + intToInt64 h : go (h+1)
118-
| otherwise = go (h+1)
119-
where on = A.unsafeIndex oarr (ooff+h)
116+
go h = case memchr oarr# (ooff+h) (olen-h) c of
117+
-1 -> chunk (i+intToInt64 olen) os
118+
x -> i + intToInt64 h + intToInt64 x : go (h + x + 1)
120119

121120
-- | First argument is a strict Text, and second is a lazy one.
122121
isPrefixOf :: T.Text -> Text -> Bool

src/Data/Text/Internal/Search.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,11 @@ indices' (Text narr noff nlen) (Text harr@(A.ByteArray harr#) hoff hlen) = loop
9191
{-# INLINE indices' #-}
9292

9393
scanOne :: Word8 -> Text -> [Int]
94-
scanOne c (Text harr hoff hlen) = loop 0
94+
scanOne c (Text (A.ByteArray harr#) hoff hlen) = loop 0
9595
where
96-
loop !i
97-
| i >= hlen = []
98-
| A.unsafeIndex harr (hoff+i) == c = i : loop (i+1)
99-
| otherwise = loop (i+1)
96+
loop !i = case memchr harr# (hoff + i) (hlen - i) c of
97+
-1 -> []
98+
x -> i + x : loop (i + x + 1)
10099
{-# INLINE scanOne #-}
101100

102101
word8ToInt :: Word8 -> Int

0 commit comments

Comments
 (0)