Skip to content

Commit

Permalink
Data.Text.Foreign: added peekCString
Browse files Browse the repository at this point in the history
Fixes: #239
  • Loading branch information
TristanCacqueray committed May 28, 2024
1 parent 86753c2 commit 1139357
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Data/Text/Foreign.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module Data.Text.Foreign
, useAsPtr
, asForeignPtr
-- ** Encoding as UTF-8
, peekCString
, withCString
, peekCStringLen
, withCStringLen
Expand All @@ -34,7 +35,7 @@ module Data.Text.Foreign
) where

import Control.Monad.ST.Unsafe (unsafeSTToIO)
import Data.ByteString.Unsafe (unsafePackCStringLen, unsafeUseAsCStringLen)
import Data.ByteString.Unsafe (unsafePackCStringLen, unsafePackCString, unsafeUseAsCStringLen)
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import Data.Text.Internal (Text(..), empty)
import Data.Text.Internal.Unsafe (unsafeWithForeignPtr)
Expand Down Expand Up @@ -179,6 +180,16 @@ peekCStringLen cs = do
bs <- unsafePackCStringLen cs
return $! decodeUtf8 bs

-- | /O(n)/ Decode a null-terminated C string, which is assumed
-- to have been encoded as UTF-8. If decoding fails, a
-- 'UnicodeException' is thrown.
--
-- @since 2.1.2
peekCString :: CString -> IO Text
peekCString cs = do
bs <- unsafePackCString cs
return $! decodeUtf8 bs

-- | Marshal a 'Text' into a C string encoded as UTF-8 in temporary
-- storage, with explicit length information. The encoded string may
-- contain NUL bytes, and is not followed by a trailing NUL byte.
Expand Down

0 comments on commit 1139357

Please sign in to comment.