Skip to content

Commit

Permalink
findIndicesEnd
Browse files Browse the repository at this point in the history
  • Loading branch information
strake committed Jul 3, 2018
1 parent f061966 commit b8a0a8a
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Data/ByteString.hs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ module Data.ByteString (
findIndex, -- :: (Word8 -> Bool) -> ByteString -> Maybe Int
findIndices, -- :: (Word8 -> Bool) -> ByteString -> [Int]
findIndexEnd, -- :: (Word8 -> Bool) -> ByteString -> Maybe Int
findIndicesEnd, -- :: (Word8 -> Bool) -> ByteString -> [Int]
count, -- :: Word8 -> ByteString -> Int

-- * Zipping and unzipping ByteStrings
Expand Down Expand Up @@ -1170,6 +1171,15 @@ findIndices p ps = loop 0 ps
| p (unsafeHead qs) = n : loop (n+1) (unsafeTail qs)
| otherwise = loop (n+1) (unsafeTail qs)

-- | The 'findIndicesEnd' function extends 'findIndexEnd', by returning the
-- indices of all elements satisfying the predicate, in ascending order.
findIndicesEnd :: (Word8 -> Bool) -> ByteString -> [Int]
findIndicesEnd p ps = loop (length ps - 1) ps
where
loop !n !qs | null qs = []
| p (unsafeLast qs) = n : loop (n-1) (unsafeInit qs)
| otherwise = loop (n-1) (unsafeInit qs)

-- ---------------------------------------------------------------------
-- Searching ByteStrings

Expand Down

0 comments on commit b8a0a8a

Please sign in to comment.