packages feed

MicroHs-0.17.4.0 / tests / ByteStringLow.hs

module ByteStringLow whereimport Data.ByteStringimport Data.ByteString.Internalimport Foreignimport Foreign.Cforeign import ccall "strlen" c_strlen :: Ptr CChar -> IO CSizeforeign import ccall "malloc" c_malloc :: CSize -> IO (Ptr CChar)foreign import ccall "strcpy" c_strcpy :: Ptr CChar -> Ptr CChar -> IO ()-- test some low level uglinessmain :: IO ()main = do  let s = "abc" :: ByteString  print s  xbs <- useAsCString s $ \ p -> do    -- p is a pointer to the C string "abc"    n <- c_strlen p    q <- c_malloc (n+1)    c_strcpy q p    grabCString q  print xbs  -- Check that NUL terminates the C string  let s = "a\NULbc" :: ByteString  print s  xbs <- useAsCString s $ \ p -> do    -- p is a pointer to the C string "a"    n <- c_strlen p    q <- c_malloc (n+1)    c_strcpy q p    grabCString q  print xbs