Skip to content

Commit

Permalink
Add fromPtr0
Browse files Browse the repository at this point in the history
  • Loading branch information
sjshuck committed Apr 15, 2022
1 parent 0941fed commit c5f8182
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/Data/Text/Foreign.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE BangPatterns, CPP, GeneralizedNewtypeDeriving #-}
{-# LANGUAGE CPP, GeneralizedNewtypeDeriving, MagicHash #-}
-- |
-- Module : Data.Text.Foreign
-- Copyright : (c) 2009, 2010 Bryan O'Sullivan
Expand All @@ -17,6 +17,7 @@ module Data.Text.Foreign
I8
-- * Safe conversion functions
, fromPtr
, fromPtr0
, useAsPtr
, asForeignPtr
-- ** Encoding as UTF-8
Expand All @@ -36,12 +37,14 @@ import Data.ByteString.Unsafe (unsafePackCStringLen, unsafeUseAsCStringLen)
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import Data.Text.Internal (Text(..), empty)
import Data.Text.Internal.Unsafe (unsafeWithForeignPtr)
import Data.Text.Show (addrLen)
import Data.Text.Unsafe (lengthWord8)
import Data.Word (Word8)
import Foreign.C.String (CStringLen)
import Foreign.ForeignPtr (ForeignPtr, mallocForeignPtrArray)
import Foreign.Marshal.Alloc (allocaBytes)
import Foreign.Ptr (Ptr, castPtr)
import GHC.Exts (Ptr(..))
import qualified Data.Text.Array as A

-- $interop
Expand Down Expand Up @@ -72,6 +75,12 @@ fromPtr ptr (I8 len) = unsafeSTToIO $ do
arr <- A.unsafeFreeze dst
return $! Text arr 0 len

-- | /O(n)/ Create a new 'Text' from a 'Ptr' 'Word8' by copying the
-- contents of the NUL-terminated array.
fromPtr0 :: Ptr Word8 -- ^ source array
-> IO Text
fromPtr0 ptr@(Ptr addr#) = fromPtr ptr (fromIntegral (addrLen addr#))

-- $lowlevel
--
-- Foreign functions that use UTF-8 internally may return indices in
Expand Down
3 changes: 2 additions & 1 deletion src/Data/Text/Show.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

module Data.Text.Show
(
singleton
addrLen
, singleton
, unpack
, unpackCString#
, unpackCStringAscii#
Expand Down
4 changes: 4 additions & 0 deletions tests/Tests/Properties/LowLevel.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ t_dropWord8 m t = dropWord8 m t `T.isSuffixOf` t
t_takeWord8 m t = takeWord8 m t `T.isPrefixOf` t
t_take_drop_8 (Small n) t = T.append (takeWord8 n t) (dropWord8 n t) === t
t_use_from t = ioProperty $ (==t) <$> useAsPtr t fromPtr
t_use_from0 t = ioProperty $ do
let t' = t `T.snoc` '\0'
(== T.takeWhile (/= '\0') t') <$> useAsPtr t' (const . fromPtr0)

t_copy t = T.copy t === t

Expand Down Expand Up @@ -118,6 +121,7 @@ testLowLevel =
testProperty "t_takeWord8" t_takeWord8,
testProperty "t_take_drop_8" t_take_drop_8,
testProperty "t_use_from" t_use_from,
testProperty "t_use_from0" t_use_from0,
testProperty "t_copy" t_copy,
testCase "t_literal_length1" t_literal_length1,
testCase "t_literal_length2" t_literal_length2,
Expand Down

0 comments on commit c5f8182

Please sign in to comment.