Movatterモバイル変換


[0]ホーム

URL:


{-# LANGUAGE CPP #-}{-# LANGUAGE Unsafe #-}{-# OPTIONS_HADDOCK not-home #-}{-# LANGUAGE TemplateHaskellQuotes #-}{-# LANGUAGE TypeFamilies #-}{-# LANGUAGE UnliftedFFITypes #-}{-# LANGUAGE ViewPatterns #-}#include "bytestring-cpp-macros.h"-- |-- Module      : Data.ByteString.Internal.Type-- Copyright   : (c) Don Stewart 2006-2008--               (c) Duncan Coutts 2006-2012-- License     : BSD-style-- Maintainer  : dons00@gmail.com, duncan@community.haskell.org-- Stability   : unstable-- Portability : non-portable---- The 'ByteString' type, its instances, and whatever related-- utilities the bytestring developers see fit to use internally.--moduleData.ByteString.Internal.Type(-- * The @ByteString@ type and representationByteString(BS,PS-- backwards compatibility shim),StrictByteString,-- * Internal indexingfindIndexOrLength,-- * Conversion with lists: packing and unpackingpackBytes,packUptoLenBytes,unsafePackLenBytes,packChars,packUptoLenChars,unsafePackLenChars,unpackBytes,unpackAppendBytesLazy,unpackAppendBytesStrict,unpackChars,unpackAppendCharsLazy,unpackAppendCharsStrict,unsafePackAddress,unsafePackLenAddress,unsafePackLiteral,unsafePackLenLiteral,-- * Low level imperative constructionempty,createFp,createFpUptoN,createFpUptoN',createFpAndTrim,createFpAndTrim',unsafeCreateFp,unsafeCreateFpUptoN,unsafeCreateFpUptoN',create,createUptoN,createUptoN',createAndTrim,createAndTrim',unsafeCreate,unsafeCreateUptoN,unsafeCreateUptoN',mallocByteString,-- * Conversion to and from ForeignPtrsmkDeferredByteString,fromForeignPtr,toForeignPtr,fromForeignPtr0,toForeignPtr0,-- * UtilitiesnullForeignPtr,peekFp,pokeFp,peekFpByteOff,pokeFpByteOff,minusForeignPtr,memcpyFp,deferForeignPtrAvailability,unsafeDupablePerformIO,SizeOverflowException,overflowError,checkedAdd,checkedMultiply,-- * Standard C Functionsc_strlen,c_free_finalizer,memchr,memcmp,memcpy,memset,-- * cbits functionsc_reverse,c_intersperse,c_maximum,c_minimum,c_count,c_count_ba,c_elem_index,c_sort,c_int_dec,c_int_dec_padded9,c_uint_dec,c_uint_hex,c_long_long_int_dec,c_long_long_int_dec_padded18,c_long_long_uint_dec,c_long_long_uint_hex,cIsValidUtf8BA,cIsValidUtf8BASafe,cIsValidUtf8,cIsValidUtf8Safe,-- * Charsw2c,c2w,isSpaceWord8,isSpaceChar8,-- * Deprecated and unmentionableaccursedUnutterablePerformIO,-- * Exported compatibility shimplusForeignPtr,unsafeWithForeignPtr)whereimportPreludehiding(concat,null)importqualifiedData.ListasListimportForeign.ForeignPtr(ForeignPtr,withForeignPtr)importForeign.PtrimportForeign.Storable(Storable(..))importForeign.C.TypesimportForeign.C.String(CString)importForeign.Marshal.UtilsimportForeign.Marshal.Alloc(finalizerFree)#if PURE_HASKELLimportqualifiedData.ByteString.Internal.PureasPureimportData.Bits(toIntegralSized,Bits)importData.Maybe(fromMaybe)importControl.Monad((<$!>))#endifimportData.Semigroup(Semigroup(..))importData.List.NonEmpty(NonEmpty((:|)))importControl.DeepSeq(NFData(rnf))importData.String(IsString(..))importControl.Exception(assert,throw,Exception)importData.Bits((.&.))importData.Char(ord)importData.WordimportData.Data(Data(..),mkConstr,mkNoRepType,Constr,DataType,Fixity(Prefix),constrIndex)importGHC.Base(nullAddr#,realWorld#,unsafeChr,unpackCString#)importGHC.Exts(IsList(..),Addr#,minusAddr#,ByteArray#,runRW#,lazy)#if HS_timesInt2_PRIMOP_AVAILABLEimportGHC.Exts(timesInt2#)#elseimportGHC.Exts(timesWord2#,or#,uncheckedShiftRL#,int2Word#,word2Int#)importData.Bits(finiteBitSize)#endifimportGHC.IO(IO(IO))importGHC.ForeignPtr(ForeignPtr(ForeignPtr)#if !HS_cstringLength_AND_FinalPtr_AVAILABLE,newForeignPtr_#endif,mallocPlainForeignPtrBytes)importGHC.ForeignPtr(plusForeignPtr)#if HS_cstringLength_AND_FinalPtr_AVAILABLEimportGHC.Exts(cstringLength#)importGHC.ForeignPtr(ForeignPtrContents(FinalPtr))#elseimportGHC.Ptr(Ptr(..))#endifimportGHC.Int(Int(..))#if HS_unsafeWithForeignPtr_AVAILABLEimportGHC.ForeignPtr(unsafeWithForeignPtr)#endifimportqualifiedLanguage.Haskell.TH.LibasTHimportqualifiedLanguage.Haskell.TH.SyntaxasTH#if !HS_unsafeWithForeignPtr_AVAILABLEunsafeWithForeignPtr::ForeignPtra->(Ptra->IOb)->IObunsafeWithForeignPtr=withForeignPtr#endif-- CFILES stuff is Hugs only{-# CFILES cbits/fpstring.c #-}minusForeignPtr::ForeignPtra->ForeignPtrb->IntminusForeignPtr :: forall a b. ForeignPtr a -> ForeignPtr b -> IntminusForeignPtr(ForeignPtrAddr#addr1ForeignPtrContents_)(ForeignPtrAddr#addr2ForeignPtrContents_)=Int# -> IntI#(Addr# -> Addr# -> Int#minusAddr#Addr#addr1Addr#addr2)peekFp::Storablea=>ForeignPtra->IOapeekFp :: forall a. Storable a => ForeignPtr a -> IO apeekFpForeignPtr afp=ForeignPtr a -> (Ptr a -> IO a) -> IO aforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr afpPtr a -> IO aforall a. Storable a => Ptr a -> IO apeekpokeFp::Storablea=>ForeignPtra->a->IO()pokeFp :: forall a. Storable a => ForeignPtr a -> a -> IO ()pokeFpForeignPtr afpaval=ForeignPtr a -> (Ptr a -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr afp((Ptr a -> IO ()) -> IO ()) -> (Ptr a -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr ap->Ptr a -> a -> IO ()forall a. Storable a => Ptr a -> a -> IO ()pokePtr apavalpeekFpByteOff::Storablea=>ForeignPtra->Int->IOapeekFpByteOff :: forall a. Storable a => ForeignPtr a -> Int -> IO apeekFpByteOffForeignPtr afpIntoff=ForeignPtr a -> (Ptr a -> IO a) -> IO aforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr afp((Ptr a -> IO a) -> IO a) -> (Ptr a -> IO a) -> IO aforall a b. (a -> b) -> a -> b$\Ptr ap->Ptr a -> Int -> IO aforall b. Ptr b -> Int -> IO aforall a b. Storable a => Ptr b -> Int -> IO apeekByteOffPtr apIntoffpokeFpByteOff::Storablea=>ForeignPtrb->Int->a->IO()pokeFpByteOff :: forall a b. Storable a => ForeignPtr b -> Int -> a -> IO ()pokeFpByteOffForeignPtr bfpIntoffaval=ForeignPtr b -> (Ptr b -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr bfp((Ptr b -> IO ()) -> IO ()) -> (Ptr b -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr bp->Ptr b -> Int -> a -> IO ()forall b. Ptr b -> Int -> a -> IO ()forall a b. Storable a => Ptr b -> Int -> a -> IO ()pokeByteOffPtr bpIntoffaval-- | Most operations on a 'ByteString' need to read from the buffer-- given by its @ForeignPtr Word8@ field.  But since most operations-- on @ByteString@ are (nominally) pure, their implementations cannot-- see the IO state thread that was used to initialize the contents of-- that buffer.  This means that under some circumstances, these-- buffer-reads may be executed before the writes used to initialize-- the buffer are executed, with unpredictable results.---- 'deferForeignPtrAvailability' exists to help solve this problem.-- At runtime, a call @'deferForeignPtrAvailability' x@ is equivalent-- to @pure $! x@, but the former is more opaque to the simplifier, so-- that reads from the pointer in its result cannot be executed until-- the @'deferForeignPtrAvailability' x@ call is complete.---- The opaque bits evaporate during CorePrep, so using-- 'deferForeignPtrAvailability' incurs no direct overhead.---- @since 0.11.5.0deferForeignPtrAvailability::ForeignPtra->IO(ForeignPtra)deferForeignPtrAvailability :: forall a. ForeignPtr a -> IO (ForeignPtr a)deferForeignPtrAvailability(ForeignPtrAddr#addr0#ForeignPtrContentsguts)=(State# RealWorld -> (# State# RealWorld, ForeignPtr a #))-> IO (ForeignPtr a)forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO aIO((State# RealWorld -> (# State# RealWorld, ForeignPtr a #)) -> IO (ForeignPtr a))-> (State# RealWorld -> (# State# RealWorld, ForeignPtr a #))-> IO (ForeignPtr a)forall a b. (a -> b) -> a -> b$\State# RealWorlds0->case((State# RealWorld -> (# State# RealWorld, Addr# #)) -> (# State# RealWorld, Addr# #))-> (State# RealWorld -> (# State# RealWorld, Addr# #))-> (# State# RealWorld, Addr# #)forall a. a -> alazy(State# RealWorld -> (# State# RealWorld, Addr# #))-> (# State# RealWorld, Addr# #)forall o. (State# RealWorld -> o) -> orunRW#(\State# RealWorld_->(#State# RealWorlds0,Addr#addr0##))of(#State# RealWorlds1,Addr#addr1##)->(#State# RealWorlds1,Addr# -> ForeignPtrContents -> ForeignPtr aforall a. Addr# -> ForeignPtrContents -> ForeignPtr aForeignPtrAddr#addr1#ForeignPtrContentsguts#)-- | Variant of 'fromForeignPtr0' that calls 'deferForeignPtrAvailability'---- @since 0.11.5.0mkDeferredByteString::ForeignPtrWord8->Int->IOByteStringmkDeferredByteString :: ForeignPtr Word8 -> Int -> IO ByteStringmkDeferredByteStringForeignPtr Word8fpIntlen=doForeignPtr Word8deferredFp<-ForeignPtr Word8 -> IO (ForeignPtr Word8)forall a. ForeignPtr a -> IO (ForeignPtr a)deferForeignPtrAvailabilityForeignPtr Word8fpByteString -> IO ByteStringforall a. a -> IO aforall (f :: * -> *) a. Applicative f => a -> f apure(ByteString -> IO ByteString) -> ByteString -> IO ByteStringforall a b. (a -> b) -> a -> b$!ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8deferredFpIntlenunsafeDupablePerformIO::IOa->a-- Why does this exist? In base-4.15.1.0 until at least base-4.18.0.0,-- the version of unsafeDupablePerformIO in base prevents unboxing of-- its results with an opaque call to GHC.Exts.lazy, for reasons described-- in Note [unsafePerformIO and strictness] in GHC.IO.Unsafe. (See-- https://hackage.haskell.org/package/base-4.18.0.0/docs/src/GHC.IO.Unsafe.html#line-30 .)-- Even if we accept the (very questionable) premise that the sort of-- function described in that note should work, we expect no such-- calls to be made in the context of bytestring.  (And we really want-- unboxing!)unsafeDupablePerformIO :: forall a. IO a -> aunsafeDupablePerformIO(IOState# RealWorld -> (# State# RealWorld, a #)act)=case(State# RealWorld -> (# State# RealWorld, a #))-> (# State# RealWorld, a #)forall o. (State# RealWorld -> o) -> orunRW#State# RealWorld -> (# State# RealWorld, a #)actof(#State# RealWorld_,ares#)->ares-- ------------------------------------------------------------------------------- | A space-efficient representation of a 'Word8' vector, supporting many-- efficient operations.---- A 'ByteString' contains 8-bit bytes, or by using the operations from-- "Data.ByteString.Char8" it can be interpreted as containing 8-bit-- characters.--dataByteString=BS{-# UNPACK#-}!(ForeignPtrWord8)-- payload{-# UNPACK#-}!Int-- length-- ^ @since 0.11.0.0-- | Type synonym for the strict flavour of 'ByteString'.---- @since 0.11.2.0typeStrictByteString=ByteString-- |-- @'PS' foreignPtr offset length@ represents a 'ByteString' with data-- backed by a given @foreignPtr@, starting at a given @offset@ in bytes-- and of a specified @length@.---- This pattern is used to emulate the legacy 'ByteString' data-- constructor, so that pre-existing code generally doesn't need to-- change to benefit from the simplified 'BS' constructor and can-- continue to function unchanged.---- /Note:/ Matching with this constructor will always be given a 0 offset,-- as the base will be manipulated by 'plusForeignPtr' instead.--patternPS::ForeignPtrWord8->Int->Int->ByteStringpattern$mPS :: forall {r}.ByteString-> (ForeignPtr Word8 -> Int -> Int -> r) -> ((# #) -> r) -> r$bPS :: ForeignPtr Word8 -> Int -> Int -> ByteStringPSfpzerolen<-BSfp((0,)->(zero,len))wherePSForeignPtr Word8fpIntoIntlen=ForeignPtr Word8 -> Int -> ByteStringBS(ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr bplusForeignPtrForeignPtr Word8fpInto)Intlen{-# COMPLETEPS#-}instanceEqByteStringwhere== :: ByteString -> ByteString -> Bool(==)=ByteString -> ByteString -> BooleqinstanceOrdByteStringwherecompare :: ByteString -> ByteString -> Orderingcompare=ByteString -> ByteString -> OrderingcompareBytesinstanceSemigroupByteStringwhere<> :: ByteString -> ByteString -> ByteString(<>)=ByteString -> ByteString -> ByteStringappendsconcat :: NonEmpty ByteString -> ByteStringsconcat(ByteStringb:|[ByteString]bs)=[ByteString] -> ByteStringconcat(ByteStringbByteString -> [ByteString] -> [ByteString]forall a. a -> [a] -> [a]:[ByteString]bs){-# INLINEstimes#-}stimes :: forall b. Integral b => b -> ByteString -> ByteStringstimes=b -> ByteString -> ByteStringforall b. Integral b => b -> ByteString -> ByteStringstimesPolymorphicinstanceMonoidByteStringwheremempty :: ByteStringmempty=ByteStringemptymappend :: ByteString -> ByteString -> ByteStringmappend=ByteString -> ByteString -> ByteStringforall a. Semigroup a => a -> a -> a(<>)mconcat :: [ByteString] -> ByteStringmconcat=[ByteString] -> ByteStringconcatinstanceNFDataByteStringwherernf :: ByteString -> ()rnfBS{}=()instanceShowByteStringwhereshowsPrec :: Int -> ByteString -> ShowSshowsPrecIntpByteStringpsStringr=Int -> String -> ShowSforall a. Show a => Int -> a -> ShowSshowsPrecIntp(ByteString -> StringunpackCharsByteStringps)StringrinstanceReadByteStringwherereadsPrec :: Int -> ReadS ByteStringreadsPrecIntpStringstr=[(String -> ByteStringpackCharsStringx,Stringy)|(Stringx,Stringy)<-Int -> ReadS Stringforall a. Read a => Int -> ReadS areadsPrecIntpStringstr]-- | @since 0.10.12.0instanceIsListByteStringwheretypeItemByteString=Word8fromList :: [Item ByteString] -> ByteStringfromList=[Word8] -> ByteString[Item ByteString] -> ByteStringpackBytestoList :: ByteString -> [Item ByteString]toList=ByteString -> [Word8]ByteString -> [Item ByteString]unpackBytes-- | Beware: 'fromString' truncates multi-byte characters to octets.-- e.g. "枯朶に烏のとまりけり秋の暮" becomes �6k�nh~�Q��n�instanceIsStringByteStringwhere{-# INLINEfromString#-}fromString :: String -> ByteStringfromString=String -> ByteStringpackCharsinstanceDataByteStringwheregfoldl :: forall (c :: * -> *).(forall d b. Data d => c (d -> b) -> d -> c b)-> (forall g. g -> c g) -> ByteString -> c ByteStringgfoldlforall d b. Data d => c (d -> b) -> d -> c bfforall g. g -> c gzByteStringtxt=([Word8] -> ByteString) -> c ([Word8] -> ByteString)forall g. g -> c gz[Word8] -> ByteStringpackBytesc ([Word8] -> ByteString) -> [Word8] -> c ByteStringforall d b. Data d => c (d -> b) -> d -> c b`f`ByteString -> [Word8]unpackBytesByteStringtxttoConstr :: ByteString -> ConstrtoConstrByteString_=ConstrpackConstrgunfold :: forall (c :: * -> *).(forall b r. Data b => c (b -> r) -> c r)-> (forall r. r -> c r) -> Constr -> c ByteStringgunfoldforall b r. Data b => c (b -> r) -> c rkforall r. r -> c rzConstrc=caseConstr -> IntconstrIndexConstrcofInt1->c ([Word8] -> ByteString) -> c ByteStringforall b r. Data b => c (b -> r) -> c rk(([Word8] -> ByteString) -> c ([Word8] -> ByteString)forall r. r -> c rz[Word8] -> ByteStringpackBytes)Int_->String -> c ByteStringforall a. (?callStack::CallStack) => String -> aerrorString"gunfold: unexpected constructor of strict ByteString"dataTypeOf :: ByteString -> DataTypedataTypeOfByteString_=DataTypebyteStringDataTypepackConstr::ConstrpackConstr :: ConstrpackConstr=DataType -> String -> [String] -> Fixity -> ConstrmkConstrDataTypebyteStringDataTypeString"pack"[]FixityPrefixbyteStringDataType::DataTypebyteStringDataType :: DataTypebyteStringDataType=String -> DataTypemkNoRepTypeString"Data.ByteString.ByteString"-- | @since 0.11.2.0instanceTH.LiftByteStringwhere#if MIN_VERSION_template_haskell(2,16,0)-- template-haskell-2.16 first ships with ghc-8.10lift :: forall (m :: * -> *). Quote m => ByteString -> m Explift(BSForeignPtr Word8ptrIntlen)=[|unsafePackLenLiteral|]m Exp -> m Exp -> m Expforall (m :: * -> *). Quote m => m Exp -> m Exp -> m Exp`TH.appE`Lit -> m Expforall (m :: * -> *). Quote m => Lit -> m ExpTH.litE(Integer -> LitTH.integerL(Int -> Integerforall a b. (Integral a, Num b) => a -> bfromIntegralIntlen))m Exp -> m Exp -> m Expforall (m :: * -> *). Quote m => m Exp -> m Exp -> m Exp`TH.appE`Lit -> m Expforall (m :: * -> *). Quote m => Lit -> m ExpTH.litE(Bytes -> LitTH.BytesPrimL(Bytes -> Lit) -> Bytes -> Litforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> Word -> Word -> BytesTH.BytesForeignPtr Word8ptrWord0(Int -> Wordforall a b. (Integral a, Num b) => a -> bfromIntegralIntlen))#elseliftbs@(BS_len)=[|unsafePackLenLiteral|]`TH.appE`TH.litE(TH.integerL(fromIntegrallen))`TH.appE`TH.litE(TH.StringPrimL$unpackBytesbs)#endif#if MIN_VERSION_template_haskell(2,17,0)-- template-haskell-2.17 first ships with ghc-9.0liftTyped :: forall (m :: * -> *). Quote m => ByteString -> Code m ByteStringliftTyped=m Exp -> Code m ByteStringforall a (m :: * -> *). Quote m => m Exp -> Code m aTH.unsafeCodeCoerce(m Exp -> Code m ByteString)-> (ByteString -> m Exp) -> ByteString -> Code m ByteStringforall b c a. (b -> c) -> (a -> b) -> a -> c.ByteString -> m Expforall t (m :: * -> *). (Lift t, Quote m) => t -> m Expforall (m :: * -> *). Quote m => ByteString -> m ExpTH.lift#elif MIN_VERSION_template_haskell(2,16,0)-- template-haskell-2.16 first ships with ghc-8.10liftTyped=TH.unsafeTExpCoerce.TH.lift#endif-------------------------------------------------------------------------- Internal indexing-- | 'findIndexOrLength' is a variant of findIndex, that returns the length-- of the string if no element is found, rather than Nothing.findIndexOrLength::(Word8->Bool)->ByteString->IntfindIndexOrLength :: (Word8 -> Bool) -> ByteString -> IntfindIndexOrLengthWord8 -> Boolk(BSForeignPtr Word8xIntl)=IO Int -> Intforall a. IO a -> aaccursedUnutterablePerformIO(IO Int -> Int) -> IO Int -> Intforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> IO IntgForeignPtr Word8xwhereg :: ForeignPtr Word8 -> IO IntgForeignPtr Word8ptr=Int -> IO IntgoInt0wherego :: Int -> IO Intgo!Intn|IntnInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Intl=Int -> IO Intforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnIntl|Boolotherwise=doWord8w<-ForeignPtr Word8 -> IO Word8forall a. Storable a => ForeignPtr a -> IO apeekFp(ForeignPtr Word8 -> IO Word8) -> ForeignPtr Word8 -> IO Word8forall a b. (a -> b) -> a -> b$ForeignPtr Word8ptrForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`IntnifWord8 -> BoolkWord8wthenInt -> IO Intforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnIntnelseInt -> IO Intgo(IntnInt -> Int -> Intforall a. Num a => a -> a -> a+Int1){-# INLINEfindIndexOrLength#-}-------------------------------------------------------------------------- Packing and unpacking from listspackBytes::[Word8]->ByteStringpackBytes :: [Word8] -> ByteStringpackBytes[Word8]ws=Int -> [Word8] -> ByteStringunsafePackLenBytes([Word8] -> Intforall a. [a] -> Intforall (t :: * -> *) a. Foldable t => t a -> IntList.length[Word8]ws)[Word8]wspackChars::[Char]->ByteStringpackChars :: String -> ByteStringpackCharsStringcs=Int -> String -> ByteStringunsafePackLenChars(String -> Intforall a. [a] -> Intforall (t :: * -> *) a. Foldable t => t a -> IntList.lengthStringcs)Stringcs{-# INLINE[0]packChars#-}{-# RULES"ByteString packChars/packAddress"foralls.packChars(unpackCString#s)=unsafePackLiterals#-}unsafePackLenBytes::Int->[Word8]->ByteStringunsafePackLenBytes :: Int -> [Word8] -> ByteStringunsafePackLenBytesIntlen[Word8]xs0=Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFpIntlen((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8p->ForeignPtr Word8 -> [Word8] -> IO ()forall {b}. Storable b => ForeignPtr b -> [b] -> IO ()goForeignPtr Word8p[Word8]xs0wherego :: ForeignPtr b -> [b] -> IO ()go!ForeignPtr b_[]=() -> IO ()forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn()go!ForeignPtr bp(bx:[b]xs)=ForeignPtr b -> b -> IO ()forall a. Storable a => ForeignPtr a -> a -> IO ()pokeFpForeignPtr bpbxIO () -> IO () -> IO ()forall a b. IO a -> IO b -> IO bforall (m :: * -> *) a b. Monad m => m a -> m b -> m b>>ForeignPtr b -> [b] -> IO ()go(ForeignPtr bpForeignPtr b -> Int -> ForeignPtr bforall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Int1)[b]xsunsafePackLenChars::Int->[Char]->ByteStringunsafePackLenChars :: Int -> String -> ByteStringunsafePackLenCharsIntlenStringcs0=Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFpIntlen((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8p->ForeignPtr Word8 -> String -> IO ()goForeignPtr Word8pStringcs0wherego :: ForeignPtr Word8 -> String -> IO ()go!ForeignPtr Word8_[]=() -> IO ()forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn()go!ForeignPtr Word8p(Charc:Stringcs)=ForeignPtr Word8 -> Word8 -> IO ()forall a. Storable a => ForeignPtr a -> a -> IO ()pokeFpForeignPtr Word8p(Char -> Word8c2wCharc)IO () -> IO () -> IO ()forall a b. IO a -> IO b -> IO bforall (m :: * -> *) a b. Monad m => m a -> m b -> m b>>ForeignPtr Word8 -> String -> IO ()go(ForeignPtr Word8pForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Int1)Stringcs-- | /O(n)/ Pack a null-terminated sequence of bytes, pointed to by an-- Addr\# (an arbitrary machine address assumed to point outside the-- garbage-collected heap) into a @ByteString@. A much faster way to-- create an 'Addr#' is with an unboxed string literal, than to pack a-- boxed string. A unboxed string literal is compiled to a static @char-- []@ by GHC. Establishing the length of the string requires a call to-- @strlen(3)@, so the 'Addr#' must point to a null-terminated buffer (as-- is the case with @\"string\"\#@ literals in GHC). Use 'Data.ByteString.Unsafe.unsafePackAddressLen'-- if you know the length of the string statically.---- An example:---- > literalFS = unsafePackAddress "literal"#---- This function is /unsafe/. If you modify the buffer pointed to by the-- original 'Addr#' this modification will be reflected in the resulting-- @ByteString@, breaking referential transparency.---- Note this also won't work if your 'Addr#' has embedded @\'\\0\'@ characters in-- the string, as @strlen@ will return too short a length.--unsafePackAddress::Addr#->IOByteStringunsafePackAddress :: Addr# -> IO ByteStringunsafePackAddressAddr#addr#=do#if HS_cstringLength_AND_FinalPtr_AVAILABLEInt -> Addr# -> IO ByteStringunsafePackLenAddress(Int# -> IntI#(Addr# -> Int#cstringLength#Addr#addr#))Addr#addr##elsel<-c_strlen(Ptraddr#)unsafePackLenAddress(fromIntegrall)addr##endif{-# INLINEunsafePackAddress#-}-- | See 'unsafePackAddress'. This function is similar,-- but takes an additional length argument rather then computing-- it with @strlen@.-- Therefore embedding @\'\\0\'@ characters is possible.---- @since 0.11.2.0unsafePackLenAddress::Int->Addr#->IOByteStringunsafePackLenAddress :: Int -> Addr# -> IO ByteStringunsafePackLenAddressIntlenAddr#addr#=do#if HS_cstringLength_AND_FinalPtr_AVAILABLEByteString -> IO ByteStringforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(ForeignPtr Word8 -> Int -> ByteStringBS(Addr# -> ForeignPtrContents -> ForeignPtr Word8forall a. Addr# -> ForeignPtrContents -> ForeignPtr aForeignPtrAddr#addr#ForeignPtrContentsFinalPtr)Intlen)#elsep<-newForeignPtr_(Ptraddr#)return$BSplen#endif{-# INLINEunsafePackLenAddress#-}-- | See 'unsafePackAddress'. This function has similar behavior. Prefer-- this function when the address in known to be an @Addr#@ literal. In-- that context, there is no need for the sequencing guarantees that 'IO'-- provides. On GHC 9.0 and up, this function uses the @FinalPtr@ data-- constructor for @ForeignPtrContents@.---- @since 0.11.1.0unsafePackLiteral::Addr#->ByteStringunsafePackLiteral :: Addr# -> ByteStringunsafePackLiteralAddr#addr#=#if HS_cstringLength_AND_FinalPtr_AVAILABLEInt -> Addr# -> ByteStringunsafePackLenLiteral(Int# -> IntI#(Addr# -> Int#cstringLength#Addr#addr#))Addr#addr##elseletlen=accursedUnutterablePerformIO(c_strlen(Ptraddr#))inunsafePackLenLiteral(fromIntegrallen)addr##endif{-# INLINEunsafePackLiteral#-}-- | See 'unsafePackLiteral'. This function is similar,-- but takes an additional length argument rather then computing-- it with @strlen@.-- Therefore embedding @\'\\0\'@ characters is possible.---- @since 0.11.2.0unsafePackLenLiteral::Int->Addr#->ByteStringunsafePackLenLiteral :: Int -> Addr# -> ByteStringunsafePackLenLiteralIntlenAddr#addr#=#if HS_cstringLength_AND_FinalPtr_AVAILABLEForeignPtr Word8 -> Int -> ByteStringBS(Addr# -> ForeignPtrContents -> ForeignPtr Word8forall a. Addr# -> ForeignPtrContents -> ForeignPtr aForeignPtrAddr#addr#ForeignPtrContentsFinalPtr)Intlen#else-- newForeignPtr_ allocates a MutVar# internally. If that MutVar#-- gets commoned up with the MutVar# of some unrelated ForeignPtr,-- it may prevent automatic finalization for that other ForeignPtr.-- So we avoid accursedUnutterablePerformIO here.BS(unsafeDupablePerformIO(newForeignPtr_(Ptraddr#)))len#endif{-# INLINEunsafePackLenLiteral#-}packUptoLenBytes::Int->[Word8]->(ByteString,[Word8])packUptoLenBytes :: Int -> [Word8] -> (ByteString, [Word8])packUptoLenBytesIntlen[Word8]xs0=Int-> (ForeignPtr Word8 -> IO (Int, [Word8])) -> (ByteString, [Word8])forall a.Int -> (ForeignPtr Word8 -> IO (Int, a)) -> (ByteString, a)unsafeCreateFpUptoN'Intlen((ForeignPtr Word8 -> IO (Int, [Word8])) -> (ByteString, [Word8]))-> (ForeignPtr Word8 -> IO (Int, [Word8])) -> (ByteString, [Word8])forall a b. (a -> b) -> a -> b$\ForeignPtr Word8p0->letp_end :: ForeignPtr Word8p_end=ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr bplusForeignPtrForeignPtr Word8p0Intlengo :: ForeignPtr Word8 -> [Word8] -> IO (Int, [Word8])go!ForeignPtr Word8p[]=(Int, [Word8]) -> IO (Int, [Word8])forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(ForeignPtr Word8pForeignPtr Word8 -> ForeignPtr Word8 -> Intforall a b. ForeignPtr a -> ForeignPtr b -> Int`minusForeignPtr`ForeignPtr Word8p0,[])go!ForeignPtr Word8p[Word8]xs|ForeignPtr Word8pForeignPtr Word8 -> ForeignPtr Word8 -> Boolforall a. Eq a => a -> a -> Bool==ForeignPtr Word8p_end=(Int, [Word8]) -> IO (Int, [Word8])forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(Intlen,[Word8]xs)go!ForeignPtr Word8p(Word8x:[Word8]xs)=ForeignPtr Word8 -> Word8 -> IO ()forall a. Storable a => ForeignPtr a -> a -> IO ()pokeFpForeignPtr Word8pWord8xIO () -> IO (Int, [Word8]) -> IO (Int, [Word8])forall a b. IO a -> IO b -> IO bforall (m :: * -> *) a b. Monad m => m a -> m b -> m b>>ForeignPtr Word8 -> [Word8] -> IO (Int, [Word8])go(ForeignPtr Word8pForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Int1)[Word8]xsinForeignPtr Word8 -> [Word8] -> IO (Int, [Word8])goForeignPtr Word8p0[Word8]xs0packUptoLenChars::Int->[Char]->(ByteString,[Char])packUptoLenChars :: Int -> String -> (ByteString, String)packUptoLenCharsIntlenStringcs0=Int-> (ForeignPtr Word8 -> IO (Int, String)) -> (ByteString, String)forall a.Int -> (ForeignPtr Word8 -> IO (Int, a)) -> (ByteString, a)unsafeCreateFpUptoN'Intlen((ForeignPtr Word8 -> IO (Int, String)) -> (ByteString, String))-> (ForeignPtr Word8 -> IO (Int, String)) -> (ByteString, String)forall a b. (a -> b) -> a -> b$\ForeignPtr Word8p0->letp_end :: ForeignPtr Word8p_end=ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr bplusForeignPtrForeignPtr Word8p0Intlengo :: ForeignPtr Word8 -> String -> IO (Int, String)go!ForeignPtr Word8p[]=(Int, String) -> IO (Int, String)forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(ForeignPtr Word8pForeignPtr Word8 -> ForeignPtr Word8 -> Intforall a b. ForeignPtr a -> ForeignPtr b -> Int`minusForeignPtr`ForeignPtr Word8p0,[])go!ForeignPtr Word8pStringcs|ForeignPtr Word8pForeignPtr Word8 -> ForeignPtr Word8 -> Boolforall a. Eq a => a -> a -> Bool==ForeignPtr Word8p_end=(Int, String) -> IO (Int, String)forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(Intlen,Stringcs)go!ForeignPtr Word8p(Charc:Stringcs)=ForeignPtr Word8 -> Word8 -> IO ()forall a. Storable a => ForeignPtr a -> a -> IO ()pokeFpForeignPtr Word8p(Char -> Word8c2wCharc)IO () -> IO (Int, String) -> IO (Int, String)forall a b. IO a -> IO b -> IO bforall (m :: * -> *) a b. Monad m => m a -> m b -> m b>>ForeignPtr Word8 -> String -> IO (Int, String)go(ForeignPtr Word8pForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Int1)StringcsinForeignPtr Word8 -> String -> IO (Int, String)goForeignPtr Word8p0Stringcs0-- Unpacking bytestrings into lists efficiently is a tradeoff: on the one hand-- we would like to write a tight loop that just blasts the list into memory, on-- the other hand we want it to be unpacked lazily so we don't end up with a-- massive list data structure in memory.---- Our strategy is to combine both: we will unpack lazily in reasonable sized-- chunks, where each chunk is unpacked strictly.---- unpackBytes and unpackChars do the lazy loop, while unpackAppendBytes and-- unpackAppendChars do the chunks strictly.unpackBytes::ByteString->[Word8]unpackBytes :: ByteString -> [Word8]unpackBytesByteStringbs=ByteString -> [Word8] -> [Word8]unpackAppendBytesLazyByteStringbs[]unpackChars::ByteString->[Char]unpackChars :: ByteString -> StringunpackCharsByteStringbs=ByteString -> ShowSunpackAppendCharsLazyByteStringbs[]unpackAppendBytesLazy::ByteString->[Word8]->[Word8]unpackAppendBytesLazy :: ByteString -> [Word8] -> [Word8]unpackAppendBytesLazy(BSForeignPtr Word8fpIntlen)[Word8]xs|IntlenInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int100=ByteString -> [Word8] -> [Word8]unpackAppendBytesStrict(ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8fpIntlen)[Word8]xs|Boolotherwise=ByteString -> [Word8] -> [Word8]unpackAppendBytesStrict(ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8fpInt100)[Word8]remainderwhereremainder :: [Word8]remainder=ByteString -> [Word8] -> [Word8]unpackAppendBytesLazy(ForeignPtr Word8 -> Int -> ByteStringBS(ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr bplusForeignPtrForeignPtr Word8fpInt100)(IntlenInt -> Int -> Intforall a. Num a => a -> a -> a-Int100))[Word8]xs-- Why 100 bytes you ask? Because on a 64bit machine the list we allocate-- takes just shy of 4k which seems like a reasonable amount.-- (5 words per list element, 8 bytes per word, 100 elements = 4000 bytes)unpackAppendCharsLazy::ByteString->[Char]->[Char]unpackAppendCharsLazy :: ByteString -> ShowSunpackAppendCharsLazy(BSForeignPtr Word8fpIntlen)Stringcs|IntlenInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=Int100=ByteString -> ShowSunpackAppendCharsStrict(ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8fpIntlen)Stringcs|Boolotherwise=ByteString -> ShowSunpackAppendCharsStrict(ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8fpInt100)Stringremainderwhereremainder :: Stringremainder=ByteString -> ShowSunpackAppendCharsLazy(ForeignPtr Word8 -> Int -> ByteStringBS(ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr bplusForeignPtrForeignPtr Word8fpInt100)(IntlenInt -> Int -> Intforall a. Num a => a -> a -> a-Int100))Stringcs-- For these unpack functions, since we're unpacking the whole list strictly we-- build up the result list in an accumulator. This means we have to build up-- the list starting at the end. So our traversal starts at the end of the-- buffer and loops down until we hit the sentinal:unpackAppendBytesStrict::ByteString->[Word8]->[Word8]unpackAppendBytesStrict :: ByteString -> [Word8] -> [Word8]unpackAppendBytesStrict(BSForeignPtr Word8fpIntlen)[Word8]xs=IO [Word8] -> [Word8]forall a. IO a -> aaccursedUnutterablePerformIO(IO [Word8] -> [Word8]) -> IO [Word8] -> [Word8]forall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO [Word8]) -> IO [Word8]forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO [Word8]) -> IO [Word8])-> (Ptr Word8 -> IO [Word8]) -> IO [Word8]forall a b. (a -> b) -> a -> b$\Ptr Word8base->Ptr Word8 -> Ptr Word8 -> [Word8] -> IO [Word8]forall {b}. Storable b => Ptr b -> Ptr b -> [b] -> IO [b]loop(Ptr Word8basePtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`(-Int1))(Ptr Word8basePtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`(-Int1Int -> Int -> Intforall a. Num a => a -> a -> a+Intlen))[Word8]xswhereloop :: Ptr b -> Ptr b -> [b] -> IO [b]loop!Ptr bsentinal!Ptr bp[b]acc|Ptr bpPtr b -> Ptr b -> Boolforall a. Eq a => a -> a -> Bool==Ptr bsentinal=[b] -> IO [b]forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn[b]acc|Boolotherwise=dobx<-Ptr b -> IO bforall a. Storable a => Ptr a -> IO apeekPtr bpPtr b -> Ptr b -> [b] -> IO [b]loopPtr bsentinal(Ptr bpPtr b -> Int -> Ptr bforall a b. Ptr a -> Int -> Ptr b`plusPtr`(-Int1))(bxb -> [b] -> [b]forall a. a -> [a] -> [a]:[b]acc)unpackAppendCharsStrict::ByteString->[Char]->[Char]unpackAppendCharsStrict :: ByteString -> ShowSunpackAppendCharsStrict(BSForeignPtr Word8fpIntlen)Stringxs=IO String -> Stringforall a. IO a -> aaccursedUnutterablePerformIO(IO String -> String) -> IO String -> Stringforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO String) -> IO Stringforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO String) -> IO String)-> (Ptr Word8 -> IO String) -> IO Stringforall a b. (a -> b) -> a -> b$\Ptr Word8base->Ptr Word8 -> Ptr Word8 -> String -> IO Stringloop(Ptr Word8basePtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`(-Int1))(Ptr Word8basePtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`(-Int1Int -> Int -> Intforall a. Num a => a -> a -> a+Intlen))Stringxswhereloop :: Ptr Word8 -> Ptr Word8 -> String -> IO Stringloop!Ptr Word8sentinal!Ptr Word8pStringacc|Ptr Word8pPtr Word8 -> Ptr Word8 -> Boolforall a. Eq a => a -> a -> Bool==Ptr Word8sentinal=String -> IO Stringforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturnStringacc|Boolotherwise=doWord8x<-Ptr Word8 -> IO Word8forall a. Storable a => Ptr a -> IO apeekPtr Word8pPtr Word8 -> Ptr Word8 -> String -> IO StringloopPtr Word8sentinal(Ptr Word8pPtr Word8 -> Int -> Ptr Word8forall a b. Ptr a -> Int -> Ptr b`plusPtr`(-Int1))(Word8 -> Charw2cWord8xChar -> ShowSforall a. a -> [a] -> [a]:Stringacc)-------------------------------------------------------------------------- | The 0 pointer. Used to indicate the empty Bytestring.nullForeignPtr::ForeignPtrWord8#if HS_cstringLength_AND_FinalPtr_AVAILABLEnullForeignPtr :: ForeignPtr Word8nullForeignPtr=Addr# -> ForeignPtrContents -> ForeignPtr Word8forall a. Addr# -> ForeignPtrContents -> ForeignPtr aForeignPtrAddr#nullAddr#ForeignPtrContentsFinalPtr#elsenullForeignPtr=ForeignPtrnullAddr#(error"nullForeignPtr")#endif-- ----------------------------------------------------------------------- Low level constructors-- | /O(1)/ Build a ByteString from a ForeignPtr.---- If you do not need the offset parameter then you should be using-- 'Data.ByteString.Unsafe.unsafePackCStringLen' or-- 'Data.ByteString.Unsafe.unsafePackCStringFinalizer' instead.--fromForeignPtr::ForeignPtrWord8->Int-- ^ Offset->Int-- ^ Length->ByteStringfromForeignPtr :: ForeignPtr Word8 -> Int -> Int -> ByteStringfromForeignPtrForeignPtr Word8fpInto=ForeignPtr Word8 -> Int -> ByteStringBS(ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr bplusForeignPtrForeignPtr Word8fpInto){-# INLINEfromForeignPtr#-}-- | @since 0.11.0.0fromForeignPtr0::ForeignPtrWord8->Int-- ^ Length->ByteStringfromForeignPtr0 :: ForeignPtr Word8 -> Int -> ByteStringfromForeignPtr0=ForeignPtr Word8 -> Int -> ByteStringBS{-# INLINEfromForeignPtr0#-}-- | /O(1)/ Deconstruct a ForeignPtr from a ByteStringtoForeignPtr::ByteString->(ForeignPtrWord8,Int,Int)-- ^ (ptr, offset, length)toForeignPtr :: ByteString -> (ForeignPtr Word8, Int, Int)toForeignPtr(BSForeignPtr Word8psIntl)=(ForeignPtr Word8ps,Int0,Intl){-# INLINEtoForeignPtr#-}-- | /O(1)/ Deconstruct a ForeignPtr from a ByteString---- @since 0.11.0.0toForeignPtr0::ByteString->(ForeignPtrWord8,Int)-- ^ (ptr, length)toForeignPtr0 :: ByteString -> (ForeignPtr Word8, Int)toForeignPtr0(BSForeignPtr Word8psIntl)=(ForeignPtr Word8ps,Intl){-# INLINEtoForeignPtr0#-}-- | A way of creating ByteStrings outside the IO monad. The @Int@-- argument gives the final size of the ByteString.unsafeCreateFp::Int->(ForeignPtrWord8->IO())->ByteStringunsafeCreateFp :: Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFpIntlForeignPtr Word8 -> IO ()f=IO ByteString -> ByteStringforall a. IO a -> aunsafeDupablePerformIO(Int -> (ForeignPtr Word8 -> IO ()) -> IO ByteStringcreateFpIntlForeignPtr Word8 -> IO ()f){-# INLINEunsafeCreateFp#-}-- | Like 'unsafeCreateFp' but instead of giving the final size of the-- ByteString, it is just an upper bound. The inner action returns-- the actual size. Unlike 'createFpAndTrim' the ByteString is not-- reallocated if the final size is less than the estimated size.unsafeCreateFpUptoN::Int->(ForeignPtrWord8->IOInt)->ByteStringunsafeCreateFpUptoN :: Int -> (ForeignPtr Word8 -> IO Int) -> ByteStringunsafeCreateFpUptoNIntlForeignPtr Word8 -> IO Intf=IO ByteString -> ByteStringforall a. IO a -> aunsafeDupablePerformIO(Int -> (ForeignPtr Word8 -> IO Int) -> IO ByteStringcreateFpUptoNIntlForeignPtr Word8 -> IO Intf){-# INLINEunsafeCreateFpUptoN#-}unsafeCreateFpUptoN'::Int->(ForeignPtrWord8->IO(Int,a))->(ByteString,a)unsafeCreateFpUptoN' :: forall a.Int -> (ForeignPtr Word8 -> IO (Int, a)) -> (ByteString, a)unsafeCreateFpUptoN'IntlForeignPtr Word8 -> IO (Int, a)f=IO (ByteString, a) -> (ByteString, a)forall a. IO a -> aunsafeDupablePerformIO(Int -> (ForeignPtr Word8 -> IO (Int, a)) -> IO (ByteString, a)forall a.Int -> (ForeignPtr Word8 -> IO (Int, a)) -> IO (ByteString, a)createFpUptoN'IntlForeignPtr Word8 -> IO (Int, a)f){-# INLINEunsafeCreateFpUptoN'#-}-- | Create ByteString of size @l@ and use action @f@ to fill its contents.createFp::Int->(ForeignPtrWord8->IO())->IOByteStringcreateFp :: Int -> (ForeignPtr Word8 -> IO ()) -> IO ByteStringcreateFpIntlenForeignPtr Word8 -> IO ()action=Bool -> IO ByteString -> IO ByteStringforall a. (?callStack::CallStack) => Bool -> a -> aassert(IntlenInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0)(IO ByteString -> IO ByteString) -> IO ByteString -> IO ByteStringforall a b. (a -> b) -> a -> b$doForeignPtr Word8fp<-Int -> IO (ForeignPtr Word8)forall a. Int -> IO (ForeignPtr a)mallocByteStringIntlenForeignPtr Word8 -> IO ()actionForeignPtr Word8fpForeignPtr Word8 -> Int -> IO ByteStringmkDeferredByteStringForeignPtr Word8fpIntlen{-# INLINEcreateFp#-}-- | Given a maximum size @l@ and an action @f@ that fills the 'ByteString'-- starting at the given 'Ptr' and returns the actual utilized length,-- @`createFpUptoN'` l f@ returns the filled 'ByteString'.createFpUptoN::Int->(ForeignPtrWord8->IOInt)->IOByteStringcreateFpUptoN :: Int -> (ForeignPtr Word8 -> IO Int) -> IO ByteStringcreateFpUptoNIntmaxLenForeignPtr Word8 -> IO Intaction=Bool -> IO ByteString -> IO ByteStringforall a. (?callStack::CallStack) => Bool -> a -> aassert(IntmaxLenInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0)(IO ByteString -> IO ByteString) -> IO ByteString -> IO ByteStringforall a b. (a -> b) -> a -> b$doForeignPtr Word8fp<-Int -> IO (ForeignPtr Word8)forall a. Int -> IO (ForeignPtr a)mallocByteStringIntmaxLenIntlen<-ForeignPtr Word8 -> IO IntactionForeignPtr Word8fpBool -> IO ByteString -> IO ByteStringforall a. (?callStack::CallStack) => Bool -> a -> aassert(Int0Int -> Int -> Boolforall a. Ord a => a -> a -> Bool<=IntlenBool -> Bool -> Bool&&IntlenInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=IntmaxLen)(IO ByteString -> IO ByteString) -> IO ByteString -> IO ByteStringforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> Int -> IO ByteStringmkDeferredByteStringForeignPtr Word8fpIntlen{-# INLINEcreateFpUptoN#-}-- | Like 'createFpUptoN', but also returns an additional value created by the-- action.createFpUptoN'::Int->(ForeignPtrWord8->IO(Int,a))->IO(ByteString,a)createFpUptoN' :: forall a.Int -> (ForeignPtr Word8 -> IO (Int, a)) -> IO (ByteString, a)createFpUptoN'IntmaxLenForeignPtr Word8 -> IO (Int, a)action=Bool -> IO (ByteString, a) -> IO (ByteString, a)forall a. (?callStack::CallStack) => Bool -> a -> aassert(IntmaxLenInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0)(IO (ByteString, a) -> IO (ByteString, a))-> IO (ByteString, a) -> IO (ByteString, a)forall a b. (a -> b) -> a -> b$doForeignPtr Word8fp<-Int -> IO (ForeignPtr Word8)forall a. Int -> IO (ForeignPtr a)mallocByteStringIntmaxLen(Intlen,ares)<-ForeignPtr Word8 -> IO (Int, a)actionForeignPtr Word8fpByteStringbs<-ForeignPtr Word8 -> Int -> IO ByteStringmkDeferredByteStringForeignPtr Word8fpIntlenBool -> IO (ByteString, a) -> IO (ByteString, a)forall a. (?callStack::CallStack) => Bool -> a -> aassert(Int0Int -> Int -> Boolforall a. Ord a => a -> a -> Bool<=IntlenBool -> Bool -> Bool&&IntlenInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=IntmaxLen)(IO (ByteString, a) -> IO (ByteString, a))-> IO (ByteString, a) -> IO (ByteString, a)forall a b. (a -> b) -> a -> b$(ByteString, a) -> IO (ByteString, a)forall a. a -> IO aforall (f :: * -> *) a. Applicative f => a -> f apure(ByteStringbs,ares){-# INLINEcreateFpUptoN'#-}-- | Given the maximum size needed and a function to make the contents-- of a ByteString, createFpAndTrim makes the 'ByteString'. The generating-- function is required to return the actual final size (<= the maximum-- size), and the resulting byte array is reallocated to this size.---- createFpAndTrim is the main mechanism for creating custom, efficient-- ByteString functions, using Haskell or C functions to fill the space.--createFpAndTrim::Int->(ForeignPtrWord8->IOInt)->IOByteStringcreateFpAndTrim :: Int -> (ForeignPtr Word8 -> IO Int) -> IO ByteStringcreateFpAndTrimIntmaxLenForeignPtr Word8 -> IO Intaction=Bool -> IO ByteString -> IO ByteStringforall a. (?callStack::CallStack) => Bool -> a -> aassert(IntmaxLenInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0)(IO ByteString -> IO ByteString) -> IO ByteString -> IO ByteStringforall a b. (a -> b) -> a -> b$doForeignPtr Word8fp<-Int -> IO (ForeignPtr Word8)forall a. Int -> IO (ForeignPtr a)mallocByteStringIntmaxLenIntlen<-ForeignPtr Word8 -> IO IntactionForeignPtr Word8fpifBool -> Bool -> Boolforall a. (?callStack::CallStack) => Bool -> a -> aassert(Int0Int -> Int -> Boolforall a. Ord a => a -> a -> Bool<=IntlenBool -> Bool -> Bool&&IntlenInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=IntmaxLen)(Bool -> Bool) -> Bool -> Boolforall a b. (a -> b) -> a -> b$IntlenInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=IntmaxLenthenForeignPtr Word8 -> Int -> IO ByteStringmkDeferredByteStringForeignPtr Word8fpIntmaxLenelseInt -> (ForeignPtr Word8 -> IO ()) -> IO ByteStringcreateFpIntlen((ForeignPtr Word8 -> IO ()) -> IO ByteString)-> (ForeignPtr Word8 -> IO ()) -> IO ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8dest->ForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()memcpyFpForeignPtr Word8destForeignPtr Word8fpIntlen{-# INLINEcreateFpAndTrim#-}createFpAndTrim'::Int->(ForeignPtrWord8->IO(Int,Int,a))->IO(ByteString,a)createFpAndTrim' :: forall a.Int -> (ForeignPtr Word8 -> IO (Int, Int, a)) -> IO (ByteString, a)createFpAndTrim'IntmaxLenForeignPtr Word8 -> IO (Int, Int, a)action=Bool -> IO (ByteString, a) -> IO (ByteString, a)forall a. (?callStack::CallStack) => Bool -> a -> aassert(IntmaxLenInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0)(IO (ByteString, a) -> IO (ByteString, a))-> IO (ByteString, a) -> IO (ByteString, a)forall a b. (a -> b) -> a -> b$doForeignPtr Word8fp<-Int -> IO (ForeignPtr Word8)forall a. Int -> IO (ForeignPtr a)mallocByteStringIntmaxLen(Intoff,Intlen,ares)<-ForeignPtr Word8 -> IO (Int, Int, a)actionForeignPtr Word8fpBool -> IO () -> IO ()forall a. (?callStack::CallStack) => Bool -> a -> aassert(Int0Int -> Int -> Boolforall a. Ord a => a -> a -> Bool<=IntlenBool -> Bool -> Bool&&IntlenInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=IntmaxLenBool -> Bool -> Bool&&-- length OK(IntlenInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==Int0Bool -> Bool -> Bool||(Int0Int -> Int -> Boolforall a. Ord a => a -> a -> Bool<=IntoffBool -> Bool -> Bool&&IntoffInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=IntmaxLenInt -> Int -> Intforall a. Num a => a -> a -> a-Intlen))-- offset OK)(IO () -> IO ()) -> IO () -> IO ()forall a b. (a -> b) -> a -> b$() -> IO ()forall a. a -> IO aforall (f :: * -> *) a. Applicative f => a -> f apure()ByteStringbs<-ifIntlenInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=IntmaxLenthenForeignPtr Word8 -> Int -> IO ByteStringmkDeferredByteStringForeignPtr Word8fpIntmaxLen-- entire buffer used => offset is zeroelseInt -> (ForeignPtr Word8 -> IO ()) -> IO ByteStringcreateFpIntlen((ForeignPtr Word8 -> IO ()) -> IO ByteString)-> (ForeignPtr Word8 -> IO ()) -> IO ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8dest->ForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()memcpyFpForeignPtr Word8dest(ForeignPtr Word8fpForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Intoff)Intlen(ByteString, a) -> IO (ByteString, a)forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(ByteStringbs,ares){-# INLINEcreateFpAndTrim'#-}wrapAction::(PtrWord8->IOres)->ForeignPtrWord8->IOreswrapAction :: forall res. (Ptr Word8 -> IO res) -> ForeignPtr Word8 -> IO reswrapAction=(ForeignPtr Word8 -> (Ptr Word8 -> IO res) -> IO res)-> (Ptr Word8 -> IO res) -> ForeignPtr Word8 -> IO resforall a b c. (a -> b -> c) -> b -> a -> cflipForeignPtr Word8 -> (Ptr Word8 -> IO res) -> IO resforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bwithForeignPtr-- Cannot use unsafeWithForeignPtr, because action can diverge-- | A way of creating ByteStrings outside the IO monad. The @Int@-- argument gives the final size of the ByteString.unsafeCreate::Int->(PtrWord8->IO())->ByteStringunsafeCreate :: Int -> (Ptr Word8 -> IO ()) -> ByteStringunsafeCreateIntlPtr Word8 -> IO ()f=Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFpIntl((Ptr Word8 -> IO ()) -> ForeignPtr Word8 -> IO ()forall res. (Ptr Word8 -> IO res) -> ForeignPtr Word8 -> IO reswrapActionPtr Word8 -> IO ()f){-# INLINEunsafeCreate#-}-- | Like 'unsafeCreate' but instead of giving the final size of the-- ByteString, it is just an upper bound. The inner action returns-- the actual size. Unlike 'createAndTrim' the ByteString is not-- reallocated if the final size is less than the estimated size.unsafeCreateUptoN::Int->(PtrWord8->IOInt)->ByteStringunsafeCreateUptoN :: Int -> (Ptr Word8 -> IO Int) -> ByteStringunsafeCreateUptoNIntlPtr Word8 -> IO Intf=Int -> (ForeignPtr Word8 -> IO Int) -> ByteStringunsafeCreateFpUptoNIntl((Ptr Word8 -> IO Int) -> ForeignPtr Word8 -> IO Intforall res. (Ptr Word8 -> IO res) -> ForeignPtr Word8 -> IO reswrapActionPtr Word8 -> IO Intf){-# INLINEunsafeCreateUptoN#-}-- | @since 0.10.12.0unsafeCreateUptoN'::Int->(PtrWord8->IO(Int,a))->(ByteString,a)unsafeCreateUptoN' :: forall a. Int -> (Ptr Word8 -> IO (Int, a)) -> (ByteString, a)unsafeCreateUptoN'IntlPtr Word8 -> IO (Int, a)f=Int -> (ForeignPtr Word8 -> IO (Int, a)) -> (ByteString, a)forall a.Int -> (ForeignPtr Word8 -> IO (Int, a)) -> (ByteString, a)unsafeCreateFpUptoN'Intl((Ptr Word8 -> IO (Int, a)) -> ForeignPtr Word8 -> IO (Int, a)forall res. (Ptr Word8 -> IO res) -> ForeignPtr Word8 -> IO reswrapActionPtr Word8 -> IO (Int, a)f){-# INLINEunsafeCreateUptoN'#-}-- | Create ByteString of size @l@ and use action @f@ to fill its contents.create::Int->(PtrWord8->IO())->IOByteStringcreate :: Int -> (Ptr Word8 -> IO ()) -> IO ByteStringcreateIntlPtr Word8 -> IO ()action=Int -> (ForeignPtr Word8 -> IO ()) -> IO ByteStringcreateFpIntl((Ptr Word8 -> IO ()) -> ForeignPtr Word8 -> IO ()forall res. (Ptr Word8 -> IO res) -> ForeignPtr Word8 -> IO reswrapActionPtr Word8 -> IO ()action){-# INLINEcreate#-}-- | Given a maximum size @l@ and an action @f@ that fills the 'ByteString'-- starting at the given 'Ptr' and returns the actual utilized length,-- @`createUptoN'` l f@ returns the filled 'ByteString'.createUptoN::Int->(PtrWord8->IOInt)->IOByteStringcreateUptoN :: Int -> (Ptr Word8 -> IO Int) -> IO ByteStringcreateUptoNIntlPtr Word8 -> IO Intaction=Int -> (ForeignPtr Word8 -> IO Int) -> IO ByteStringcreateFpUptoNIntl((Ptr Word8 -> IO Int) -> ForeignPtr Word8 -> IO Intforall res. (Ptr Word8 -> IO res) -> ForeignPtr Word8 -> IO reswrapActionPtr Word8 -> IO Intaction){-# INLINEcreateUptoN#-}-- | Like 'createUptoN', but also returns an additional value created by the-- action.---- @since 0.10.12.0createUptoN'::Int->(PtrWord8->IO(Int,a))->IO(ByteString,a)createUptoN' :: forall a. Int -> (Ptr Word8 -> IO (Int, a)) -> IO (ByteString, a)createUptoN'IntlPtr Word8 -> IO (Int, a)action=Int -> (ForeignPtr Word8 -> IO (Int, a)) -> IO (ByteString, a)forall a.Int -> (ForeignPtr Word8 -> IO (Int, a)) -> IO (ByteString, a)createFpUptoN'Intl((Ptr Word8 -> IO (Int, a)) -> ForeignPtr Word8 -> IO (Int, a)forall res. (Ptr Word8 -> IO res) -> ForeignPtr Word8 -> IO reswrapActionPtr Word8 -> IO (Int, a)action){-# INLINEcreateUptoN'#-}-- | Given the maximum size needed and a function to make the contents-- of a ByteString, createAndTrim makes the 'ByteString'. The generating-- function is required to return the actual final size (<= the maximum-- size), and the resulting byte array is reallocated to this size.---- createAndTrim is the main mechanism for creating custom, efficient-- ByteString functions, using Haskell or C functions to fill the space.--createAndTrim::Int->(PtrWord8->IOInt)->IOByteStringcreateAndTrim :: Int -> (Ptr Word8 -> IO Int) -> IO ByteStringcreateAndTrimIntlPtr Word8 -> IO Intaction=Int -> (ForeignPtr Word8 -> IO Int) -> IO ByteStringcreateFpAndTrimIntl((Ptr Word8 -> IO Int) -> ForeignPtr Word8 -> IO Intforall res. (Ptr Word8 -> IO res) -> ForeignPtr Word8 -> IO reswrapActionPtr Word8 -> IO Intaction){-# INLINEcreateAndTrim#-}createAndTrim'::Int->(PtrWord8->IO(Int,Int,a))->IO(ByteString,a)createAndTrim' :: forall a.Int -> (Ptr Word8 -> IO (Int, Int, a)) -> IO (ByteString, a)createAndTrim'IntlPtr Word8 -> IO (Int, Int, a)action=Int -> (ForeignPtr Word8 -> IO (Int, Int, a)) -> IO (ByteString, a)forall a.Int -> (ForeignPtr Word8 -> IO (Int, Int, a)) -> IO (ByteString, a)createFpAndTrim'Intl((Ptr Word8 -> IO (Int, Int, a))-> ForeignPtr Word8 -> IO (Int, Int, a)forall res. (Ptr Word8 -> IO res) -> ForeignPtr Word8 -> IO reswrapActionPtr Word8 -> IO (Int, Int, a)action){-# INLINEcreateAndTrim'#-}-- | Wrapper of 'Foreign.ForeignPtr.mallocForeignPtrBytes' with faster implementation for GHC--mallocByteString::Int->IO(ForeignPtra)mallocByteString :: forall a. Int -> IO (ForeignPtr a)mallocByteString=Int -> IO (ForeignPtr a)forall a. Int -> IO (ForeignPtr a)mallocPlainForeignPtrBytes{-# INLINEmallocByteString#-}-------------------------------------------------------------------------- Implementations for Eq, Ord and Monoid instanceseq::ByteString->ByteString->Booleq :: ByteString -> ByteString -> Booleqa :: ByteStringa@(BSForeignPtr Word8fpIntlen)b :: ByteStringb@(BSForeignPtr Word8fp'Intlen')|IntlenInt -> Int -> Boolforall a. Eq a => a -> a -> Bool/=Intlen'=BoolFalse-- short cut on length|ForeignPtr Word8fpForeignPtr Word8 -> ForeignPtr Word8 -> Boolforall a. Eq a => a -> a -> Bool==ForeignPtr Word8fp'=BoolTrue-- short cut for the same string|Boolotherwise=ByteString -> ByteString -> OrderingcompareBytesByteStringaByteStringbOrdering -> Ordering -> Boolforall a. Eq a => a -> a -> Bool==OrderingEQ{-# INLINEeq#-}-- ^ still neededcompareBytes::ByteString->ByteString->OrderingcompareBytes :: ByteString -> ByteString -> OrderingcompareBytes(BSForeignPtr Word8_Int0)(BSForeignPtr Word8_Int0)=OrderingEQ-- short cut for empty stringscompareBytes(BSForeignPtr Word8fp1Intlen1)(BSForeignPtr Word8fp2Intlen2)=IO Ordering -> Orderingforall a. IO a -> aaccursedUnutterablePerformIO(IO Ordering -> Ordering) -> IO Ordering -> Orderingforall a b. (a -> b) -> a -> b$ForeignPtr Word8 -> (Ptr Word8 -> IO Ordering) -> IO Orderingforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp1((Ptr Word8 -> IO Ordering) -> IO Ordering)-> (Ptr Word8 -> IO Ordering) -> IO Orderingforall a b. (a -> b) -> a -> b$\Ptr Word8p1->ForeignPtr Word8 -> (Ptr Word8 -> IO Ordering) -> IO Orderingforall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp2((Ptr Word8 -> IO Ordering) -> IO Ordering)-> (Ptr Word8 -> IO Ordering) -> IO Orderingforall a b. (a -> b) -> a -> b$\Ptr Word8p2->doCInti<-Ptr Word8 -> Ptr Word8 -> Int -> IO CIntmemcmpPtr Word8p1Ptr Word8p2(Int -> Int -> Intforall a. Ord a => a -> a -> aminIntlen1Intlen2)Ordering -> IO Orderingforall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn(Ordering -> IO Ordering) -> Ordering -> IO Orderingforall a b. (a -> b) -> a -> b$!caseCIntiCInt -> CInt -> Orderingforall a. Ord a => a -> a -> Ordering`compare`CInt0ofOrderingEQ->Intlen1Int -> Int -> Orderingforall a. Ord a => a -> a -> Ordering`compare`Intlen2Orderingx->Orderingx-- | /O(1)/ The empty 'ByteString'empty::ByteString-- This enables bypassing #457 by not using (polymorphic) mempty in-- any definitions used by the (Monoid ByteString) instanceempty :: ByteStringempty=ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8nullForeignPtrInt0append::ByteString->ByteString->ByteStringappend :: ByteString -> ByteString -> ByteStringappend(BSForeignPtr Word8_Int0)ByteStringb=ByteStringbappendByteStringa(BSForeignPtr Word8_Int0)=ByteStringaappend(BSForeignPtr Word8fp1Intlen1)(BSForeignPtr Word8fp2Intlen2)=Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFp(String -> Int -> Int -> IntcheckedAddString"append"Intlen1Intlen2)((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8destptr1->doletdestptr2 :: ForeignPtr Word8destptr2=ForeignPtr Word8destptr1ForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Intlen1ForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()memcpyFpForeignPtr Word8destptr1ForeignPtr Word8fp1Intlen1ForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()memcpyFpForeignPtr Word8destptr2ForeignPtr Word8fp2Intlen2concat::[ByteString]->ByteStringconcat :: [ByteString] -> ByteStringconcat=\[ByteString]bss0->[ByteString] -> [ByteString] -> ByteStringgoLen0[ByteString]bss0[ByteString]bss0-- The idea here is we first do a pass over the input list to determine:----  1. is a copy necessary? e.g. @concat []@, @concat [mempty, "hello"]@,--     and @concat ["hello", mempty, mempty]@ can all be handled without--     copying.--  2. if a copy is necessary, how large is the result going to be?---- If a copy is necessary then we create a buffer of the appropriate size-- and do another pass over the input list, copying the chunks into the-- buffer. Also, since foreign calls aren't entirely free we skip over-- empty chunks while copying.---- We pass the original [ByteString] (bss0) through as an argument through-- goLen0, goLen1, and goLen since we will need it again in goCopy. Passing-- it as an explicit argument avoids capturing it in these functions'-- closures which would result in unnecessary closure allocation.where-- It's still possible that the result is emptygoLen0 :: [ByteString] -> [ByteString] -> ByteStringgoLen0[ByteString]_[]=ByteStringemptygoLen0[ByteString]bss0(BSForeignPtr Word8_Int0:[ByteString]bss)=[ByteString] -> [ByteString] -> ByteStringgoLen0[ByteString]bss0[ByteString]bssgoLen0[ByteString]bss0(ByteStringbs:[ByteString]bss)=[ByteString] -> ByteString -> [ByteString] -> ByteStringgoLen1[ByteString]bss0ByteStringbs[ByteString]bss-- It's still possible that the result is a single chunkgoLen1 :: [ByteString] -> ByteString -> [ByteString] -> ByteStringgoLen1[ByteString]_ByteStringbs[]=ByteStringbsgoLen1[ByteString]bss0ByteStringbs(BSForeignPtr Word8_Int0:[ByteString]bss)=[ByteString] -> ByteString -> [ByteString] -> ByteStringgoLen1[ByteString]bss0ByteStringbs[ByteString]bssgoLen1[ByteString]bss0ByteStringbs(BSForeignPtr Word8_Intlen:[ByteString]bss)=[ByteString] -> Int -> [ByteString] -> ByteStringgoLen[ByteString]bss0(String -> Int -> Int -> IntcheckedAddString"concat"Intlen'Intlen)[ByteString]bsswhereBSForeignPtr Word8_Intlen'=ByteStringbs-- General case, just find the total length we'll needgoLen :: [ByteString] -> Int -> [ByteString] -> ByteStringgoLen[ByteString]bss0!Inttotal(BSForeignPtr Word8_Intlen:[ByteString]bss)=[ByteString] -> Int -> [ByteString] -> ByteStringgoLen[ByteString]bss0Inttotal'[ByteString]bsswheretotal' :: Inttotal'=String -> Int -> Int -> IntcheckedAddString"concat"InttotalIntlengoLen[ByteString]bss0Inttotal[]=Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFpInttotal((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8ptr->[ByteString] -> ForeignPtr Word8 -> IO ()goCopy[ByteString]bss0ForeignPtr Word8ptr-- Copy the datagoCopy :: [ByteString] -> ForeignPtr Word8 -> IO ()goCopy[]!ForeignPtr Word8_=() -> IO ()forall a. a -> IO aforall (m :: * -> *) a. Monad m => a -> m areturn()goCopy(BSForeignPtr Word8_Int0:[ByteString]bss)!ForeignPtr Word8ptr=[ByteString] -> ForeignPtr Word8 -> IO ()goCopy[ByteString]bssForeignPtr Word8ptrgoCopy(BSForeignPtr Word8fpIntlen:[ByteString]bss)!ForeignPtr Word8ptr=doForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()memcpyFpForeignPtr Word8ptrForeignPtr Word8fpIntlen[ByteString] -> ForeignPtr Word8 -> IO ()goCopy[ByteString]bss(ForeignPtr Word8ptrForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Intlen){-# NOINLINEconcat#-}{-# RULES"ByteString concat [] -> empty"concat[]=empty"ByteString concat [bs] -> bs"forallx.concat[x]=x#-}-- | Repeats the given ByteString n times.-- Polymorphic wrapper to make sure any generated-- specializations are reasonably small.stimesPolymorphic::Integrala=>a->ByteString->ByteString{-# INLINABLEstimesPolymorphic#-}stimesPolymorphic :: forall b. Integral b => b -> ByteString -> ByteStringstimesPolymorphicanRaw!ByteStringbs=caseInteger -> Maybe IntcheckedIntegerToIntIntegernofJustIntnInt|IntnIntInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0->Int -> ByteString -> ByteStringstimesNonNegativeIntIntnIntByteStringbs|Boolotherwise->ByteStringstimesNegativeErrMaybe IntNothing|IntegernInteger -> Integer -> Boolforall a. Ord a => a -> a -> Bool<Integer0->ByteStringstimesNegativeErr|BSForeignPtr Word8_Int0<-ByteStringbs->ByteStringempty|Boolotherwise->ByteStringstimesOverflowErrwheren :: Integern=a -> Integerforall a. Integral a => a -> IntegertoIntegeranRaw-- By exclusively using n instead of nRaw, the semantics are kept simple-- and the likelihood of potentially dangerous mistakes minimized.{-Note [Float error calls out of INLINABLE things]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~If a function is marked INLINE or INLINABLE, then when ghc inlines orspecializes it, it duplicates the function body exactly as written.This feature is useful for systems of rewrite rules, but sometimescomes at a code-size cost.  One situation where this cost generallycomes with no compensating up-side is when the function in questioncalls `error` or something similar.Such an `error` call is not meaningfully improved by the extra contextinlining or specialization provides, and if inlining or specializationhappens in a different module from where the function was originallydefined, CSE will not be able to de-duplicate the error call floatedout of the inlined RHS and the error call floated out of the originalRHS.  See also https://gitlab.haskell.org/ghc/ghc/-/issues/23823To mitigate this, we manually float the error calls out of INLINABLEfunctions when it is possible to do so.-}stimesNegativeErr::ByteString-- See Note [Float error calls out of INLINABLE things]stimesNegativeErr :: ByteStringstimesNegativeErr=String -> ByteStringforall a. String -> aerrorWithoutStackTraceString"stimes @ByteString: non-negative multiplier expected"stimesOverflowErr::ByteString-- See Note [Float error calls out of INLINABLE things]stimesOverflowErr :: ByteStringstimesOverflowErr=String -> ByteStringforall a. String -> aoverflowErrorString"stimes"-- | Repeats the given ByteString n times.stimesNonNegativeInt::Int->ByteString->ByteStringstimesNonNegativeInt :: Int -> ByteString -> ByteStringstimesNonNegativeIntIntn(BSForeignPtr Word8fpIntlen)|IntnInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==Int0=ByteStringempty|IntnInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==Int1=ForeignPtr Word8 -> Int -> ByteStringBSForeignPtr Word8fpIntlen|IntlenInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==Int0=ByteStringempty|IntlenInt -> Int -> Boolforall a. Eq a => a -> a -> Bool==Int1=Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFpIntn((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8destfptr->doWord8byte<-ForeignPtr Word8 -> IO Word8forall a. Storable a => ForeignPtr a -> IO apeekFpForeignPtr Word8fpForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8destfptr((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Word8destptr->Ptr Word8 -> Word8 -> Int -> IO ()forall a. Ptr a -> Word8 -> Int -> IO ()fillBytesPtr Word8destptrWord8byteIntn|Boolotherwise=Int -> (ForeignPtr Word8 -> IO ()) -> ByteStringunsafeCreateFpIntsize((ForeignPtr Word8 -> IO ()) -> ByteString)-> (ForeignPtr Word8 -> IO ()) -> ByteStringforall a b. (a -> b) -> a -> b$\ForeignPtr Word8destptr->doForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()memcpyFpForeignPtr Word8destptrForeignPtr Word8fpIntlenForeignPtr Word8 -> Int -> IO ()fillFromForeignPtr Word8destptrIntlenwheresize :: Intsize=String -> Int -> Int -> IntcheckedMultiplyString"stimes"IntnIntlenhalfSize :: InthalfSize=(IntsizeInt -> Int -> Intforall a. Num a => a -> a -> a-Int1)Int -> Int -> Intforall a. Integral a => a -> a -> a`div`Int2-- subtraction and division won't overflowfillFrom::ForeignPtrWord8->Int->IO()fillFrom :: ForeignPtr Word8 -> Int -> IO ()fillFromForeignPtr Word8destptrIntcopied|IntcopiedInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<=InthalfSize=doForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()memcpyFp(ForeignPtr Word8destptrForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Intcopied)ForeignPtr Word8destptrIntcopiedForeignPtr Word8 -> Int -> IO ()fillFromForeignPtr Word8destptr(IntcopiedInt -> Int -> Intforall a. Num a => a -> a -> a*Int2)|Boolotherwise=ForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()memcpyFp(ForeignPtr Word8destptrForeignPtr Word8 -> Int -> ForeignPtr Word8forall a b. ForeignPtr a -> Int -> ForeignPtr b`plusForeignPtr`Intcopied)ForeignPtr Word8destptr(IntsizeInt -> Int -> Intforall a. Num a => a -> a -> a-Intcopied)-------------------------------------------------------------------------- | Conversion between 'Word8' and 'Char'. Should compile to a no-op.w2c::Word8->Charw2c :: Word8 -> Charw2c=Int -> CharunsafeChr(Int -> Char) -> (Word8 -> Int) -> Word8 -> Charforall b c a. (b -> c) -> (a -> b) -> a -> c.Word8 -> Intforall a b. (Integral a, Num b) => a -> bfromIntegral{-# INLINEw2c#-}-- | Unsafe conversion between 'Char' and 'Word8'. This is a no-op and-- silently truncates to 8 bits Chars > '\255'. It is provided as-- convenience for ByteString construction.c2w::Char->Word8c2w :: Char -> Word8c2w=Int -> Word8forall a b. (Integral a, Num b) => a -> bfromIntegral(Int -> Word8) -> (Char -> Int) -> Char -> Word8forall b c a. (b -> c) -> (a -> b) -> a -> c.Char -> Intord{-# INLINEc2w#-}-- | Selects words corresponding to white-space characters in the Latin-1 rangeisSpaceWord8::Word8->BoolisSpaceWord8 :: Word8 -> BoolisSpaceWord8Word8w8=-- Avoid the cost of narrowing arithmetic results to Word8,-- the conversion from Word8 to Word is free.letw::Word!w :: Wordw=Word8 -> Wordforall a b. (Integral a, Num b) => a -> bfromIntegralWord8w8inWordwWord -> Word -> Wordforall a. Bits a => a -> a -> a.&.Word0x50Word -> Word -> Boolforall a. Eq a => a -> a -> Bool==Word0-- Quick non-whitespace filterBool -> Bool -> Bool&&WordwWord -> Word -> Wordforall a. Num a => a -> a -> a-Word0x21Word -> Word -> Boolforall a. Ord a => a -> a -> Bool>Word0x7e-- Second non-whitespace filterBool -> Bool -> Bool&&(WordwWord -> Word -> Boolforall a. Eq a => a -> a -> Bool==Word0x20-- SPBool -> Bool -> Bool||WordwWord -> Word -> Boolforall a. Eq a => a -> a -> Bool==Word0xa0-- NBSPBool -> Bool -> Bool||WordwWord -> Word -> Wordforall a. Num a => a -> a -> a-Word0x09Word -> Word -> Boolforall a. Ord a => a -> a -> Bool<Word5)-- HT, NL, VT, FF, CR{-# INLINEisSpaceWord8#-}-- | Selects white-space characters in the Latin-1 rangeisSpaceChar8::Char->BoolisSpaceChar8 :: Char -> BoolisSpaceChar8=Word8 -> BoolisSpaceWord8(Word8 -> Bool) -> (Char -> Word8) -> Char -> Boolforall b c a. (b -> c) -> (a -> b) -> a -> c.Char -> Word8c2w{-# INLINEisSpaceChar8#-}-------------------------------------------------------------------------- | The type of exception raised by 'overflowError'-- and on failure by overflow-checked arithmetic operations.newtypeSizeOverflowException=SizeOverflowExceptionStringinstanceShowSizeOverflowExceptionwhereshow :: SizeOverflowException -> Stringshow(SizeOverflowExceptionStringerr)=StringerrinstanceExceptionSizeOverflowException-- | Raises a 'SizeOverflowException',-- with a message using the given function name.overflowError::String->aoverflowError :: forall a. String -> aoverflowErrorStringfun=SizeOverflowException -> aforall a e. Exception e => e -> athrow(SizeOverflowException -> a) -> SizeOverflowException -> aforall a b. (a -> b) -> a -> b$String -> SizeOverflowExceptionSizeOverflowExceptionStringmsgwheremsg :: Stringmsg=String"Data.ByteString."String -> ShowSforall a. [a] -> [a] -> [a]++StringfunString -> ShowSforall a. [a] -> [a] -> [a]++String": size overflow"-- | Add two non-negative numbers.-- Calls 'overflowError' on overflow.checkedAdd::String->Int->Int->Int{-# INLINEcheckedAdd#-}checkedAdd :: String -> Int -> Int -> IntcheckedAddStringfunIntxInty-- checking "r < 0" here matches the condition in mallocPlainForeignPtrBytes,-- helping the compiler see the latter is redundant in some places|IntrInt -> Int -> Boolforall a. Ord a => a -> a -> Bool<Int0=String -> Intforall a. String -> aoverflowErrorStringfun|Boolotherwise=Intrwherer :: Intr=Bool -> Int -> Intforall a. (?callStack::CallStack) => Bool -> a -> aassert(Int -> Int -> Intforall a. Ord a => a -> a -> aminIntxIntyInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0)(Int -> Int) -> Int -> Intforall a b. (a -> b) -> a -> b$IntxInt -> Int -> Intforall a. Num a => a -> a -> a+Inty-- | Multiplies two non-negative numbers.-- Calls 'overflowError' on overflow.checkedMultiply::String->Int->Int->Int{-# INLINEcheckedMultiply#-}checkedMultiply :: String -> Int -> Int -> IntcheckedMultiplyStringfun!x :: Intx@(I#Int#x#)!y :: Inty@(I#Int#y#)=Bool -> Int -> Intforall a. (?callStack::CallStack) => Bool -> a -> aassert(Int -> Int -> Intforall a. Ord a => a -> a -> aminIntxIntyInt -> Int -> Boolforall a. Ord a => a -> a -> Bool>=Int0)(Int -> Int) -> Int -> Intforall a b. (a -> b) -> a -> b$#if HS_timesInt2_PRIMOP_AVAILABLEcaseInt# -> Int# -> (# Int#, Int#, Int# #)timesInt2#Int#x#Int#y#of(#Int#0#,Int#_,Int#result#)->Int# -> IntI#Int#result(# Int#, Int#, Int# #)_->String -> Intforall a. String -> aoverflowErrorStringfun#elsecasetimesWord2#(int2Word#x#)(int2Word#y#)of(#hi,lo#)->caseor#hi(uncheckedShiftRL#loshiftAmt)of0##->I#(word2Int#lo)_->overflowErrorfunwhere!(I#shiftAmt)=finiteBitSize(0::Word)-1#endif-- | Attempts to convert an 'Integer' value to an 'Int', returning-- 'Nothing' if doing so would result in an overflow.checkedIntegerToInt::Integer->MaybeInt{-# INLINEcheckedIntegerToInt#-}-- We could use Data.Bits.toIntegralSized, but this hand-rolled-- version is currently a bit faster as of GHC 9.2.-- It's even faster to just match on the Integer constructors, but-- we'd still need a fallback implementation for integer-simple.checkedIntegerToInt :: Integer -> Maybe IntcheckedIntegerToIntIntegerx|IntegerxInteger -> Integer -> Boolforall a. Eq a => a -> a -> Bool==Int -> Integerforall a. Integral a => a -> IntegertoIntegerIntres=Int -> Maybe Intforall a. a -> Maybe aJustIntres|Boolotherwise=Maybe Intforall a. Maybe aNothingwhereres :: Intres=Integer -> Intforall a. Num a => Integer -> afromIntegerIntegerx::Int-------------------------------------------------------------------------- | This \"function\" has a superficial similarity to 'System.IO.Unsafe.unsafePerformIO' but-- it is in fact a malevolent agent of chaos. It unpicks the seams of reality-- (and the 'IO' monad) so that the normal rules no longer apply. It lulls you-- into thinking it is reasonable, but when you are not looking it stabs you-- in the back and aliases all of your mutable buffers. The carcass of many a-- seasoned Haskell programmer lie strewn at its feet.---- Witness the trail of destruction:---- * <https://github.com/haskell/bytestring/commit/71c4b438c675aa360c79d79acc9a491e7bbc26e7>---- * <https://github.com/haskell/bytestring/commit/210c656390ae617d9ee3b8bcff5c88dd17cef8da>---- * <https://github.com/haskell/aeson/commit/720b857e2e0acf2edc4f5512f2b217a89449a89d>---- * <https://ghc.haskell.org/trac/ghc/ticket/3486>---- * <https://ghc.haskell.org/trac/ghc/ticket/3487>---- * <https://ghc.haskell.org/trac/ghc/ticket/7270>---- * <https://gitlab.haskell.org/ghc/ghc/-/issues/22204>---- Do not talk about \"safe\"! You do not know what is safe!---- Yield not to its blasphemous call! Flee traveller! Flee or you will be-- corrupted and devoured!--{-# INLINEaccursedUnutterablePerformIO#-}accursedUnutterablePerformIO::IOa->aaccursedUnutterablePerformIO :: forall a. IO a -> aaccursedUnutterablePerformIO(IOState# RealWorld -> (# State# RealWorld, a #)m)=caseState# RealWorld -> (# State# RealWorld, a #)mState# RealWorldrealWorld#of(#State# RealWorld_,ar#)->ar-- ------------------------------------------------------------------------- Standard C functions--memchr::PtrWord8->Word8->CSize->IO(PtrWord8)memcmp::PtrWord8->PtrWord8->Int->IOCInt{-# DEPRECATEDmemset"Use Foreign.Marshal.Utils.fillBytes instead"#-}-- | deprecated since @bytestring-0.11.5.0@memset::PtrWord8->Word8->CSize->IO(PtrWord8)#if !PURE_HASKELLforeignimportccallunsafe"string.h strlen"c_strlen::CString->IOCSizeforeignimportccallunsafe"string.h memchr"c_memchr::PtrWord8->CInt->CSize->IO(PtrWord8)memchr :: Ptr Word8 -> Word8 -> CSize -> IO (Ptr Word8)memchrPtr Word8pWord8wCSizesz=Ptr Word8 -> CInt -> CSize -> IO (Ptr Word8)c_memchrPtr Word8p(Word8 -> CIntforall a b. (Integral a, Num b) => a -> bfromIntegralWord8w)CSizeszforeignimportccallunsafe"string.h memcmp"c_memcmp::PtrWord8->PtrWord8->CSize->IOCIntmemcmp :: Ptr Word8 -> Ptr Word8 -> Int -> IO CIntmemcmpPtr Word8pPtr Word8qInts=Ptr Word8 -> Ptr Word8 -> CSize -> IO CIntc_memcmpPtr Word8pPtr Word8q(Int -> CSizeforall a b. (Integral a, Num b) => a -> bfromIntegralInts)foreignimportccallunsafe"string.h memset"c_memset::PtrWord8->CInt->CSize->IO(PtrWord8)memset :: Ptr Word8 -> Word8 -> CSize -> IO (Ptr Word8)memsetPtr Word8pWord8wCSizesz=Ptr Word8 -> CInt -> CSize -> IO (Ptr Word8)c_memsetPtr Word8p(Word8 -> CIntforall a b. (Integral a, Num b) => a -> bfromIntegralWord8w)CSizesz#elsec_strlen::CString->IOCSizec_strlenp=checkedCast<$!>Pure.strlen(castPtrp)memchrpwlen=Pure.memchrpw(checkedCastlen)memcmppqs=checkedCast<$!>Pure.memcmppqsmemsetpwlen=p<$fillBytespw(checkedCastlen)#endif{-# DEPRECATEDmemcpy"Use Foreign.Marshal.Utils.copyBytes instead"#-}-- | deprecated since @bytestring-0.11.5.0@memcpy::PtrWord8->PtrWord8->Int->IO()memcpy :: Ptr Word8 -> Ptr Word8 -> Int -> IO ()memcpy=Ptr Word8 -> Ptr Word8 -> Int -> IO ()forall a. Ptr a -> Ptr a -> Int -> IO ()copyBytesmemcpyFp::ForeignPtrWord8->ForeignPtrWord8->Int->IO()memcpyFp :: ForeignPtr Word8 -> ForeignPtr Word8 -> Int -> IO ()memcpyFpForeignPtr Word8fpForeignPtr Word8fqInts=ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fp((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Word8p->ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO bunsafeWithForeignPtrForeignPtr Word8fq((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()forall a b. (a -> b) -> a -> b$\Ptr Word8q->Ptr Word8 -> Ptr Word8 -> Int -> IO ()forall a. Ptr a -> Ptr a -> Int -> IO ()copyBytesPtr Word8pPtr Word8qIntsc_free_finalizer::FunPtr(PtrWord8->IO())c_free_finalizer :: FunPtr (Ptr Word8 -> IO ())c_free_finalizer=FunPtr (Ptr Word8 -> IO ())forall a. FinalizerPtr afinalizerFree-- ------------------------------------------------------------------------- Uses our C code--#if !PURE_HASKELLforeignimportccallunsafe"static fpstring.h fps_reverse"c_reverse::PtrWord8->PtrWord8->CSize->IO()foreignimportccallunsafe"static fpstring.h fps_intersperse"c_intersperse::PtrWord8->PtrWord8->CSize->Word8->IO()foreignimportccallunsafe"static fpstring.h fps_maximum"c_maximum::PtrWord8->CSize->IOWord8foreignimportccallunsafe"static fpstring.h fps_minimum"c_minimum::PtrWord8->CSize->IOWord8foreignimportccallunsafe"static fpstring.h fps_count"c_count::PtrWord8->CSize->Word8->IOCSize-- fps_count works with both pointers and ByteArray#foreignimportccallunsafe"static fpstring.h fps_count"c_count_ba::ByteArray#->CSize->Word8->IOCSizeforeignimportccallunsafe"static fpstring.h fps_sort"c_sort::PtrWord8->CSize->IO()foreignimportccallunsafe"static sbs_elem_index"c_elem_index::ByteArray#->Word8->CSize->IOCPtrdiffforeignimportccallunsafe"static _hs_bytestring_uint_dec"c_uint_dec::CUInt->PtrWord8->IO(PtrWord8)foreignimportccallunsafe"static _hs_bytestring_long_long_uint_dec"c_long_long_uint_dec::CULLong->PtrWord8->IO(PtrWord8)foreignimportccallunsafe"static _hs_bytestring_int_dec"c_int_dec::CInt->PtrWord8->IO(PtrWord8)foreignimportccallunsafe"static _hs_bytestring_long_long_int_dec"c_long_long_int_dec::CLLong->PtrWord8->IO(PtrWord8)foreignimportccallunsafe"static _hs_bytestring_uint_hex"c_uint_hex::CUInt->PtrWord8->IO(PtrWord8)foreignimportccallunsafe"static _hs_bytestring_long_long_uint_hex"c_long_long_uint_hex::CULLong->PtrWord8->IO(PtrWord8)foreignimportccallunsafe"static _hs_bytestring_int_dec_padded9"c_int_dec_padded9::CInt->PtrWord8->IO()foreignimportccallunsafe"static _hs_bytestring_long_long_int_dec_padded18"c_long_long_int_dec_padded18::CLLong->PtrWord8->IO()-- 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"cIsValidUtf8BA::ByteArray#->CSize->IOCIntforeignimportccallsafe"bytestring_is_valid_utf8"cIsValidUtf8BASafe::ByteArray#->CSize->IOCIntforeignimportccallunsafe"bytestring_is_valid_utf8"cIsValidUtf8::PtrWord8->CSize->IOCIntforeignimportccallsafe"bytestring_is_valid_utf8"cIsValidUtf8Safe::PtrWord8->CSize->IOCInt#else------------------------------------------------------------------ Haskell version of functions in fpstring.c------------------------------------------------------------------ | Reverse n-bytes from the second pointer into the firstc_reverse::PtrWord8->PtrWord8->CSize->IO()c_reversep1p2sz=Pure.reverseBytesp1p2(checkedCastsz)-- | find maximum char in a packed stringc_maximum::PtrWord8->CSize->IOWord8c_maximumptrsz=Pure.findMaximumptr(checkedCastsz)-- | find minimum char in a packed stringc_minimum::PtrWord8->CSize->IOWord8c_minimumptrsz=Pure.findMinimumptr(checkedCastsz)-- | count the number of occurrences of a char in a stringc_count::PtrWord8->CSize->Word8->IOCSizec_countptrszc=checkedCast<$!>Pure.countOccptr(checkedCastsz)c-- | count the number of occurrences of a char in a stringc_count_ba::ByteArray#->Int->Word8->IOCSizec_count_babaoc=checkedCast<$!>Pure.countOccBAbaoc-- | duplicate a string, interspersing the character through the elements of the-- duplicated stringc_intersperse::PtrWord8->PtrWord8->CSize->Word8->IO()c_interspersep1p2sze=Pure.interspersep1p2(checkedCastsz)e-- | Quick sort bytesc_sort::PtrWord8->CSize->IO()c_sortptrsz=Pure.quickSortptr(checkedCastsz)c_elem_index::ByteArray#->Word8->CSize->IOCPtrdiffc_elem_indexbaesz=checkedCast<$!>Pure.elemIndexbae(checkedCastsz)cIsValidUtf8BA::ByteArray#->CSize->IOCIntcIsValidUtf8BAbasz=bool_to_cint<$>Pure.isValidUtf8BAba(checkedCastsz)cIsValidUtf8::PtrWord8->CSize->IOCIntcIsValidUtf8ptrsz=bool_to_cint<$>Pure.isValidUtf8ptr(checkedCastsz)-- Pure module is compiled with `-fno-omit-yields` so it's always safe (it won't-- block on large inputs)cIsValidUtf8BASafe::ByteArray#->CSize->IOCIntcIsValidUtf8BASafe=cIsValidUtf8BAcIsValidUtf8Safe::PtrWord8->CSize->IOCIntcIsValidUtf8Safe=cIsValidUtf8bool_to_cint::Bool->CIntbool_to_cintTrue=1bool_to_cintFalse=0checkedCast::(Bitsa,Bitsb,Integrala,Integralb)=>a->bcheckedCastx=fromMaybe(errorWithoutStackTrace"checkedCast: overflow")(toIntegralSizedx)------------------------------------------------------------------ Haskell version of functions in itoa.c----------------------------------------------------------------c_int_dec::CInt->PtrWord8->IO(PtrWord8)c_int_dec=Pure.encodeSignedDecc_long_long_int_dec::CLLong->PtrWord8->IO(PtrWord8)c_long_long_int_dec=Pure.encodeSignedDecc_uint_dec::CUInt->PtrWord8->IO(PtrWord8)c_uint_dec=Pure.encodeUnsignedDecc_long_long_uint_dec::CULLong->PtrWord8->IO(PtrWord8)c_long_long_uint_dec=Pure.encodeUnsignedDecc_uint_hex::CUInt->PtrWord8->IO(PtrWord8)c_uint_hex=Pure.encodeUnsignedHexc_long_long_uint_hex::CULLong->PtrWord8->IO(PtrWord8)c_long_long_uint_hex=Pure.encodeUnsignedHexc_int_dec_padded9::CInt->PtrWord8->IO()c_int_dec_padded9=Pure.encodeUnsignedDecPadded9c_long_long_int_dec_padded18::CLLong->PtrWord8->IO()c_long_long_int_dec_padded18=Pure.encodeUnsignedDecPadded18#endif

[8]ページ先頭

©2009-2025 Movatter.jp