mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-03 08:12:51 -06:00
use interleaved IO for peeking strings when possible
This commit is contained in:
@@ -15,6 +15,7 @@ import Control.Exception
|
||||
import GHC.Ptr
|
||||
import Data.Int
|
||||
import Data.Word
|
||||
import System.IO.Unsafe
|
||||
|
||||
type Touch = IO ()
|
||||
|
||||
@@ -198,6 +199,23 @@ peekUtf8CStringBuf sbuf = do
|
||||
len <- gu_string_buf_length sbuf
|
||||
peekUtf8CStringLen ptr (fromIntegral len)
|
||||
|
||||
peekUtf8CStringBufResult :: Ptr GuStringBuf -> Ptr GuPool -> IO String
|
||||
peekUtf8CStringBufResult sbuf pool = do
|
||||
fptr <- newForeignPtr gu_pool_finalizer pool
|
||||
ptr <- gu_string_buf_data sbuf
|
||||
len <- gu_string_buf_length sbuf
|
||||
pptr <- gu_malloc pool (#size GuString*)
|
||||
poke pptr ptr >> decode fptr pptr (ptr `plusPtr` fromIntegral len)
|
||||
where
|
||||
decode fptr pptr end = do
|
||||
ptr <- peek pptr
|
||||
if ptr >= end
|
||||
then return []
|
||||
else do x <- gu_utf8_decode pptr
|
||||
cs <- unsafeInterleaveIO (decode fptr pptr end)
|
||||
touchForeignPtr fptr
|
||||
return (((toEnum . fromEnum) x) : cs)
|
||||
|
||||
pokeUtf8CString :: String -> CString -> IO ()
|
||||
pokeUtf8CString s ptr =
|
||||
alloca $ \pptr ->
|
||||
|
||||
Reference in New Issue
Block a user