-- These are not really unsafe, but provided for compatibilitymodule Data.ByteString.Unsafe ( -- * Unchecked access unsafeHead, unsafeTail, unsafeInit, unsafeLast, unsafeIndex, unsafeTake, unsafeDrop, -- * Low level interaction with CStrings -- ** Using ByteStrings with functions for CStrings unsafeUseAsCString, unsafeUseAsCStringLen, -- ** Converting CStrings to ByteStrings{- unsafePackCString, unsafePackCStringLen,-} unsafePackMallocCString, unsafePackMallocCStringLen,{- unsafePackAddress, unsafePackAddressLen, unsafePackCStringFinalizer, unsafeFinalize,-} ) whereimport qualified Prelude ()import Primitivesimport Data.Word.Word8import Data.ByteString.Internalimport Foreign.C.String (CString, CStringLen)import Foreign.ForeignPtr (withForeignPtr)unsafeHead :: ByteString -> Word8unsafeHead bs = primBSindex bs 0unsafeTail :: ByteString -> ByteStringunsafeTail bs = primBSsubstr bs 1 (primBSlength bs `primIntSub` 1)unsafeInit :: ByteString -> ByteStringunsafeInit bs = primBSsubstr bs 0 (primBSlength bs `primIntSub` 1)unsafeLast :: ByteString -> Word8unsafeLast bs = primBSindex bs (primBSlength bs `primIntSub` 1)unsafeIndex :: ByteString -> Int -> Word8unsafeIndex = primBSindexunsafeTake :: Int -> ByteString -> ByteStringunsafeTake n bs = primBSsubstr bs 0 nunsafeDrop :: Int -> ByteString -> ByteStringunsafeDrop n bs = primBSsubstr bs n (primBSlength bs `primIntSub` n)-- Get actual data pointer out of a ByteString. There is no copying involved.-- The RTS retains ownership of the string, and the pointer-- should not be used after the subcomputations finishes.unsafeUseAsCString :: ByteString -> (CString -> IO a) -> IO aunsafeUseAsCString bs act = withForeignPtr (primBS2FPtr bs) actunsafeUseAsCStringLen :: ByteString -> (CStringLen -> IO a) -> IO aunsafeUseAsCStringLen bs act = withForeignPtr (primBS2FPtr bs) (\p -> act (p, primBSlength bs))unsafePackMallocCString :: CString -> IO ByteStringunsafePackMallocCString = primBSgrabunsafePackMallocCStringLen :: CString -> Int -> IO ByteStringunsafePackMallocCStringLen = primBSgrabLen