Skip to content

Commit 6034af8

Browse files
Bodigrimclyring
andauthored
Document that useAsCStringLen does not append NUL (#612)
* Document that useAsCStringLen does not append NUL * Update Data/ByteString.hs Co-authored-by: Matthew Craven <[email protected]> --------- Co-authored-by: Matthew Craven <[email protected]>
1 parent 16d6b7e commit 6034af8

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

Data/ByteString.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,9 +1797,13 @@ useAsCString (BS fp l) action =
17971797
pokeByteOff buf l (0::Word8)
17981798
action (castPtr buf)
17991799

1800-
-- | /O(n) construction/ Use a @ByteString@ with a function requiring a @CStringLen@.
1801-
-- As for @useAsCString@ this function makes a copy of the original @ByteString@.
1800+
-- | /O(n) construction/ Use a @ByteString@ with a function requiring a 'CStringLen'.
1801+
-- As for 'useAsCString' this function makes a copy of the original @ByteString@.
18021802
-- It must not be stored or used after the subcomputation finishes.
1803+
--
1804+
-- Beware that this function is not required to add a terminating @\NUL@ byte at the end of the 'CStringLen' it provides.
1805+
-- If you need to construct a pointer to a null-terminated sequence, use 'useAsCString'
1806+
-- (and measure length independently if desired).
18031807
useAsCStringLen :: ByteString -> (CStringLen -> IO a) -> IO a
18041808
useAsCStringLen p@(BS _ l) f = useAsCString p $ \cstr -> f (cstr,l)
18051809

Data/ByteString/Short/Internal.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,10 +1758,14 @@ useAsCString sbs action =
17581758
action buf
17591759
where l = length sbs
17601760

1761-
-- | /O(n) construction./ Use a @ShortByteString@ with a function requiring a @CStringLen@.
1762-
-- As for @useAsCString@ this function makes a copy of the original @ShortByteString@.
1761+
-- | /O(n) construction./ Use a @ShortByteString@ with a function requiring a 'CStringLen'.
1762+
-- As for 'useAsCString' this function makes a copy of the original @ShortByteString@.
17631763
-- It must not be stored or used after the subcomputation finishes.
17641764
--
1765+
-- Beware that this function does not add a terminating @\NUL@ byte at the end of 'CStringLen'.
1766+
-- If you need to construct a pointer to a null-terminated sequence, use 'useAsCString'
1767+
-- (and measure length independently if desired).
1768+
--
17651769
-- @since 0.10.10.0
17661770
useAsCStringLen :: ShortByteString -> (CStringLen -> IO a) -> IO a
17671771
useAsCStringLen sbs action =

0 commit comments

Comments
 (0)