Movatterモバイル変換


[0]ホーム

URL:


{-# LANGUAGE Trustworthy #-}{-# OPTIONS_HADDOCK prune #-}-- |-- Module      : Data.ByteString-- Copyright   : (c) The University of Glasgow 2001,--               (c) David Roundy 2003-2005,--               (c) Simon Marlow 2005,--               (c) Bjorn Bringert 2006,--               (c) Don Stewart 2005-2008,--               (c) Duncan Coutts 2006-2013-- License     : BSD-style---- Maintainer  : dons00@gmail.com, duncan@community.haskell.org-- Stability   : stable-- Portability : portable---- A time- and space-efficient implementation of byte vectors using-- packed Word8 arrays, suitable for high performance use, both in terms-- of large data quantities and high speed requirements. Byte vectors-- are encoded as strict 'Word8' arrays of bytes, held in a 'ForeignPtr',-- and can be passed between C and Haskell with little effort.---- The recomended way to assemble ByteStrings from smaller parts-- is to use the builder monoid from "Data.ByteString.Builder".---- This module is intended to be imported @qualified@, to avoid name-- clashes with "Prelude" functions.  eg.---- > import qualified Data.ByteString as B---- Original GHC implementation by Bryan O\'Sullivan.-- Rewritten to use 'Data.Array.Unboxed.UArray' by Simon Marlow.-- Rewritten to support slices and use 'ForeignPtr' by David Roundy.-- Rewritten again and extended by Don Stewart and Duncan Coutts.--moduleData.ByteString(-- * Strict @ByteString@ByteString,StrictByteString,-- ** Heap fragmentation-- | With GHC, the 'ByteString' representation uses /pinned memory/,-- meaning it cannot be moved by GC. While this is ideal for use with-- the foreign function interface and is usually efficient, this-- representation may lead to issues with heap fragmentation and wasted-- space if the program selectively retains a fraction of many small-- 'ByteString's, keeping them live in memory over long durations.---- While 'ByteString' is indispensable when working with large blobs of-- data and especially when interfacing with native C libraries, be sure-- to also check the 'Data.ByteString.Short.ShortByteString' type.-- As a type backed by /unpinned/ memory, @ShortByteString@ behaves-- similarly to @Text@ (from the @text@ package) on the heap, completely-- avoids fragmentation issues, and in many use-cases may better suit-- your bytestring-storage needs.-- * Introducing and eliminating 'ByteString'sempty,singleton,pack,unpack,fromStrict,toStrict,fromFilePath,toFilePath,-- * Basic interfacecons,snoc,append,head,uncons,unsnoc,last,tail,init,null,length,-- * Transforming ByteStringsmap,reverse,intersperse,intercalate,transpose,-- * Reducing 'ByteString's (folds)foldl,foldl',foldl1,foldl1',foldr,foldr',foldr1,foldr1',-- ** Special foldsconcat,concatMap,any,all,maximum,minimum,-- * Building ByteStrings-- ** Scansscanl,scanl1,scanr,scanr1,-- ** Accumulating mapsmapAccumL,mapAccumR,-- ** Generating and unfolding ByteStringsreplicate,unfoldr,unfoldrN,-- * Substrings-- ** Breaking stringstake,takeEnd,drop,dropEnd,splitAt,takeWhile,takeWhileEnd,dropWhile,dropWhileEnd,span,spanEnd,break,breakEnd,group,groupBy,inits,tails,initsNE,tailsNE,stripPrefix,stripSuffix,-- ** Breaking into many substringssplit,splitWith,-- * PredicatesisPrefixOf,isSuffixOf,isInfixOf,-- ** Encoding validationisValidUtf8,-- ** Search for arbitrary substringsbreakSubstring,-- * Searching ByteStrings-- ** Searching by equalityelem,notElem,-- ** Searching with a predicatefind,filter,partition,-- * Indexing ByteStringsindex,indexMaybe,(!?),elemIndex,elemIndices,elemIndexEnd,findIndex,findIndices,findIndexEnd,count,-- * Zipping and unzipping ByteStringszip,zipWith,packZipWith,unzip,-- * Ordered ByteStringssort,-- * Low level conversions-- ** Copying ByteStringscopy,-- ** Packing 'CString's and pointerspackCString,packCStringLen,-- ** Using ByteStrings as 'CString'suseAsCString,useAsCStringLen,-- * I\/O with 'ByteString's-- ** Standard input and outputgetLine,getContents,putStr,interact,-- ** FilesreadFile,writeFile,appendFile,-- ** I\/O with HandleshGetLine,hGetContents,hGet,hGetSome,hGetNonBlocking,hPut,hPutNonBlocking,hPutStr,)whereimportqualifiedPreludeasPimportPreludehiding(reverse,head,tail,last,init,Foldable(..),map,lines,unlines,concat,any,take,drop,splitAt,takeWhile,dropWhile,span,break,filter,all,concatMap,scanl,scanl1,scanr,scanr1,readFile,writeFile,appendFile,replicate,getContents,getLine,putStr,putStrLn,interact,zip,zipWith,unzip,notElem)importData.Bits(finiteBitSize,shiftL,(.|.),(.&.))importData.ByteString.Internal.TypeimportData.ByteString.Lazy.Internal(fromStrict,toStrict)importData.ByteString.UnsafeimportqualifiedData.ListasListimportqualifiedData.List.NonEmptyasNEimportData.List.NonEmpty(NonEmpty(..))importData.Word(Word8)importControl.Exception(IOException,catch,finally,assert,throwIO)importControl.Monad(when)importForeign.C.String(CString,CStringLen)importForeign.ForeignPtr(ForeignPtr,touchForeignPtr)importForeign.ForeignPtr.Unsafe(unsafeForeignPtrToPtr)importForeign.Marshal.Alloc(allocaBytes)importForeign.Marshal.Array(allocaArray)importForeign.Marshal.UtilsimportForeign.PtrimportForeign.Storable(Storable(..))-- hGetBuf and hPutBuf not available in yhc or nhcimportSystem.IO(stdin,stdout,hClose,hFileSize,hGetBuf,hPutBuf,hGetBufNonBlocking,hPutBufNonBlocking,withBinaryFile,IOMode(..),hGetBufSome)importSystem.IO.Error(mkIOError,illegalOperationErrorType)importData.IORefimportGHC.IO.Handle.InternalsimportGHC.IO.Handle.TypesimportGHC.IO.BufferimportGHC.IO.BufferedIOasBufferedimportGHC.IO.Encoding(getFileSystemEncoding)importGHC.Foreign(newCStringLen,peekCStringLen)importGHC.Stack.Types(HasCallStack)importData.Char(ord)importGHC.Base(build)importGHC.Wordhiding(Word8)-- ------------------------------------------------------------------------------- Introducing and eliminating 'ByteString's-- | /O(1)/ Convert a 'Word8' into a 'ByteString'singleton::Word8->ByteString-- Taking a slice of some static data rather than allocating a new-- buffer for each call is nice for several reasons. Since it doesn't-- involve any side effects hidden in a 'GHC.Magic.runRW#' call, it-- can be simplified to a constructor application. This may enable GHC-- to perform further optimizations after inlining, and also causes a-- fresh singleton to take only 4 words of heap space instead of 9.-- (The buffer object itself would take up 3 words: header, size, and-- 1 word of content. The ForeignPtrContents object used to keep the-- buffer alive would need two more.)singleton :: Word8 -> ByteStringsingletonWord8c=Int -> ByteString -> ByteStringunsafeTakeInt1(ByteString -> ByteString) -> ByteString -> ByteStringforall a b. (a -> b) -> a -> b$Int -> ByteString -> ByteStringunsafeDrop(Word8 -> Intforall a b. (Integral a, Num b) => a -> bfromIntegralWord8c)ByteStringallBytes{-# INLINEsingleton#-}-- | A static blob of all possible bytes (0x00 to 0xff) in orderallBytes::ByteStringallBytes :: ByteStringallBytes=Int -> Addr# -> ByteStringunsafePackLenLiteralInt0x100Addr#"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"#-- | /O(n)/ Convert a @['Word8']@ into a 'ByteString'.---- For applications with large numbers of string literals, 'pack' can be a-- bottleneck. In such cases, consider using 'unsafePackAddress' (GHC only).pack::[Word8]->ByteStringpack :: [Word8] -> ByteStringpack=[Word8] -> ByteStringpackBytes-- | /O(n)/ Converts a 'ByteString' to a @['Word8']@.unpack::ByteString->[Word8]unpack :: ByteString -> [Word8]unpackByteStringbs=(forall b. (Word8 -> b -> b) -> b -> b) -> [Word8]forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]build(ByteString -> (Word8 -> b -> b) -> b -> bforall a. ByteString -> (Word8 -> a -> a) -> a -> aunpackFoldrByteStringbs){-# INLINEunpack#-}---- Have unpack fuse with good list consumers--unpackFoldr::ByteString->(Word8->a->a)->a->aunpackFoldr :: forall a. ByteString -> (Word8 -> a -> a) -> a -> aunpackFoldrByteStringbsWord8 -> a -> akaz=(Word8 -> a -> a) -> a -> ByteString -> aforall a. (Word8 -> a -> a) -> a -> ByteString -> afoldrWord8 -> a -> akazByteStringbs{-# INLINE[0]unpackFoldr#-}{-# RULES"ByteString unpack-list"[1]forallbs.unpackFoldrbs(:)[]=unpackBytesbs#-}-- | Convert a 'FilePath' to a 'ByteString'.---- The 'FilePath' type is expected to use the file system encoding-- as reported by 'GHC.IO.Encoding.getFileSystemEncoding'. This-- encoding allows for round-tripping of arbitrary data on platforms-- that allow arbitrary bytes in their paths. This conversion-- function does the same thing that `System.IO.openFile` would-- do when decoding the 'FilePath'.---- This function is in 'IO' because the file system encoding can be-- changed. If the encoding can be assumed to be constant in your-- use case, you may invoke this function via 'unsafePerformIO'.---- @since 0.11.2.0fromFilePath::FilePath->IOByteStringfromFilePath :: FilePath -> IO ByteStringfromFilePathFilePathpath=doTextEncodingenc<-IO TextEncodinggetFileSystemEncodingTextEncoding -> FilePath -> IO CStringLennewCStringLenTextEncodingencFilePathpathIO CStringLen -> (CStringLen -> IO ByteString) -> IO ByteStringforall a b. IO a -> (a -> IO b) -> IO bforall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b>>=CStringLen -> IO ByteStringunsafePackMallocCStringLen-- | Convert a 'ByteString' to a 'FilePath'.---- This function uses the file system encoding, and resulting 'FilePath's-- can be safely used with standard IO functions and will reference the-- correct path in the presence of arbitrary non-UTF-8 encoded paths.---- This function is in 'IO' because the file system encoding can be-- changed. If the encoding can be assumed to be constant in your-- use case, you may invoke this function via 'unsafePerformIO'.---- @since 0.11.2.0toFilePath::ByteString->IOFilePathtoFilePath :: ByteString -> IO FilePathtoFilePathByteStringpath=doTextEncodingenc<-IO TextEncodinggetFileSystemEncodingByteString -> (CStringLen -> IO FilePath) -> IO FilePathforall a. ByteString -> (CStringLen -> IO a) -> IO auseAsCStringLenByteStringpath(TextEncoding -> CStringLen -> IO FilePathpeekCStringLenTextEncodingenc)-- ----------------------------------------------------------------------- Basic interface-- | /O(1)/ Test whether a ByteString is empty.null::ByteString->Boolnull :: ByteString -> Boolnull(BSForeignPtr Word8_Intl)=Bool -> Bool -> Boolforall a. HasCallStack => Bool -> a -> aassert(IntlInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0)(Bool -> Bool) -> Bool -> Boolforall a b. (a -> b) -> a -> b$IntlInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0{-# INLINEnull#-}-- ----------------------------------------------------------------------- | /O(1)/ 'length' returns the length of a ByteString as an 'Int'.length::ByteString->Intlength :: ByteString -> Intlength(BSForeignPtr Word8_Intl)=Bool -> Int -> Intforall a. HasCallStack => Bool -> a -> aassert(IntlInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0)Intl{-# INLINElength#-}------------------------------------------------------------------------infixr5`cons`--same as list (:)infixl5`snoc`-- | /O(n)/ 'cons' is analogous to (:) for lists, but of different-- complexity, as it requires making a copy.cons::Word8->ByteString->ByteStringcons :: Word8 -> ByteString -> ByteStringconsWord8c(BSForeignPtr Word8xIntlen)=Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFp(FilePath -> Int -> Int -> IntcheckedAddFilePath"cons"IntlenInt1)((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8p->doForeignPtr Word8 -> Word8 -> IO ()forall a. Storable a => ForeignPtr a -> a -> IO ()pokeFpForeignPtr Word8pWord8cForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()memcpyFp(ForeignPtr Word8pForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Int1)ForeignPtr Word8xIntlen{-# INLINEcons#-}-- | /O(n)/ Append a byte to the end of a 'ByteString'snoc::ByteString->Word8->ByteStringsnoc :: ByteString -> Word8 -> ByteStringsnoc(BSForeignPtr Word8xIntlen)Word8c=Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFp(FilePath -> Int -> Int -> IntcheckedAddFilePath"snoc"IntlenInt1)((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8p->doForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()memcpyFpForeignPtr Word8pForeignPtr Word8xIntlenForeignPtr Word8 -> Word8 -> IO ()forall a. Storable a => ForeignPtr a -> a -> IO ()pokeFp(ForeignPtr Word8pForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Intlen)Word8c{-# INLINEsnoc#-}-- | /O(1)/ Extract the first element of a ByteString, which must be non-empty.-- An exception will be thrown in the case of an empty ByteString.---- This is a partial function, consider using 'uncons' instead.head::HasCallStack=>ByteString->Word8head :: HasCallStack => ByteString -> Word8head(BSForeignPtr Word8xIntl)|IntlInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0=FilePath -> Word8forall a. HasCallStack => FilePath -> aerrorEmptyListFilePath"head"|Boolotherwise=IO Word8 -> Word8forall a. IO a -> aaccursedUnutterablePerformIO(IO Word8 -> Word8) -> IO Word8 -> Word8forall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO Word8) -> IO Word8forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8x((Ptr Word8 -> IO Word8) -> IO Word8)-> (Ptr Word8 -> IO Word8) -> IO Word8forall a b. (a -> b) -> a -> b$\Ptr Word8p->Ptr Word8 -> IO Word8forall a. Storable a => Ptr a -> IO apeekPtr Word8p{-# INLINEhead#-}-- | /O(1)/ Extract the elements after the head of a ByteString, which must be non-empty.-- An exception will be thrown in the case of an empty ByteString.---- This is a partial function, consider using 'uncons' instead.tail::HasCallStack=>ByteString->ByteStringtail :: HasCallStack => ByteString -> ByteStringtail(BSForeignPtr Word8pIntl)|IntlInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0=FilePath -> ByteStringforall a. HasCallStack => FilePath -> aerrorEmptyListFilePath"tail"|Boolotherwise=ForeignPtr Word8 -> Int -> ByteStringBS(ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr bplusForeignPtrForeignPtr Word8pInt1)(IntlInt -> Int -> Intforall a. Num a => a -> a -> a-Int1){-# INLINEtail#-}-- | /O(1)/ Extract the 'head' and 'tail' of a ByteString, returning 'Nothing'-- if it is empty.uncons::ByteString->Maybe(Word8,ByteString)uncons :: ByteString -> Maybe (Word8, ByteString)uncons(BSForeignPtr Word8xIntl)|IntlInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0=Maybe (Word8, ByteString)forall a. Maybe aNothing|Boolotherwise=(Word8, ByteString) -> Maybe (Word8, ByteString)forall a. a -> Maybe aJust(IO Word8 -> Word8forall a. IO a -> aaccursedUnutterablePerformIO(IO Word8 -> Word8) -> IO Word8 -> Word8forall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO Word8) -> IO Word8forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8x((Ptr Word8 -> IO Word8) -> IO Word8)-> (Ptr Word8 -> IO Word8) -> IO Word8forall a b. (a -> b) -> a -> b$\Ptr Word8p->Ptr Word8 -> IO Word8forall a. Storable a => Ptr a -> IO apeekPtr Word8p,ForeignPtr Word8 -> Int -> ByteStringBS(ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr bplusForeignPtrForeignPtr Word8xInt1)(IntlInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)){-# INLINEuncons#-}-- | /O(1)/ Extract the last element of a ByteString, which must be finite and non-empty.-- An exception will be thrown in the case of an empty ByteString.---- This is a partial function, consider using 'unsnoc' instead.last::HasCallStack=>ByteString->Word8last :: HasCallStack => ByteString -> Word8lastps :: ByteStringps@(BSForeignPtr Word8xIntl)|ByteString -> BoolnullByteStringps=FilePath -> Word8forall a. HasCallStack => FilePath -> aerrorEmptyListFilePath"last"|Boolotherwise=IO Word8 -> Word8forall a. IO a -> aaccursedUnutterablePerformIO(IO Word8 -> Word8) -> IO Word8 -> Word8forall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO Word8) -> IO Word8forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8x((Ptr Word8 -> IO Word8) -> IO Word8)-> (Ptr Word8 -> IO Word8) -> IO Word8forall a b. (a -> b) -> a -> b$\Ptr Word8p->Ptr Word8 -> Int -> IO Word8forall b. Ptr b -> Int -> IO Word8forall a b. Storable a => Ptr b -> Int -> IO apeekByteOffPtr Word8p(IntlInt -> Int -> Intforall a. Num a => a -> a -> a-Int1){-# INLINElast#-}-- | /O(1)/ Returns all the elements of a 'ByteString' except the last one.-- An exception will be thrown in the case of an empty ByteString.---- This is a partial function, consider using 'unsnoc' instead.init::HasCallStack=>ByteString->ByteStringinit :: HasCallStack => ByteString -> ByteStringinitps :: ByteStringps@(BSForeignPtr Word8pIntl)|ByteString -> BoolnullByteStringps=FilePath -> ByteStringforall a. HasCallStack => FilePath -> aerrorEmptyListFilePath"init"|Boolotherwise=ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8p(IntlInt -> Int -> Intforall a. Num a => a -> a -> a-Int1){-# INLINEinit#-}-- | /O(1)/ Extract the 'init' and 'last' of a ByteString, returning 'Nothing'-- if it is empty.unsnoc::ByteString->Maybe(ByteString,Word8)unsnoc :: ByteString -> Maybe (ByteString, Word8)unsnoc(BSForeignPtr Word8xIntl)|IntlInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0=Maybe (ByteString, Word8)forall a. Maybe aNothing|Boolotherwise=(ByteString, Word8) -> Maybe (ByteString, Word8)forall a. a -> Maybe aJust(ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8x(IntlInt -> Int -> Intforall a. Num a => a -> a -> a-Int1),IO Word8 -> Word8forall a. IO a -> aaccursedUnutterablePerformIO(IO Word8 -> Word8) -> IO Word8 -> Word8forall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO Word8) -> IO Word8forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8x((Ptr Word8 -> IO Word8) -> IO Word8)-> (Ptr Word8 -> IO Word8) -> IO Word8forall a b. (a -> b) -> a -> b$\Ptr Word8p->Ptr Word8 -> Int -> IO Word8forall b. Ptr b -> Int -> IO Word8forall a b. Storable a => Ptr b -> Int -> IO apeekByteOffPtr Word8p(IntlInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)){-# INLINEunsnoc#-}-- | /O(n)/ Append two ByteStringsappend::ByteString->ByteString->ByteStringappend :: ByteString -> ByteString -> ByteStringappend=ByteString -> ByteString -> ByteStringforall a. Monoid a => a -> a -> amappend{-# INLINEappend#-}-- ----------------------------------------------------------------------- Transformations-- | /O(n)/ 'map' @f xs@ is the ByteString obtained by applying @f@ to each-- element of @xs@.map::(Word8->Word8)->ByteString->ByteStringmap :: (Word8 -> Word8) -> ByteString -> ByteStringmapWord8 -> Word8f(BSForeignPtr Word8srcPtrIntlen)=Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFpIntlen((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8dstPtr->ForeignPtr Word8 -> ForeignPtr Word8 -> IO ()forall {b}. ForeignPtr Word8 -> ForeignPtr b -> IO ()mForeignPtr Word8srcPtrForeignPtr Word8dstPtrwherem :: ForeignPtr Word8 -> ForeignPtr b -> IO ()m!ForeignPtr Word8p1!ForeignPtr bp2=Int -> IO ()map_Int0wheremap_::Int->IO()map_ :: Int -> IO ()map_!Intn|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intlen=() -> IO ()forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn()|Boolotherwise=doWord8x<-ForeignPtr Word8 -> Int -> IO Word8forall a. Storable a => ForeignPtr a -> Int -> IO apeekFpByteOffForeignPtr Word8p1IntnForeignPtr b -> Int -> Word8 -> IO ()forall a b. Storable a => ForeignPtr b -> Int -> a -> IO ()pokeFpByteOffForeignPtr bp2Intn(Word8 -> Word8fWord8x)Int -> IO ()map_(IntnInt -> Int -> Intforall a. Num a => a -> a -> a+Int1){-# INLINEmap#-}-- | /O(n)/ 'reverse' @xs@ efficiently returns the elements of @xs@ in reverse order.reverse::ByteString->ByteStringreverse :: ByteString -> ByteStringreverse(BSForeignPtr Word8xIntl)=Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFpIntl((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8fp->ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Word8p->ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8x((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Word8f->Ptr Word8 -> Ptr Word8 -> CSize -> IO ()c_reversePtr Word8pPtr Word8f(Int -> CSizeforall a b. (Integral a, Num b) => a -> bfromIntegralIntl)-- | /O(n)/ The 'intersperse' function takes a 'Word8' and a-- 'ByteString' and \`intersperses\' that byte between the elements of-- the 'ByteString'.  It is analogous to the intersperse function on-- Lists.intersperse::Word8->ByteString->ByteStringintersperse :: Word8 -> ByteString -> ByteStringintersperseWord8cps :: ByteStringps@(BSForeignPtr Word8xIntl)|ByteString -> IntlengthByteStringpsInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int2=ByteStringps|Boolotherwise=Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFp(Int2Int -> Int -> Intforall a. Num a => a -> a -> a*IntlInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8fp->ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Word8p->ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8x((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Word8f->Ptr Word8 -> Ptr Word8 -> CSize -> Word8 -> IO ()c_interspersePtr Word8pPtr Word8f(Int -> CSizeforall a b. (Integral a, Num b) => a -> bfromIntegralIntl)Word8c-- | The 'transpose' function transposes the rows and columns of its-- 'ByteString' argument.transpose::[ByteString]->[ByteString]transpose :: [ByteString] -> [ByteString]transpose=([Word8] -> ByteString) -> [[Word8]] -> [ByteString]forall a b. (a -> b) -> [a] -> [b]P.map[Word8] -> ByteStringpack([[Word8]] -> [ByteString])-> ([ByteString] -> [[Word8]]) -> [ByteString] -> [ByteString]forall b c a. (b -> c) -> (a -> b) -> a -> c.[[Word8]] -> [[Word8]]forall a. [[a]] -> [[a]]List.transpose([[Word8]] -> [[Word8]])-> ([ByteString] -> [[Word8]]) -> [ByteString] -> [[Word8]]forall b c a. (b -> c) -> (a -> b) -> a -> c.(ByteString -> [Word8]) -> [ByteString] -> [[Word8]]forall a b. (a -> b) -> [a] -> [b]P.mapByteString -> [Word8]unpack-- ----------------------------------------------------------------------- Reducing 'ByteString's-- | 'foldl', applied to a binary operator, a starting value (typically-- the left-identity of the operator), and a ByteString, reduces the-- ByteString using the binary operator, from left to right.--foldl::(a->Word8->a)->a->ByteString->afoldl :: forall a. (a -> Word8 -> a) -> a -> ByteString -> afoldla -> Word8 -> afaz=\(BSForeignPtr Word8fpIntlen)->letend :: Ptr bend=ForeignPtr Word8 -> Ptr Word8forall a. ForeignPtr a -> Ptr aunsafeForeignPtrToPtrForeignPtr Word8fpPtr Word8 -> Int -> Ptr bforall a b. Ptr a -> Int -> Ptr b`plusPtr`(-Int1)-- not tail recursive; traverses array right to leftgo :: Ptr Word8 -> ago!Ptr Word8p|Ptr Word8pPtr Word8 -> Ptr Word8 -> Boolforall a. Eq a => a -> a -> Bool==Ptr Word8forall {b}. Ptr bend=az|Boolotherwise=let!x :: Word8x=IO Word8 -> Word8forall a. IO a -> aaccursedUnutterablePerformIO(IO Word8 -> Word8) -> IO Word8 -> Word8forall a b. (a -> b) -> a -> b$doWord8x'<-Ptr Word8 -> IO Word8forall a. Storable a => Ptr a -> IO apeekPtr Word8pForeignPtr Word8 -> IO ()forall a. ForeignPtr a -> IO ()touchForeignPtrForeignPtr Word8fpWord8 -> IO Word8forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnWord8x'ina -> Word8 -> af(Ptr Word8 -> ago(Ptr Word8pPtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`(-Int1)))Word8xinPtr Word8 -> ago(Ptr Anyforall {b}. Ptr bendPtr Any -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`Intlen){-# INLINEfoldl#-}{-Note [fold inlining]:GHC will only inline a function marked INLINEif it is fully saturated (meaning the number ofarguments provided at the call site is at leastequal to the number of lhs arguments).-}-- | 'foldl'' is like 'foldl', but strict in the accumulator.--foldl'::(a->Word8->a)->a->ByteString->afoldl' :: forall a. (a -> Word8 -> a) -> a -> ByteString -> afoldl'a -> Word8 -> afav=\(BSForeignPtr Word8fpIntlen)->-- see fold inliningletg :: ForeignPtr Word8 -> IO agForeignPtr Word8ptr=a -> ForeignPtr Word8 -> IO agoavForeignPtr Word8ptrwhereend :: ForeignPtr bend=ForeignPtr Word8ptrForeignPtr Word8 -> Int -> ForeignPtr bforall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Intlen-- tail recursive; traverses array left to rightgo :: a -> ForeignPtr Word8 -> IO ago!az!ForeignPtr Word8p|ForeignPtr Word8pForeignPtr Word8 -> ForeignPtr Word8 -> Boolforall a. Eq a => a -> a -> Bool==ForeignPtr Word8forall {b}. ForeignPtr bend=a -> IO aforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnaz|Boolotherwise=doWord8x<-ForeignPtr Word8 -> IO Word8forall a. Storable a => ForeignPtr a -> IO apeekFpForeignPtr Word8pa -> ForeignPtr Word8 -> IO ago(a -> Word8 -> afazWord8x)(ForeignPtr Word8pForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Int1)inIO a -> aforall a. IO a -> aaccursedUnutterablePerformIO(IO a -> a) -> IO a -> aforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> IO agForeignPtr Word8fp{-# INLINEfoldl'#-}-- | 'foldr', applied to a binary operator, a starting value-- (typically the right-identity of the operator), and a ByteString,-- reduces the ByteString using the binary operator, from right to left.foldr::(Word8->a->a)->a->ByteString->afoldr :: forall a. (Word8 -> a -> a) -> a -> ByteString -> afoldrWord8 -> a -> akaz=\(BSForeignPtr Word8fpIntlen)->-- see fold inliningletptr :: Ptr Word8ptr=ForeignPtr Word8 -> Ptr Word8forall a. ForeignPtr a -> Ptr aunsafeForeignPtrToPtrForeignPtr Word8fpend :: Ptr bend=Ptr Word8ptrPtr Word8 -> Int -> Ptr bforall a b. Ptr a -> Int -> Ptr b`plusPtr`Intlen-- not tail recursive; traverses array left to rightgo :: Ptr Word8 -> ago!Ptr Word8p|Ptr Word8pPtr Word8 -> Ptr Word8 -> Boolforall a. Eq a => a -> a -> Bool==Ptr Word8forall {b}. Ptr bend=az|Boolotherwise=let!x :: Word8x=IO Word8 -> Word8forall a. IO a -> aaccursedUnutterablePerformIO(IO Word8 -> Word8) -> IO Word8 -> Word8forall a b. (a -> b) -> a -> b$doWord8x'<-Ptr Word8 -> IO Word8forall a. Storable a => Ptr a -> IO apeekPtr Word8pForeignPtr Word8 -> IO ()forall a. ForeignPtr a -> IO ()touchForeignPtrForeignPtr Word8fpWord8 -> IO Word8forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnWord8x'inWord8 -> a -> akWord8x(Ptr Word8 -> ago(Ptr Word8pPtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`Int1))inPtr Word8 -> agoPtr Word8ptr{-# INLINEfoldr#-}-- | 'foldr'' is like 'foldr', but strict in the accumulator.foldr'::(Word8->a->a)->a->ByteString->afoldr' :: forall a. (Word8 -> a -> a) -> a -> ByteString -> afoldr'Word8 -> a -> akav=\(BSForeignPtr Word8fpIntlen)->-- see fold inliningletg :: ForeignPtr a -> IO agForeignPtr aptr=a -> ForeignPtr Word8 -> IO agoav(ForeignPtr Anyforall {b}. ForeignPtr bendForeignPtr Any -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Intlen)whereend :: ForeignPtr bend=ForeignPtr aptrForeignPtr a -> Int -> ForeignPtr bforall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`(-Int1)-- tail recursive; traverses array right to leftgo :: a -> ForeignPtr Word8 -> IO ago!az!ForeignPtr Word8p|ForeignPtr Word8pForeignPtr Word8 -> ForeignPtr Word8 -> Boolforall a. Eq a => a -> a -> Bool==ForeignPtr Word8forall {b}. ForeignPtr bend=a -> IO aforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnaz|Boolotherwise=doWord8x<-ForeignPtr Word8 -> IO Word8forall a. Storable a => ForeignPtr a -> IO apeekFpForeignPtr Word8pa -> ForeignPtr Word8 -> IO ago(Word8 -> a -> akWord8xaz)(ForeignPtr Word8pForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`(-Int1))inIO a -> aforall a. IO a -> aaccursedUnutterablePerformIO(IO a -> a) -> IO a -> aforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> IO aforall {a}. ForeignPtr a -> IO agForeignPtr Word8fp{-# INLINEfoldr'#-}-- | 'foldl1' is a variant of 'foldl' that has no starting value-- argument, and thus must be applied to non-empty 'ByteString's.-- An exception will be thrown in the case of an empty ByteString.foldl1::HasCallStack=>(Word8->Word8->Word8)->ByteString->Word8foldl1 :: HasCallStack => (Word8 -> Word8 -> Word8) -> ByteString -> Word8foldl1Word8 -> Word8 -> Word8fByteStringps=caseByteString -> Maybe (Word8, ByteString)unconsByteStringpsofMaybe (Word8, ByteString)Nothing->FilePath -> Word8forall a. HasCallStack => FilePath -> aerrorEmptyListFilePath"foldl1"Just(Word8h,ByteStringt)->(Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> Word8forall a. (a -> Word8 -> a) -> a -> ByteString -> afoldlWord8 -> Word8 -> Word8fWord8hByteStringt{-# INLINEfoldl1#-}-- | 'foldl1'' is like 'foldl1', but strict in the accumulator.-- An exception will be thrown in the case of an empty ByteString.foldl1'::HasCallStack=>(Word8->Word8->Word8)->ByteString->Word8foldl1' :: HasCallStack => (Word8 -> Word8 -> Word8) -> ByteString -> Word8foldl1'Word8 -> Word8 -> Word8fByteStringps=caseByteString -> Maybe (Word8, ByteString)unconsByteStringpsofMaybe (Word8, ByteString)Nothing->FilePath -> Word8forall a. HasCallStack => FilePath -> aerrorEmptyListFilePath"foldl1'"Just(Word8h,ByteStringt)->(Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> Word8forall a. (a -> Word8 -> a) -> a -> ByteString -> afoldl'Word8 -> Word8 -> Word8fWord8hByteStringt{-# INLINEfoldl1'#-}-- | 'foldr1' is a variant of 'foldr' that has no starting value argument,-- and thus must be applied to non-empty 'ByteString's-- An exception will be thrown in the case of an empty ByteString.foldr1::HasCallStack=>(Word8->Word8->Word8)->ByteString->Word8foldr1 :: HasCallStack => (Word8 -> Word8 -> Word8) -> ByteString -> Word8foldr1Word8 -> Word8 -> Word8fByteStringps=caseByteString -> Maybe (ByteString, Word8)unsnocByteStringpsofMaybe (ByteString, Word8)Nothing->FilePath -> Word8forall a. HasCallStack => FilePath -> aerrorEmptyListFilePath"foldr1"Just(ByteStringb,Word8c)->(Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> Word8forall a. (Word8 -> a -> a) -> a -> ByteString -> afoldrWord8 -> Word8 -> Word8fWord8cByteStringb{-# INLINEfoldr1#-}-- | 'foldr1'' is a variant of 'foldr1', but is strict in the-- accumulator.foldr1'::HasCallStack=>(Word8->Word8->Word8)->ByteString->Word8foldr1' :: HasCallStack => (Word8 -> Word8 -> Word8) -> ByteString -> Word8foldr1'Word8 -> Word8 -> Word8fByteStringps=caseByteString -> Maybe (ByteString, Word8)unsnocByteStringpsofMaybe (ByteString, Word8)Nothing->FilePath -> Word8forall a. HasCallStack => FilePath -> aerrorEmptyListFilePath"foldr1'"Just(ByteStringb,Word8c)->(Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> Word8forall a. (Word8 -> a -> a) -> a -> ByteString -> afoldr'Word8 -> Word8 -> Word8fWord8cByteStringb{-# INLINEfoldr1'#-}-- ----------------------------------------------------------------------- Special folds-- | /O(n)/ Concatenate a list of ByteStrings.concat::[ByteString]->ByteStringconcat :: [ByteString] -> ByteStringconcat=[ByteString] -> ByteStringforall a. Monoid a => [a] -> amconcat-- | Map a function over a 'ByteString' and concatenate the resultsconcatMap::(Word8->ByteString)->ByteString->ByteStringconcatMap :: (Word8 -> ByteString) -> ByteString -> ByteStringconcatMapWord8 -> ByteStringf=[ByteString] -> ByteStringconcat([ByteString] -> ByteString)-> (ByteString -> [ByteString]) -> ByteString -> ByteStringforall b c a. (b -> c) -> (a -> b) -> a -> c.(Word8 -> [ByteString] -> [ByteString])-> [ByteString] -> ByteString -> [ByteString]forall a. (Word8 -> a -> a) -> a -> ByteString -> afoldr((:)(ByteString -> [ByteString] -> [ByteString])-> (Word8 -> ByteString) -> Word8 -> [ByteString] -> [ByteString]forall b c a. (b -> c) -> (a -> b) -> a -> c.Word8 -> ByteStringf)[]-- foldr (append . f) empty-- | /O(n)/ Applied to a predicate and a ByteString, 'any' determines if-- any element of the 'ByteString' satisfies the predicate.any::(Word8->Bool)->ByteString->Boolany :: (Word8 -> Bool) -> ByteString -> BoolanyWord8 -> Bool_(BSForeignPtr Word8_Int0)=BoolFalseanyWord8 -> Boolf(BSForeignPtr Word8xIntlen)=IO Bool -> Boolforall a. IO a -> aaccursedUnutterablePerformIO(IO Bool -> Bool) -> IO Bool -> Boolforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> IO BoolgForeignPtr Word8xwhereg :: ForeignPtr Word8 -> IO BoolgForeignPtr Word8ptr=ForeignPtr Word8 -> IO BoolgoForeignPtr Word8ptrwhereend :: ForeignPtr bend=ForeignPtr Word8ptrForeignPtr Word8 -> Int -> ForeignPtr bforall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Intlengo :: ForeignPtr Word8 -> IO Boolgo!ForeignPtr Word8p|ForeignPtr Word8pForeignPtr Word8 -> ForeignPtr Word8 -> Boolforall a. Eq a => a -> a -> Bool==ForeignPtr Word8forall {b}. ForeignPtr bend=Bool -> IO Boolforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnBoolFalse|Boolotherwise=doWord8c<-ForeignPtr Word8 -> IO Word8forall a. Storable a => ForeignPtr a -> IO apeekFpForeignPtr Word8pifWord8 -> BoolfWord8cthenBool -> IO Boolforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnBoolTrueelseForeignPtr Word8 -> IO Boolgo(ForeignPtr Word8pForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Int1){-# INLINE[1]any#-}{-# RULES"ByteString specialise any (x ==)"forallx.any(x`eqWord8`)=anyBytex"ByteString specialise any (== x)"forallx.any(`eqWord8`x)=anyBytex#-}-- | Is any element of 'ByteString' equal to c?anyByte::Word8->ByteString->BoolanyByte :: Word8 -> ByteString -> BoolanyByteWord8c(BSForeignPtr Word8xIntl)=IO Bool -> Boolforall a. IO a -> aaccursedUnutterablePerformIO(IO Bool -> Bool) -> IO Bool -> Boolforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO Bool) -> IO Boolforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8x((Ptr Word8 -> IO Bool) -> IO Bool)-> (Ptr Word8 -> IO Bool) -> IO Boolforall a b. (a -> b) -> a -> b$\Ptr Word8p->doPtr Word8q<-Ptr Word8 -> Word8 -> CSize -> IO (Ptr Word8)memchrPtr Word8pWord8c(Int -> CSizeforall a b. (Integral a, Num b) => a -> bfromIntegralIntl)Bool -> IO Boolforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(Bool -> IO Bool) -> Bool -> IO Boolforall a b. (a -> b) -> a -> b$!Ptr Word8qPtr Word8 -> Ptr Word8 -> Boolforall a. Eq a => a -> a -> Bool/=Ptr Word8forall {b}. Ptr bnullPtr{-# INLINEanyByte#-}-- | /O(n)/ Applied to a predicate and a 'ByteString', 'all' determines-- if all elements of the 'ByteString' satisfy the predicate.all::(Word8->Bool)->ByteString->Boolall :: (Word8 -> Bool) -> ByteString -> BoolallWord8 -> Bool_(BSForeignPtr Word8_Int0)=BoolTrueallWord8 -> Boolf(BSForeignPtr Word8xIntlen)=IO Bool -> Boolforall a. IO a -> aaccursedUnutterablePerformIO(IO Bool -> Bool) -> IO Bool -> Boolforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> IO BoolgForeignPtr Word8xwhereg :: ForeignPtr Word8 -> IO BoolgForeignPtr Word8ptr=ForeignPtr Word8 -> IO BoolgoForeignPtr Word8ptrwhereend :: ForeignPtr bend=ForeignPtr Word8ptrForeignPtr Word8 -> Int -> ForeignPtr bforall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Intlengo :: ForeignPtr Word8 -> IO Boolgo!ForeignPtr Word8p|ForeignPtr Word8pForeignPtr Word8 -> ForeignPtr Word8 -> Boolforall a. Eq a => a -> a -> Bool==ForeignPtr Word8forall {b}. ForeignPtr bend=Bool -> IO Boolforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnBoolTrue-- end of list|Boolotherwise=doWord8c<-ForeignPtr Word8 -> IO Word8forall a. Storable a => ForeignPtr a -> IO apeekFpForeignPtr Word8pifWord8 -> BoolfWord8cthenForeignPtr Word8 -> IO Boolgo(ForeignPtr Word8pForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Int1)elseBool -> IO Boolforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnBoolFalse{-# INLINE[1]all#-}{-# RULES"ByteString specialise all (x /=)"forallx.all(x`neWord8`)=not.anyBytex"ByteString specialise all (/= x)"forallx.all(`neWord8`x)=not.anyBytex#-}-------------------------------------------------------------------------- | /O(n)/ 'maximum' returns the maximum value from a 'ByteString'-- An exception will be thrown in the case of an empty ByteString.maximum::HasCallStack=>ByteString->Word8maximum :: HasCallStack => ByteString -> Word8maximumxs :: ByteStringxs@(BSForeignPtr Word8xIntl)|ByteString -> BoolnullByteStringxs=FilePath -> Word8forall a. HasCallStack => FilePath -> aerrorEmptyListFilePath"maximum"|Boolotherwise=IO Word8 -> Word8forall a. IO a -> aaccursedUnutterablePerformIO(IO Word8 -> Word8) -> IO Word8 -> Word8forall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO Word8) -> IO Word8forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8x((Ptr Word8 -> IO Word8) -> IO Word8)-> (Ptr Word8 -> IO Word8) -> IO Word8forall a b. (a -> b) -> a -> b$\Ptr Word8p->Ptr Word8 -> CSize -> IO Word8c_maximumPtr Word8p(Int -> CSizeforall a b. (Integral a, Num b) => a -> bfromIntegralIntl){-# INLINEmaximum#-}-- | /O(n)/ 'minimum' returns the minimum value from a 'ByteString'-- An exception will be thrown in the case of an empty ByteString.minimum::HasCallStack=>ByteString->Word8minimum :: HasCallStack => ByteString -> Word8minimumxs :: ByteStringxs@(BSForeignPtr Word8xIntl)|ByteString -> BoolnullByteStringxs=FilePath -> Word8forall a. HasCallStack => FilePath -> aerrorEmptyListFilePath"minimum"|Boolotherwise=IO Word8 -> Word8forall a. IO a -> aaccursedUnutterablePerformIO(IO Word8 -> Word8) -> IO Word8 -> Word8forall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO Word8) -> IO Word8forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8x((Ptr Word8 -> IO Word8) -> IO Word8)-> (Ptr Word8 -> IO Word8) -> IO Word8forall a b. (a -> b) -> a -> b$\Ptr Word8p->Ptr Word8 -> CSize -> IO Word8c_minimumPtr Word8p(Int -> CSizeforall a b. (Integral a, Num b) => a -> bfromIntegralIntl){-# INLINEminimum#-}-------------------------------------------------------------------------- | The 'mapAccumL' function behaves like a combination of 'map' and-- 'foldl'; it applies a function to each element of a ByteString,-- passing an accumulating parameter from left to right, and returning a-- final value of this accumulator together with the new ByteString.mapAccumL::(acc->Word8->(acc,Word8))->acc->ByteString->(acc,ByteString)mapAccumL :: forall acc.(acc -> Word8 -> (acc, Word8))-> acc -> ByteString -> (acc, ByteString)mapAccumLacc -> Word8 -> (acc, Word8)faccacc=\(BSForeignPtr Word8aIntlen)->IO (acc, ByteString) -> (acc, ByteString)forall a. IO a -> aunsafeDupablePerformIO(IO (acc, ByteString) -> (acc, ByteString))-> IO (acc, ByteString) -> (acc, ByteString)forall a b. (a -> b) -> a -> b$do-- see fold inliningForeignPtr Word8gp<-Int -> IO (ForeignPtr Word8)forall a. Int -> IO (ForeignPtr a)mallocByteStringIntlenletgo :: ForeignPtr Word8 -> ForeignPtr b -> IO accgoForeignPtr Word8srcForeignPtr bdst=acc -> Int -> IO accmapAccumL_accaccInt0wheremapAccumL_ :: acc -> Int -> IO accmapAccumL_!accs!Intn|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intlen=acc -> IO accforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnaccs|Boolotherwise=doWord8x<-ForeignPtr Word8 -> Int -> IO Word8forall a. Storable a => ForeignPtr a -> Int -> IO apeekFpByteOffForeignPtr Word8srcIntnlet(accs',Word8y)=acc -> Word8 -> (acc, Word8)faccsWord8xForeignPtr b -> Int -> Word8 -> IO ()forall a b. Storable a => ForeignPtr b -> Int -> a -> IO ()pokeFpByteOffForeignPtr bdstIntnWord8yacc -> Int -> IO accmapAccumL_accs'(IntnInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)accacc'<-ForeignPtr Word8 -> ForeignPtr Word8 -> IO accforall {b}. ForeignPtr Word8 -> ForeignPtr b -> IO accgoForeignPtr Word8aForeignPtr Word8gp(acc, ByteString) -> IO (acc, ByteString)forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(accacc',ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8gpIntlen){-# INLINEmapAccumL#-}-- | The 'mapAccumR' function behaves like a combination of 'map' and-- 'foldr'; it applies a function to each element of a ByteString,-- passing an accumulating parameter from right to left, and returning a-- final value of this accumulator together with the new ByteString.mapAccumR::(acc->Word8->(acc,Word8))->acc->ByteString->(acc,ByteString)mapAccumR :: forall acc.(acc -> Word8 -> (acc, Word8))-> acc -> ByteString -> (acc, ByteString)mapAccumRacc -> Word8 -> (acc, Word8)faccacc=\(BSForeignPtr Word8aIntlen)->IO (acc, ByteString) -> (acc, ByteString)forall a. IO a -> aunsafeDupablePerformIO(IO (acc, ByteString) -> (acc, ByteString))-> IO (acc, ByteString) -> (acc, ByteString)forall a b. (a -> b) -> a -> b$do-- see fold inliningForeignPtr Word8gp<-Int -> IO (ForeignPtr Word8)forall a. Int -> IO (ForeignPtr a)mallocByteStringIntlenletgo :: ForeignPtr Word8 -> ForeignPtr b -> IO accgoForeignPtr Word8srcForeignPtr bdst=acc -> Int -> IO accmapAccumR_accacc(IntlenInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)wheremapAccumR_ :: acc -> Int -> IO accmapAccumR_!accs(-1)=acc -> IO accforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnaccsmapAccumR_!accs!Intn=doWord8x<-ForeignPtr Word8 -> Int -> IO Word8forall a. Storable a => ForeignPtr a -> Int -> IO apeekFpByteOffForeignPtr Word8srcIntnlet(accs',Word8y)=acc -> Word8 -> (acc, Word8)faccsWord8xForeignPtr b -> Int -> Word8 -> IO ()forall a b. Storable a => ForeignPtr b -> Int -> a -> IO ()pokeFpByteOffForeignPtr bdstIntnWord8yacc -> Int -> IO accmapAccumR_accs'(IntnInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)accacc'<-ForeignPtr Word8 -> ForeignPtr Word8 -> IO accforall {b}. ForeignPtr Word8 -> ForeignPtr b -> IO accgoForeignPtr Word8aForeignPtr Word8gp(acc, ByteString) -> IO (acc, ByteString)forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(accacc',ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8gpIntlen){-# INLINEmapAccumR#-}-- ----------------------------------------------------------------------- Building ByteStrings-- | 'scanl' is similar to 'foldl', but returns a list of successive-- reduced values from the left.---- > scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]---- Note that---- > head (scanl f z xs) == z-- > last (scanl f z xs) == foldl f z xs--scanl::(Word8->Word8->Word8)-- ^ accumulator -> element -> new accumulator->Word8-- ^ starting value of accumulator->ByteString-- ^ input of length n->ByteString-- ^ output of length n+1scanl :: (Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> ByteStringscanlWord8 -> Word8 -> Word8fWord8v=\(BSForeignPtr Word8aIntlen)->Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFp(FilePath -> Int -> Int -> IntcheckedAddFilePath"scanl"IntlenInt1)((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8q->do-- see fold inliningForeignPtr Word8 -> Word8 -> IO ()forall a. Storable a => ForeignPtr a -> a -> IO ()pokeFpForeignPtr Word8qWord8vletgo :: ForeignPtr Word8 -> ForeignPtr b -> IO ()goForeignPtr Word8srcForeignPtr bdst=Word8 -> Int -> IO ()scanl_Word8vInt0wherescanl_ :: Word8 -> Int -> IO ()scanl_!Word8z!Intn|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intlen=() -> IO ()forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn()|Boolotherwise=doWord8x<-ForeignPtr Word8 -> Int -> IO Word8forall a. Storable a => ForeignPtr a -> Int -> IO apeekFpByteOffForeignPtr Word8srcIntnletz' :: Word8z'=Word8 -> Word8 -> Word8fWord8zWord8xForeignPtr b -> Int -> Word8 -> IO ()forall a b. Storable a => ForeignPtr b -> Int -> a -> IO ()pokeFpByteOffForeignPtr bdstIntnWord8z'Word8 -> Int -> IO ()scanl_Word8z'(IntnInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)ForeignPtr Word8 -> ForeignPtr Any -> IO ()forall {b}. ForeignPtr Word8 -> ForeignPtr b -> IO ()goForeignPtr Word8a(ForeignPtr Word8qForeignPtr Word8 -> Int -> ForeignPtr Anyforall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Int1){-# INLINEscanl#-}-- | 'scanl1' is a variant of 'scanl' that has no starting value argument.---- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]scanl1::(Word8->Word8->Word8)->ByteString->ByteStringscanl1 :: (Word8 -> Word8 -> Word8) -> ByteString -> ByteStringscanl1Word8 -> Word8 -> Word8fByteStringps=caseByteString -> Maybe (Word8, ByteString)unconsByteStringpsofMaybe (Word8, ByteString)Nothing->ByteStringemptyJust(Word8h,ByteStringt)->(Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> ByteStringscanlWord8 -> Word8 -> Word8fWord8hByteStringt{-# INLINEscanl1#-}-- | 'scanr' is similar to 'foldr', but returns a list of successive-- reduced values from the right.---- > scanr f z [..., x{n-1}, xn] == [..., x{n-1} `f` (xn `f` z), xn `f` z, z]---- Note that---- > head (scanr f z xs) == foldr f z xs-- > last (scanr f z xs) == z--scanr::(Word8->Word8->Word8)-- ^ element -> accumulator -> new accumulator->Word8-- ^ starting value of accumulator->ByteString-- ^ input of length n->ByteString-- ^ output of length n+1scanr :: (Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> ByteStringscanrWord8 -> Word8 -> Word8fWord8v=\(BSForeignPtr Word8aIntlen)->Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFp(FilePath -> Int -> Int -> IntcheckedAddFilePath"scanr"IntlenInt1)((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8b->do-- see fold inliningForeignPtr Word8 -> Int -> Word8 -> IO ()forall a b. Storable a => ForeignPtr b -> Int -> a -> IO ()pokeFpByteOffForeignPtr Word8bIntlenWord8vletgo :: ForeignPtr Word8 -> ForeignPtr b -> IO ()goForeignPtr Word8pForeignPtr bq=Word8 -> Int -> IO ()scanr_Word8v(IntlenInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)wherescanr_ :: Word8 -> Int -> IO ()scanr_!Word8z!Intn|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int0=() -> IO ()forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn()|Boolotherwise=doWord8x<-ForeignPtr Word8 -> Int -> IO Word8forall a. Storable a => ForeignPtr a -> Int -> IO apeekFpByteOffForeignPtr Word8pIntnletz' :: Word8z'=Word8 -> Word8 -> Word8fWord8xWord8zForeignPtr b -> Int -> Word8 -> IO ()forall a b. Storable a => ForeignPtr b -> Int -> a -> IO ()pokeFpByteOffForeignPtr bqIntnWord8z'Word8 -> Int -> IO ()scanr_Word8z'(IntnInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)ForeignPtr Word8 -> ForeignPtr Word8 -> IO ()forall {b}. ForeignPtr Word8 -> ForeignPtr b -> IO ()goForeignPtr Word8aForeignPtr Word8b{-# INLINEscanr#-}-- | 'scanr1' is a variant of 'scanr' that has no starting value argument.scanr1::(Word8->Word8->Word8)->ByteString->ByteStringscanr1 :: (Word8 -> Word8 -> Word8) -> ByteString -> ByteStringscanr1Word8 -> Word8 -> Word8fByteStringps=caseByteString -> Maybe (ByteString, Word8)unsnocByteStringpsofMaybe (ByteString, Word8)Nothing->ByteStringemptyJust(ByteStringb,Word8c)->(Word8 -> Word8 -> Word8) -> Word8 -> ByteString -> ByteStringscanrWord8 -> Word8 -> Word8fWord8cByteStringb{-# INLINEscanr1#-}-- ----------------------------------------------------------------------- Unfolds and replicates-- | /O(n)/ 'replicate' @n x@ is a ByteString of length @n@ with @x@-- the value of every element. The following holds:---- > replicate w c = fst (unfoldrN w (\u -> Just (u,u)) c)replicate::Int->Word8->ByteStringreplicate :: Int -> Word8 -> ByteStringreplicateIntwWord8c|IntwInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0=ByteStringempty|Boolotherwise=Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFpIntw((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8fptr->ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fptr((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Word8ptr->Ptr Word8 -> Word8 -> Int -> IO ()forall a. Ptr a -> Word8 -> Int -> IO ()fillBytesPtr Word8ptrWord8cIntw{-# INLINEreplicate#-}-- | /O(n)/, where /n/ is the length of the result.  The 'unfoldr'-- function is analogous to the List \'unfoldr\'.  'unfoldr' builds a-- ByteString from a seed value.  The function takes the element and-- returns 'Nothing' if it is done producing the ByteString or returns-- 'Just' @(a,b)@, in which case, @a@ is the next byte in the string,-- and @b@ is the seed value for further production.---- Examples:---- >    unfoldr (\x -> if x <= 5 then Just (x, x + 1) else Nothing) 0-- > == pack [0, 1, 2, 3, 4, 5]--unfoldr::(a->Maybe(Word8,a))->a->ByteStringunfoldr :: forall a. (a -> Maybe (Word8, a)) -> a -> ByteStringunfoldra -> Maybe (Word8, a)f=[ByteString] -> ByteStringconcat([ByteString] -> ByteString)-> (a -> [ByteString]) -> a -> ByteStringforall b c a. (b -> c) -> (a -> b) -> a -> c.Int -> Int -> a -> [ByteString]unfoldChunkInt32Int64whereunfoldChunk :: Int -> Int -> a -> [ByteString]unfoldChunkIntnIntn'ax=caseInt -> (a -> Maybe (Word8, a)) -> a -> (ByteString, Maybe a)forall a.Int -> (a -> Maybe (Word8, a)) -> a -> (ByteString, Maybe a)unfoldrNIntna -> Maybe (Word8, a)faxof(ByteStrings,Maybe aNothing)->[ByteStrings](ByteStrings,Justax')->ByteStringsByteString -> [ByteString] -> [ByteString]forall a. a -> [a] -> [a]:Int -> Int -> a -> [ByteString]unfoldChunkIntn'(IntnInt -> Int -> Intforall a. Num a => a -> a -> a+Intn')ax'{-# INLINEunfoldr#-}-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a ByteString from a seed-- value.  However, the length of the result is limited by the first-- argument to 'unfoldrN'.  This function is more efficient than 'unfoldr'-- when the maximum length of the result is known.---- The following equation relates 'unfoldrN' and 'unfoldr':---- > fst (unfoldrN n f s) == take n (unfoldr f s)--unfoldrN::Int->(a->Maybe(Word8,a))->a->(ByteString,Maybea)unfoldrN :: forall a.Int -> (a -> Maybe (Word8, a)) -> a -> (ByteString, Maybe a)unfoldrNIntia -> Maybe (Word8, a)fax0|IntiInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int0=(ByteStringempty,a -> Maybe aforall a. a -> Maybe aJustax0)|Boolotherwise=IO (ByteString, Maybe a) -> (ByteString, Maybe a)forall a. IO a -> aunsafeDupablePerformIO(IO (ByteString, Maybe a) -> (ByteString, Maybe a))-> IO (ByteString, Maybe a) -> (ByteString, Maybe a)forall a b. (a -> b) -> a -> b$Int-> (ForeignPtr Word8 -> IO (Int, Int, Maybe a))-> IO (ByteString, Maybe a)forall a.Int -> (ForeignPtr Word8 -> IO (Int, Int, a)) -> IO (ByteString, a)createFpAndTrim'Inti((ForeignPtr Word8 -> IO (Int, Int, Maybe a)) -> IO (ByteString, Maybe a))-> (ForeignPtr Word8 -> IO (Int, Int, Maybe a))-> IO (ByteString, Maybe a)forall a b. (a -> b) -> a -> b$\ForeignPtr Word8p->ForeignPtr Word8 -> a -> Int -> IO (Int, Int, Maybe a)forall {a} {b}.Num a =>ForeignPtr b -> a -> Int -> IO (a, Int, Maybe a)goForeignPtr Word8pax0Int0wherego :: ForeignPtr b -> a -> Int -> IO (a, Int, Maybe a)go!ForeignPtr bp!ax!Intn=a -> Int -> IO (a, Int, Maybe a)forall {a}. Num a => a -> Int -> IO (a, Int, Maybe a)go'axIntnwherego' :: a -> Int -> IO (a, Int, Maybe a)go'!ax'!Intn'|Intn'Int -> Int -> Boolforall a. Eq a => a -> a -> Bool==Inti=(a, Int, Maybe a) -> IO (a, Int, Maybe a)forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(a0,Intn',a -> Maybe aforall a. a -> Maybe aJustax')|Boolotherwise=casea -> Maybe (Word8, a)fax'ofMaybe (Word8, a)Nothing->(a, Int, Maybe a) -> IO (a, Int, Maybe a)forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(a0,Intn',Maybe aforall a. Maybe aNothing)Just(Word8w,ax'')->doForeignPtr b -> Int -> Word8 -> IO ()forall a b. Storable a => ForeignPtr b -> Int -> a -> IO ()pokeFpByteOffForeignPtr bpIntn'Word8wa -> Int -> IO (a, Int, Maybe a)go'ax''(Intn'Int -> Int -> Intforall a. Num a => a -> a -> a+Int1){-# INLINEunfoldrN#-}-- ----------------------------------------------------------------------- Substrings-- | /O(1)/ 'take' @n@, applied to a ByteString @xs@, returns the prefix-- of @xs@ of length @n@, or @xs@ itself if @n > 'length' xs@.take::Int->ByteString->ByteStringtake :: Int -> ByteString -> ByteStringtakeIntnps :: ByteStringps@(BSForeignPtr Word8xIntl)|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0=ByteStringempty|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intl=ByteStringps|Boolotherwise=ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8xIntn{-# INLINEtake#-}-- | /O(1)/ @'takeEnd' n xs@ is equivalent to @'drop' ('length' xs - n) xs@.-- Takes @n@ elements from end of bytestring.---- >>> takeEnd 3 "abcdefg"-- "efg"-- >>> takeEnd 0 "abcdefg"-- ""-- >>> takeEnd 4 "abc"-- "abc"---- @since 0.11.1.0takeEnd::Int->ByteString->ByteStringtakeEnd :: Int -> ByteString -> ByteStringtakeEndIntnps :: ByteStringps@(BSForeignPtr Word8xIntlen)|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intlen=ByteStringps|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0=ByteStringempty|Boolotherwise=ForeignPtr Word8 -> Int -> ByteStringBS(ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr bplusForeignPtrForeignPtr Word8x(IntlenInt -> Int -> Intforall a. Num a => a -> a -> a-Intn))Intn{-# INLINEtakeEnd#-}-- | /O(1)/ 'drop' @n xs@ returns the suffix of @xs@ after the first @n@-- elements, or 'empty' if @n > 'length' xs@.drop::Int->ByteString->ByteStringdrop :: Int -> ByteString -> ByteStringdropIntnps :: ByteStringps@(BSForeignPtr Word8xIntl)|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0=ByteStringps|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intl=ByteStringempty|Boolotherwise=ForeignPtr Word8 -> Int -> ByteStringBS(ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr bplusForeignPtrForeignPtr Word8xIntn)(IntlInt -> Int -> Intforall a. Num a => a -> a -> a-Intn){-# INLINEdrop#-}-- | /O(1)/ @'dropEnd' n xs@ is equivalent to @'take' ('length' xs - n) xs@.-- Drops @n@ elements from end of bytestring.---- >>> dropEnd 3 "abcdefg"-- "abcd"-- >>> dropEnd 0 "abcdefg"-- "abcdefg"-- >>> dropEnd 4 "abc"-- ""---- @since 0.11.1.0dropEnd::Int->ByteString->ByteStringdropEnd :: Int -> ByteString -> ByteStringdropEndIntnps :: ByteStringps@(BSForeignPtr Word8xIntlen)|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0=ByteStringps|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intlen=ByteStringempty|Boolotherwise=ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8x(IntlenInt -> Int -> Intforall a. Num a => a -> a -> a-Intn){-# INLINEdropEnd#-}-- | /O(1)/ 'splitAt' @n xs@ is equivalent to @('take' n xs, 'drop' n xs)@.splitAt::Int->ByteString->(ByteString,ByteString)splitAt :: Int -> ByteString -> (ByteString, ByteString)splitAtIntnps :: ByteStringps@(BSForeignPtr Word8xIntl)|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0=(ByteStringempty,ByteStringps)|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intl=(ByteStringps,ByteStringempty)|Boolotherwise=(ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8xIntn,ForeignPtr Word8 -> Int -> ByteStringBS(ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr bplusForeignPtrForeignPtr Word8xIntn)(IntlInt -> Int -> Intforall a. Num a => a -> a -> a-Intn)){-# INLINEsplitAt#-}-- | Similar to 'Prelude.takeWhile',-- returns the longest (possibly empty) prefix of elements-- satisfying the predicate.takeWhile::(Word8->Bool)->ByteString->ByteStringtakeWhile :: (Word8 -> Bool) -> ByteString -> ByteStringtakeWhileWord8 -> BoolfByteStringps=Int -> ByteString -> ByteStringunsafeTake((Word8 -> Bool) -> ByteString -> IntfindIndexOrLength(Bool -> Boolnot(Bool -> Bool) -> (Word8 -> Bool) -> Word8 -> Boolforall b c a. (b -> c) -> (a -> b) -> a -> c.Word8 -> Boolf)ByteStringps)ByteStringps{-# INLINE[1]takeWhile#-}{-# RULES"ByteString specialise takeWhile (x /=)"forallx.takeWhile(x`neWord8`)=fst.breakBytex"ByteString specialise takeWhile (/= x)"forallx.takeWhile(`neWord8`x)=fst.breakBytex"ByteString specialise takeWhile (x ==)"forallx.takeWhile(x`eqWord8`)=fst.spanBytex"ByteString specialise takeWhile (== x)"forallx.takeWhile(`eqWord8`x)=fst.spanBytex#-}-- | Returns the longest (possibly empty) suffix of elements-- satisfying the predicate.---- @'takeWhileEnd' p@ is equivalent to @'reverse' . 'takeWhile' p . 'reverse'@.---- @since 0.10.12.0takeWhileEnd::(Word8->Bool)->ByteString->ByteStringtakeWhileEnd :: (Word8 -> Bool) -> ByteString -> ByteStringtakeWhileEndWord8 -> BoolfByteStringps=Int -> ByteString -> ByteStringunsafeDrop((Word8 -> Bool) -> ByteString -> IntfindFromEndUntil(Bool -> Boolnot(Bool -> Bool) -> (Word8 -> Bool) -> Word8 -> Boolforall b c a. (b -> c) -> (a -> b) -> a -> c.Word8 -> Boolf)ByteStringps)ByteStringps{-# INLINEtakeWhileEnd#-}-- | Similar to 'Prelude.dropWhile',-- drops the longest (possibly empty) prefix of elements-- satisfying the predicate and returns the remainder.dropWhile::(Word8->Bool)->ByteString->ByteStringdropWhile :: (Word8 -> Bool) -> ByteString -> ByteStringdropWhileWord8 -> BoolfByteStringps=Int -> ByteString -> ByteStringunsafeDrop((Word8 -> Bool) -> ByteString -> IntfindIndexOrLength(Bool -> Boolnot(Bool -> Bool) -> (Word8 -> Bool) -> Word8 -> Boolforall b c a. (b -> c) -> (a -> b) -> a -> c.Word8 -> Boolf)ByteStringps)ByteStringps{-# INLINE[1]dropWhile#-}{-# RULES"ByteString specialise dropWhile (x /=)"forallx.dropWhile(x`neWord8`)=snd.breakBytex"ByteString specialise dropWhile (/= x)"forallx.dropWhile(`neWord8`x)=snd.breakBytex"ByteString specialise dropWhile (x ==)"forallx.dropWhile(x`eqWord8`)=snd.spanBytex"ByteString specialise dropWhile (== x)"forallx.dropWhile(`eqWord8`x)=snd.spanBytex#-}-- | Similar to 'Prelude.dropWhileEnd',-- drops the longest (possibly empty) suffix of elements-- satisfying the predicate and returns the remainder.---- @'dropWhileEnd' p@ is equivalent to @'reverse' . 'dropWhile' p . 'reverse'@.---- @since 0.10.12.0dropWhileEnd::(Word8->Bool)->ByteString->ByteStringdropWhileEnd :: (Word8 -> Bool) -> ByteString -> ByteStringdropWhileEndWord8 -> BoolfByteStringps=Int -> ByteString -> ByteStringunsafeTake((Word8 -> Bool) -> ByteString -> IntfindFromEndUntil(Bool -> Boolnot(Bool -> Bool) -> (Word8 -> Bool) -> Word8 -> Boolforall b c a. (b -> c) -> (a -> b) -> a -> c.Word8 -> Boolf)ByteStringps)ByteStringps{-# INLINEdropWhileEnd#-}-- | Similar to 'Prelude.break',-- returns the longest (possibly empty) prefix of elements which __do not__-- satisfy the predicate and the remainder of the string.---- 'break' @p@ is equivalent to @'span' (not . p)@ and to @('takeWhile' (not . p) &&& 'dropWhile' (not . p))@.---- Under GHC, a rewrite rule will transform break (==) into a-- call to the specialised breakByte:---- > break ((==) x) = breakByte x-- > break (==x) = breakByte x--break::(Word8->Bool)->ByteString->(ByteString,ByteString)break :: (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)breakWord8 -> BoolpByteStringps=case(Word8 -> Bool) -> ByteString -> IntfindIndexOrLengthWord8 -> BoolpByteStringpsofIntn->(Int -> ByteString -> ByteStringunsafeTakeIntnByteStringps,Int -> ByteString -> ByteStringunsafeDropIntnByteStringps){-# INLINE[1]break#-}-- See bytestring #70{-# RULES"ByteString specialise break (x ==)"forallx.break(x`eqWord8`)=breakBytex"ByteString specialise break (== x)"forallx.break(`eqWord8`x)=breakBytex#-}-- INTERNAL:-- | 'breakByte' breaks its ByteString argument at the first occurrence-- of the specified byte. It is more efficient than 'break' as it is-- implemented with @memchr(3)@. I.e.---- > break (==99) "abcd" == breakByte 99 "abcd" -- fromEnum 'c' == 99--breakByte::Word8->ByteString->(ByteString,ByteString)breakByte :: Word8 -> ByteString -> (ByteString, ByteString)breakByteWord8cByteStringp=caseWord8 -> ByteString -> Maybe IntelemIndexWord8cByteStringpofMaybe IntNothing->(ByteStringp,ByteStringempty)JustIntn->(Int -> ByteString -> ByteStringunsafeTakeIntnByteStringp,Int -> ByteString -> ByteStringunsafeDropIntnByteStringp){-# INLINEbreakByte#-}-- | Returns the longest (possibly empty) suffix of elements which __do not__-- satisfy the predicate and the remainder of the string.---- 'breakEnd' @p@ is equivalent to @'spanEnd' (not . p)@ and to @('dropWhileEnd' (not . p) &&& 'takeWhileEnd' (not . p))@.--breakEnd::(Word8->Bool)->ByteString->(ByteString,ByteString)breakEnd :: (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)breakEndWord8 -> BoolpByteStringps=Int -> ByteString -> (ByteString, ByteString)splitAt((Word8 -> Bool) -> ByteString -> IntfindFromEndUntilWord8 -> BoolpByteStringps)ByteStringps-- | Similar to 'Prelude.span',-- returns the longest (possibly empty) prefix of elements-- satisfying the predicate and the remainder of the string.---- 'span' @p@ is equivalent to @'break' (not . p)@ and to @('takeWhile' p &&& 'dropWhile' p)@.--span::(Word8->Bool)->ByteString->(ByteString,ByteString)span :: (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)spanWord8 -> Boolp=(Word8 -> Bool) -> ByteString -> (ByteString, ByteString)break(Bool -> Boolnot(Bool -> Bool) -> (Word8 -> Bool) -> Word8 -> Boolforall b c a. (b -> c) -> (a -> b) -> a -> c.Word8 -> Boolp){-# INLINE[1]span#-}-- | 'spanByte' breaks its ByteString argument at the first-- occurrence of a byte other than its argument. It is more efficient-- than 'span (==)'---- > span  (==99) "abcd" == spanByte 99 "abcd" -- fromEnum 'c' == 99--spanByte::Word8->ByteString->(ByteString,ByteString)spanByte :: Word8 -> ByteString -> (ByteString, ByteString)spanByteWord8cps :: ByteStringps@(BSForeignPtr Word8xIntl)=IO (ByteString, ByteString) -> (ByteString, ByteString)forall a. IO a -> aaccursedUnutterablePerformIO(IO (ByteString, ByteString) -> (ByteString, ByteString))-> IO (ByteString, ByteString) -> (ByteString, ByteString)forall a b. (a -> b) -> a -> b$ForeignPtr Word8-> (Ptr Word8 -> IO (ByteString, ByteString))-> IO (ByteString, ByteString)forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8xPtr Word8 -> IO (ByteString, ByteString)forall {b}. Ptr b -> IO (ByteString, ByteString)gwhereg :: Ptr b -> IO (ByteString, ByteString)gPtr bp=Int -> IO (ByteString, ByteString)goInt0wherego :: Int -> IO (ByteString, ByteString)go!Inti|IntiInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intl=(ByteString, ByteString) -> IO (ByteString, ByteString)forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(ByteStringps,ByteStringempty)|Boolotherwise=doWord8c'<-Ptr b -> Int -> IO Word8forall b. Ptr b -> Int -> IO Word8forall a b. Storable a => Ptr b -> Int -> IO apeekByteOffPtr bpIntiifWord8cWord8 -> Word8 -> Boolforall a. Eq a => a -> a -> Bool/=Word8c'then(ByteString, ByteString) -> IO (ByteString, ByteString)forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(Int -> ByteString -> ByteStringunsafeTakeIntiByteStringps,Int -> ByteString -> ByteStringunsafeDropIntiByteStringps)elseInt -> IO (ByteString, ByteString)go(IntiInt -> Int -> Intforall a. Num a => a -> a -> a+Int1){-# INLINEspanByte#-}-- See bytestring #70{-# RULES"ByteString specialise span (x ==)"forallx.span(x`eqWord8`)=spanBytex"ByteString specialise span (== x)"forallx.span(`eqWord8`x)=spanBytex#-}-- | Returns the longest (possibly empty) suffix of elements-- satisfying the predicate and the remainder of the string.---- 'spanEnd' @p@ is equivalent to @'breakEnd' (not . p)@ and to @('dropWhileEnd' p &&& 'takeWhileEnd' p)@.---- We have---- > spanEnd (not . isSpace) "x y z" == ("x y ", "z")---- and---- > spanEnd (not . isSpace) ps-- >    ==-- > let (x, y) = span (not . isSpace) (reverse ps) in (reverse y, reverse x)--spanEnd::(Word8->Bool)->ByteString->(ByteString,ByteString)spanEnd :: (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)spanEndWord8 -> BoolpByteStringps=Int -> ByteString -> (ByteString, ByteString)splitAt((Word8 -> Bool) -> ByteString -> IntfindFromEndUntil(Bool -> Boolnot(Bool -> Bool) -> (Word8 -> Bool) -> Word8 -> Boolforall b c a. (b -> c) -> (a -> b) -> a -> c.Word8 -> Boolp)ByteStringps)ByteStringps-- | /O(n)/ Splits a 'ByteString' into components delimited by-- separators, where the predicate returns True for a separator element.-- The resulting components do not contain the separators.  Two adjacent-- separators result in an empty component in the output.  eg.---- > splitWith (==97) "aabbaca" == ["","","bb","c",""] -- fromEnum 'a' == 97-- > splitWith undefined ""     == []                  -- and not [""]--splitWith::(Word8->Bool)->ByteString->[ByteString]splitWith :: (Word8 -> Bool) -> ByteString -> [ByteString]splitWithWord8 -> Bool_(BSForeignPtr Word8_Int0)=[]splitWithWord8 -> Boolpredicate(BSForeignPtr Word8fpIntlen)=Int -> Int -> ForeignPtr Word8 -> [ByteString]splitWith0Int0IntlenForeignPtr Word8fpwheresplitWith0 :: Int -> Int -> ForeignPtr Word8 -> [ByteString]splitWith0!Intoff'!Intlen'!ForeignPtr Word8fp'=IO [ByteString] -> [ByteString]forall a. IO a -> aaccursedUnutterablePerformIO(IO [ByteString] -> [ByteString])-> IO [ByteString] -> [ByteString]forall a b. (a -> b) -> a -> b$ForeignPtr Word8-> Int -> Int -> Int -> ForeignPtr Word8 -> IO [ByteString]splitLoopForeignPtr Word8fpInt0Intoff'Intlen'ForeignPtr Word8fp'splitLoop::ForeignPtrWord8->Int->Int->Int->ForeignPtrWord8->IO[ByteString]splitLoop :: ForeignPtr Word8-> Int -> Int -> Int -> ForeignPtr Word8 -> IO [ByteString]splitLoopForeignPtr Word8pIntidx2Intoff'Intlen'ForeignPtr Word8fp'=Int -> IO [ByteString]goIntidx2wherego :: Int -> IO [ByteString]goIntidx'|Intidx'Int -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intlen'=[ByteString] -> IO [ByteString]forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn[ForeignPtr Word8 -> Int -> ByteStringBS(ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr bplusForeignPtrForeignPtr Word8fp'Intoff')Intidx']|Boolotherwise=doWord8w<-ForeignPtr Word8 -> Int -> IO Word8forall a. Storable a => ForeignPtr a -> Int -> IO apeekFpByteOffForeignPtr Word8p(Intoff'Int -> Int -> Intforall a. Num a => a -> a -> a+Intidx')ifWord8 -> BoolpredicateWord8wthen[ByteString] -> IO [ByteString]forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(ForeignPtr Word8 -> Int -> ByteStringBS(ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr bplusForeignPtrForeignPtr Word8fp'Intoff')Intidx'ByteString -> [ByteString] -> [ByteString]forall a. a -> [a] -> [a]:Int -> Int -> ForeignPtr Word8 -> [ByteString]splitWith0(Intoff'Int -> Int -> Intforall a. Num a => a -> a -> a+Intidx'Int -> Int -> Intforall a. Num a => a -> a -> a+Int1)(Intlen'Int -> Int -> Intforall a. Num a => a -> a -> a-Intidx'Int -> Int -> Intforall a. Num a => a -> a -> a-Int1)ForeignPtr Word8fp')elseInt -> IO [ByteString]go(Intidx'Int -> Int -> Intforall a. Num a => a -> a -> a+Int1){-# INLINEsplitWith#-}-- | /O(n)/ Break a 'ByteString' into pieces separated by the byte-- argument, consuming the delimiter. I.e.---- > split 10  "a\nb\nd\ne" == ["a","b","d","e"]   -- fromEnum '\n' == 10-- > split 97  "aXaXaXa"    == ["","X","X","X",""] -- fromEnum 'a' == 97-- > split 120 "x"          == ["",""]             -- fromEnum 'x' == 120-- > split undefined ""     == []                  -- and not [""]---- and---- > intercalate [c] . split c == id-- > split == splitWith . (==)---- As for all splitting functions in this library, this function does-- not copy the substrings, it just constructs new 'ByteString's that-- are slices of the original.--split::Word8->ByteString->[ByteString]split :: Word8 -> ByteString -> [ByteString]splitWord8_(BSForeignPtr Word8_Int0)=[]splitWord8w(BSForeignPtr Word8xIntl)=Int -> [ByteString]loopInt0whereloop :: Int -> [ByteString]loop!Intn=letq :: Ptr Word8q=IO (Ptr Word8) -> Ptr Word8forall a. IO a -> aaccursedUnutterablePerformIO(IO (Ptr Word8) -> Ptr Word8) -> IO (Ptr Word8) -> Ptr Word8forall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO (Ptr Word8)) -> IO (Ptr Word8)forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8x((Ptr Word8 -> IO (Ptr Word8)) -> IO (Ptr Word8))-> (Ptr Word8 -> IO (Ptr Word8)) -> IO (Ptr Word8)forall a b. (a -> b) -> a -> b$\Ptr Word8p->Ptr Word8 -> Word8 -> CSize -> IO (Ptr Word8)memchr(Ptr Word8pPtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`Intn)Word8w(Int -> CSizeforall a b. (Integral a, Num b) => a -> bfromIntegral(IntlInt -> Int -> Intforall a. Num a => a -> a -> a-Intn))inifPtr Word8qPtr Word8 -> Ptr Word8 -> Boolforall a. Eq a => a -> a -> Bool==Ptr Word8forall {b}. Ptr bnullPtrthen[ForeignPtr Word8 -> Int -> ByteStringBS(ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr bplusForeignPtrForeignPtr Word8xIntn)(IntlInt -> Int -> Intforall a. Num a => a -> a -> a-Intn)]elseleti :: Inti=Ptr Word8qPtr Word8 -> Ptr Word8 -> Intforall a b. Ptr a -> Ptr b -> Int`minusPtr`ForeignPtr Word8 -> Ptr Word8forall a. ForeignPtr a -> Ptr aunsafeForeignPtrToPtrForeignPtr Word8xinForeignPtr Word8 -> Int -> ByteStringBS(ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr bplusForeignPtrForeignPtr Word8xIntn)(IntiInt -> Int -> Intforall a. Num a => a -> a -> a-Intn)ByteString -> [ByteString] -> [ByteString]forall a. a -> [a] -> [a]:Int -> [ByteString]loop(IntiInt -> Int -> Intforall a. Num a => a -> a -> a+Int1){-# INLINEsplit#-}-- | The 'group' function takes a ByteString and returns a list of-- ByteStrings such that the concatenation of the result is equal to the-- argument.  Moreover, each string in the result contains only equal-- elements.  For example,---- > group "Mississippi" = ["M","i","ss","i","ss","i","pp","i"]---- It is a special case of 'groupBy', which allows the programmer to-- supply their own equality test. It is about 40% faster than-- /groupBy (==)/group::ByteString->[ByteString]group :: ByteString -> [ByteString]groupByteStringxs=caseByteString -> Maybe (Word8, ByteString)unconsByteStringxsofMaybe (Word8, ByteString)Nothing->[]Just(Word8h,ByteString_)->ByteStringysByteString -> [ByteString] -> [ByteString]forall a. a -> [a] -> [a]:ByteString -> [ByteString]groupByteStringzswhere(ByteStringys,ByteStringzs)=Word8 -> ByteString -> (ByteString, ByteString)spanByteWord8hByteStringxs-- | The 'groupBy' function is the non-overloaded version of 'group'.groupBy::(Word8->Word8->Bool)->ByteString->[ByteString]groupBy :: (Word8 -> Word8 -> Bool) -> ByteString -> [ByteString]groupByWord8 -> Word8 -> BoolkByteStringxs=caseByteString -> Maybe (Word8, ByteString)unconsByteStringxsofMaybe (Word8, ByteString)Nothing->[]Just(Word8h,ByteStringt)->Int -> ByteString -> ByteStringunsafeTakeIntnByteStringxsByteString -> [ByteString] -> [ByteString]forall a. a -> [a] -> [a]:(Word8 -> Word8 -> Bool) -> ByteString -> [ByteString]groupByWord8 -> Word8 -> Boolk(Int -> ByteString -> ByteStringunsafeDropIntnByteStringxs)wheren :: Intn=Int1Int -> Int -> Intforall a. Num a => a -> a -> a+(Word8 -> Bool) -> ByteString -> IntfindIndexOrLength(Bool -> Boolnot(Bool -> Bool) -> (Word8 -> Bool) -> Word8 -> Boolforall b c a. (b -> c) -> (a -> b) -> a -> c.Word8 -> Word8 -> BoolkWord8h)ByteStringt-- | /O(n)/ The 'intercalate' function takes a 'ByteString' and a list of-- 'ByteString's and concatenates the list after interspersing the first-- argument between each element of the list.intercalate::ByteString->[ByteString]->ByteStringintercalate :: ByteString -> [ByteString] -> ByteStringintercalateByteString_[]=ByteStringforall a. Monoid a => amemptyintercalateByteString_[ByteStringx]=ByteStringx-- This branch exists for laziness, not speedintercalate(BSForeignPtr Word8sepPtrIntsepLen)(BSForeignPtr Word8hPtrInthLen:[ByteString]t)=Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFpInttotalLen((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8dstPtr0->doForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()memcpyFpForeignPtr Word8dstPtr0ForeignPtr Word8hPtrInthLenletgo :: ForeignPtr Word8 -> [ByteString] -> IO ()goForeignPtr Word8_[]=() -> IO ()forall a. a -> IO aforall (f :: * -> *) a. Applicative f => a -> f apure()goForeignPtr Word8dstPtr(BSForeignPtr Word8chunkPtrIntchunkLen:[ByteString]chunks)=doForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()memcpyFpForeignPtr Word8dstPtrForeignPtr Word8sepPtrIntsepLenletdestPtr' :: ForeignPtr bdestPtr'=ForeignPtr Word8dstPtrForeignPtr Word8 -> Int -> ForeignPtr bforall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`IntsepLenForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()memcpyFpForeignPtr Word8forall {b}. ForeignPtr bdestPtr'ForeignPtr Word8chunkPtrIntchunkLenForeignPtr Word8 -> [ByteString] -> IO ()go(ForeignPtr Anyforall {b}. ForeignPtr bdestPtr'ForeignPtr Any -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`IntchunkLen)[ByteString]chunksForeignPtr Word8 -> [ByteString] -> IO ()go(ForeignPtr Word8dstPtr0ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`InthLen)[ByteString]twheretotalLen :: InttotalLen=(Int -> ByteString -> Int) -> Int -> [ByteString] -> Intforall b a. (b -> a -> b) -> b -> [a] -> bforall (t :: * -> *) b a.Foldable t =>(b -> a -> b) -> b -> t a -> bList.foldl'(\IntaccByteStringchunk->IntaccInt -> Int -> Int+!IntsepLenInt -> Int -> Int+!ByteString -> IntlengthByteStringchunk)InthLen[ByteString]t+! :: Int -> Int -> Int(+!)=FilePath -> Int -> Int -> IntcheckedAddFilePath"intercalate"{-# INLINABLEintercalate#-}-- ----------------------------------------------------------------------- Indexing ByteStrings-- | /O(1)/ 'ByteString' index (subscript) operator, starting from 0.---- This is a partial function, consider using 'indexMaybe' instead.index::HasCallStack=>ByteString->Int->Word8index :: HasCallStack => ByteString -> Int -> Word8indexByteStringpsIntn|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int0=FilePath -> FilePath -> Word8forall a. HasCallStack => FilePath -> FilePath -> amoduleErrorFilePath"index"(FilePath"negative index: "FilePath -> FilePath -> FilePathforall a. [a] -> [a] -> [a]++Int -> FilePathforall a. Show a => a -> FilePathshowIntn)|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=ByteString -> IntlengthByteStringps=FilePath -> FilePath -> Word8forall a. HasCallStack => FilePath -> FilePath -> amoduleErrorFilePath"index"(FilePath"index too large: "FilePath -> FilePath -> FilePathforall a. [a] -> [a] -> [a]++Int -> FilePathforall a. Show a => a -> FilePathshowIntnFilePath -> FilePath -> FilePathforall a. [a] -> [a] -> [a]++FilePath", length = "FilePath -> FilePath -> FilePathforall a. [a] -> [a] -> [a]++Int -> FilePathforall a. Show a => a -> FilePathshow(ByteString -> IntlengthByteStringps))|Boolotherwise=ByteStringpsByteString -> Int -> Word8`unsafeIndex`Intn{-# INLINEindex#-}-- | /O(1)/ 'ByteString' index, starting from 0, that returns 'Just' if:---- > 0 <= n < length bs---- @since 0.11.0.0indexMaybe::ByteString->Int->MaybeWord8indexMaybe :: ByteString -> Int -> Maybe Word8indexMaybeByteStringpsIntn|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int0=Maybe Word8forall a. Maybe aNothing|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=ByteString -> IntlengthByteStringps=Maybe Word8forall a. Maybe aNothing|Boolotherwise=Word8 -> Maybe Word8forall a. a -> Maybe aJust(Word8 -> Maybe Word8) -> Word8 -> Maybe Word8forall a b. (a -> b) -> a -> b$!ByteStringpsByteString -> Int -> Word8`unsafeIndex`Intn{-# INLINEindexMaybe#-}-- | /O(1)/ 'ByteString' index, starting from 0, that returns 'Just' if:---- > 0 <= n < length bs---- @since 0.11.0.0(!?)::ByteString->Int->MaybeWord8!? :: ByteString -> Int -> Maybe Word8(!?)=ByteString -> Int -> Maybe Word8indexMaybe{-# 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.-- This implementation uses memchr(3).elemIndex::Word8->ByteString->MaybeIntelemIndex :: Word8 -> ByteString -> Maybe IntelemIndexWord8c(BSForeignPtr Word8xIntl)=IO (Maybe Int) -> Maybe Intforall a. IO a -> aaccursedUnutterablePerformIO(IO (Maybe Int) -> Maybe Int) -> IO (Maybe Int) -> Maybe Intforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO (Maybe Int)) -> IO (Maybe Int)forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8x((Ptr Word8 -> IO (Maybe Int)) -> IO (Maybe Int))-> (Ptr Word8 -> IO (Maybe Int)) -> IO (Maybe Int)forall a b. (a -> b) -> a -> b$\Ptr Word8p->doPtr Word8q<-Ptr Word8 -> Word8 -> CSize -> IO (Ptr Word8)memchrPtr Word8pWord8c(Int -> CSizeforall a b. (Integral a, Num b) => a -> bfromIntegralIntl)Maybe Int -> IO (Maybe Int)forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(Maybe Int -> IO (Maybe Int)) -> Maybe Int -> IO (Maybe Int)forall a b. (a -> b) -> a -> b$!ifPtr Word8qPtr Word8 -> Ptr Word8 -> Boolforall a. Eq a => a -> a -> Bool==Ptr Word8forall {b}. Ptr bnullPtrthenMaybe Intforall a. Maybe aNothingelseInt -> Maybe Intforall a. a -> Maybe aJust(Int -> Maybe Int) -> Int -> Maybe Intforall a b. (a -> b) -> a -> b$!Ptr Word8qPtr Word8 -> Ptr Word8 -> Intforall a b. Ptr a -> Ptr b -> Int`minusPtr`Ptr Word8p{-# INLINEelemIndex#-}-- | /O(n)/ The 'elemIndexEnd' function returns the last index of the-- element in the given 'ByteString' which is equal to the query-- element, or 'Nothing' if there is no such element. The following-- holds:---- > elemIndexEnd c xs = case elemIndex c (reverse xs) of-- >   Nothing -> Nothing-- >   Just i  -> Just (length xs - 1 - i)--elemIndexEnd::Word8->ByteString->MaybeIntelemIndexEnd :: Word8 -> ByteString -> Maybe IntelemIndexEnd=(Word8 -> Bool) -> ByteString -> Maybe IntfindIndexEnd((Word8 -> Bool) -> ByteString -> Maybe Int)-> (Word8 -> Word8 -> Bool) -> Word8 -> ByteString -> Maybe Intforall b c a. (b -> c) -> (a -> b) -> a -> c.Word8 -> Word8 -> Boolforall a. Eq a => a -> a -> Bool(==){-# INLINEelemIndexEnd#-}-- | /O(n)/ The 'elemIndices' function extends 'elemIndex', by returning-- the indices of all elements equal to the query element, in ascending order.-- This implementation uses memchr(3).elemIndices::Word8->ByteString->[Int]elemIndices :: Word8 -> ByteString -> [Int]elemIndicesWord8w(BSForeignPtr Word8xIntl)=Int -> [Int]loopInt0whereloop :: Int -> [Int]loop!Intn=IO [Int] -> [Int]forall a. IO a -> aaccursedUnutterablePerformIO(IO [Int] -> [Int]) -> IO [Int] -> [Int]forall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO [Int]) -> IO [Int]forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8x((Ptr Word8 -> IO [Int]) -> IO [Int])-> (Ptr Word8 -> IO [Int]) -> IO [Int]forall a b. (a -> b) -> a -> b$\Ptr Word8p->doPtr Word8q<-Ptr Word8 -> Word8 -> CSize -> IO (Ptr Word8)memchr(Ptr Word8pPtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`Intn)Word8w(Int -> CSizeforall a b. (Integral a, Num b) => a -> bfromIntegral(IntlInt -> Int -> Intforall a. Num a => a -> a -> a-Intn))ifPtr Word8qPtr Word8 -> Ptr Word8 -> Boolforall a. Eq a => a -> a -> Bool==Ptr Word8forall {b}. Ptr bnullPtrthen[Int] -> IO [Int]forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn[]elselet!i :: Inti=Ptr Word8qPtr Word8 -> Ptr Word8 -> Intforall a b. Ptr a -> Ptr b -> Int`minusPtr`Ptr Word8pin[Int] -> IO [Int]forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn([Int] -> IO [Int]) -> [Int] -> IO [Int]forall a b. (a -> b) -> a -> b$IntiInt -> [Int] -> [Int]forall a. a -> [a] -> [a]:Int -> [Int]loop(IntiInt -> Int -> Intforall a. Num a => a -> a -> a+Int1){-# INLINEelemIndices#-}-- | count returns the number of times its argument appears in the ByteString---- > count = length . elemIndices---- But more efficiently than using length on the intermediate list.count::Word8->ByteString->Intcount :: Word8 -> ByteString -> IntcountWord8w(BSForeignPtr Word8xIntm)=IO Int -> Intforall a. IO a -> aaccursedUnutterablePerformIO(IO Int -> Int) -> IO Int -> Intforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO Int) -> IO Intforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8x((Ptr Word8 -> IO Int) -> IO Int)-> (Ptr Word8 -> IO Int) -> IO Intforall a b. (a -> b) -> a -> b$\Ptr Word8p->CSize -> Intforall a b. (Integral a, Num b) => a -> bfromIntegral(CSize -> Int) -> IO CSize -> IO Intforall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b<$>Ptr Word8 -> CSize -> Word8 -> IO CSizec_countPtr Word8p(Int -> CSizeforall a b. (Integral a, Num b) => a -> bfromIntegralIntm)Word8w{-# INLINEcount#-}-- | /O(n)/ The 'findIndex' function takes a predicate and a 'ByteString' and-- returns the index of the first element in the ByteString-- satisfying the predicate.findIndex::(Word8->Bool)->ByteString->MaybeIntfindIndex :: (Word8 -> Bool) -> ByteString -> Maybe IntfindIndexWord8 -> Boolk(BSForeignPtr Word8xIntl)=IO (Maybe Int) -> Maybe Intforall a. IO a -> aaccursedUnutterablePerformIO(IO (Maybe Int) -> Maybe Int) -> IO (Maybe Int) -> Maybe Intforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> IO (Maybe Int)forall {a}. ForeignPtr a -> IO (Maybe Int)gForeignPtr Word8xwhereg :: ForeignPtr a -> IO (Maybe Int)g!ForeignPtr aptr=Int -> IO (Maybe Int)goInt0wherego :: Int -> IO (Maybe Int)go!Intn|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intl=Maybe Int -> IO (Maybe Int)forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnMaybe Intforall a. Maybe aNothing|Boolotherwise=doWord8w<-ForeignPtr Word8 -> IO Word8forall a. Storable a => ForeignPtr a -> IO apeekFp(ForeignPtr Word8 -> IO Word8) -> ForeignPtr Word8 -> IO Word8forall a b. (a -> b) -> a -> b$ForeignPtr aptrForeignPtr a -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`IntnifWord8 -> BoolkWord8wthenMaybe Int -> IO (Maybe Int)forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(Int -> Maybe Intforall a. a -> Maybe aJustIntn)elseInt -> IO (Maybe Int)go(IntnInt -> Int -> Intforall a. Num a => a -> a -> a+Int1){-# INLINE[1]findIndex#-}-- | /O(n)/ The 'findIndexEnd' function takes a predicate and a 'ByteString' and-- returns the index of the last element in the ByteString-- satisfying the predicate.---- @since 0.10.12.0findIndexEnd::(Word8->Bool)->ByteString->MaybeIntfindIndexEnd :: (Word8 -> Bool) -> ByteString -> Maybe IntfindIndexEndWord8 -> Boolk(BSForeignPtr Word8xIntl)=IO (Maybe Int) -> Maybe Intforall a. IO a -> aaccursedUnutterablePerformIO(IO (Maybe Int) -> Maybe Int) -> IO (Maybe Int) -> Maybe Intforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> IO (Maybe Int)gForeignPtr Word8xwhereg :: ForeignPtr Word8 -> IO (Maybe Int)g!ForeignPtr Word8ptr=Int -> IO (Maybe Int)go(IntlInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)wherego :: Int -> IO (Maybe Int)go!Intn|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int0=Maybe Int -> IO (Maybe Int)forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnMaybe Intforall a. Maybe aNothing|Boolotherwise=doWord8w<-ForeignPtr Word8 -> Int -> IO Word8forall a. Storable a => ForeignPtr a -> Int -> IO apeekFpByteOffForeignPtr Word8ptrIntnifWord8 -> BoolkWord8wthenMaybe Int -> IO (Maybe Int)forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(Int -> Maybe Intforall a. a -> Maybe aJustIntn)elseInt -> IO (Maybe Int)go(IntnInt -> Int -> Intforall a. Num a => a -> a -> a-Int1){-# INLINEfindIndexEnd#-}-- | /O(n)/ The 'findIndices' function extends 'findIndex', by returning the-- indices of all elements satisfying the predicate, in ascending order.findIndices::(Word8->Bool)->ByteString->[Int]findIndices :: (Word8 -> Bool) -> ByteString -> [Int]findIndicesWord8 -> Boolp=Int -> ByteString -> [Int]loopInt0whereloop :: Int -> ByteString -> [Int]loop!Intn!ByteStringqs=case(Word8 -> Bool) -> ByteString -> Maybe IntfindIndexWord8 -> BoolpByteStringqsofJust!Inti->let!j :: Intj=IntnInt -> Int -> Intforall a. Num a => a -> a -> a+IntiinIntjInt -> [Int] -> [Int]forall a. a -> [a] -> [a]:Int -> ByteString -> [Int]loop(IntjInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)(Int -> ByteString -> ByteStringunsafeDrop(IntiInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)ByteStringqs)Maybe IntNothing->[]{-# INLINE[1]findIndices#-}{-# RULES"ByteString specialise findIndex (x ==)"forallx.findIndex(x`eqWord8`)=elemIndexx"ByteString specialise findIndex (== x)"forallx.findIndex(`eqWord8`x)=elemIndexx"ByteString specialise findIndices (x ==)"forallx.findIndices(x`eqWord8`)=elemIndicesx"ByteString specialise findIndices (== x)"forallx.findIndices(`eqWord8`x)=elemIndicesx#-}-- ----------------------------------------------------------------------- Searching ByteStrings-- | /O(n)/ 'elem' is the 'ByteString' membership predicate.elem::Word8->ByteString->Boolelem :: Word8 -> ByteString -> BoolelemWord8cByteStringps=caseWord8 -> ByteString -> Maybe IntelemIndexWord8cByteStringpsofMaybe IntNothing->BoolFalse;Maybe Int_->BoolTrue{-# INLINEelem#-}-- | /O(n)/ 'notElem' is the inverse of 'elem'notElem::Word8->ByteString->BoolnotElem :: Word8 -> ByteString -> BoolnotElemWord8cByteStringps=Bool -> Boolnot(Word8cWord8 -> ByteString -> Bool`elem`ByteStringps){-# INLINEnotElem#-}-- | /O(n)/ 'filter', applied to a predicate and a ByteString,-- returns a ByteString containing those characters that satisfy the-- predicate.filter::(Word8->Bool)->ByteString->ByteStringfilter :: (Word8 -> Bool) -> ByteString -> ByteStringfilterWord8 -> Boolk=\ps :: ByteStringps@(BSForeignPtr Word8pInIntl)->-- see fold inlining.ifByteString -> BoolnullByteStringpsthenByteStringpselseIO ByteString -> ByteStringforall a. IO a -> aunsafeDupablePerformIO(IO ByteString -> ByteString) -> IO ByteString -> ByteStringforall a b. (a -> b) -> a -> b$Int -> (ForeignPtr Word8 -> IO Int) -> IO ByteStringcreateFpAndTrimIntl((ForeignPtr Word8 -> IO Int) -> IO ByteString)-> (ForeignPtr Word8 -> IO Int) -> IO ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8pOut->doletgo' :: ForeignPtr Word8 -> ForeignPtr Word8 -> IO (ForeignPtr Word8)go'ForeignPtr Word8pfForeignPtr Word8pt=ForeignPtr Word8 -> ForeignPtr Word8 -> IO (ForeignPtr Word8)goForeignPtr Word8pfForeignPtr Word8ptwhereend :: ForeignPtr bend=ForeignPtr Word8pfForeignPtr Word8 -> Int -> ForeignPtr bforall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Intlgo :: ForeignPtr Word8 -> ForeignPtr Word8 -> IO (ForeignPtr Word8)go!ForeignPtr Word8f!ForeignPtr Word8t|ForeignPtr Word8fForeignPtr Word8 -> ForeignPtr Word8 -> Boolforall a. Eq a => a -> a -> Bool==ForeignPtr Word8forall {b}. ForeignPtr bend=ForeignPtr Word8 -> IO (ForeignPtr Word8)forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnForeignPtr Word8t|Boolotherwise=doWord8w<-ForeignPtr Word8 -> IO Word8forall a. Storable a => ForeignPtr a -> IO apeekFpForeignPtr Word8fifWord8 -> BoolkWord8wthenForeignPtr Word8 -> Word8 -> IO ()forall a. Storable a => ForeignPtr a -> a -> IO ()pokeFpForeignPtr Word8tWord8wIO () -> IO (ForeignPtr Word8) -> IO (ForeignPtr Word8)forall a b. IO a -> IO b -> IO bforall (m :: * -> *) a b. Monad m => m a -> m b -> m b>>ForeignPtr Word8 -> ForeignPtr Word8 -> IO (ForeignPtr Word8)go(ForeignPtr Word8fForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Int1)(ForeignPtr Word8tForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Int1)elseForeignPtr Word8 -> ForeignPtr Word8 -> IO (ForeignPtr Word8)go(ForeignPtr Word8fForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Int1)ForeignPtr Word8tForeignPtr Word8t<-ForeignPtr Word8 -> ForeignPtr Word8 -> IO (ForeignPtr Word8)go'ForeignPtr Word8pInForeignPtr Word8pOutInt -> IO Intforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(Int -> IO Int) -> Int -> IO Intforall a b. (a -> b) -> a -> b$!ForeignPtr Word8tForeignPtr Word8 -> ForeignPtr Word8 -> Intforall a b. ForeignPtr a -> ForeignPtr b -> Int`minusForeignPtr`ForeignPtr Word8pOut-- actual length{-# INLINEfilter#-}{----- | /O(n)/ A first order equivalent of /filter . (==)/, for the common-- case of filtering a single byte. It is more efficient to use-- /filterByte/ in this case.---- > filterByte == filter . (==)---- filterByte is around 10x faster, and uses much less space, than its-- filter equivalent--filterByte :: Word8 -> ByteString -> ByteStringfilterByte w ps = replicate (count w ps) w{-# INLINE filterByte #-}{-# RULES"ByteString specialise filter (== x)" forall x.    filter ((==) x) = filterByte x"ByteString specialise filter (== x)" forall x.    filter (== x) = filterByte x  #-}-}-- | /O(n)/ The 'find' function takes a predicate and a ByteString,-- and returns the first element in matching the predicate, or 'Nothing'-- if there is no such element.---- > find f p = case findIndex f p of Just n -> Just (p ! n) ; _ -> Nothing--find::(Word8->Bool)->ByteString->MaybeWord8find :: (Word8 -> Bool) -> ByteString -> Maybe Word8findWord8 -> BoolfByteStringp=case(Word8 -> Bool) -> ByteString -> Maybe IntfindIndexWord8 -> BoolfByteStringpofJustIntn->Word8 -> Maybe Word8forall a. a -> Maybe aJust(ByteStringpByteString -> Int -> Word8`unsafeIndex`Intn)Maybe Int_->Maybe Word8forall a. Maybe aNothing{-# INLINEfind#-}-- | /O(n)/ The 'partition' function takes a predicate a ByteString and returns-- the pair of ByteStrings with elements which do and do not satisfy the-- predicate, respectively; i.e.,---- > partition p bs == (filter p xs, filter (not . p) xs)--partition::(Word8->Bool)->ByteString->(ByteString,ByteString)partition :: (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)partitionWord8 -> BoolfByteStrings=IO (ByteString, ByteString) -> (ByteString, ByteString)forall a. IO a -> aunsafeDupablePerformIO(IO (ByteString, ByteString) -> (ByteString, ByteString))-> IO (ByteString, ByteString) -> (ByteString, ByteString)forall a b. (a -> b) -> a -> b$doForeignPtr Word8p<-Int -> IO (ForeignPtr Word8)forall a. Int -> IO (ForeignPtr a)mallocByteStringIntlenletend :: ForeignPtr bend=ForeignPtr Word8pForeignPtr Word8 -> Int -> ForeignPtr bforall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`(IntlenInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)ForeignPtr Word8mid<-Int-> ForeignPtr Word8 -> ForeignPtr Word8 -> IO (ForeignPtr Word8)sepInt0ForeignPtr Word8pForeignPtr Word8forall {b}. ForeignPtr bendForeignPtr Word8 -> ForeignPtr Word8 -> IO ()forall {b}. Storable b => ForeignPtr b -> ForeignPtr b -> IO ()revForeignPtr Word8midForeignPtr Word8forall {b}. ForeignPtr bendleti :: Inti=ForeignPtr Word8midForeignPtr Word8 -> ForeignPtr Word8 -> Intforall a b. ForeignPtr a -> ForeignPtr b -> Int`minusForeignPtr`ForeignPtr Word8p(ByteString, ByteString) -> IO (ByteString, ByteString)forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8pInti,ForeignPtr Word8 -> Int -> ByteStringBS(ForeignPtr Word8pForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Inti)(IntlenInt -> Int -> Intforall a. Num a => a -> a -> a-Inti))wherelen :: Intlen=ByteString -> IntlengthByteStringsincr :: ForeignPtr a -> ForeignPtr bincr=(ForeignPtr a -> Int -> ForeignPtr bforall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Int1)decr :: ForeignPtr a -> ForeignPtr bdecr=(ForeignPtr a -> Int -> ForeignPtr bforall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`(-Int1))sep :: Int-> ForeignPtr Word8 -> ForeignPtr Word8 -> IO (ForeignPtr Word8)sep!Inti!ForeignPtr Word8p1!ForeignPtr Word8p2|IntiInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==Intlen=ForeignPtr Word8 -> IO (ForeignPtr Word8)forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnForeignPtr Word8p1|Word8 -> BoolfWord8w=doForeignPtr Word8 -> Word8 -> IO ()forall a. Storable a => ForeignPtr a -> a -> IO ()pokeFpForeignPtr Word8p1Word8wInt-> ForeignPtr Word8 -> ForeignPtr Word8 -> IO (ForeignPtr Word8)sep(IntiInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)(ForeignPtr Word8 -> ForeignPtr Word8forall {a} {b}. ForeignPtr a -> ForeignPtr bincrForeignPtr Word8p1)ForeignPtr Word8p2|Boolotherwise=doForeignPtr Word8 -> Word8 -> IO ()forall a. Storable a => ForeignPtr a -> a -> IO ()pokeFpForeignPtr Word8p2Word8wInt-> ForeignPtr Word8 -> ForeignPtr Word8 -> IO (ForeignPtr Word8)sep(IntiInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)ForeignPtr Word8p1(ForeignPtr Word8 -> ForeignPtr Word8forall {a} {b}. ForeignPtr a -> ForeignPtr bdecrForeignPtr Word8p2)wherew :: Word8w=ByteStringsByteString -> Int -> Word8`unsafeIndex`Intirev :: ForeignPtr b -> ForeignPtr b -> IO ()rev!ForeignPtr bp1!ForeignPtr bp2-- fixme: surely there are faster ways to do this|ForeignPtr bp1ForeignPtr b -> ForeignPtr b -> Boolforall a. Ord a => a -> a -> Bool>=ForeignPtr bp2=() -> IO ()forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn()|Boolotherwise=doba<-ForeignPtr b -> IO bforall a. Storable a => ForeignPtr a -> IO apeekFpForeignPtr bp1bb<-ForeignPtr b -> IO bforall a. Storable a => ForeignPtr a -> IO apeekFpForeignPtr bp2ForeignPtr b -> b -> IO ()forall a. Storable a => ForeignPtr a -> a -> IO ()pokeFpForeignPtr bp1bbForeignPtr b -> b -> IO ()forall a. Storable a => ForeignPtr a -> a -> IO ()pokeFpForeignPtr bp2baForeignPtr b -> ForeignPtr b -> IO ()rev(ForeignPtr b -> ForeignPtr bforall {a} {b}. ForeignPtr a -> ForeignPtr bincrForeignPtr bp1)(ForeignPtr b -> ForeignPtr bforall {a} {b}. ForeignPtr a -> ForeignPtr bdecrForeignPtr bp2)-- ---------------------------------------------------------------------- Sarching for substrings-- |/O(n)/ The 'isPrefixOf' function takes two ByteStrings and returns 'True'-- if the first is a prefix of the second.isPrefixOf::ByteString->ByteString->BoolisPrefixOf :: ByteString -> ByteString -> BoolisPrefixOf(BSForeignPtr Word8x1Intl1)(BSForeignPtr Word8x2Intl2)|Intl1Int -> Int -> Boolforall a. Eq a => a -> a -> Bool==Int0=BoolTrue|Intl2Int -> Int -> Boolforall a. Ord a => a -> a -> Bool<Intl1=BoolFalse|Boolotherwise=IO Bool -> Boolforall a. IO a -> aaccursedUnutterablePerformIO(IO Bool -> Bool) -> IO Bool -> Boolforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO Bool) -> IO Boolforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8x1((Ptr Word8 -> IO Bool) -> IO Bool)-> (Ptr Word8 -> IO Bool) -> IO Boolforall a b. (a -> b) -> a -> b$\Ptr Word8p1->ForeignPtr Word8 -> (Ptr Word8 -> IO Bool) -> IO Boolforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8x2((Ptr Word8 -> IO Bool) -> IO Bool)-> (Ptr Word8 -> IO Bool) -> IO Boolforall a b. (a -> b) -> a -> b$\Ptr Word8p2->doCInti<-Ptr Word8 -> Ptr Word8 -> Int -> IO CIntmemcmpPtr Word8p1Ptr Word8p2(Int -> Intforall a b. (Integral a, Num b) => a -> bfromIntegralIntl1)Bool -> IO Boolforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(Bool -> IO Bool) -> Bool -> IO Boolforall a b. (a -> b) -> a -> b$!CIntiCInt -> CInt -> Boolforall a. Eq a => a -> a -> Bool==CInt0-- | /O(n)/ The 'stripPrefix' function takes two ByteStrings and returns 'Just'-- the remainder of the second iff the first is its prefix, and otherwise-- 'Nothing'.---- @since 0.10.8.0stripPrefix::ByteString->ByteString->MaybeByteStringstripPrefix :: ByteString -> ByteString -> Maybe ByteStringstripPrefixbs1 :: ByteStringbs1@(BSForeignPtr Word8_Intl1)ByteStringbs2|ByteStringbs1ByteString -> ByteString -> Bool`isPrefixOf`ByteStringbs2=ByteString -> Maybe ByteStringforall a. a -> Maybe aJust(Int -> ByteString -> ByteStringunsafeDropIntl1ByteStringbs2)|Boolotherwise=Maybe ByteStringforall a. Maybe aNothing-- | /O(n)/ The 'isSuffixOf' function takes two ByteStrings and returns 'True'-- iff the first is a suffix of the second.---- The following holds:---- > isSuffixOf x y == reverse x `isPrefixOf` reverse y---- However, the real implementation uses memcmp to compare the end of the-- string only, with no reverse required..isSuffixOf::ByteString->ByteString->BoolisSuffixOf :: ByteString -> ByteString -> BoolisSuffixOf(BSForeignPtr Word8x1Intl1)(BSForeignPtr Word8x2Intl2)|Intl1Int -> Int -> Boolforall a. Eq a => a -> a -> Bool==Int0=BoolTrue|Intl2Int -> Int -> Boolforall a. Ord a => a -> a -> Bool<Intl1=BoolFalse|Boolotherwise=IO Bool -> Boolforall a. IO a -> aaccursedUnutterablePerformIO(IO Bool -> Bool) -> IO Bool -> Boolforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO Bool) -> IO Boolforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8x1((Ptr Word8 -> IO Bool) -> IO Bool)-> (Ptr Word8 -> IO Bool) -> IO Boolforall a b. (a -> b) -> a -> b$\Ptr Word8p1->ForeignPtr Word8 -> (Ptr Word8 -> IO Bool) -> IO Boolforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8x2((Ptr Word8 -> IO Bool) -> IO Bool)-> (Ptr Word8 -> IO Bool) -> IO Boolforall a b. (a -> b) -> a -> b$\Ptr Word8p2->doCInti<-Ptr Word8 -> Ptr Word8 -> Int -> IO CIntmemcmpPtr Word8p1(Ptr Word8p2Ptr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`(Intl2Int -> Int -> Intforall a. Num a => a -> a -> a-Intl1))(Int -> Intforall a b. (Integral a, Num b) => a -> bfromIntegralIntl1)Bool -> IO Boolforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(Bool -> IO Bool) -> Bool -> IO Boolforall a b. (a -> b) -> a -> b$!CIntiCInt -> CInt -> Boolforall a. Eq a => a -> a -> Bool==CInt0-- | /O(n)/ The 'stripSuffix' function takes two ByteStrings and returns 'Just'-- the remainder of the second iff the first is its suffix, and otherwise-- 'Nothing'.stripSuffix::ByteString->ByteString->MaybeByteStringstripSuffix :: ByteString -> ByteString -> Maybe ByteStringstripSuffixbs1 :: ByteStringbs1@(BSForeignPtr Word8_Intl1)bs2 :: ByteStringbs2@(BSForeignPtr Word8_Intl2)|ByteStringbs1ByteString -> ByteString -> Bool`isSuffixOf`ByteStringbs2=ByteString -> Maybe ByteStringforall a. a -> Maybe aJust(Int -> ByteString -> ByteStringunsafeTake(Intl2Int -> Int -> Intforall a. Num a => a -> a -> a-Intl1)ByteStringbs2)|Boolotherwise=Maybe ByteStringforall a. Maybe aNothing-- | Check whether one string is a substring of another.isInfixOf::ByteString->ByteString->BoolisInfixOf :: ByteString -> ByteString -> BoolisInfixOfByteStringpByteStrings=ByteString -> BoolnullByteStringpBool -> Bool -> Bool||Bool -> Boolnot(ByteString -> Boolnull(ByteString -> Bool) -> ByteString -> Boolforall a b. (a -> b) -> a -> b$(ByteString, ByteString) -> ByteStringforall a b. (a, b) -> bsnd((ByteString, ByteString) -> ByteString)-> (ByteString, ByteString) -> ByteStringforall a b. (a -> b) -> a -> b$ByteString -> ByteString -> (ByteString, ByteString)breakSubstringByteStringpByteStrings)-- | /O(n)/ Check whether a 'ByteString' represents valid UTF-8.---- @since 0.11.2.0isValidUtf8::ByteString->BoolisValidUtf8 :: ByteString -> BoolisValidUtf8(BSForeignPtr Word8ptrIntlen)=IO Bool -> Boolforall a. IO a -> aaccursedUnutterablePerformIO(IO Bool -> Bool) -> IO Bool -> Boolforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO Bool) -> IO Boolforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8ptr((Ptr Word8 -> IO Bool) -> IO Bool)-> (Ptr Word8 -> IO Bool) -> IO Boolforall a b. (a -> b) -> a -> b$\Ptr Word8p->do-- Use a safe FFI call for large inputs to avoid GC synchronization pauses-- in multithreaded contexts.-- This specific limit was chosen based on results of a simple benchmark, see:-- https://github.com/haskell/bytestring/issues/451#issuecomment-991879338-- When changing this function, also consider changing the related function:-- Data.ByteString.Short.Internal.isValidUtf8CInti<-ifIntlenInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int1000000thenPtr Word8 -> CSize -> IO CIntcIsValidUtf8Ptr Word8p(Int -> CSizeforall a b. (Integral a, Num b) => a -> bfromIntegralIntlen)elsePtr Word8 -> CSize -> IO CIntcIsValidUtf8SafePtr Word8p(Int -> CSizeforall a b. (Integral a, Num b) => a -> bfromIntegralIntlen)Bool -> IO Boolforall a. a -> IO aforall (f :: * -> *) a. Applicative f => a -> f apure(Bool -> IO Bool) -> Bool -> IO Boolforall a b. (a -> b) -> a -> b$CIntiCInt -> CInt -> Boolforall a. Eq a => a -> a -> Bool/=CInt0-- | Break a string on a substring, returning a pair of the part of the-- string prior to the match, and the rest of the string.---- The following relationships hold:---- > break (== c) l == breakSubstring (singleton c) l---- For example, to tokenise a string, dropping delimiters:---- > tokenise x y = h : if null t then [] else tokenise x (drop (length x) t)-- >     where (h,t) = breakSubstring x y---- To skip to the first occurrence of a string:---- > snd (breakSubstring x y)---- To take the parts of a string before a delimiter:---- > fst (breakSubstring x y)---- Note that calling `breakSubstring x` does some preprocessing work, so-- you should avoid unnecessarily duplicating breakSubstring calls with the same-- pattern.--breakSubstring::ByteString-- ^ String to search for->ByteString-- ^ String to search in->(ByteString,ByteString)-- ^ Head and tail of string broken at substringbreakSubstring :: ByteString -> ByteString -> (ByteString, ByteString)breakSubstringByteStringpat=caseIntlpofInt0->(ByteStringempty,)Int1->Word8 -> ByteString -> (ByteString, ByteString)breakByte(ByteString -> Word8unsafeHeadByteStringpat)Int_->ifIntlpInt -> Int -> Intforall a. Num a => a -> a -> a*Int8Int -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Word -> Intforall b. FiniteBits b => b -> IntfiniteBitSize(Word0::Word)thenByteString -> (ByteString, ByteString)shiftelseByteString -> (ByteString, ByteString)karpRabinwhereunsafeSplitAt :: Int -> ByteString -> (ByteString, ByteString)unsafeSplitAtIntiByteStrings=(Int -> ByteString -> ByteStringunsafeTakeIntiByteStrings,Int -> ByteString -> ByteStringunsafeDropIntiByteStrings)lp :: Intlp=ByteString -> IntlengthByteStringpatkarpRabin::ByteString->(ByteString,ByteString)karpRabin :: ByteString -> (ByteString, ByteString)karpRabinByteStringsrc|ByteString -> IntlengthByteStringsrcInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Intlp=(ByteStringsrc,ByteStringempty)|Boolotherwise=Word32 -> Int -> (ByteString, ByteString)search(ByteString -> Word32rollingHash(ByteString -> Word32) -> ByteString -> Word32forall a b. (a -> b) -> a -> b$Int -> ByteString -> ByteStringunsafeTakeIntlpByteStringsrc)Intlpwherek :: Word32k=Word322891336453::Word32rollingHash :: ByteString -> Word32rollingHash=(Word32 -> Word8 -> Word32) -> Word32 -> ByteString -> Word32forall a. (a -> Word8 -> a) -> a -> ByteString -> afoldl'(\Word32hWord8b->Word32hWord32 -> Word32 -> Word32forall a. Num a => a -> a -> a*Word32kWord32 -> Word32 -> Word32forall a. Num a => a -> a -> a+Word8 -> Word32forall a b. (Integral a, Num b) => a -> bfromIntegralWord8b)Word320hp :: Word32hp=ByteString -> Word32rollingHashByteStringpatm :: Word32m=Word32kWord32 -> Int -> Word32forall a b. (Num a, Integral b) => a -> b -> a^Intlpget :: Int -> Word32get=Word8 -> Word32forall a b. (Integral a, Num b) => a -> bfromIntegral(Word8 -> Word32) -> (Int -> Word8) -> Int -> Word32forall b c a. (b -> c) -> (a -> b) -> a -> c.ByteString -> Int -> Word8unsafeIndexByteStringsrcsearch :: Word32 -> Int -> (ByteString, ByteString)search!Word32hs!Inti|Word32hpWord32 -> Word32 -> Boolforall a. Eq a => a -> a -> Bool==Word32hsBool -> Bool -> Bool&&ByteStringpatByteString -> ByteString -> Boolforall a. Eq a => a -> a -> Bool==Int -> ByteString -> ByteStringunsafeTakeIntlpByteStringb=(ByteString, ByteString)u|ByteString -> IntlengthByteStringsrcInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Inti=(ByteStringsrc,ByteStringempty)-- not found|Boolotherwise=Word32 -> Int -> (ByteString, ByteString)searchWord32hs'(IntiInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)whereu :: (ByteString, ByteString)u@(ByteString_,ByteStringb)=Int -> ByteString -> (ByteString, ByteString)unsafeSplitAt(IntiInt -> Int -> Intforall a. Num a => a -> a -> a-Intlp)ByteStringsrchs' :: Word32hs'=Word32hsWord32 -> Word32 -> Word32forall a. Num a => a -> a -> a*Word32kWord32 -> Word32 -> Word32forall a. Num a => a -> a -> a+Int -> Word32getIntiWord32 -> Word32 -> Word32forall a. Num a => a -> a -> a-Word32mWord32 -> Word32 -> Word32forall a. Num a => a -> a -> a*Int -> Word32get(IntiInt -> Int -> Intforall a. Num a => a -> a -> a-Intlp){-# INLINEkarpRabin#-}shift::ByteString->(ByteString,ByteString)shift :: ByteString -> (ByteString, ByteString)shift!ByteStringsrc|ByteString -> IntlengthByteStringsrcInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Intlp=(ByteStringsrc,ByteStringempty)|Boolotherwise=Word -> Int -> (ByteString, ByteString)search(ByteString -> WordintoWord(ByteString -> Word) -> ByteString -> Wordforall a b. (a -> b) -> a -> b$Int -> ByteString -> ByteStringunsafeTakeIntlpByteStringsrc)IntlpwhereintoWord::ByteString->WordintoWord :: ByteString -> WordintoWord=(Word -> Word8 -> Word) -> Word -> ByteString -> Wordforall a. (a -> Word8 -> a) -> a -> ByteString -> afoldl'(\WordwWord8b->(WordwWord -> Int -> Wordforall a. Bits a => a -> Int -> a`shiftL`Int8)Word -> Word -> Wordforall a. Bits a => a -> a -> a.|.Word8 -> Wordforall a b. (Integral a, Num b) => a -> bfromIntegralWord8b)Word0wp :: Wordwp=ByteString -> WordintoWordByteStringpatmask :: Wordmask=(Word1Word -> Int -> Wordforall a. Bits a => a -> Int -> a`shiftL`(Int8Int -> Int -> Intforall a. Num a => a -> a -> a*Intlp))Word -> Word -> Wordforall a. Num a => a -> a -> a-Word1search :: Word -> Int -> (ByteString, ByteString)search!Wordw!Inti|WordwWord -> Word -> Boolforall a. Eq a => a -> a -> Bool==Wordwp=Int -> ByteString -> (ByteString, ByteString)unsafeSplitAt(IntiInt -> Int -> Intforall a. Num a => a -> a -> a-Intlp)ByteStringsrc|ByteString -> IntlengthByteStringsrcInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Inti=(ByteStringsrc,ByteStringempty)|Boolotherwise=Word -> Int -> (ByteString, ByteString)searchWordw'(IntiInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)whereb :: Wordb=Word8 -> Wordforall a b. (Integral a, Num b) => a -> bfromIntegral(ByteString -> Int -> Word8unsafeIndexByteStringsrcInti)w' :: Wordw'=WordmaskWord -> Word -> Wordforall a. Bits a => a -> a -> a.&.((WordwWord -> Int -> Wordforall a. Bits a => a -> Int -> a`shiftL`Int8)Word -> Word -> Wordforall a. Bits a => a -> a -> a.|.Wordb){-# INLINEshift#-}-- ----------------------------------------------------------------------- Zipping-- | /O(n)/ 'zip' takes two ByteStrings and returns a list of-- corresponding pairs of bytes. If one input ByteString is short,-- excess elements of the longer ByteString are discarded. This is-- equivalent to a pair of 'unpack' operations.zip::ByteString->ByteString->[(Word8,Word8)]zip :: ByteString -> ByteString -> [(Word8, Word8)]zipByteStringpsByteStringqs=caseByteString -> Maybe (Word8, ByteString)unconsByteStringpsofMaybe (Word8, ByteString)Nothing->[]Just(Word8psH,ByteStringpsT)->caseByteString -> Maybe (Word8, ByteString)unconsByteStringqsofMaybe (Word8, ByteString)Nothing->[]Just(Word8qsH,ByteStringqsT)->(Word8psH,Word8qsH)(Word8, Word8) -> [(Word8, Word8)] -> [(Word8, Word8)]forall a. a -> [a] -> [a]:ByteString -> ByteString -> [(Word8, Word8)]zipByteStringpsTByteStringqsT-- | 'zipWith' generalises 'zip' by zipping with the function given as-- the first argument, instead of a tupling function.  For example,-- @'zipWith' (+)@ is applied to two ByteStrings to produce the list of-- corresponding sums.zipWith::(Word8->Word8->a)->ByteString->ByteString->[a]zipWith :: forall a. (Word8 -> Word8 -> a) -> ByteString -> ByteString -> [a]zipWithWord8 -> Word8 -> afByteStringpsByteStringqs=caseByteString -> Maybe (Word8, ByteString)unconsByteStringpsofMaybe (Word8, ByteString)Nothing->[]Just(Word8psH,ByteStringpsT)->caseByteString -> Maybe (Word8, ByteString)unconsByteStringqsofMaybe (Word8, ByteString)Nothing->[]Just(Word8qsH,ByteStringqsT)->Word8 -> Word8 -> afWord8psHWord8qsHa -> [a] -> [a]forall a. a -> [a] -> [a]:(Word8 -> Word8 -> a) -> ByteString -> ByteString -> [a]forall a. (Word8 -> Word8 -> a) -> ByteString -> ByteString -> [a]zipWithWord8 -> Word8 -> afByteStringpsTByteStringqsT{-# NOINLINE[1]zipWith#-}-- | A specialised version of `zipWith` for the common case of a-- simultaneous map over two ByteStrings, to build a 3rd.---- @since 0.11.1.0packZipWith::(Word8->Word8->Word8)->ByteString->ByteString->ByteStringpackZipWith :: (Word8 -> Word8 -> Word8) -> ByteString -> ByteString -> ByteStringpackZipWithWord8 -> Word8 -> Word8f(BSForeignPtr Word8aIntl)(BSForeignPtr Word8bIntm)=IO ByteString -> ByteStringforall a. IO a -> aunsafeDupablePerformIO(IO ByteString -> ByteString) -> IO ByteString -> ByteStringforall a b. (a -> b) -> a -> b$Int -> (ForeignPtr Word8 -> IO ()) -> IO ByteStringcreateFpIntlen((ForeignPtr Word8 -> IO ()) -> IO ByteString)-> (ForeignPtr Word8 -> IO ()) -> IO ByteStringforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> ForeignPtr Word8 -> ForeignPtr Word8 -> IO ()goForeignPtr Word8aForeignPtr Word8bwherego :: ForeignPtr Word8 -> ForeignPtr Word8 -> ForeignPtr Word8 -> IO ()goForeignPtr Word8p1ForeignPtr Word8p2=Int -> ForeignPtr Word8 -> IO ()zipWith_Int0wherezipWith_::Int->ForeignPtrWord8->IO()zipWith_ :: Int -> ForeignPtr Word8 -> IO ()zipWith_!Intn!ForeignPtr Word8r|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intlen=() -> IO ()forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn()|Boolotherwise=doWord8x<-ForeignPtr Word8 -> Int -> IO Word8forall a. Storable a => ForeignPtr a -> Int -> IO apeekFpByteOffForeignPtr Word8p1IntnWord8y<-ForeignPtr Word8 -> Int -> IO Word8forall a. Storable a => ForeignPtr a -> Int -> IO apeekFpByteOffForeignPtr Word8p2IntnForeignPtr Word8 -> Int -> Word8 -> IO ()forall a b. Storable a => ForeignPtr b -> Int -> a -> IO ()pokeFpByteOffForeignPtr Word8rIntn(Word8 -> Word8 -> Word8fWord8xWord8y)Int -> ForeignPtr Word8 -> IO ()zipWith_(IntnInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)ForeignPtr Word8rlen :: Intlen=Int -> Int -> Intforall a. Ord a => a -> a -> aminIntlIntm{-# INLINEpackZipWith#-}-- | /O(n)/ 'unzip' transforms a list of pairs of bytes into a pair of-- ByteStrings. Note that this performs two 'pack' operations.unzip::[(Word8,Word8)]->(ByteString,ByteString)unzip :: [(Word8, Word8)] -> (ByteString, ByteString)unzip[(Word8, Word8)]ls=([Word8] -> ByteStringpack(((Word8, Word8) -> Word8) -> [(Word8, Word8)] -> [Word8]forall a b. (a -> b) -> [a] -> [b]P.map(Word8, Word8) -> Word8forall a b. (a, b) -> afst[(Word8, Word8)]ls),[Word8] -> ByteStringpack(((Word8, Word8) -> Word8) -> [(Word8, Word8)] -> [Word8]forall a b. (a -> b) -> [a] -> [b]P.map(Word8, Word8) -> Word8forall a b. (a, b) -> bsnd[(Word8, Word8)]ls)){-# INLINEunzip#-}-- ----------------------------------------------------------------------- Special lists-- | /O(n)/ Returns all initial segments of the given 'ByteString', shortest first.inits::ByteString->[ByteString]-- see Note [Avoid NonEmpty combinators]inits :: ByteString -> [ByteString]initsByteStringbs=NonEmpty ByteString -> [ByteString]forall a. NonEmpty a -> [a]NE.toList(NonEmpty ByteString -> [ByteString])-> NonEmpty ByteString -> [ByteString]forall a b. (a -> b) -> a -> b$!ByteString -> NonEmpty ByteStringinitsNEByteStringbs-- | /O(n)/ Returns all initial segments of the given 'ByteString', shortest first.---- @since 0.11.4.0initsNE::ByteString->NonEmptyByteString-- see Note [Avoid NonEmpty combinators]initsNE :: ByteString -> NonEmpty ByteStringinitsNE(BSForeignPtr Word8xIntlen)=ByteStringemptyByteString -> [ByteString] -> NonEmpty ByteStringforall a. a -> [a] -> NonEmpty a:|[ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8xIntn|Intn<-[Int1..Intlen]]-- | /O(n)/ Returns all final segments of the given 'ByteString', longest first.tails::ByteString->[ByteString]-- see Note [Avoid NonEmpty combinators]tails :: ByteString -> [ByteString]tailsByteStringbs=NonEmpty ByteString -> [ByteString]forall a. NonEmpty a -> [a]NE.toList(NonEmpty ByteString -> [ByteString])-> NonEmpty ByteString -> [ByteString]forall a b. (a -> b) -> a -> b$!ByteString -> NonEmpty ByteStringtailsNEByteStringbs-- | /O(n)/ Returns all final segments of the given 'ByteString', longest first.---- @since 0.11.4.0tailsNE::ByteString->NonEmptyByteString-- see Note [Avoid NonEmpty combinators]tailsNE :: ByteString -> NonEmpty ByteStringtailsNEByteStringp|ByteString -> BoolnullByteStringp=ByteStringemptyByteString -> [ByteString] -> NonEmpty ByteStringforall a. a -> [a] -> NonEmpty a:|[]|Boolotherwise=ByteStringpByteString -> [ByteString] -> NonEmpty ByteStringforall a. a -> [a] -> NonEmpty a:|ByteString -> [ByteString]tails(ByteString -> ByteStringunsafeTailByteStringp)-- less efficent spacewise: tails (BS x l) = [BS (plusForeignPtr x n) (l-n) | n <- [0..l]]{-Note [Avoid NonEmpty combinators]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~As of base-4.18, most of the NonEmpty API is surprisingly lazy.Using it without forcing the arguments yourself is just begging GHCto make your code waste time allocating useless selector thunks.This may change in the future. See also this CLC issue:  https://github.com/haskell/core-libraries-committee/issues/107But until then, "refactor" with care!(Even for uses of NonEmpty near lazy ByteStrings, we don't wantthe extra laziness of the NonEmpty API.)-}-- ----------------------------------------------------------------------- ** Ordered 'ByteString's-- | /O(n)/ Sort a ByteString efficiently, using counting sort.sort::ByteString->ByteStringsort :: ByteString -> ByteStringsort(BSForeignPtr Word8inputIntl)-- qsort outperforms counting sort for small arrays|IntlInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int20=Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFpIntl((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8destFP->doForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()memcpyFpForeignPtr Word8destFPForeignPtr Word8inputIntlForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8destFP((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Word8dest->Ptr Word8 -> CSize -> IO ()c_sortPtr Word8dest(Int -> CSizeforall a b. (Integral a, Num b) => a -> bfromIntegralIntl)|Boolotherwise=Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFpIntl((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8p->Int -> (Ptr Int -> IO ()) -> IO ()forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO ballocaArrayInt256((Ptr Int -> IO ()) -> IO ()) -> (Ptr Int -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Intarr->doPtr Any -> Word8 -> Int -> IO ()forall a. Ptr a -> Word8 -> Int -> IO ()fillBytes(Ptr Int -> Ptr Anyforall a b. Ptr a -> Ptr bcastPtrPtr Intarr)Word80(Int256Int -> Int -> Intforall a. Num a => a -> a -> a*Int -> Intforall a. Storable a => a -> IntsizeOf(Intforall a. HasCallStack => aundefined::Int))ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8input(\Ptr Word8x->Ptr Int -> Ptr Word8 -> Int -> IO ()countOccurrencesPtr IntarrPtr Word8xIntl)letgo :: Int -> Ptr b -> IO ()goInt256!Ptr b_=() -> IO ()forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn()goInti!Ptr bptr=doIntn<-Ptr Int -> Int -> IO Intforall a. Storable a => Ptr a -> Int -> IO apeekElemOffPtr IntarrIntiBool -> IO () -> IO ()forall (f :: * -> *). Applicative f => Bool -> f () -> f ()when(IntnInt -> Int -> Boolforall a. Eq a => a -> a -> Bool/=Int0)(IO () -> IO ()) -> IO () -> IO ()forall a b. (a -> b) -> a -> b$Ptr b -> Word8 -> Int -> IO ()forall a. Ptr a -> Word8 -> Int -> IO ()fillBytesPtr bptr(forall a b. (Integral a, Num b) => a -> bfromIntegral@Int@Word8Inti)IntnInt -> Ptr b -> IO ()go(IntiInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)(Ptr bptrPtr b -> Int -> Ptr bforall a b. Ptr a -> Int -> Ptr b`plusPtr`Int -> Intforall a b. (Integral a, Num b) => a -> bfromIntegralIntn)ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8p(Int -> Ptr Word8 -> IO ()forall {b}. Int -> Ptr b -> IO ()goInt0)where-- Count the number of occurrences of each byte.countOccurrences::PtrInt->PtrWord8->Int->IO()countOccurrences :: Ptr Int -> Ptr Word8 -> Int -> IO ()countOccurrences!Ptr Intcounts!Ptr Word8str!Intlen=Int -> IO ()goInt0wherego :: Int -> IO ()go!Inti|IntiInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==Intlen=() -> IO ()forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn()|Boolotherwise=doIntk<-Word8 -> Intforall a b. (Integral a, Num b) => a -> bfromIntegral(Word8 -> Int) -> IO Word8 -> IO Intforall a b. (a -> b) -> IO a -> IO bforall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b`fmap`Ptr Word8 -> Int -> IO Word8forall a. Storable a => Ptr a -> Int -> IO apeekElemOffPtr Word8strIntiIntx<-Ptr Int -> Int -> IO Intforall a. Storable a => Ptr a -> Int -> IO apeekElemOffPtr IntcountsIntkPtr Int -> Int -> Int -> IO ()forall a. Storable a => Ptr a -> Int -> a -> IO ()pokeElemOffPtr IntcountsIntk(IntxInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)Int -> IO ()go(IntiInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)-- ----------------------------------------------------------------------- Low level constructors-- | /O(n) construction/ Use a @ByteString@ with a function requiring a-- null-terminated @CString@.  The @CString@ is a copy and will be freed-- automatically; it must not be stored or used after the-- subcomputation finishes.useAsCString::ByteString->(CString->IOa)->IOauseAsCString :: forall a. ByteString -> (CString -> IO a) -> IO auseAsCString(BSForeignPtr Word8fpIntl)CString -> IO aaction=Int -> (Ptr Word8 -> IO a) -> IO aforall a b. Int -> (Ptr a -> IO b) -> IO ballocaBytes(IntlInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)((Ptr Word8 -> IO a) -> IO a) -> (Ptr Word8 -> IO a) -> IO aforall a b. (a -> b) -> a -> b$\Ptr Word8buf->doForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Word8p->Ptr Word8 -> Ptr Word8 -> Int -> IO ()forall a. Ptr a -> Ptr a -> Int -> IO ()copyBytesPtr Word8bufPtr Word8pIntlPtr Word8 -> Int -> Word8 -> IO ()forall b. Ptr b -> Int -> Word8 -> IO ()forall a b. Storable a => Ptr b -> Int -> a -> IO ()pokeByteOffPtr Word8bufIntl(Word80::Word8)CString -> IO aaction(Ptr Word8 -> CStringforall a b. Ptr a -> Ptr bcastPtrPtr Word8buf)-- | /O(n) construction/ Use a @ByteString@ with a function requiring a 'CStringLen'.-- As for 'useAsCString' this function makes a copy of the original @ByteString@.-- It must not be stored or used after the subcomputation finishes.---- Beware that this function is not required to add a terminating @\NUL@ byte at the end of the 'CStringLen' it provides.-- If you need to construct a pointer to a null-terminated sequence, use 'useAsCString'-- (and measure length independently if desired).useAsCStringLen::ByteString->(CStringLen->IOa)->IOauseAsCStringLen :: forall a. ByteString -> (CStringLen -> IO a) -> IO auseAsCStringLenp :: ByteStringp@(BSForeignPtr Word8_Intl)CStringLen -> IO af=ByteString -> (CString -> IO a) -> IO aforall a. ByteString -> (CString -> IO a) -> IO auseAsCStringByteStringp((CString -> IO a) -> IO a) -> (CString -> IO a) -> IO aforall a b. (a -> b) -> a -> b$\CStringcstr->CStringLen -> IO af(CStringcstr,Intl)-------------------------------------------------------------------------- | /O(n)./ Construct a new @ByteString@ from a @CString@. The-- resulting @ByteString@ is an immutable copy of the original-- @CString@, and is managed on the Haskell heap. The original-- @CString@ must be null terminated.packCString::CString->IOByteStringpackCString :: CString -> IO ByteStringpackCStringCStringcstr=doCSizelen<-CString -> IO CSizec_strlenCStringcstrCStringLen -> IO ByteStringpackCStringLen(CStringcstr,CSize -> Intforall a b. (Integral a, Num b) => a -> bfromIntegralCSizelen)-- | /O(n)./ Construct a new @ByteString@ from a @CStringLen@. The-- resulting @ByteString@ is an immutable copy of the original @CStringLen@.-- The @ByteString@ is a normal Haskell value and will be managed on the-- Haskell heap.packCStringLen::CStringLen->IOByteStringpackCStringLen :: CStringLen -> IO ByteStringpackCStringLen(CStringcstr,Intlen)|IntlenInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0=Int -> (ForeignPtr Word8 -> IO ()) -> IO ByteStringcreateFpIntlen((ForeignPtr Word8 -> IO ()) -> IO ByteString)-> (ForeignPtr Word8 -> IO ()) -> IO ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8fp->ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Word8p->Ptr Word8 -> Ptr Word8 -> Int -> IO ()forall a. Ptr a -> Ptr a -> Int -> IO ()copyBytesPtr Word8p(CString -> Ptr Word8forall a b. Ptr a -> Ptr bcastPtrCStringcstr)IntlenpackCStringLen(CString_,Intlen)=FilePath -> FilePath -> IO ByteStringforall a. HasCallStack => FilePath -> FilePath -> IO amoduleErrorIOFilePath"packCStringLen"(FilePath"negative length: "FilePath -> FilePath -> FilePathforall a. [a] -> [a] -> [a]++Int -> FilePathforall a. Show a => a -> FilePathshowIntlen)-------------------------------------------------------------------------- | /O(n)/ Make a copy of the 'ByteString' with its own storage.-- This is mainly useful to allow the rest of the data pointed-- to by the 'ByteString' to be garbage collected, for example-- if a large string has been read in, and only a small part of it-- is needed in the rest of the program.--copy::ByteString->ByteStringcopy :: ByteString -> ByteStringcopy(BSForeignPtr Word8xIntl)=Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFpIntl((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8p->ForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()memcpyFpForeignPtr Word8pForeignPtr Word8xIntl-- ----------------------------------------------------------------------- Line IO-- | Read a line from stdin.getLine::IOByteStringgetLine :: IO ByteStringgetLine=Handle -> IO ByteStringhGetLineHandlestdin{-# DEPRECATEDgetLine"Deprecated since @bytestring-0.12@. Use 'Data.ByteString.Char8.getLine' instead. (Functions that rely on ASCII encodings belong in \"Data.ByteString.Char8\")"#-}-- | Read a line from a handlehGetLine::Handle->IOByteStringhGetLine :: Handle -> IO ByteStringhGetLineHandleh=FilePath -> Handle -> (Handle__ -> IO ByteString) -> IO ByteStringforall a. FilePath -> Handle -> (Handle__ -> IO a) -> IO awantReadableHandle_FilePath"Data.ByteString.hGetLine"Handleh((Handle__ -> IO ByteString) -> IO ByteString)-> (Handle__ -> IO ByteString) -> IO ByteStringforall a b. (a -> b) -> a -> b$\h_ :: Handle__h_@Handle__{IORef (Buffer Word8)haByteBuffer :: IORef (Buffer Word8)haByteBuffer :: Handle__ -> IORef (Buffer Word8)haByteBuffer}->doHandle__ -> IO ()flushCharReadBufferHandle__h_Buffer Word8buf<-IORef (Buffer Word8) -> IO (Buffer Word8)forall a. IORef a -> IO areadIORefIORef (Buffer Word8)haByteBufferifBuffer Word8 -> Boolforall e. Buffer e -> BoolisEmptyBufferBuffer Word8bufthenHandle__ -> Buffer Word8 -> Int -> [ByteString] -> IO ByteStringfillHandle__h_Buffer Word8bufInt0[]elseHandle__ -> Buffer Word8 -> Int -> [ByteString] -> IO ByteStringhaveBufHandle__h_Buffer Word8bufInt0[]wherefill :: Handle__ -> Buffer Word8 -> Int -> [ByteString] -> IO ByteStringfillh_ :: Handle__h_@Handle__{IORef (Buffer Word8)haByteBuffer :: Handle__ -> IORef (Buffer Word8)haByteBuffer :: IORef (Buffer Word8)haByteBuffer,devhaDevice :: devhaDevice :: ()haDevice}Buffer Word8buf!Intlen[ByteString]xss=do(Intr,Buffer Word8buf')<-dev -> Buffer Word8 -> IO (Int, Buffer Word8)forall dev.BufferedIO dev =>dev -> Buffer Word8 -> IO (Int, Buffer Word8)Buffered.fillReadBufferdevhaDeviceBuffer Word8bufifIntrInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==Int0thendoIORef (Buffer Word8) -> Buffer Word8 -> IO ()forall a. IORef a -> a -> IO ()writeIORefIORef (Buffer Word8)haByteBufferBuffer Word8buf{bufR=0,bufL=0}ifIntlenInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>Int0thenInt -> [ByteString] -> IO ByteStringmkBigPSIntlen[ByteString]xsselseIO ByteStringforall a. IO aioe_EOFelseHandle__ -> Buffer Word8 -> Int -> [ByteString] -> IO ByteStringhaveBufHandle__h_Buffer Word8buf'Intlen[ByteString]xsshaveBuf :: Handle__ -> Buffer Word8 -> Int -> [ByteString] -> IO ByteStringhaveBufh_ :: Handle__h_@Handle__{IORef (Buffer Word8)haByteBuffer :: Handle__ -> IORef (Buffer Word8)haByteBuffer :: IORef (Buffer Word8)haByteBuffer}buf :: Buffer Word8buf@Buffer{bufRaw :: forall e. Buffer e -> RawBuffer ebufRaw=ForeignPtr Word8raw,bufR :: forall e. Buffer e -> IntbufR=Intw,bufL :: forall e. Buffer e -> IntbufL=Intr}Intlen[ByteString]xss=doIntoff<-Int -> Int -> ForeignPtr Word8 -> IO IntfindEOLIntrIntwForeignPtr Word8rawletnew_len :: Intnew_len=IntlenInt -> Int -> Intforall a. Num a => a -> a -> a+IntoffInt -> Int -> Intforall a. Num a => a -> a -> a-IntrByteStringxs<-ForeignPtr Word8 -> Int -> Int -> IO ByteStringmkPSForeignPtr Word8rawIntrIntoff-- if eol == True, then off is the offset of the '\n'-- otherwise off == w and the buffer is now empty.ifIntoffInt -> Int -> Boolforall a. Eq a => a -> a -> Bool/=IntwthendoifIntwInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==IntoffInt -> Int -> Intforall a. Num a => a -> a -> a+Int1thenIORef (Buffer Word8) -> Buffer Word8 -> IO ()forall a. IORef a -> a -> IO ()writeIORefIORef (Buffer Word8)haByteBufferBuffer Word8buf{bufL=0,bufR=0}elseIORef (Buffer Word8) -> Buffer Word8 -> IO ()forall a. IORef a -> a -> IO ()writeIORefIORef (Buffer Word8)haByteBufferBuffer Word8buf{bufL=off+1}Int -> [ByteString] -> IO ByteStringmkBigPSIntnew_len(ByteStringxsByteString -> [ByteString] -> [ByteString]forall a. a -> [a] -> [a]:[ByteString]xss)elseHandle__ -> Buffer Word8 -> Int -> [ByteString] -> IO ByteStringfillHandle__h_Buffer Word8buf{bufL=0,bufR=0}Intnew_len(ByteStringxsByteString -> [ByteString] -> [ByteString]forall a. a -> [a] -> [a]:[ByteString]xss)-- find the end-of-line character, if there is onefindEOL :: Int -> Int -> ForeignPtr Word8 -> IO IntfindEOLIntrIntwForeignPtr Word8raw|IntrInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==Intw=Int -> IO Intforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnIntw|Boolotherwise=doWord8c<-ForeignPtr Word8 -> Int -> IO Word8readWord8BufForeignPtr Word8rawIntrifWord8cWord8 -> Word8 -> Boolforall a. Eq a => a -> a -> Bool==Int -> Word8forall a b. (Integral a, Num b) => a -> bfromIntegral(Char -> IntordChar'\n')thenInt -> IO Intforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnIntr-- NB. not r+1: don't include the '\n'elseInt -> Int -> ForeignPtr Word8 -> IO IntfindEOL(IntrInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)IntwForeignPtr Word8raw{-# DEPRECATEDhGetLine"Deprecated since @bytestring-0.12@. Use 'Data.ByteString.Char8.hGetLine' instead. (Functions that rely on ASCII encodings belong in \"Data.ByteString.Char8\")"#-}mkPS::RawBufferWord8->Int->Int->IOByteStringmkPS :: ForeignPtr Word8 -> Int -> Int -> IO ByteStringmkPSForeignPtr Word8bufIntstartIntend=Int -> (ForeignPtr Word8 -> IO ()) -> IO ByteStringcreateFpIntlen((ForeignPtr Word8 -> IO ()) -> IO ByteString)-> (ForeignPtr Word8 -> IO ()) -> IO ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8fp->ForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()memcpyFpForeignPtr Word8fp(ForeignPtr Word8bufForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Intstart)Intlenwherelen :: Intlen=IntendInt -> Int -> Intforall a. Num a => a -> a -> a-IntstartmkBigPS::Int->[ByteString]->IOByteStringmkBigPS :: Int -> [ByteString] -> IO ByteStringmkBigPSInt_[ByteStringps]=ByteString -> IO ByteStringforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnByteStringpsmkBigPSInt_[ByteString]pss=ByteString -> IO ByteStringforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(ByteString -> IO ByteString) -> ByteString -> IO ByteStringforall a b. (a -> b) -> a -> b$![ByteString] -> ByteStringconcat([ByteString] -> [ByteString]forall a. [a] -> [a]P.reverse[ByteString]pss)-- ----------------------------------------------------------------------- Block IO-- | Outputs a 'ByteString' to the specified 'Handle'.hPut::Handle->ByteString->IO()hPut :: Handle -> ByteString -> IO ()hPutHandle_(BSForeignPtr Word8_Int0)=() -> IO ()forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn()hPutHandleh(BSForeignPtr Word8psIntl)=ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8ps((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Word8p->Handle -> Ptr Word8 -> Int -> IO ()forall a. Handle -> Ptr a -> Int -> IO ()hPutBufHandlehPtr Word8pIntl-- | Similar to 'hPut' except that it will never block. Instead it returns-- any tail that did not get written. This tail may be 'empty' in the case that-- the whole string was written, or the whole original string if nothing was-- written. Partial writes are also possible.---- Note: on Windows and with Haskell implementation other than GHC, this-- function does not work correctly; it behaves identically to 'hPut'.--hPutNonBlocking::Handle->ByteString->IOByteStringhPutNonBlocking :: Handle -> ByteString -> IO ByteStringhPutNonBlockingHandlehbs :: ByteStringbs@(BSForeignPtr Word8psIntl)=doIntbytesWritten<-ForeignPtr Word8 -> (Ptr Word8 -> IO Int) -> IO Intforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8ps((Ptr Word8 -> IO Int) -> IO Int)-> (Ptr Word8 -> IO Int) -> IO Intforall a b. (a -> b) -> a -> b$\Ptr Word8p->Handle -> Ptr Word8 -> Int -> IO Intforall a. Handle -> Ptr a -> Int -> IO InthPutBufNonBlockingHandlehPtr Word8pIntlByteString -> IO ByteStringforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(ByteString -> IO ByteString) -> ByteString -> IO ByteStringforall a b. (a -> b) -> a -> b$!Int -> ByteString -> ByteStringdropIntbytesWrittenByteStringbs-- | A synonym for 'hPut', for compatibilityhPutStr::Handle->ByteString->IO()hPutStr :: Handle -> ByteString -> IO ()hPutStr=Handle -> ByteString -> IO ()hPut-- | Write a ByteString to 'stdout'.putStr::ByteString->IO()putStr :: ByteString -> IO ()putStr=Handle -> ByteString -> IO ()hPutHandlestdout-------------------------------------------------------------------------- Low level IO-- | Read a 'ByteString' directly from the specified 'Handle'.  This-- is far more efficient than reading the characters into a 'String'-- and then using 'pack'. First argument is the Handle to read from,-- and the second is the number of bytes to read. It returns the bytes-- read, up to n, or 'empty' if EOF has been reached.---- 'hGet' is implemented in terms of 'hGetBuf'.---- If the handle is a pipe or socket, and the writing end-- is closed, 'hGet' will behave as if EOF was reached.--hGet::Handle->Int->IOByteStringhGet :: Handle -> Int -> IO ByteStringhGetHandlehInti|IntiInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>Int0=Int -> (ForeignPtr Word8 -> IO Int) -> IO ByteStringcreateFpAndTrimInti((ForeignPtr Word8 -> IO Int) -> IO ByteString)-> (ForeignPtr Word8 -> IO Int) -> IO ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8fp->ForeignPtr Word8 -> (Ptr Word8 -> IO Int) -> IO Intforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO Int) -> IO Int)-> (Ptr Word8 -> IO Int) -> IO Intforall a b. (a -> b) -> a -> b$\Ptr Word8p->Handle -> Ptr Word8 -> Int -> IO Intforall a. Handle -> Ptr a -> Int -> IO InthGetBufHandlehPtr Word8pInti|IntiInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==Int0=ByteString -> IO ByteStringforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnByteStringempty|Boolotherwise=Handle -> FilePath -> Int -> IO ByteStringforall a. Handle -> FilePath -> Int -> IO aillegalBufferSizeHandlehFilePath"hGet"Inti-- | hGetNonBlocking is similar to 'hGet', except that it will never block-- waiting for data to become available, instead it returns only whatever data-- is available.  If there is no data available to be read, 'hGetNonBlocking'-- returns 'empty'.---- Note: on Windows and with Haskell implementation other than GHC, this-- function does not work correctly; it behaves identically to 'hGet'.--hGetNonBlocking::Handle->Int->IOByteStringhGetNonBlocking :: Handle -> Int -> IO ByteStringhGetNonBlockingHandlehInti|IntiInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>Int0=Int -> (ForeignPtr Word8 -> IO Int) -> IO ByteStringcreateFpAndTrimInti((ForeignPtr Word8 -> IO Int) -> IO ByteString)-> (ForeignPtr Word8 -> IO Int) -> IO ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8fp->ForeignPtr Word8 -> (Ptr Word8 -> IO Int) -> IO Intforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO Int) -> IO Int)-> (Ptr Word8 -> IO Int) -> IO Intforall a b. (a -> b) -> a -> b$\Ptr Word8p->Handle -> Ptr Word8 -> Int -> IO Intforall a. Handle -> Ptr a -> Int -> IO InthGetBufNonBlockingHandlehPtr Word8pInti|IntiInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==Int0=ByteString -> IO ByteStringforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnByteStringempty|Boolotherwise=Handle -> FilePath -> Int -> IO ByteStringforall a. Handle -> FilePath -> Int -> IO aillegalBufferSizeHandlehFilePath"hGetNonBlocking"Inti-- | Like 'hGet', except that a shorter 'ByteString' may be returned-- if there are not enough bytes immediately available to satisfy the-- whole request.  'hGetSome' only blocks if there is no data-- available, and EOF has not yet been reached.--hGetSome::Handle->Int->IOByteStringhGetSome :: Handle -> Int -> IO ByteStringhGetSomeHandlehhInti|IntiInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>Int0=Int -> (ForeignPtr Word8 -> IO Int) -> IO ByteStringcreateFpAndTrimInti((ForeignPtr Word8 -> IO Int) -> IO ByteString)-> (ForeignPtr Word8 -> IO Int) -> IO ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8fp->ForeignPtr Word8 -> (Ptr Word8 -> IO Int) -> IO Intforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO Int) -> IO Int)-> (Ptr Word8 -> IO Int) -> IO Intforall a b. (a -> b) -> a -> b$\Ptr Word8p->Handle -> Ptr Word8 -> Int -> IO Intforall a. Handle -> Ptr a -> Int -> IO InthGetBufSomeHandlehhPtr Word8pInti|IntiInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==Int0=ByteString -> IO ByteStringforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnByteStringempty|Boolotherwise=Handle -> FilePath -> Int -> IO ByteStringforall a. Handle -> FilePath -> Int -> IO aillegalBufferSizeHandlehhFilePath"hGetSome"IntiillegalBufferSize::Handle->String->Int->IOaillegalBufferSize :: forall a. Handle -> FilePath -> Int -> IO aillegalBufferSizeHandlehandleFilePathfnIntsz=IOException -> IO aforall a. IOException -> IO aioError(IOErrorType-> FilePath -> Maybe Handle -> Maybe FilePath -> IOExceptionmkIOErrorIOErrorTypeillegalOperationErrorTypeFilePathmsg(Handle -> Maybe Handleforall a. a -> Maybe aJustHandlehandle)Maybe FilePathforall a. Maybe aNothing)--TODO: System.IO uses InvalidArgument here, but it's not exported :-(wheremsg :: FilePathmsg=FilePathfnFilePath -> FilePath -> FilePathforall a. [a] -> [a] -> [a]++FilePath": illegal ByteString size "FilePath -> FilePath -> FilePathforall a. [a] -> [a] -> [a]++Int -> Int -> FilePath -> FilePathforall a. Show a => Int -> a -> FilePath -> FilePathshowsPrecInt9Intsz[]-- | Read a handle's entire contents strictly into a 'ByteString'.---- This function reads chunks at a time, increasing the chunk size on each-- read. The final string is then reallocated to the appropriate size. For-- files > half of available memory, this may lead to memory exhaustion.-- Consider using 'readFile' in this case.---- The Handle is closed once the contents have been read,-- or if an exception is thrown.--hGetContents::Handle->IOByteStringhGetContents :: Handle -> IO ByteStringhGetContentsHandlehnd=doByteStringbs<-Handle -> Int -> Int -> IO ByteStringhGetContentsSizeHintHandlehndInt1024Int2048IO ByteString -> IO () -> IO ByteStringforall a b. IO a -> IO b -> IO a`finally`Handle -> IO ()hCloseHandlehnd-- don't waste too much space for small files:ifByteString -> IntlengthByteStringbsInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int900thenByteString -> IO ByteStringforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(ByteString -> IO ByteString) -> ByteString -> IO ByteStringforall a b. (a -> b) -> a -> b$!ByteString -> ByteStringcopyByteStringbselseByteString -> IO ByteStringforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnByteStringbshGetContentsSizeHint::Handle->Int-- ^ first read size->Int-- ^ initial buffer size increment->IOByteStringhGetContentsSizeHint :: Handle -> Int -> Int -> IO ByteStringhGetContentsSizeHintHandlehnd=[ByteString] -> Int -> Int -> IO ByteStringreadChunks[]wherereadChunks :: [ByteString] -> Int -> Int -> IO ByteStringreadChunks[ByteString]chunksIntszIntsz'=doForeignPtr Word8fp<-Int -> IO (ForeignPtr Word8)forall a. Int -> IO (ForeignPtr a)mallocByteStringIntszIntreadcount<-ForeignPtr Word8 -> (Ptr Word8 -> IO Int) -> IO Intforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO Int) -> IO Int)-> (Ptr Word8 -> IO Int) -> IO Intforall a b. (a -> b) -> a -> b$\Ptr Word8buf->Handle -> Ptr Word8 -> Int -> IO Intforall a. Handle -> Ptr a -> Int -> IO InthGetBufHandlehndPtr Word8bufIntszletchunk :: ByteStringchunk=ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8fpIntreadcount-- We rely on the hGetBuf behaviour (not hGetBufSome) where it reads up-- to the size we ask for, or EOF. So short reads indicate EOF.ifIntreadcountInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<IntszBool -> Bool -> Bool&&IntszInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>Int0thenByteString -> IO ByteStringforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(ByteString -> IO ByteString) -> ByteString -> IO ByteStringforall a b. (a -> b) -> a -> b$![ByteString] -> ByteStringconcat([ByteString] -> [ByteString]forall a. [a] -> [a]P.reverse(ByteStringchunkByteString -> [ByteString] -> [ByteString]forall a. a -> [a] -> [a]:[ByteString]chunks))else[ByteString] -> Int -> Int -> IO ByteStringreadChunks(ByteStringchunkByteString -> [ByteString] -> [ByteString]forall a. a -> [a] -> [a]:[ByteString]chunks)Intsz'((IntszInt -> Int -> Intforall a. Num a => a -> a -> a+Intsz')Int -> Int -> Intforall a. Ord a => a -> a -> a`min`Int32752)-- we grow the buffer sizes, but not too huge-- we concatenate in the end anyway-- | getContents. Read stdin strictly. Equivalent to hGetContents stdin-- The 'Handle' is closed after the contents have been read.--getContents::IOByteStringgetContents :: IO ByteStringgetContents=Handle -> IO ByteStringhGetContentsHandlestdin-- | The interact function takes a function of type @ByteString -> ByteString@-- as its argument. The entire input from the standard input device is passed-- to this function as its argument, and the resulting string is output on the-- standard output device.--interact::(ByteString->ByteString)->IO()interact :: (ByteString -> ByteString) -> IO ()interactByteString -> ByteStringtransformer=ByteString -> IO ()putStr(ByteString -> IO ())-> (ByteString -> ByteString) -> ByteString -> IO ()forall b c a. (b -> c) -> (a -> b) -> a -> c.ByteString -> ByteStringtransformer(ByteString -> IO ()) -> IO ByteString -> IO ()forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b=<<IO ByteStringgetContents-- | Read an entire file strictly into a 'ByteString'.--readFile::FilePath->IOByteStringreadFile :: FilePath -> IO ByteStringreadFileFilePathf=FilePath -> IOMode -> (Handle -> IO ByteString) -> IO ByteStringforall r. FilePath -> IOMode -> (Handle -> IO r) -> IO rwithBinaryFileFilePathfIOModeReadMode((Handle -> IO ByteString) -> IO ByteString)-> (Handle -> IO ByteString) -> IO ByteStringforall a b. (a -> b) -> a -> b$\Handleh->do-- hFileSize fails if file is not regular file (like-- /dev/null). Catch exception and try reading anyway.Integerfilesz<-IO Integer -> (IOException -> IO Integer) -> IO Integerforall e a. Exception e => IO a -> (e -> IO a) -> IO acatch(Handle -> IO IntegerhFileSizeHandleh)IOException -> IO IntegeruseZeroIfNotRegularFileletreadsz :: Intreadsz=(Integer -> Intforall a b. (Integral a, Num b) => a -> bfromIntegralIntegerfileszInt -> Int -> Intforall a. Ord a => a -> a -> a`max`Int0)Int -> Int -> Intforall a. Num a => a -> a -> a+Int1Handle -> Int -> Int -> IO ByteStringhGetContentsSizeHintHandlehIntreadsz(IntreadszInt -> Int -> Intforall a. Ord a => a -> a -> a`max`Int255)-- Our initial size is one bigger than the file size so that in the-- typical case we will read the whole file in one go and not have-- to allocate any more chunks. We'll still do the right thing if the-- file size is 0 or is changed before we do the read.whereuseZeroIfNotRegularFile::IOException->IOIntegeruseZeroIfNotRegularFile :: IOException -> IO IntegeruseZeroIfNotRegularFileIOException_=Integer -> IO Integerforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnInteger0modifyFile::IOMode->FilePath->ByteString->IO()modifyFile :: IOMode -> FilePath -> ByteString -> IO ()modifyFileIOModemodeFilePathfByteStringtxt=FilePath -> IOMode -> (Handle -> IO ()) -> IO ()forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO rwithBinaryFileFilePathfIOModemode(Handle -> ByteString -> IO ()`hPut`ByteStringtxt)-- | Write a 'ByteString' to a file.writeFile::FilePath->ByteString->IO()writeFile :: FilePath -> ByteString -> IO ()writeFile=IOMode -> FilePath -> ByteString -> IO ()modifyFileIOModeWriteMode-- | Append a 'ByteString' to a file.appendFile::FilePath->ByteString->IO()appendFile :: FilePath -> ByteString -> IO ()appendFile=IOMode -> FilePath -> ByteString -> IO ()modifyFileIOModeAppendMode-- ----------------------------------------------------------------------- Internal utilities-- Common up near identical calls to `error' to reduce the number-- constant strings created when compiled:errorEmptyList::HasCallStack=>String->aerrorEmptyList :: forall a. HasCallStack => FilePath -> aerrorEmptyListFilePathfun=FilePath -> FilePath -> aforall a. HasCallStack => FilePath -> FilePath -> amoduleErrorFilePathfunFilePath"empty ByteString"{-# NOINLINEerrorEmptyList#-}moduleError::HasCallStack=>String->String->amoduleError :: forall a. HasCallStack => FilePath -> FilePath -> amoduleErrorFilePathfunFilePathmsg=FilePath -> aforall a. HasCallStack => FilePath -> aerror(FilePath -> FilePath -> FilePathmoduleErrorMsgFilePathfunFilePathmsg){-# NOINLINEmoduleError#-}moduleErrorIO::HasCallStack=>String->String->IOamoduleErrorIO :: forall a. HasCallStack => FilePath -> FilePath -> IO amoduleErrorIOFilePathfunFilePathmsg=IOException -> IO aforall e a. Exception e => e -> IO athrowIO(IOException -> IO a)-> (FilePath -> IOException) -> FilePath -> IO aforall b c a. (b -> c) -> (a -> b) -> a -> c.FilePath -> IOExceptionuserError(FilePath -> IO a) -> FilePath -> IO aforall a b. (a -> b) -> a -> b$FilePath -> FilePath -> FilePathmoduleErrorMsgFilePathfunFilePathmsg{-# NOINLINEmoduleErrorIO#-}moduleErrorMsg::String->String->StringmoduleErrorMsg :: FilePath -> FilePath -> FilePathmoduleErrorMsgFilePathfunFilePathmsg=FilePath"Data.ByteString."FilePath -> FilePath -> FilePathforall a. [a] -> [a] -> [a]++FilePathfunFilePath -> FilePath -> FilePathforall a. [a] -> [a] -> [a]++Char':'Char -> FilePath -> FilePathforall a. a -> [a] -> [a]:Char' 'Char -> FilePath -> FilePathforall a. a -> [a] -> [a]:FilePathmsg-- Find from the end of the string using predicatefindFromEndUntil::(Word8->Bool)->ByteString->IntfindFromEndUntil :: (Word8 -> Bool) -> ByteString -> IntfindFromEndUntilWord8 -> Boolfps :: ByteStringps@(BSForeignPtr Word8_Intl)=caseByteString -> Maybe (ByteString, Word8)unsnocByteStringpsofMaybe (ByteString, Word8)Nothing->Int0Just(ByteStringb,Word8c)->ifWord8 -> BoolfWord8cthenIntlelse(Word8 -> Bool) -> ByteString -> IntfindFromEndUntilWord8 -> BoolfByteStringb

[8]ページ先頭

©2009-2025 Movatter.jp