Skip to content

Commit

Permalink
Rewrite countOcc as countOccBA
Browse files Browse the repository at this point in the history
  • Loading branch information
hsyl20 committed Jan 16, 2024
1 parent 5fbd607 commit a106fcc
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions Data/ByteString/Internal/Pure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,15 @@ countOccBA ba len w = pure (go 0 0)
| otherwise = go n (i+1)

countOcc :: Ptr Word8 -> Int -> Word8 -> IO Int
countOcc p len w
| len == 0 = pure 0
| otherwise = count_occ (plusPtr p (len - 1)) w 0 p

count_occ :: Ptr Word8 -> Word8 -> Int -> Ptr Word8 -> IO Int
count_occ !plast !w !count !p = do
c <- peek p
let !count' = if c == w then count+1 else count
if p == plast
then pure count'
else count_occ plast w count' (plusPtr p 1)
countOcc p len w = go 0 0
where
go !n !i
| i == len = pure n
| otherwise = do
c <- peekByteOff p i
if c == w
then go (n+1) (i+1)
else go n (i+1)

-- | Haskell equivalent of C `sbs_elem_index`
elemIndex :: ByteArray# -> Word8 -> Int -> IO Int
Expand Down

0 comments on commit a106fcc

Please sign in to comment.