Skip to content

Commit

Permalink
Add synonym for indexMaybe
Browse files Browse the repository at this point in the history
  • Loading branch information
FintanH authored and Cole Miller committed Aug 21, 2020
1 parent 32c6ed8 commit f2e918c
Show file tree
Hide file tree
Showing 4 changed files with 40 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 @@ -155,6 +155,7 @@ module Data.ByteString (
-- * Indexing ByteStrings
index, -- :: ByteString -> Int -> Word8
indexMaybe, -- :: ByteString -> Int -> Maybe Word8
(!?), -- :: ByteString -> Int -> Maybe Word8
elemIndex, -- :: Word8 -> ByteString -> Maybe Int
elemIndices, -- :: Word8 -> ByteString -> [Int]
elemIndexEnd, -- :: Word8 -> ByteString -> Maybe Int
Expand Down Expand Up @@ -1110,6 +1111,15 @@ indexMaybe ps n
| otherwise = Just $ ps `unsafeIndex` n
{-# INLINE indexMaybe #-}

-- | /O(1)/ 'ByteString' index, starting from 0, that returns 'Just' if:
--
-- > 0 <= n < length bs
--
-- @since 0.10.10.0
(!?) :: ByteString -> Int -> Maybe Word8
(!?) = indexMaybe
{-# INLINE (!?) #-}

-- | /O(n)/ The 'elemIndex' function returns the index of the first
-- element in the given 'ByteString' which is equal to the query
-- element, or 'Nothing' if there is no such element.
Expand Down
10 changes: 10 additions & 0 deletions Data/ByteString/Char8.hs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ module Data.ByteString.Char8 (
-- * Indexing ByteStrings
index, -- :: ByteString -> Int -> Char
indexMaybe, -- :: ByteString -> Int -> Maybe Char
(!?), -- :: ByteString -> Int -> Maybe Char
elemIndex, -- :: Char -> ByteString -> Maybe Int
elemIndices, -- :: Char -> ByteString -> [Int]
elemIndexEnd, -- :: Char -> ByteString -> Maybe Int
Expand Down Expand Up @@ -654,6 +655,15 @@ indexMaybe :: ByteString -> Int -> Maybe Char
indexMaybe = ((fmap w2c) .) . B.indexMaybe
{-# INLINE indexMaybe #-}

-- | /O(1)/ 'ByteString' index, starting from 0, that returns 'Just' if:
--
-- > 0 <= n < length bs
--
-- @since 0.10.10.0
(!?) :: ByteString -> Int -> Maybe Char
(!?) = indexMaybe
{-# INLINE (!?) #-}

-- | /O(n)/ The 'elemIndex' function returns the index of the first
-- element in the given 'ByteString' which is equal (by memchr) to the
-- query element, or 'Nothing' if there is no such element.
Expand Down
10 changes: 10 additions & 0 deletions Data/ByteString/Lazy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ module Data.ByteString.Lazy (
-- * Indexing ByteStrings
index, -- :: ByteString -> Int64 -> Word8
indexMaybe, -- :: ByteString -> Int64 -> Maybe Word8
(!?), -- :: ByteString -> Int64 -> Maybe Word8
elemIndex, -- :: Word8 -> ByteString -> Maybe Int64
elemIndexEnd, -- :: Word8 -> ByteString -> Maybe Int64
elemIndices, -- :: Word8 -> ByteString -> [Int64]
Expand Down Expand Up @@ -910,6 +911,15 @@ indexMaybe cs0 i = index' cs0 i
index' cs (n - fromIntegral (S.length c))
| otherwise = Just $ S.unsafeIndex c (fromIntegral n)

-- | /O(1)/ 'ByteString' index, starting from 0, that returns 'Just' if:
--
-- > 0 <= n < length bs
--
-- @since 0.10.10.0
(!?) :: ByteString -> Int64 -> Maybe Word8
(!?) = indexMaybe
{-# INLINE (!?) #-}

-- | /O(n)/ The 'elemIndex' function returns the index of the first
-- element in the given 'ByteString' which is equal to the query
-- element, or 'Nothing' if there is no such element.
Expand Down
10 changes: 10 additions & 0 deletions Data/ByteString/Lazy/Char8.hs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ module Data.ByteString.Lazy.Char8 (
-- * Indexing ByteStrings
index, -- :: ByteString -> Int64 -> Char
indexMaybe, -- :: ByteString -> Int64 -> Maybe Char
(!?), -- :: ByteString -> Int64 -> Maybe Char
elemIndex, -- :: Char -> ByteString -> Maybe Int64
elemIndices, -- :: Char -> ByteString -> [Int64]
findIndex, -- :: (Char -> Bool) -> ByteString -> Maybe Int64
Expand Down Expand Up @@ -537,6 +538,15 @@ indexMaybe :: ByteString -> Int64 -> Maybe Char
indexMaybe = ((fmap w2c) .) . L.indexMaybe
{-# INLINE indexMaybe #-}

-- | /O(1)/ 'ByteString' index, starting from 0, that returns 'Just' if:
--
-- > 0 <= n < length bs
--
-- @since 0.10.10.0
(!?) :: ByteString -> Int64 -> Maybe Char
(!?) = indexMaybe
{-# INLINE (!?) #-}

-- | /O(n)/ The 'elemIndex' function returns the index of the first
-- element in the given 'ByteString' which is equal (by memchr) to the
-- query element, or 'Nothing' if there is no such element.
Expand Down

0 comments on commit f2e918c

Please sign in to comment.