Movatterモバイル変換


[0]ホーム

URL:


{-# LANGUAGE BangPatterns #-}{-# LANGUAGE MagicHash #-}{-# LANGUAGE NamedFieldPuns #-}{-# LANGUAGE TupleSections #-}{-# OPTIONS_HADDOCK prune #-}{-# LANGUAGE Trustworthy #-}-- |-- 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,-- * 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,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,null,length,map,lines,foldl,foldr,unlines,concat,any,take,drop,splitAt,takeWhile,dropWhile,span,break,elem,filter,maximum,minimum,all,concatMap,foldl1,foldr1,scanl,scanl1,scanr,scanr1,readFile,writeFile,appendFile,replicate,getContents,getLine,putStr,putStrLn,interact,zip,zipWith,unzip,notElem)importData.Bits(finiteBitSize,shiftL,(.|.),(.&.))importData.ByteString.InternalimportData.ByteString.Lazy.Internal(fromStrict,toStrict)importData.ByteString.UnsafeimportqualifiedData.ListasListimportData.Word(Word8)importControl.Exception(IOException,catch,finally,assert,throwIO)importControl.Monad(when,void)importForeign.C.String(CString,CStringLen)importForeign.C.Types(CSize(CSize),CInt(CInt))importForeign.ForeignPtr(ForeignPtr,withForeignPtr,touchForeignPtr)importForeign.ForeignPtr.Unsafe(unsafeForeignPtrToPtr)importForeign.Marshal.Alloc(allocaBytes)importForeign.Marshal.Array(allocaArray)importForeign.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.IO(unsafePerformIO,unsafeDupablePerformIO)importGHC.Foreign(newCStringLen,peekCStringLen)importGHC.Stack.Types(HasCallStack)importData.Char(ord)importForeign.Marshal.Utils(copyBytes)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 :: 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 (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. (?callStack::CallStack) => 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. (?callStack::CallStack) => 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 Word8xIntl)=Int -> (Ptr Word8 -> IO ()) -> ByteStringunsafeCreate(IntlInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)((Ptr Word8 -> IO ()) -> ByteString)-> (Ptr Word8 -> IO ()) -> ByteStringforall 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->doPtr Word8 -> Word8 -> IO ()forall a. Storable a => Ptr a -> a -> IO ()pokePtr Word8pWord8cPtr Word8 -> Ptr Word8 -> Int -> IO ()memcpy(Ptr Word8pPtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`Int1)Ptr Word8fIntl{-# INLINEcons#-}-- | /O(n)/ Append a byte to the end of a 'ByteString'snoc::ByteString->Word8->ByteStringsnoc :: ByteString -> Word8 -> ByteStringsnoc(BSForeignPtr Word8xIntl)Word8c=Int -> (Ptr Word8 -> IO ()) -> ByteStringunsafeCreate(IntlInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)((Ptr Word8 -> IO ()) -> ByteString)-> (Ptr Word8 -> IO ()) -> ByteStringforall 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->doPtr Word8 -> Ptr Word8 -> Int -> IO ()memcpyPtr Word8pPtr Word8fIntlPtr Word8 -> Word8 -> IO ()forall a. Storable a => Ptr a -> a -> IO ()poke(Ptr Word8pPtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`Intl)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.head::HasCallStack=>ByteString->Word8head :: ByteString -> Word8head(BSForeignPtr Word8xIntl)|IntlInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0=FilePath -> Word8forall a. (?callStack::CallStack) => 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.tail::HasCallStack=>ByteString->ByteStringtail :: ByteString -> ByteStringtail(BSForeignPtr Word8pIntl)|IntlInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0=FilePath -> ByteStringforall a. (?callStack::CallStack) => 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.last::HasCallStack=>ByteString->Word8last :: ByteString -> Word8lastps :: ByteStringps@(BSForeignPtr Word8xIntl)|ByteString -> BoolnullByteStringps=FilePath -> Word8forall a. (?callStack::CallStack) => 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 a b. Storable a => Ptr b -> Int -> IO apeekByteOffPtr Word8p(IntlInt -> Int -> Intforall a. Num a => a -> a -> a-Int1){-# INLINElast#-}-- | /O(1)/ Return all the elements of a 'ByteString' except the last one.-- An exception will be thrown in the case of an empty ByteString.init::HasCallStack=>ByteString->ByteStringinit :: ByteString -> ByteStringinitps :: ByteStringps@(BSForeignPtr Word8pIntl)|ByteString -> BoolnullByteStringps=FilePath -> ByteStringforall a. (?callStack::CallStack) => 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 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 Word8fpIntlen)=IO ByteString -> ByteStringforall a. IO a -> aunsafeDupablePerformIO(IO ByteString -> ByteString) -> IO ByteString -> ByteStringforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO ByteString) -> IO ByteStringforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO ByteString) -> IO ByteString)-> (Ptr Word8 -> IO ByteString) -> IO ByteStringforall a b. (a -> b) -> a -> b$\Ptr Word8srcPtr->Int -> (Ptr Word8 -> IO ()) -> IO ByteStringcreateIntlen((Ptr Word8 -> IO ()) -> IO ByteString)-> (Ptr Word8 -> IO ()) -> IO ByteStringforall a b. (a -> b) -> a -> b$\Ptr Word8dstPtr->Ptr Word8 -> Ptr Word8 -> IO ()forall b b. Ptr b -> Ptr b -> IO ()mPtr Word8srcPtrPtr Word8dstPtrwherem :: Ptr b -> Ptr b -> IO ()m!Ptr bp1!Ptr bp2=Int -> IO ()map_Int0wheremap_::Int->IO()map_ :: Int -> IO ()map_!Intn|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intlen=() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn()|Boolotherwise=doWord8x<-Ptr b -> Int -> IO Word8forall a b. Storable a => Ptr b -> Int -> IO apeekByteOffPtr bp1IntnPtr b -> Int -> Word8 -> IO ()forall a b. Storable a => Ptr b -> Int -> a -> IO ()pokeByteOffPtr 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 -> (Ptr Word8 -> IO ()) -> ByteStringunsafeCreateIntl((Ptr Word8 -> IO ()) -> ByteString)-> (Ptr Word8 -> IO ()) -> ByteStringforall 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 -> (Ptr Word8 -> IO ()) -> ByteStringunsafeCreate(Int2Int -> Int -> Intforall a. Num a => a -> a -> a*IntlInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)((Ptr Word8 -> IO ()) -> ByteString)-> (Ptr Word8 -> IO ()) -> ByteStringforall 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 :: (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 (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' :: (a -> Word8 -> a) -> a -> ByteString -> afoldl'a -> Word8 -> afav=\(BSForeignPtr Word8fpIntlen)->-- see fold inliningletg :: Ptr Word8 -> IO agPtr Word8ptr=a -> Ptr Word8 -> IO agoavPtr Word8ptrwhereend :: Ptr bend=Ptr Word8ptrPtr Word8 -> Int -> Ptr bforall a b. Ptr a -> Int -> Ptr b`plusPtr`Intlen-- tail recursive; traverses array left to rightgo :: a -> Ptr Word8 -> IO ago!az!Ptr Word8p|Ptr Word8pPtr Word8 -> Ptr Word8 -> Boolforall a. Eq a => a -> a -> Bool==Ptr Word8forall b. Ptr bend=a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnaz|Boolotherwise=doWord8x<-Ptr Word8 -> IO Word8forall a. Storable a => Ptr a -> IO apeekPtr Word8pa -> Ptr Word8 -> IO ago(a -> Word8 -> afazWord8x)(Ptr Word8pPtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`Int1)inIO a -> aforall a. IO a -> aaccursedUnutterablePerformIO(IO a -> a) -> IO a -> aforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO a) -> IO aforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fpPtr Word8 -> IO ag{-# 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 :: (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 (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' :: (Word8 -> a -> a) -> a -> ByteString -> afoldr'Word8 -> a -> akav=\(BSForeignPtr Word8fpIntlen)->-- see fold inliningletg :: Ptr a -> IO agPtr aptr=a -> Ptr Word8 -> IO agoav(Ptr Anyforall b. Ptr bendPtr Any -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`Intlen)whereend :: Ptr bend=Ptr aptrPtr a -> Int -> Ptr bforall a b. Ptr a -> Int -> Ptr b`plusPtr`(-Int1)-- tail recursive; traverses array right to leftgo :: a -> Ptr Word8 -> IO ago!az!Ptr Word8p|Ptr Word8pPtr Word8 -> Ptr Word8 -> Boolforall a. Eq a => a -> a -> Bool==Ptr Word8forall b. Ptr bend=a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnaz|Boolotherwise=doWord8x<-Ptr Word8 -> IO Word8forall a. Storable a => Ptr a -> IO apeekPtr Word8pa -> Ptr Word8 -> IO ago(Word8 -> a -> akWord8xaz)(Ptr Word8pPtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`(-Int1))inIO a -> aforall a. IO a -> aaccursedUnutterablePerformIO(IO a -> a) -> IO a -> aforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO a) -> IO aforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fpPtr Word8 -> IO aforall a. Ptr a -> IO ag{-# 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 :: (Word8 -> Word8 -> Word8) -> ByteString -> Word8foldl1Word8 -> Word8 -> Word8fByteStringps=caseByteString -> Maybe (Word8, ByteString)unconsByteStringpsofMaybe (Word8, ByteString)Nothing->FilePath -> Word8forall a. (?callStack::CallStack) => 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' :: (Word8 -> Word8 -> Word8) -> ByteString -> Word8foldl1'Word8 -> Word8 -> Word8fByteStringps=caseByteString -> Maybe (Word8, ByteString)unconsByteStringpsofMaybe (Word8, ByteString)Nothing->FilePath -> Word8forall a. (?callStack::CallStack) => 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 :: (Word8 -> Word8 -> Word8) -> ByteString -> Word8foldr1Word8 -> Word8 -> Word8fByteStringps=caseByteString -> Maybe (ByteString, Word8)unsnocByteStringpsofMaybe (ByteString, Word8)Nothing->FilePath -> Word8forall a. (?callStack::CallStack) => 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' :: (Word8 -> Word8 -> Word8) -> ByteString -> Word8foldr1'Word8 -> Word8 -> Word8fByteStringps=caseByteString -> Maybe (ByteString, Word8)unsnocByteStringpsofMaybe (ByteString, Word8)Nothing->FilePath -> Word8forall a. (?callStack::CallStack) => 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 -> (Ptr Word8 -> IO Bool) -> IO Boolforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8xPtr Word8 -> IO Boolgwhereg :: Ptr Word8 -> IO BoolgPtr Word8ptr=Ptr Word8 -> IO BoolgoPtr Word8ptrwhereend :: Ptr bend=Ptr Word8ptrPtr Word8 -> Int -> Ptr bforall a b. Ptr a -> Int -> Ptr b`plusPtr`Intlengo :: Ptr Word8 -> IO Boolgo!Ptr Word8p|Ptr Word8pPtr Word8 -> Ptr Word8 -> Boolforall a. Eq a => a -> a -> Bool==Ptr Word8forall b. Ptr bend=Bool -> IO Boolforall (m :: * -> *) a. Monad m => a -> m areturnBoolFalse|Boolotherwise=doWord8c<-Ptr Word8 -> IO Word8forall a. Storable a => Ptr a -> IO apeekPtr Word8pifWord8 -> BoolfWord8cthenBool -> IO Boolforall (m :: * -> *) a. Monad m => a -> m areturnBoolTrueelsePtr Word8 -> IO Boolgo(Ptr Word8pPtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`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 (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 -> (Ptr Word8 -> IO Bool) -> IO Boolforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8xPtr Word8 -> IO Boolgwhereg :: Ptr Word8 -> IO BoolgPtr Word8ptr=Ptr Word8 -> IO BoolgoPtr Word8ptrwhereend :: Ptr bend=Ptr Word8ptrPtr Word8 -> Int -> Ptr bforall a b. Ptr a -> Int -> Ptr b`plusPtr`Intlengo :: Ptr Word8 -> IO Boolgo!Ptr Word8p|Ptr Word8pPtr Word8 -> Ptr Word8 -> Boolforall a. Eq a => a -> a -> Bool==Ptr Word8forall b. Ptr bend=Bool -> IO Boolforall (m :: * -> *) a. Monad m => a -> m areturnBoolTrue-- end of list|Boolotherwise=doWord8c<-Ptr Word8 -> IO Word8forall a. Storable a => Ptr a -> IO apeekPtr Word8pifWord8 -> BoolfWord8cthenPtr Word8 -> IO Boolgo(Ptr Word8pPtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`Int1)elseBool -> IO Boolforall (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 :: ByteString -> Word8maximumxs :: ByteStringxs@(BSForeignPtr Word8xIntl)|ByteString -> BoolnullByteStringxs=FilePath -> Word8forall a. (?callStack::CallStack) => 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 :: ByteString -> Word8minimumxs :: ByteStringxs@(BSForeignPtr Word8xIntl)|ByteString -> BoolnullByteStringxs=FilePath -> Word8forall a. (?callStack::CallStack) => 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 :: (acc -> Word8 -> (acc, Word8))-> acc -> ByteString -> (acc, ByteString)mapAccumLacc -> Word8 -> (acc, Word8)faccacc=\(BSForeignPtr Word8fpIntlen)->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$ForeignPtr Word8-> (Ptr Word8 -> IO (acc, ByteString)) -> IO (acc, ByteString)forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO (acc, ByteString)) -> IO (acc, ByteString))-> (Ptr Word8 -> IO (acc, ByteString)) -> IO (acc, ByteString)forall a b. (a -> b) -> a -> b$\Ptr Word8a->do-- see fold inliningForeignPtr Word8gp<-Int -> IO (ForeignPtr Word8)forall a. Int -> IO (ForeignPtr a)mallocByteStringIntlenletgo :: Ptr b -> Ptr b -> IO accgoPtr bsrcPtr 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 (m :: * -> *) a. Monad m => a -> m areturnaccs|Boolotherwise=doWord8x<-Ptr b -> Int -> IO Word8forall a b. Storable a => Ptr b -> Int -> IO apeekByteOffPtr bsrcIntnlet(accs',Word8y)=acc -> Word8 -> (acc, Word8)faccsWord8xPtr b -> Int -> Word8 -> IO ()forall a b. Storable a => Ptr b -> Int -> a -> IO ()pokeByteOffPtr bdstIntnWord8yacc -> Int -> IO accmapAccumL_accs'(IntnInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)accacc'<-ForeignPtr Word8 -> (Ptr Word8 -> IO acc) -> IO accforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8gp(Ptr Word8 -> Ptr Word8 -> IO accforall b b. Ptr b -> Ptr b -> IO accgoPtr Word8a)(acc, ByteString) -> IO (acc, ByteString)forall (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 :: (acc -> Word8 -> (acc, Word8))-> acc -> ByteString -> (acc, ByteString)mapAccumRacc -> Word8 -> (acc, Word8)faccacc=\(BSForeignPtr Word8fpIntlen)->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$ForeignPtr Word8-> (Ptr Word8 -> IO (acc, ByteString)) -> IO (acc, ByteString)forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO (acc, ByteString)) -> IO (acc, ByteString))-> (Ptr Word8 -> IO (acc, ByteString)) -> IO (acc, ByteString)forall a b. (a -> b) -> a -> b$\Ptr Word8a->do-- see fold inliningForeignPtr Word8gp<-Int -> IO (ForeignPtr Word8)forall a. Int -> IO (ForeignPtr a)mallocByteStringIntlenletgo :: Ptr b -> Ptr b -> IO accgoPtr bsrcPtr 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 (m :: * -> *) a. Monad m => a -> m areturnaccsmapAccumR_!accs!Intn=doWord8x<-Ptr b -> Int -> IO Word8forall a b. Storable a => Ptr b -> Int -> IO apeekByteOffPtr bsrcIntnlet(accs',Word8y)=acc -> Word8 -> (acc, Word8)faccsWord8xPtr b -> Int -> Word8 -> IO ()forall a b. Storable a => Ptr b -> Int -> a -> IO ()pokeByteOffPtr bdstIntnWord8yacc -> Int -> IO accmapAccumR_accs'(IntnInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)accacc'<-ForeignPtr Word8 -> (Ptr Word8 -> IO acc) -> IO accforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8gp(Ptr Word8 -> Ptr Word8 -> IO accforall b b. Ptr b -> Ptr b -> IO accgoPtr Word8a)(acc, ByteString) -> IO (acc, ByteString)forall (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 Word8fpIntlen)->IO ByteString -> ByteStringforall a. IO a -> aunsafeDupablePerformIO(IO ByteString -> ByteString) -> IO ByteString -> ByteStringforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO ByteString) -> IO ByteStringforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO ByteString) -> IO ByteString)-> (Ptr Word8 -> IO ByteString) -> IO ByteStringforall a b. (a -> b) -> a -> b$\Ptr Word8a->-- see fold inliningInt -> (Ptr Word8 -> IO ()) -> IO ByteStringcreate(IntlenInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)((Ptr Word8 -> IO ()) -> IO ByteString)-> (Ptr Word8 -> IO ()) -> IO ByteStringforall a b. (a -> b) -> a -> b$\Ptr Word8q->doPtr Word8 -> Word8 -> IO ()forall a. Storable a => Ptr a -> a -> IO ()pokePtr Word8qWord8vletgo :: Ptr b -> Ptr b -> IO ()goPtr bsrcPtr bdst=Word8 -> Int -> IO ()scanl_Word8vInt0wherescanl_ :: Word8 -> Int -> IO ()scanl_!Word8z!Intn|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intlen=() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn()|Boolotherwise=doWord8x<-Ptr b -> Int -> IO Word8forall a b. Storable a => Ptr b -> Int -> IO apeekByteOffPtr bsrcIntnletz' :: Word8z'=Word8 -> Word8 -> Word8fWord8zWord8xPtr b -> Int -> Word8 -> IO ()forall a b. Storable a => Ptr b -> Int -> a -> IO ()pokeByteOffPtr bdstIntnWord8z'Word8 -> Int -> IO ()scanl_Word8z'(IntnInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)Ptr Word8 -> Ptr Any -> IO ()forall b b. Ptr b -> Ptr b -> IO ()goPtr Word8a(Ptr Word8qPtr Word8 -> Int -> Ptr Anyforall a b. Ptr a -> Int -> Ptr b`plusPtr`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 Word8fpIntlen)->IO ByteString -> ByteStringforall a. IO a -> aunsafeDupablePerformIO(IO ByteString -> ByteString) -> IO ByteString -> ByteStringforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO ByteString) -> IO ByteStringforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO ByteString) -> IO ByteString)-> (Ptr Word8 -> IO ByteString) -> IO ByteStringforall a b. (a -> b) -> a -> b$\Ptr Word8a->-- see fold inliningInt -> (Ptr Word8 -> IO ()) -> IO ByteStringcreate(IntlenInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)((Ptr Word8 -> IO ()) -> IO ByteString)-> (Ptr Word8 -> IO ()) -> IO ByteStringforall a b. (a -> b) -> a -> b$\Ptr Word8b->doPtr Word8 -> Word8 -> IO ()forall a. Storable a => Ptr a -> a -> IO ()poke(Ptr Word8bPtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`Intlen)Word8vletgo :: Ptr b -> Ptr b -> IO ()goPtr bpPtr 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 (m :: * -> *) a. Monad m => a -> m areturn()|Boolotherwise=doWord8x<-Ptr b -> Int -> IO Word8forall a b. Storable a => Ptr b -> Int -> IO apeekByteOffPtr bpIntnletz' :: Word8z'=Word8 -> Word8 -> Word8fWord8xWord8zPtr b -> Int -> Word8 -> IO ()forall a b. Storable a => Ptr b -> Int -> a -> IO ()pokeByteOffPtr bqIntnWord8z'Word8 -> Int -> IO ()scanr_Word8z'(IntnInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)Ptr Word8 -> Ptr Word8 -> IO ()forall b b. Ptr b -> Ptr b -> IO ()goPtr Word8aPtr 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 = unfoldr w (\u -> Just (u,u)) c---- This implementation uses @memset(3)@replicate::Int->Word8->ByteStringreplicate :: Int -> Word8 -> ByteStringreplicateIntwWord8c|IntwInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int0=ByteStringempty|Boolotherwise=Int -> (Ptr Word8 -> IO ()) -> ByteStringunsafeCreateIntw((Ptr Word8 -> IO ()) -> ByteString)-> (Ptr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\Ptr Word8ptr->IO (Ptr Word8) -> IO ()forall (f :: * -> *) a. Functor f => f a -> f ()void(IO (Ptr Word8) -> IO ()) -> IO (Ptr Word8) -> IO ()forall a b. (a -> b) -> a -> b$Ptr Word8 -> Word8 -> CSize -> IO (Ptr Word8)memsetPtr Word8ptrWord8c(Int -> CSizeforall a b. (Integral a, Num b) => a -> bfromIntegralIntw){-# 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 :: (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 :: 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 -> aunsafePerformIO(IO (ByteString, Maybe a) -> (ByteString, Maybe a))-> IO (ByteString, Maybe a) -> (ByteString, Maybe a)forall a b. (a -> b) -> a -> b$Int-> (Ptr Word8 -> IO (Int, Int, Maybe a))-> IO (ByteString, Maybe a)forall a.Int -> (Ptr Word8 -> IO (Int, Int, a)) -> IO (ByteString, a)createAndTrim'Inti((Ptr Word8 -> IO (Int, Int, Maybe a)) -> IO (ByteString, Maybe a))-> (Ptr Word8 -> IO (Int, Int, Maybe a))-> IO (ByteString, Maybe a)forall a b. (a -> b) -> a -> b$\Ptr Word8p->Ptr Word8 -> a -> Int -> IO (Int, Int, Maybe a)forall a b. Num a => Ptr b -> a -> Int -> IO (a, Int, Maybe a)goPtr Word8pax0Int0wherego :: Ptr b -> a -> Int -> IO (a, Int, Maybe a)go!Ptr 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 (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 (m :: * -> *) a. Monad m => a -> m areturn(a0,Intn',Maybe aforall a. Maybe aNothing)Just(Word8w,ax'')->doPtr b -> Int -> Word8 -> IO ()forall a b. Storable a => Ptr b -> Int -> a -> IO ()pokeByteOffPtr 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 @('takeWhileEnd' (not . p) &&& 'dropWhileEnd' (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 (m :: * -> *) a. Monad m => a -> m areturn(ByteStringps,ByteStringempty)|Boolotherwise=doWord8c'<-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 (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 @('takeWhileEnd' p &&& 'dropWhileEnd' 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-> (Ptr Word8 -> IO [ByteString]) -> IO [ByteString]forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO [ByteString]) -> IO [ByteString])-> (Ptr Word8 -> IO [ByteString]) -> IO [ByteString]forall a b. (a -> b) -> a -> b$\Ptr Word8p->Ptr Word8-> Int -> Int -> Int -> ForeignPtr Word8 -> IO [ByteString]splitLoopPtr Word8pInt0Intoff'Intlen'ForeignPtr Word8fp'splitLoop::PtrWord8->Int->Int->Int->ForeignPtrWord8->IO[ByteString]splitLoop :: Ptr Word8-> Int -> Int -> Int -> ForeignPtr Word8 -> IO [ByteString]splitLoopPtr 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 (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<-Ptr Word8 -> Int -> IO Word8forall a. Storable a => Ptr a -> Int -> IO apeekElemOffPtr Word8p(Intoff'Int -> Int -> Intforall a. Num a => a -> a -> a+Intidx')ifWord8 -> BoolpredicateWord8wthen[ByteString] -> IO [ByteString]forall (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 Word8fSepPtrIntsepLen)(BSForeignPtr Word8fhPtrInthLen:[ByteString]t)=Int -> (Ptr Word8 -> IO ()) -> ByteStringunsafeCreateInttotalLen((Ptr Word8 -> IO ()) -> ByteString)-> (Ptr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\Ptr Word8dstPtr0->ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fSepPtr((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Word8sepPtr->doForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fhPtr((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Word8hPtr->Ptr Word8 -> Ptr Word8 -> Int -> IO ()memcpyPtr Word8dstPtr0Ptr Word8hPtrInthLenletgo :: Ptr Word8 -> [ByteString] -> IO ()goPtr Word8_[]=() -> IO ()forall (f :: * -> *) a. Applicative f => a -> f apure()goPtr Word8dstPtr(BSForeignPtr Word8fChunkPtrIntchunkLen:[ByteString]chunks)=doPtr Word8 -> Ptr Word8 -> Int -> IO ()memcpyPtr Word8dstPtrPtr Word8sepPtrIntsepLenletdestPtr' :: Ptr bdestPtr'=Ptr Word8dstPtrPtr Word8 -> Int -> Ptr bforall a b. Ptr a -> Int -> Ptr b`plusPtr`IntsepLenForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fChunkPtr((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Word8chunkPtr->Ptr Word8 -> Ptr Word8 -> Int -> IO ()memcpyPtr Word8forall b. Ptr bdestPtr'Ptr Word8chunkPtrIntchunkLenPtr Word8 -> [ByteString] -> IO ()go(Ptr Anyforall b. Ptr bdestPtr'Ptr Any -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`IntchunkLen)[ByteString]chunksPtr Word8 -> [ByteString] -> IO ()go(Ptr Word8dstPtr0Ptr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`InthLen)[ByteString]twheretotalLen :: InttotalLen=(Int -> ByteString -> Int) -> Int -> [ByteString] -> Intforall (t :: * -> *) b a.Foldable t =>(b -> a -> b) -> b -> t a -> bList.foldl'(\Intacc(BSForeignPtr Word8_IntchunkLen)->IntaccInt -> Int -> Intforall a. Num a => a -> a -> a+IntchunkLenInt -> Int -> Intforall a. Num a => a -> a -> a+IntsepLen)InthLen[ByteString]t{-# INLINEintercalate#-}-- ----------------------------------------------------------------------- Indexing ByteStrings-- | /O(1)/ 'ByteString' index (subscript) operator, starting from 0.index::HasCallStack=>ByteString->Int->Word8index :: ByteString -> Int -> Word8indexByteStringpsIntn|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int0=FilePath -> FilePath -> Word8forall a. (?callStack::CallStack) => 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. (?callStack::CallStack) => 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 (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 (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 (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 -> (Ptr Word8 -> IO (Maybe Int)) -> IO (Maybe Int)forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8xPtr Word8 -> IO (Maybe Int)forall a. Ptr a -> IO (Maybe Int)gwhereg :: Ptr a -> IO (Maybe Int)g!Ptr 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 (m :: * -> *) a. Monad m => a -> m areturnMaybe Intforall a. Maybe aNothing|Boolotherwise=doWord8w<-Ptr Word8 -> IO Word8forall a. Storable a => Ptr a -> IO apeek(Ptr Word8 -> IO Word8) -> Ptr Word8 -> IO Word8forall a b. (a -> b) -> a -> b$Ptr aptrPtr a -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`IntnifWord8 -> BoolkWord8wthenMaybe Int -> IO (Maybe Int)forall (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 -> (Ptr Word8 -> IO (Maybe Int)) -> IO (Maybe Int)forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8xPtr Word8 -> IO (Maybe Int)forall a. Ptr a -> IO (Maybe Int)gwhereg :: Ptr b -> IO (Maybe Int)g!Ptr bptr=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 (m :: * -> *) a. Monad m => a -> m areturnMaybe Intforall a. Maybe aNothing|Boolotherwise=doWord8w<-Ptr b -> Int -> IO Word8forall a b. Storable a => Ptr b -> Int -> IO apeekByteOffPtr bptrIntnifWord8 -> BoolkWord8wthenMaybe Int -> IO (Maybe Int)forall (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 Word8xIntl)->-- see fold inlining.ifByteString -> BoolnullByteStringpsthenByteStringpselseIO ByteString -> ByteStringforall a. IO a -> aunsafePerformIO(IO ByteString -> ByteString) -> IO ByteString -> ByteStringforall a b. (a -> b) -> a -> b$Int -> (Ptr Word8 -> IO Int) -> IO ByteStringcreateAndTrimIntl((Ptr Word8 -> IO Int) -> IO ByteString)-> (Ptr Word8 -> IO Int) -> IO ByteStringforall a b. (a -> b) -> a -> b$\Ptr Word8pOut->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 Word8pIn->doletgo' :: Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)go'Ptr Word8pfPtr Word8pt=Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)goPtr Word8pfPtr Word8ptwhereend :: Ptr bend=Ptr Word8pfPtr Word8 -> Int -> Ptr bforall a b. Ptr a -> Int -> Ptr b`plusPtr`Intlgo :: Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)go!Ptr Word8f!Ptr Word8t|Ptr Word8fPtr Word8 -> Ptr Word8 -> Boolforall a. Eq a => a -> a -> Bool==Ptr Word8forall b. Ptr bend=Ptr Word8 -> IO (Ptr Word8)forall (m :: * -> *) a. Monad m => a -> m areturnPtr Word8t|Boolotherwise=doWord8w<-Ptr Word8 -> IO Word8forall a. Storable a => Ptr a -> IO apeekPtr Word8fifWord8 -> BoolkWord8wthenPtr Word8 -> Word8 -> IO ()forall a. Storable a => Ptr a -> a -> IO ()pokePtr Word8tWord8wIO () -> IO (Ptr Word8) -> IO (Ptr Word8)forall (m :: * -> *) a b. Monad m => m a -> m b -> m b>>Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)go(Ptr Word8fPtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`Int1)(Ptr Word8tPtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`Int1)elsePtr Word8 -> Ptr Word8 -> IO (Ptr Word8)go(Ptr Word8fPtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`Int1)Ptr Word8tPtr Word8t<-Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)go'Ptr Word8pInPtr Word8pOutInt -> IO Intforall (m :: * -> *) a. Monad m => a -> m areturn(Int -> IO Int) -> Int -> IO Intforall a b. (a -> b) -> a -> b$!Ptr Word8tPtr Word8 -> Ptr Word8 -> Intforall a b. Ptr a -> Ptr b -> Int`minusPtr`Ptr 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 Word8fp'<-Int -> IO (ForeignPtr Word8)forall a. Int -> IO (ForeignPtr a)mallocByteStringIntlenForeignPtr Word8-> (Ptr Word8 -> IO (ByteString, ByteString))-> IO (ByteString, ByteString)forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp'((Ptr Word8 -> IO (ByteString, ByteString)) -> IO (ByteString, ByteString))-> (Ptr Word8 -> IO (ByteString, ByteString))-> IO (ByteString, ByteString)forall a b. (a -> b) -> a -> b$\Ptr Word8p->doletend :: Ptr bend=Ptr Word8pPtr Word8 -> Int -> Ptr bforall a b. Ptr a -> Int -> Ptr b`plusPtr`(IntlenInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)Ptr Word8mid<-Int -> Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)sepInt0Ptr Word8pPtr Word8forall b. Ptr bendPtr Word8 -> Ptr Word8 -> IO ()forall b. Storable b => Ptr b -> Ptr b -> IO ()revPtr Word8midPtr Word8forall b. Ptr bendleti :: Inti=Ptr Word8midPtr Word8 -> Ptr Word8 -> Intforall a b. Ptr a -> Ptr b -> Int`minusPtr`Ptr Word8p(ByteString, ByteString) -> IO (ByteString, ByteString)forall (m :: * -> *) a. Monad m => a -> m areturn(ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8fp'Inti,ForeignPtr Word8 -> Int -> ByteStringBS(ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr bplusForeignPtrForeignPtr Word8fp'Inti)(IntlenInt -> Int -> Intforall a. Num a => a -> a -> a-Inti))wherelen :: Intlen=ByteString -> IntlengthByteStringsincr :: Ptr a -> Ptr bincr=(Ptr a -> Int -> Ptr bforall a b. Ptr a -> Int -> Ptr b`plusPtr`Int1)decr :: Ptr a -> Ptr bdecr=(Ptr a -> Int -> Ptr bforall a b. Ptr a -> Int -> Ptr b`plusPtr`(-Int1))sep :: Int -> Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)sep!Inti!Ptr Word8p1!Ptr Word8p2|IntiInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==Intlen=Ptr Word8 -> IO (Ptr Word8)forall (m :: * -> *) a. Monad m => a -> m areturnPtr Word8p1|Word8 -> BoolfWord8w=doPtr Word8 -> Word8 -> IO ()forall a. Storable a => Ptr a -> a -> IO ()pokePtr Word8p1Word8wInt -> Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)sep(IntiInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)(Ptr Word8 -> Ptr Word8forall a b. Ptr a -> Ptr bincrPtr Word8p1)Ptr Word8p2|Boolotherwise=doPtr Word8 -> Word8 -> IO ()forall a. Storable a => Ptr a -> a -> IO ()pokePtr Word8p2Word8wInt -> Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)sep(IntiInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)Ptr Word8p1(Ptr Word8 -> Ptr Word8forall a b. Ptr a -> Ptr bdecrPtr Word8p2)wherew :: Word8w=ByteStringsByteString -> Int -> Word8`unsafeIndex`Intirev :: Ptr b -> Ptr b -> IO ()rev!Ptr bp1!Ptr bp2|Ptr bp1Ptr b -> Ptr b -> Boolforall a. Ord a => a -> a -> Bool>=Ptr bp2=() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn()|Boolotherwise=doba<-Ptr b -> IO bforall a. Storable a => Ptr a -> IO apeekPtr bp1bb<-Ptr b -> IO bforall a. Storable a => Ptr a -> IO apeekPtr bp2Ptr b -> b -> IO ()forall a. Storable a => Ptr a -> a -> IO ()pokePtr bp1bbPtr b -> b -> IO ()forall a. Storable a => Ptr a -> a -> IO ()pokePtr bp2baPtr b -> Ptr b -> IO ()rev(Ptr b -> Ptr bforall a b. Ptr a -> Ptr bincrPtr bp1)(Ptr b -> Ptr bforall a b. Ptr a -> Ptr bdecrPtr 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 (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 (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 (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-- We import bytestring_is_valid_utf8 both unsafe and safe. For small inputs-- we can use the unsafe version to get a bit more performance, but for large-- inputs the safe version should be used to avoid GC synchronization pauses-- in multithreaded contexts.foreignimportccallunsafe"bytestring_is_valid_utf8"cIsValidUtf8::PtrWord8->CSize->IOCIntforeignimportccallsafe"bytestring_is_valid_utf8"cIsValidUtf8Safe::PtrWord8->CSize->IOCInt-- | 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 :: (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 Word8fpIntl)(BSForeignPtr Word8fqIntm)=IO ByteString -> ByteStringforall a. IO a -> aunsafeDupablePerformIO(IO ByteString -> ByteString) -> IO ByteString -> ByteStringforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO ByteString) -> IO ByteStringforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO ByteString) -> IO ByteString)-> (Ptr Word8 -> IO ByteString) -> IO ByteStringforall a b. (a -> b) -> a -> b$\Ptr Word8a->ForeignPtr Word8 -> (Ptr Word8 -> IO ByteString) -> IO ByteStringforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fq((Ptr Word8 -> IO ByteString) -> IO ByteString)-> (Ptr Word8 -> IO ByteString) -> IO ByteStringforall a b. (a -> b) -> a -> b$\Ptr Word8b->Int -> (Ptr Word8 -> IO ()) -> IO ByteStringcreateIntlen((Ptr Word8 -> IO ()) -> IO ByteString)-> (Ptr Word8 -> IO ()) -> IO ByteStringforall a b. (a -> b) -> a -> b$Ptr Word8 -> Ptr Word8 -> Ptr Word8 -> IO ()forall b b. Ptr b -> Ptr b -> Ptr Word8 -> IO ()goPtr Word8aPtr Word8bwherego :: Ptr b -> Ptr b -> Ptr Word8 -> IO ()goPtr bp1Ptr bp2=Int -> Ptr Word8 -> IO ()zipWith_Int0wherezipWith_::Int->PtrWord8->IO()zipWith_ :: Int -> Ptr Word8 -> IO ()zipWith_!Intn!Ptr Word8r|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intlen=() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn()|Boolotherwise=doWord8x<-Ptr b -> Int -> IO Word8forall a b. Storable a => Ptr b -> Int -> IO apeekByteOffPtr bp1IntnWord8y<-Ptr b -> Int -> IO Word8forall a b. Storable a => Ptr b -> Int -> IO apeekByteOffPtr bp2IntnPtr Word8 -> Int -> Word8 -> IO ()forall a b. Storable a => Ptr b -> Int -> a -> IO ()pokeByteOffPtr Word8rIntn(Word8 -> Word8 -> Word8fWord8xWord8y)Int -> Ptr Word8 -> IO ()zipWith_(IntnInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)Ptr Word8rlen :: Intlen=Int -> Int -> Intforall a. Ord a => a -> a -> aminIntlIntm{-# INLINEpackZipWith#-}{-# RULES"ByteString specialise zipWith"forall(f::Word8->Word8->Word8)pq.zipWithfpq=unpack(packZipWithfpq)#-}-- | /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)/ Return all initial segments of the given 'ByteString', shortest first.inits::ByteString->[ByteString]inits :: ByteString -> [ByteString]inits(BSForeignPtr Word8xIntl)=[ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8xIntn|Intn<-[Int0..Intl]]-- | /O(n)/ Return all final segments of the given 'ByteString', longest first.tails::ByteString->[ByteString]tails :: ByteString -> [ByteString]tailsByteStringp|ByteString -> BoolnullByteStringp=[ByteStringempty]|Boolotherwise=ByteStringpByteString -> [ByteString] -> [ByteString]forall a. a -> [a] -> [a]:ByteString -> [ByteString]tails(ByteString -> ByteStringunsafeTailByteStringp)-- less efficent spacewise: tails (BS x l) = [BS (plusForeignPtr x n) (l-n) | n <- [0..l]]-- ----------------------------------------------------------------------- ** 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 -> (Ptr Word8 -> IO ()) -> ByteStringunsafeCreateIntl((Ptr Word8 -> IO ()) -> ByteString)-> (Ptr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\Ptr Word8ptr->ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8input((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Word8inp->doPtr Word8 -> Ptr Word8 -> Int -> IO ()memcpyPtr Word8ptrPtr Word8inpIntlPtr Word8 -> CSize -> IO ()c_sortPtr Word8ptr(Int -> CSizeforall a b. (Integral a, Num b) => a -> bfromIntegralIntl)|Boolotherwise=Int -> (Ptr Word8 -> IO ()) -> ByteStringunsafeCreateIntl((Ptr Word8 -> IO ()) -> ByteString)-> (Ptr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\Ptr Word8p->Int -> (Ptr CSize -> IO ()) -> IO ()forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO ballocaArrayInt256((Ptr CSize -> IO ()) -> IO ()) -> (Ptr CSize -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr CSizearr->doPtr Word8_<-Ptr Word8 -> Word8 -> CSize -> IO (Ptr Word8)memset(Ptr CSize -> Ptr Word8forall a b. Ptr a -> Ptr bcastPtrPtr CSizearr)Word80(CSize256CSize -> CSize -> CSizeforall a. Num a => a -> a -> a*Int -> CSizeforall a b. (Integral a, Num b) => a -> bfromIntegral(CSize -> Intforall a. Storable a => a -> IntsizeOf(CSizeforall a. (?callStack::CallStack) => aundefined::CSize)))ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8input(\Ptr Word8x->Ptr CSize -> Ptr Word8 -> Int -> IO ()countOccurrencesPtr CSizearrPtr Word8xIntl)letgo :: Int -> Ptr Word8 -> IO ()goInt256!Ptr Word8_=() -> IO ()forall (m :: * -> *) a. Monad m => a -> m areturn()goInti!Ptr Word8ptr=doCSizen<-Ptr CSize -> Int -> IO CSizeforall a. Storable a => Ptr a -> Int -> IO apeekElemOffPtr CSizearrIntiBool -> IO () -> IO ()forall (f :: * -> *). Applicative f => Bool -> f () -> f ()when(CSizenCSize -> CSize -> Boolforall a. Eq a => a -> a -> Bool/=CSize0)(IO () -> IO ()) -> IO () -> IO ()forall a b. (a -> b) -> a -> b$IO (Ptr Word8) -> IO ()forall (f :: * -> *) a. Functor f => f a -> f ()void(IO (Ptr Word8) -> IO ()) -> IO (Ptr Word8) -> IO ()forall a b. (a -> b) -> a -> b$Ptr Word8 -> Word8 -> CSize -> IO (Ptr Word8)memsetPtr Word8ptr(Int -> Word8forall a b. (Integral a, Num b) => a -> bfromIntegralInti)CSizenInt -> Ptr Word8 -> IO ()go(IntiInt -> Int -> Intforall a. Num a => a -> a -> a+Int1)(Ptr Word8ptrPtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`CSize -> Intforall a b. (Integral a, Num b) => a -> bfromIntegralCSizen)Int -> Ptr Word8 -> IO ()goInt0Ptr Word8pwhere-- Count the number of occurrences of each byte.-- Used by 'sort'countOccurrences::PtrCSize->PtrWord8->Int->IO()countOccurrences :: Ptr CSize -> Ptr Word8 -> Int -> IO ()countOccurrences!Ptr CSizecounts!Ptr Word8str!Intlen=Int -> IO ()goInt0wherego :: Int -> IO ()go!Inti|IntiInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==Intlen=() -> IO ()forall (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 (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 Word8strIntiCSizex<-Ptr CSize -> Int -> IO CSizeforall a. Storable a => Ptr a -> Int -> IO apeekElemOffPtr CSizecountsIntkPtr CSize -> Int -> CSize -> IO ()forall a. Storable a => Ptr a -> Int -> a -> IO ()pokeElemOffPtr CSizecountsIntk(CSizexCSize -> CSize -> CSizeforall a. Num a => a -> a -> a+CSize1)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 :: 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->-- Cannot use unsafeWithForeignPtr, because action can divergeForeignPtr Word8 -> (Ptr Word8 -> IO a) -> IO aforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bwithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO a) -> IO a) -> (Ptr Word8 -> IO a) -> IO aforall a b. (a -> b) -> a -> b$\Ptr Word8p->doPtr Word8 -> Ptr Word8 -> Int -> IO ()memcpyPtr Word8bufPtr Word8pIntlPtr Word8 -> 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.useAsCStringLen::ByteString->(CStringLen->IOa)->IOauseAsCStringLen :: 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 -> (Ptr Word8 -> IO ()) -> IO ByteStringcreateIntlen((Ptr Word8 -> IO ()) -> IO ByteString)-> (Ptr Word8 -> IO ()) -> IO ByteStringforall a b. (a -> b) -> a -> b$\Ptr Word8p->Ptr Word8 -> Ptr Word8 -> Int -> IO ()memcpyPtr Word8p(CString -> Ptr Word8forall a b. Ptr a -> Ptr bcastPtrCStringcstr)IntlenpackCStringLen(CString_,Intlen)=FilePath -> FilePath -> IO ByteStringforall a. (?callStack::CallStack) => 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 -> (Ptr Word8 -> IO ()) -> ByteStringunsafeCreateIntl((Ptr Word8 -> IO ()) -> ByteString)-> (Ptr Word8 -> IO ()) -> ByteStringforall 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 -> Int -> IO ()memcpyPtr Word8pPtr Word8fIntl-- ----------------------------------------------------------------------- Line IO-- | Read a line from stdin.getLine::IOByteStringgetLine :: IO ByteStringgetLine=Handle -> IO ByteStringhGetLineHandlestdin-- | 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 :: Handle__ -> IORef (Buffer Word8)haByteBuffer :: 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 :: IORef (Buffer Word8)haByteBuffer :: Handle__ -> IORef (Buffer Word8)haByteBuffer,devhaDevice :: ()haDevice :: devhaDevice}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 :: IntbufR=Int0,bufL :: IntbufL=Int0}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 :: IORef (Buffer Word8)haByteBuffer :: Handle__ -> IORef (Buffer Word8)haByteBuffer}buf :: Buffer Word8buf@Buffer{bufRaw=raw,bufR=w,bufL=r}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 :: IntbufL=Int0,bufR :: IntbufR=Int0}elseIORef (Buffer Word8) -> Buffer Word8 -> IO ()forall a. IORef a -> a -> IO ()writeIORefIORef (Buffer Word8)haByteBufferBuffer Word8buf{bufL :: IntbufL=IntoffInt -> Int -> Intforall a. Num a => a -> a -> a+Int1}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 :: IntbufL=Int0,bufR :: IntbufR=Int0}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 (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 (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 Word8rawmkPS::RawBufferWord8->Int->Int->IOByteStringmkPS :: ForeignPtr Word8 -> Int -> Int -> IO ByteStringmkPSForeignPtr Word8bufIntstartIntend=Int -> (Ptr Word8 -> IO ()) -> IO ByteStringcreateIntlen((Ptr Word8 -> IO ()) -> IO ByteString)-> (Ptr Word8 -> IO ()) -> IO ByteStringforall a b. (a -> b) -> a -> b$\Ptr Word8p->ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bwithRawBufferForeignPtr Word8buf((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Word8pbuf->Ptr Word8 -> Ptr Word8 -> Int -> IO ()forall a. Ptr a -> Ptr a -> Int -> IO ()copyBytesPtr Word8p(Ptr Word8pbufPtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`Intstart)Intlenwherelen :: Intlen=IntendInt -> Int -> Intforall a. Num a => a -> a -> a-IntstartmkBigPS::Int->[ByteString]->IOByteStringmkBigPS :: Int -> [ByteString] -> IO ByteStringmkBigPSInt_[ByteStringps]=ByteString -> IO ByteStringforall (m :: * -> *) a. Monad m => a -> m areturnByteStringpsmkBigPSInt_[ByteString]pss=ByteString -> IO ByteStringforall (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 (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 (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 stdoutputStr::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 -> (Ptr Word8 -> IO Int) -> IO ByteStringcreateAndTrimInti((Ptr Word8 -> IO Int) -> IO ByteString)-> (Ptr Word8 -> IO Int) -> IO ByteStringforall 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 (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 -> (Ptr Word8 -> IO Int) -> IO ByteStringcreateAndTrimInti((Ptr Word8 -> IO Int) -> IO ByteString)-> (Ptr Word8 -> IO Int) -> IO ByteStringforall 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 (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 -> (Ptr Word8 -> IO Int) -> IO ByteStringcreateAndTrimInti((Ptr Word8 -> IO Int) -> IO ByteString)-> (Ptr Word8 -> IO Int) -> IO ByteStringforall 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 (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 :: Handle -> FilePath -> Int -> IO aillegalBufferSizeHandlehandleFilePathfnIntsz=IOError -> IO aforall a. IOError -> IO aioError(IOErrorType-> FilePath -> Maybe Handle -> Maybe FilePath -> IOErrormkIOErrorIOErrorTypeillegalOperationErrorTypeFilePathmsg(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 (m :: * -> *) a. Monad m => a -> m areturn(ByteString -> IO ByteString) -> ByteString -> IO ByteStringforall a b. (a -> b) -> a -> b$!ByteString -> ByteStringcopyByteStringbselseByteString -> IO ByteStringforall (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 (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 -> (IOError -> IO Integer) -> IO Integerforall e a. Exception e => IO a -> (e -> IO a) -> IO acatch(Handle -> IO IntegerhFileSizeHandleh)IOError -> 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 :: IOError -> IO IntegeruseZeroIfNotRegularFileIOError_=Integer -> IO Integerforall (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 :: FilePath -> aerrorEmptyListFilePathfun=FilePath -> FilePath -> aforall a. (?callStack::CallStack) => FilePath -> FilePath -> amoduleErrorFilePathfunFilePath"empty ByteString"{-# NOINLINEerrorEmptyList#-}moduleError::HasCallStack=>String->String->amoduleError :: FilePath -> FilePath -> amoduleErrorFilePathfunFilePathmsg=FilePath -> aforall a. (?callStack::CallStack) => FilePath -> aerror(FilePath -> FilePath -> FilePathmoduleErrorMsgFilePathfunFilePathmsg){-# NOINLINEmoduleError#-}moduleErrorIO::HasCallStack=>String->String->IOamoduleErrorIO :: FilePath -> FilePath -> IO amoduleErrorIOFilePathfunFilePathmsg=IOError -> IO aforall e a. Exception e => e -> IO athrowIO(IOError -> IO a) -> (FilePath -> IOError) -> FilePath -> IO aforall b c a. (b -> c) -> (a -> b) -> a -> c.FilePath -> IOErroruserError(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