diff --git a/Data/ByteString.hs b/Data/ByteString.hs index bb1be36a6..a6c1b8bfb 100644 --- a/Data/ByteString.hs +++ b/Data/ByteString.hs @@ -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 @@ -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. diff --git a/Data/ByteString/Char8.hs b/Data/ByteString/Char8.hs index 2884e3771..466468c11 100644 --- a/Data/ByteString/Char8.hs +++ b/Data/ByteString/Char8.hs @@ -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 @@ -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. diff --git a/Data/ByteString/Lazy.hs b/Data/ByteString/Lazy.hs index e264be4e8..a434820a9 100644 --- a/Data/ByteString/Lazy.hs +++ b/Data/ByteString/Lazy.hs @@ -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] @@ -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. diff --git a/Data/ByteString/Lazy/Char8.hs b/Data/ByteString/Lazy/Char8.hs index de06713bb..a41a3902d 100644 --- a/Data/ByteString/Lazy/Char8.hs +++ b/Data/ByteString/Lazy/Char8.hs @@ -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 @@ -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.