Skip to content

Commit

Permalink
Simplify useAsCString
Browse files Browse the repository at this point in the history
  • Loading branch information
Lysxia committed May 18, 2022
1 parent 0f49510 commit 1472bc2
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions Data/ByteString.hs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ import Control.Monad (when, void)

import Foreign.C.String (CString, CStringLen)
import Foreign.C.Types (CSize (CSize), CInt (CInt))
import Foreign.ForeignPtr (ForeignPtr, withForeignPtr, touchForeignPtr)
import Foreign.ForeignPtr (ForeignPtr, touchForeignPtr)
import Foreign.ForeignPtr.Unsafe(unsafeForeignPtrToPtr)
import Foreign.Marshal.Alloc (allocaBytes)
import Foreign.Marshal.Array (allocaArray)
Expand Down Expand Up @@ -1734,12 +1734,10 @@ sort (BS input l)
-- subcomputation finishes.
useAsCString :: ByteString -> (CString -> IO a) -> IO a
useAsCString (BS fp l) action =
allocaBytes (l+1) $ \buf ->
-- Cannot use unsafeWithForeignPtr, because action can diverge
withForeignPtr fp $ \p -> do
memcpy buf p l
pokeByteOff buf l (0::Word8)
action (castPtr buf)
allocaBytes (l+1) $ \buf -> do
unsafeWithForeignPtr fp $ \p -> memcpy buf p l
pokeByteOff buf l (0::Word8)
action (castPtr buf)

-- | /O(n) construction/ Use a @ByteString@ with a function requiring a @CStringLen@.
-- As for @useAsCString@ this function makes a copy of the original @ByteString@.
Expand Down

0 comments on commit 1472bc2

Please sign in to comment.