Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data.Text.Foreign: added peekCString #599

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 11 additions & 1 deletion tests/Tests/Properties/LowLevel.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import Test.QuickCheck hiding ((.&.))
import Tests.QuickCheckUtils
import Tests.Utils
import qualified Data.Text as T
import qualified Data.Text.Foreign as T
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.IO as TL
Expand Down Expand Up @@ -72,6 +73,14 @@ t_use_from0 t = ioProperty $ do
let t' = t `T.snoc` '\0'
(== T.takeWhile (/= '\0') t') <$> useAsPtr t' (const . fromPtr0)

t_peek_cstring t = ioProperty $ do
roundTrip <- T.withCString t T.peekCString
assertEqual "cstring" t roundTrip

t_peek_cstring_len t = ioProperty $ do
roundTrip <- T.withCStringLen t T.peekCStringLen
assertEqual "cstring_len" t roundTrip

t_copy t = T.copy t === t

t_literal_length1 = assertEqual xs (length xs) byteLen
Expand Down Expand Up @@ -125,6 +134,8 @@ testLowLevel =
testProperty "t_use_from" t_use_from,
testProperty "t_use_from0" t_use_from0,
testProperty "t_copy" t_copy,
testProperty "t_peek_cstring" t_peek_cstring,
testProperty "t_peek_cstring_len" t_peek_cstring_len,
testCase "t_literal_length1" t_literal_length1,
testCase "t_literal_length2" t_literal_length2,
testCase "t_literal_surrogates" t_literal_surrogates
Expand All @@ -151,4 +162,3 @@ testLowLevel =
-- testProperty "tl_put_get" tl_put_get
]
]

Loading