    {-# LANGUAGE CPP #-}
    {-# LANGUAGE Rank2Types #-}
    {-# LANGUAGE BangPatterns #-}
    {-# LANGUAGE PatternGuards #-}
    {-# LANGUAGE FlexibleContexts #-}
    #ifdef TRUSTWORTHY
    {-# LANGUAGE Trustworthy #-}
    #endif

    #ifndef MIN_VERSION_base
    #define MIN_VERSION_base(x,y,z) 1
    #endif

    #ifndef MIN_VERSION_bytestring
    #define MIN_VERSION_bytestring(x,y,z) 1
    #endif

    -----------------------------------------------------------------------------
    -- |
    -- Module      :  Data.ByteString.Strict.Lens
    -- Copyright   :  (C) 2012-2016 Edward Kmett
    -- License     :  BSD-style (see the file LICENSE)
    -- Maintainer  :  Edward Kmett <ekmett@gmail.com>
    -- Stability   :  experimental
    -- Portability :  non-portable
    --
    -- This module spends a lot of time fiddling around with 'Data.ByteString' internals
    -- to work around <http://hackage.haskell.org/trac/ghc/ticket/7556> on older
    -- Haskell Platforms and to improve constant and asymptotic factors in our performance.
    ----------------------------------------------------------------------------
    module Control.Lens.Internal.ByteString
      ( unpackStrict, traversedStrictTree
      , unpackStrict8, traversedStrictTree8
      , unpackLazy, traversedLazy
      , unpackLazy8, traversedLazy8
      ) where

    #if !MIN_VERSION_base(4,8,0)
    import Control.Applicative
    #endif

    import Control.Lens.Type
    import Control.Lens.Getter
    import Control.Lens.Fold
    import Control.Lens.Indexed
    import Control.Lens.Setter
    import qualified Data.ByteString               as B
    import qualified Data.ByteString.Char8         as B8
    import qualified Data.ByteString.Lazy          as BL
    import qualified Data.ByteString.Lazy.Char8    as BL8
    import qualified Data.ByteString.Internal      as BI
    import qualified Data.ByteString.Unsafe        as BU
    import Data.Bits
    import Data.Char
    import Data.Int (Int64)
    import Data.Word (Word8)
    import Data.Monoid
    import Foreign.Ptr
    import Foreign.Storable
    #if MIN_VERSION_base(4,8,0)
    import Foreign.ForeignPtr
    #else
    import Foreign.ForeignPtr.Safe
    #endif
    #if !MIN_VERSION_bytestring(0,10,4)
    import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
    #endif
    import GHC.Base (unsafeChr)
    import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
    import GHC.IO (unsafeDupablePerformIO)

    grain :: Int
    grain :: Int
    grain = 32
    {-# INLINE grain #-}

    -- | Traverse a strict 'B.ByteString' in a relatively balanced fashion, as a balanced tree with biased runs of
    -- elements at the leaves.
    traversedStrictTree :: IndexedTraversal' Int B.ByteString Word8
    traversedStrictTree :: p Word8 (f Word8) -> ByteString -> f ByteString
    traversedStrictTree pafb :: p Word8 (f Word8)
    pafb bs :: ByteString
    bs = Int -> (Ptr Word8 -> IO ()) -> ByteString
    unsafeCreate Int
    len ((Ptr Word8 -> IO ()) -> ByteString)
    -> f (Ptr Word8 -> IO ()) -> f ByteString
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Int -> Int -> f (Ptr Word8 -> IO ())
    forall b. Int -> Int -> f (Ptr b -> IO ())
    go 0 Int
    len
     where
       len :: Int
    len = ByteString -> Int
    B.length ByteString
    bs
       go :: Int -> Int -> f (Ptr b -> IO ())
    go !Int
    i !Int
    j
         | Int
    i Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + Int
    grain Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    < Int
    j, Int
    k <- Int
    i Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + Int -> Int -> Int
    forall a. Bits a => a -> Int -> a
    shiftR (Int
    j Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    i) 1 = (\l :: Ptr b -> IO ()
    l r :: Ptr b -> IO ()
    r q :: Ptr b
    q -> Ptr b -> IO ()
    l Ptr b
    q IO () -> IO () -> IO ()
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Ptr b -> IO ()
    r Ptr b
    q) ((Ptr b -> IO ()) -> (Ptr b -> IO ()) -> Ptr b -> IO ())
    -> f (Ptr b -> IO ()) -> f ((Ptr b -> IO ()) -> Ptr b -> IO ())
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Int -> Int -> f (Ptr b -> IO ())
    go Int
    i Int
    k f ((Ptr b -> IO ()) -> Ptr b -> IO ())
    -> f (Ptr b -> IO ()) -> f (Ptr b -> IO ())
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Int -> Int -> f (Ptr b -> IO ())
    go Int
    k Int
    j
         | Bool
    otherwise = Int -> Int -> f (Ptr b -> IO ())
    forall b. Int -> Int -> f (Ptr b -> IO ())
    run Int
    i Int
    j
       run :: Int -> Int -> f (Ptr b -> IO ())
    run !Int
    i !Int
    j
         | Int
    i Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == Int
    j    = (Ptr b -> IO ()) -> f (Ptr b -> IO ())
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (\_ -> () -> IO ()
    forall (m :: * -> *) a. Monad m => a -> m a
    return ())
         | Bool
    otherwise = let !x :: Word8
    x = ByteString -> Int -> Word8
    BU.unsafeIndex ByteString
    bs Int
    i
                       in (\y :: Word8
    y ys :: Ptr b -> IO ()
    ys q :: Ptr b
    q -> Ptr b -> Int -> Word8 -> IO ()
    forall a b. Storable a => Ptr b -> Int -> a -> IO ()
    pokeByteOff Ptr b
    q Int
    i Word8
    y IO () -> IO () -> IO ()
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Ptr b -> IO ()
    ys Ptr b
    q) (Word8 -> (Ptr b -> IO ()) -> Ptr b -> IO ())
    -> f Word8 -> f ((Ptr b -> IO ()) -> Ptr b -> IO ())
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> p Word8 (f Word8) -> Int -> Word8 -> f Word8
    forall i (p :: * -> * -> *) a b.
    Indexable i p =>
    p a b -> i -> a -> b
    indexed p Word8 (f Word8)
    pafb (Int
    i :: Int) Word8
    x f ((Ptr b -> IO ()) -> Ptr b -> IO ())
    -> f (Ptr b -> IO ()) -> f (Ptr b -> IO ())
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Int -> Int -> f (Ptr b -> IO ())
    run (Int
    i Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + 1) Int
    j
    {-# INLINE [0] traversedStrictTree #-}

    {-# RULES
    "bytes -> map"    traversedStrictTree = sets B.map        :: ASetter' B.ByteString Word8;
    "bytes -> imap"   traversedStrictTree = isets imapB       :: AnIndexedSetter' Int B.ByteString Word8;
    "bytes -> foldr"  traversedStrictTree = foldring B.foldr  :: Getting (Endo r) B.ByteString Word8;
    "bytes -> ifoldr" traversedStrictTree = ifoldring ifoldrB :: IndexedGetting Int (Endo r) B.ByteString Word8;
     #-}

    imapB :: (Int -> Word8 -> Word8) -> B.ByteString -> B.ByteString
    imapB :: (Int -> Word8 -> Word8) -> ByteString -> ByteString
    imapB f :: Int -> Word8 -> Word8
    f = (Int, ByteString) -> ByteString
    forall a b. (a, b) -> b
    snd ((Int, ByteString) -> ByteString)
    -> (ByteString -> (Int, ByteString)) -> ByteString -> ByteString
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Int -> Word8 -> (Int, Word8))
    -> Int -> ByteString -> (Int, ByteString)
    forall acc.
    (acc -> Word8 -> (acc, Word8))
    -> acc -> ByteString -> (acc, ByteString)
    B.mapAccumL (\i :: Int
    i a :: Word8
    a -> Int
    i Int -> (Int, Word8) -> (Int, Word8)
    forall a b. a -> b -> b
    `seq` (Int
    i Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + 1, Int -> Word8 -> Word8
    f Int
    i Word8
    a)) 0
    {-# INLINE imapB #-}

    ifoldrB :: (Int -> Word8 -> a -> a) -> a -> B.ByteString -> a
    ifoldrB :: (Int -> Word8 -> a -> a) -> a -> ByteString -> a
    ifoldrB f :: Int -> Word8 -> a -> a
    f z :: a
    z xs :: ByteString
    xs = (Word8 -> (Int -> a) -> Int -> a)
    -> (Int -> a) -> ByteString -> Int -> a
    forall a. (Word8 -> a -> a) -> a -> ByteString -> a
    B.foldr (\ x :: Word8
    x g :: Int -> a
    g i :: Int
    i -> Int
    i Int -> a -> a
    forall a b. a -> b -> b
    `seq` Int -> Word8 -> a -> a
    f Int
    i Word8
    x (Int -> a
    g (Int
    iInt -> Int -> Int
    forall a. Num a => a -> a -> a
    +1))) (a -> Int -> a
    forall a b. a -> b -> a
    const a
    z) ByteString
    xs 0
    {-# INLINE ifoldrB #-}

    -- | Traverse a strict 'B.ByteString' in a relatively balanced fashion, as a balanced tree with biased runs of
    -- elements at the leaves, pretending the bytes are chars.
    traversedStrictTree8 :: IndexedTraversal' Int B.ByteString Char
    traversedStrictTree8 :: p Char (f Char) -> ByteString -> f ByteString
    traversedStrictTree8 pafb :: p Char (f Char)
    pafb bs :: ByteString
    bs = Int -> (Ptr Word8 -> IO ()) -> ByteString
    unsafeCreate Int
    len ((Ptr Word8 -> IO ()) -> ByteString)
    -> f (Ptr Word8 -> IO ()) -> f ByteString
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Int -> Int -> f (Ptr Word8 -> IO ())
    forall b. Int -> Int -> f (Ptr b -> IO ())
    go 0 Int
    len
     where
       len :: Int
    len = ByteString -> Int
    B.length ByteString
    bs
       go :: Int -> Int -> f (Ptr b -> IO ())
    go !Int
    i !Int
    j
         | Int
    i Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + Int
    grain Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    < Int
    j    = let k :: Int
    k = Int
    i Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + Int -> Int -> Int
    forall a. Bits a => a -> Int -> a
    shiftR (Int
    j Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    i) 1
                              in (\l :: Ptr b -> IO ()
    l r :: Ptr b -> IO ()
    r q :: Ptr b
    q -> Ptr b -> IO ()
    l Ptr b
    q IO () -> IO () -> IO ()
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Ptr b -> IO ()
    r Ptr b
    q) ((Ptr b -> IO ()) -> (Ptr b -> IO ()) -> Ptr b -> IO ())
    -> f (Ptr b -> IO ()) -> f ((Ptr b -> IO ()) -> Ptr b -> IO ())
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Int -> Int -> f (Ptr b -> IO ())
    go Int
    i Int
    k f ((Ptr b -> IO ()) -> Ptr b -> IO ())
    -> f (Ptr b -> IO ()) -> f (Ptr b -> IO ())
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Int -> Int -> f (Ptr b -> IO ())
    go Int
    k Int
    j
         | Bool
    otherwise        = Int -> Int -> f (Ptr b -> IO ())
    forall b. Int -> Int -> f (Ptr b -> IO ())
    run Int
    i Int
    j
       run :: Int -> Int -> f (Ptr b -> IO ())
    run !Int
    i !Int
    j
         | Int
    i Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == Int
    j           = (Ptr b -> IO ()) -> f (Ptr b -> IO ())
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (\_ -> () -> IO ()
    forall (m :: * -> *) a. Monad m => a -> m a
    return ())
         | Bool
    otherwise        = let !x :: Word8
    x = ByteString -> Int -> Word8
    BU.unsafeIndex ByteString
    bs Int
    i
                              in (\y :: Char
    y ys :: Ptr b -> IO ()
    ys q :: Ptr b
    q -> Ptr b -> Int -> Word8 -> IO ()
    forall a b. Storable a => Ptr b -> Int -> a -> IO ()
    pokeByteOff Ptr b
    q Int
    i (Char -> Word8
    c2w Char
    y) IO () -> IO () -> IO ()
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Ptr b -> IO ()
    ys Ptr b
    q)
                             (Char -> (Ptr b -> IO ()) -> Ptr b -> IO ())
    -> f Char -> f ((Ptr b -> IO ()) -> Ptr b -> IO ())
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> p Char (f Char) -> Int -> Char -> f Char
    forall i (p :: * -> * -> *) a b.
    Indexable i p =>
    p a b -> i -> a -> b
    indexed p Char (f Char)
    pafb (Int
    i :: Int) (Word8 -> Char
    w2c Word8
    x)
                             f ((Ptr b -> IO ()) -> Ptr b -> IO ())
    -> f (Ptr b -> IO ()) -> f (Ptr b -> IO ())
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Int -> Int -> f (Ptr b -> IO ())
    run (Int
    i Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + 1) Int
    j
    {-# INLINE [0] traversedStrictTree8 #-}

    {-# RULES
    "chars -> map"    traversedStrictTree8 = sets B8.map        :: ASetter' B.ByteString Char;
    "chars -> imap"   traversedStrictTree8 = isets imapB8       :: AnIndexedSetter' Int B.ByteString Char;
    "chars -> foldr"  traversedStrictTree8 = foldring B8.foldr  :: Getting (Endo r) B.ByteString Char;
    "chars -> ifoldr" traversedStrictTree8 = ifoldring ifoldrB8 :: IndexedGetting Int (Endo r) B.ByteString Char;
     #-}

    imapB8 :: (Int -> Char -> Char) -> B.ByteString -> B.ByteString
    imapB8 :: (Int -> Char -> Char) -> ByteString -> ByteString
    imapB8 f :: Int -> Char -> Char
    f = (Int, ByteString) -> ByteString
    forall a b. (a, b) -> b
    snd ((Int, ByteString) -> ByteString)
    -> (ByteString -> (Int, ByteString)) -> ByteString -> ByteString
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Int -> Char -> (Int, Char))
    -> Int -> ByteString -> (Int, ByteString)
    forall acc.
    (acc -> Char -> (acc, Char))
    -> acc -> ByteString -> (acc, ByteString)
    B8.mapAccumL (\i :: Int
    i a :: Char
    a -> Int
    i Int -> (Int, Char) -> (Int, Char)
    forall a b. a -> b -> b
    `seq` (Int
    i Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + 1, Int -> Char -> Char
    f Int
    i Char
    a)) 0
    {-# INLINE imapB8 #-}

    ifoldrB8 :: (Int -> Char -> a -> a) -> a -> B.ByteString -> a
    ifoldrB8 :: (Int -> Char -> a -> a) -> a -> ByteString -> a
    ifoldrB8 f :: Int -> Char -> a -> a
    f z :: a
    z xs :: ByteString
    xs = (Char -> (Int -> a) -> Int -> a)
    -> (Int -> a) -> ByteString -> Int -> a
    forall a. (Char -> a -> a) -> a -> ByteString -> a
    B8.foldr (\ x :: Char
    x g :: Int -> a
    g i :: Int
    i -> Int
    i Int -> a -> a
    forall a b. a -> b -> b
    `seq` Int -> Char -> a -> a
    f Int
    i Char
    x (Int -> a
    g (Int
    iInt -> Int -> Int
    forall a. Num a => a -> a -> a
    +1))) (a -> Int -> a
    forall a b. a -> b -> a
    const a
    z) ByteString
    xs 0
    {-# INLINE ifoldrB8 #-}

    -- | Unpack a lazy 'Bytestring'
    unpackLazy :: BL.ByteString -> [Word8]
    unpackLazy :: ByteString -> [Word8]
    unpackLazy = ByteString -> [Word8]
    BL.unpack
    {-# INLINE unpackLazy #-}

    -- | An 'IndexedTraversal' of the individual bytes in a lazy 'BL.ByteString'
    traversedLazy :: IndexedTraversal' Int64 BL.ByteString Word8
    traversedLazy :: p Word8 (f Word8) -> ByteString -> f ByteString
    traversedLazy pafb :: p Word8 (f Word8)
    pafb = \lbs :: ByteString
    lbs -> (ByteString -> (Int64 -> f ByteString) -> Int64 -> f ByteString)
    -> (Int64 -> f ByteString) -> ByteString -> Int64 -> f ByteString
    forall r. (ByteString -> r -> r) -> r -> ByteString -> r
    foldrChunks ByteString -> (Int64 -> f ByteString) -> Int64 -> f ByteString
    go (\_ -> ByteString -> f ByteString
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure ByteString
    BL.empty) ByteString
    lbs 0
      where
      go :: ByteString -> (Int64 -> f ByteString) -> Int64 -> f ByteString
    go c :: ByteString
    c fcs :: Int64 -> f ByteString
    fcs acc :: Int64
    acc = ByteString -> ByteString -> ByteString
    BL.append (ByteString -> ByteString -> ByteString)
    -> (ByteString -> ByteString)
    -> ByteString
    -> ByteString
    -> ByteString
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . ByteString -> ByteString
    fromStrict
                 (ByteString -> ByteString -> ByteString)
    -> f ByteString -> f (ByteString -> ByteString)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (Int -> Int64)
    -> (Indexed Int Word8 (f Word8) -> ByteString -> f ByteString)
    -> p Word8 (f Word8)
    -> ByteString
    -> f ByteString
    forall j (p :: * -> * -> *) i a b r.
    Indexable j p =>
    (i -> j) -> (Indexed i a b -> r) -> p a b -> r
    reindexed (\x :: Int
    x -> Int64
    acc Int64 -> Int64 -> Int64
    forall a. Num a => a -> a -> a
    + Int -> Int64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    x :: Int64) Indexed Int Word8 (f Word8) -> ByteString -> f ByteString
    IndexedTraversal' Int ByteString Word8
    traversedStrictTree p Word8 (f Word8)
    pafb ByteString
    c
                 f (ByteString -> ByteString) -> f ByteString -> f ByteString
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Int64 -> f ByteString
    fcs Int64
    acc'
        where
        acc' :: Int64
        !acc' :: Int64
    acc' = Int64
    acc Int64 -> Int64 -> Int64
    forall a. Num a => a -> a -> a
    + Int -> Int64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (ByteString -> Int
    B.length ByteString
    c)
    {-# INLINE [1] traversedLazy #-}

    {-# RULES
      "sets lazy bytestring"
        traversedLazy = sets BL.map :: ASetter' BL.ByteString Word8;
      "isets lazy bytestring"
        traversedLazy = isets imapBL :: AnIndexedSetter' Int BL.ByteString Word8;
      "gets lazy bytestring"
        traversedLazy = foldring BL.foldr :: Getting (Endo r) BL.ByteString Word8;
      "igets lazy bytestring"
        traversedLazy = ifoldring ifoldrBL :: IndexedGetting Int (Endo r) BL.ByteString Word8;
     #-}

    imapBL :: (Int -> Word8 -> Word8) -> BL.ByteString -> BL.ByteString
    imapBL :: (Int -> Word8 -> Word8) -> ByteString -> ByteString
    imapBL f :: Int -> Word8 -> Word8
    f = (Int, ByteString) -> ByteString
    forall a b. (a, b) -> b
    snd ((Int, ByteString) -> ByteString)
    -> (ByteString -> (Int, ByteString)) -> ByteString -> ByteString
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Int -> Word8 -> (Int, Word8))
    -> Int -> ByteString -> (Int, ByteString)
    forall acc.
    (acc -> Word8 -> (acc, Word8))
    -> acc -> ByteString -> (acc, ByteString)
    BL.mapAccumL (\i :: Int
    i a :: Word8
    a -> Int
    i Int -> (Int, Word8) -> (Int, Word8)
    forall a b. a -> b -> b
    `seq` (Int
    i Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + 1, Int -> Word8 -> Word8
    f Int
    i Word8
    a)) 0
    {-# INLINE imapBL #-}

    ifoldrBL :: (Int -> Word8 -> a -> a) -> a -> BL.ByteString -> a
    ifoldrBL :: (Int -> Word8 -> a -> a) -> a -> ByteString -> a
    ifoldrBL f :: Int -> Word8 -> a -> a
    f z :: a
    z xs :: ByteString
    xs = (Word8 -> (Int -> a) -> Int -> a)
    -> (Int -> a) -> ByteString -> Int -> a
    forall a. (Word8 -> a -> a) -> a -> ByteString -> a
    BL.foldr (\ x :: Word8
    x g :: Int -> a
    g i :: Int
    i -> Int
    i Int -> a -> a
    forall a b. a -> b -> b
    `seq` Int -> Word8 -> a -> a
    f Int
    i Word8
    x (Int -> a
    g (Int
    iInt -> Int -> Int
    forall a. Num a => a -> a -> a
    +1))) (a -> Int -> a
    forall a b. a -> b -> a
    const a
    z) ByteString
    xs 0
    {-# INLINE ifoldrBL #-}

    -- | Unpack a lazy 'BL.ByteString' pretending the bytes are chars.
    unpackLazy8 :: BL.ByteString -> String
    unpackLazy8 :: ByteString -> String
    unpackLazy8 = ByteString -> String
    BL8.unpack
    {-# INLINE unpackLazy8 #-}

    -- | An 'IndexedTraversal' of the individual bytes in a lazy 'BL.ByteString' pretending the bytes are chars.
    traversedLazy8 :: IndexedTraversal' Int64 BL.ByteString Char
    traversedLazy8 :: p Char (f Char) -> ByteString -> f ByteString
    traversedLazy8 pafb :: p Char (f Char)
    pafb = \lbs :: ByteString
    lbs -> (ByteString -> (Int64 -> f ByteString) -> Int64 -> f ByteString)
    -> (Int64 -> f ByteString) -> ByteString -> Int64 -> f ByteString
    forall r. (ByteString -> r -> r) -> r -> ByteString -> r
    foldrChunks ByteString -> (Int64 -> f ByteString) -> Int64 -> f ByteString
    go (\_ -> ByteString -> f ByteString
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure ByteString
    BL.empty) ByteString
    lbs 0
      where
      go :: ByteString -> (Int64 -> f ByteString) -> Int64 -> f ByteString
    go c :: ByteString
    c fcs :: Int64 -> f ByteString
    fcs acc :: Int64
    acc = ByteString -> ByteString -> ByteString
    BL.append (ByteString -> ByteString -> ByteString)
    -> (ByteString -> ByteString)
    -> ByteString
    -> ByteString
    -> ByteString
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . ByteString -> ByteString
    fromStrict
                 (ByteString -> ByteString -> ByteString)
    -> f ByteString -> f (ByteString -> ByteString)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (Int -> Int64)
    -> (Indexed Int Char (f Char) -> ByteString -> f ByteString)
    -> p Char (f Char)
    -> ByteString
    -> f ByteString
    forall j (p :: * -> * -> *) i a b r.
    Indexable j p =>
    (i -> j) -> (Indexed i a b -> r) -> p a b -> r
    reindexed (\x :: Int
    x -> Int64
    acc Int64 -> Int64 -> Int64
    forall a. Num a => a -> a -> a
    + Int -> Int64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    x :: Int64) Indexed Int Char (f Char) -> ByteString -> f ByteString
    IndexedTraversal' Int ByteString Char
    traversedStrictTree8 p Char (f Char)
    pafb ByteString
    c
                 f (ByteString -> ByteString) -> f ByteString -> f ByteString
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Int64 -> f ByteString
    fcs Int64
    acc'
        where
        acc' :: Int64
        !acc' :: Int64
    acc' = Int64
    acc Int64 -> Int64 -> Int64
    forall a. Num a => a -> a -> a
    + Int -> Int64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (ByteString -> Int
    B.length ByteString
    c)
    {-# INLINE [1] traversedLazy8 #-}

    {-# RULES
      "sets lazy bytestring"
        traversedLazy8 = sets BL8.map :: ASetter' BL8.ByteString Char;
      "isets lazy bytestring"
        traversedLazy8 = isets imapBL8 :: AnIndexedSetter' Int BL8.ByteString Char;
      "gets lazy bytestring"
        traversedLazy8 = foldring BL8.foldr :: Getting (Endo r) BL8.ByteString Char;
      "igets lazy bytestring"
        traversedLazy8 = ifoldring ifoldrBL8 :: IndexedGetting Int (Endo r) BL8.ByteString Char;
     #-}

    imapBL8 :: (Int -> Char -> Char) -> BL8.ByteString -> BL8.ByteString
    imapBL8 :: (Int -> Char -> Char) -> ByteString -> ByteString
    imapBL8 f :: Int -> Char -> Char
    f = (Int, ByteString) -> ByteString
    forall a b. (a, b) -> b
    snd ((Int, ByteString) -> ByteString)
    -> (ByteString -> (Int, ByteString)) -> ByteString -> ByteString
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Int -> Char -> (Int, Char))
    -> Int -> ByteString -> (Int, ByteString)
    forall acc.
    (acc -> Char -> (acc, Char))
    -> acc -> ByteString -> (acc, ByteString)
    BL8.mapAccumL (\i :: Int
    i a :: Char
    a -> Int
    i Int -> (Int, Char) -> (Int, Char)
    forall a b. a -> b -> b
    `seq` (Int
    i Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + 1, Int -> Char -> Char
    f Int
    i Char
    a)) 0
    {-# INLINE imapBL8 #-}

    ifoldrBL8 :: (Int -> Char -> a -> a) -> a -> BL8.ByteString -> a
    ifoldrBL8 :: (Int -> Char -> a -> a) -> a -> ByteString -> a
    ifoldrBL8 f :: Int -> Char -> a -> a
    f z :: a
    z xs :: ByteString
    xs = (Char -> (Int -> a) -> Int -> a)
    -> (Int -> a) -> ByteString -> Int -> a
    forall a. (Char -> a -> a) -> a -> ByteString -> a
    BL8.foldr (\ x :: Char
    x g :: Int -> a
    g i :: Int
    i -> Int
    i Int -> a -> a
    forall a b. a -> b -> b
    `seq` Int -> Char -> a -> a
    f Int
    i Char
    x (Int -> a
    g (Int
    iInt -> Int -> Int
    forall a. Num a => a -> a -> a
    +1))) (a -> Int -> a
    forall a b. a -> b -> a
    const a
    z) ByteString
    xs 0
    {-# INLINE ifoldrBL8 #-}

    ------------------------------------------------------------------------------
    -- ByteString guts
    ------------------------------------------------------------------------------

    fromStrict :: B.ByteString -> BL.ByteString
    #if MIN_VERSION_bytestring(0,10,0)
    fromStrict :: ByteString -> ByteString
    fromStrict = ByteString -> ByteString
    BL.fromStrict
    #else
    fromStrict = \x -> BL.fromChunks [x]
    #endif
    {-# INLINE fromStrict #-}

    foldrChunks :: (B.ByteString -> r -> r) -> r -> BL.ByteString -> r
    #if MIN_VERSION_bytestring(0,10,0)
    foldrChunks :: (ByteString -> r -> r) -> r -> ByteString -> r
    foldrChunks = (ByteString -> r -> r) -> r -> ByteString -> r
    forall r. (ByteString -> r -> r) -> r -> ByteString -> r
    BL.foldrChunks
    #else
    foldrChunks f z b = foldr f z (BL.toChunks b)
    #endif
    {-# INLINE foldrChunks #-}

    -- | Conversion between 'Word8' and 'Char'. Should compile to a no-op.
    w2c :: Word8 -> Char
    w2c :: Word8 -> Char
    w2c = Int -> Char
    unsafeChr (Int -> Char) -> (Word8 -> Int) -> Word8 -> Char
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Word8 -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral
    {-# INLINE w2c #-}

    -- | Unsafe conversion between 'Char' and 'Word8'. This is a no-op and
    -- silently truncates to 8 bits Chars > '\255'. It is provided as
    -- convenience for ByteString construction.
    c2w :: Char -> Word8
    c2w :: Char -> Word8
    c2w = Int -> Word8
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int -> Word8) -> (Char -> Int) -> Char -> Word8
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Char -> Int
    ord
    {-# INLINE c2w #-}

    -- | Unpack a strict 'B.Bytestring'
    unpackStrict :: B.ByteString -> [Word8]
    #if MIN_VERSION_bytestring(0,10,4)
    unpackStrict :: ByteString -> [Word8]
    unpackStrict = ByteString -> [Word8]
    B.unpack
    #else
    unpackStrict (BI.PS fp off len) =
          let p = unsafeForeignPtrToPtr fp
           in go (p `plusPtr` off) (p `plusPtr` (off+len))
        where
          go !p !q | p == q    = []
                   | otherwise = let !x = BI.inlinePerformIO $ do
                                            x' <- peek p
                                            touchForeignPtr fp
                                            return x'
                                 in x : go (p `plusPtr` 1) q
    #endif
    {-# INLINE unpackStrict #-}

    -- | Unpack a strict 'B.Bytestring', pretending the bytes are chars.
    unpackStrict8 :: B.ByteString -> String
    #if MIN_VERSION_bytestring(0,10,4)
    unpackStrict8 :: ByteString -> String
    unpackStrict8 = ByteString -> String
    B8.unpack
    #else
    unpackStrict8 (BI.PS fp off len) =
          let p = unsafeForeignPtrToPtr fp
           in go (p `plusPtr` off) (p `plusPtr` (off+len))
        where
          go !p !q | p == q    = []
                   | otherwise = let !x = BI.inlinePerformIO $ do
                                            x' <- peek p
                                            touchForeignPtr fp
                                            return x'
                                 in w2c x : go (p `plusPtr` 1) q
    #endif
    {-# INLINE unpackStrict8 #-}

    -- | A way of creating ByteStrings outside the IO monad. The @Int@
    -- argument gives the final size of the ByteString. Unlike
    -- 'createAndTrim' the ByteString is not reallocated if the final size
    -- is less than the estimated size.
    unsafeCreate :: Int -> (Ptr Word8 -> IO ()) -> B.ByteString
    unsafeCreate :: Int -> (Ptr Word8 -> IO ()) -> ByteString
    unsafeCreate l :: Int
    l f :: Ptr Word8 -> IO ()
    f = IO ByteString -> ByteString
    forall a. IO a -> a
    unsafeDupablePerformIO (Int -> (Ptr Word8 -> IO ()) -> IO ByteString
    create Int
    l Ptr Word8 -> IO ()
    f)
    {-# INLINE unsafeCreate #-}

    -- | Create ByteString of size @l@ and use action @f@ to fill it's contents.
    create :: Int -> (Ptr Word8 -> IO ()) -> IO B.ByteString
    create :: Int -> (Ptr Word8 -> IO ()) -> IO ByteString
    create l :: Int
    l f :: Ptr Word8 -> IO ()
    f = do
        ForeignPtr Word8
    fp <- Int -> IO (ForeignPtr Word8)
    forall a. Int -> IO (ForeignPtr a)
    mallocPlainForeignPtrBytes Int
    l
        ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()
    forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
    withForeignPtr ForeignPtr Word8
    fp ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
    forall a b. (a -> b) -> a -> b
    $ \p :: Ptr Word8
    p -> Ptr Word8 -> IO ()
    f Ptr Word8
    p
        ByteString -> IO ByteString
    forall (m :: * -> *) a. Monad m => a -> m a
    return (ByteString -> IO ByteString) -> ByteString -> IO ByteString
    forall a b. (a -> b) -> a -> b
    $! ForeignPtr Word8 -> Int -> Int -> ByteString
    BI.PS ForeignPtr Word8
    fp 0 Int
    l
    {-# INLINE create #-}