{-# LANGUAGE CPP #-}
module DearImGui.Internal.Text
( withCString
, withCStringOrNull
, withCStringLen
, withCStringEnd
, peekCString
, Text
, pack
, unpack
) where
import Foreign (nullPtr, plusPtr)
import Foreign.C.String (CString)
import qualified GHC.Foreign as Foreign
import System.IO (utf8)
import Data.Text (Text, pack, unpack)
import Data.Text.Foreign (withCStringLen)
import UnliftIO (MonadUnliftIO, UnliftIO(..), withUnliftIO)
#if MIN_VERSION_text(2,0,1)
import qualified Data.Text.Foreign as Text
withCString :: MonadUnliftIO m => Text -> (CString -> m a) -> m a
withCString :: forall (m :: * -> *) a.
MonadUnliftIO m =>
Text -> (CString -> m a) -> m a
withCString Text
text CString -> m a
action =
(UnliftIO m -> IO a) -> m a
forall (m :: * -> *) a.
MonadUnliftIO m =>
(UnliftIO m -> IO a) -> m a
withUnliftIO ((UnliftIO m -> IO a) -> m a) -> (UnliftIO m -> IO a) -> m a
forall a b. (a -> b) -> a -> b
$ \(UnliftIO forall a. m a -> IO a
unlift) ->
Text -> (CString -> IO a) -> IO a
forall a. Text -> (CString -> IO a) -> IO a
Text.withCString Text
text ((CString -> IO a) -> IO a) -> (CString -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \CString
buf ->
m a -> IO a
forall a. m a -> IO a
unlift (m a -> IO a) -> m a -> IO a
forall a b. (a -> b) -> a -> b
$ CString -> m a
action CString
buf
#elif MIN_VERSION_text(2,0,0)
import Data.Text.Foreign (lengthWord8, unsafeCopyToPtr)
import Data.Word (Word8)
import Foreign (allocaBytes, castPtr, pokeByteOff)
withCString :: MonadUnliftIO m => Text -> (CString -> m a) -> m a
withCString t@(Text _arr _off len) action =
withUnliftIO $ \(UnliftIO unlift) ->
allocaBytes (len + 1) $ \buf -> do
unsafeCopyToPtr t buf
pokeByteOff buf len (0 :: Word8)
unlift $ action (castPtr buf)
#else
withCString :: MonadUnliftIO m => Text -> (CString -> m a) -> m a
withCString t action = do
withUnliftIO $ \(UnliftIO unlift) ->
Foreign.withCString utf8 (unpack t) $ \textPtr ->
unlift $ action textPtr
#endif
peekCString :: CString -> IO Text
peekCString :: CString -> IO Text
peekCString = (String -> Text) -> IO String -> IO Text
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Text
pack (IO String -> IO Text)
-> (CString -> IO String) -> CString -> IO Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TextEncoding -> CString -> IO String
Foreign.peekCString TextEncoding
utf8
withCStringOrNull :: Maybe Text -> (CString -> IO a) -> IO a
withCStringOrNull :: forall a. Maybe Text -> (CString -> IO a) -> IO a
withCStringOrNull Maybe Text
Nothing CString -> IO a
k = CString -> IO a
k CString
forall a. Ptr a
nullPtr
withCStringOrNull (Just Text
s) CString -> IO a
k = Text -> (CString -> IO a) -> IO a
forall (m :: * -> *) a.
MonadUnliftIO m =>
Text -> (CString -> m a) -> m a
withCString Text
s CString -> IO a
k
withCStringEnd :: MonadUnliftIO m => Text -> (CString -> CString -> m a) -> m a
withCStringEnd :: forall (m :: * -> *) a.
MonadUnliftIO m =>
Text -> (CString -> CString -> m a) -> m a
withCStringEnd Text
t CString -> CString -> m a
action =
(UnliftIO m -> IO a) -> m a
forall (m :: * -> *) a.
MonadUnliftIO m =>
(UnliftIO m -> IO a) -> m a
withUnliftIO ((UnliftIO m -> IO a) -> m a) -> (UnliftIO m -> IO a) -> m a
forall a b. (a -> b) -> a -> b
$ \(UnliftIO forall a. m a -> IO a
unlift) ->
Text -> (CStringLen -> IO a) -> IO a
forall a. Text -> (CStringLen -> IO a) -> IO a
withCStringLen Text
t ((CStringLen -> IO a) -> IO a) -> (CStringLen -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \(CString
textPtr, Int
size) ->
m a -> IO a
forall a. m a -> IO a
unlift (m a -> IO a) -> m a -> IO a
forall a b. (a -> b) -> a -> b
$ CString -> CString -> m a
action CString
textPtr (CString
textPtr CString -> Int -> CString
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
size)