newCAStringLen str  = do
  a <- newArray (charsToCChars str)
  return (pairLength str a)
#else
newCAStringLen str = do
  ptr <- mallocArray0 len
  let
	go [] n     = n `seq` return ()	-- make it strict in n
    	go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
  go str 0
  return (ptr, len)
  where
    len = length str
#endif

-- | Marshal a Haskell string into a NUL terminated C string using temporary
-- storage.
--
-- * the Haskell string may /not/ contain any NUL characters
--
-- * the memory is freed when the subcomputation terminates (either
--   normally or via an exception), so the pointer to the temporary
--   storage must /not/ be used after this.
--
newCAStringLen str  = do
  a <- newArray (charsToCChars str)
  return (pairLength str a)
#else
newCAStringLen str = do
  ptr <- mallocArray0 len
  let
	go [] n     = n `seq` return ()	-- make it strict in n
    	go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
  go str 0
  return (ptr, len)
  where
    len = length str
#endif

-- | Marshal a Haskell string into a NUL terminated C string using temporary
-- storage.
--
-- * the Haskell string may /not/ contain any NUL characters
--
-- * the memory is freed when the subcomputation terminates (either
--   normally or via an exception), so the pointer to the temporary
--   storage must /not/ be used after this.
--
newCAStringLen str  = do
  a <- newArray (charsToCChars str)
  return (pairLength str a)
#else
newCAStringLen str = do
  ptr <- mallocArray0 len
  let
	go [] n     = n `seq` return ()	-- make it strict in n
    	go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
  go str 0
  return (ptr, len)
  where
    len = length str
#endif

-- | Marshal a Haskell string into a NUL terminated C string using temporary
-- storage.
--
-- * the Haskell string may /not/ contain any NUL characters
--
-- * the memory is freed when the subcomputation terminates (either
--   normally or via an exception), so the pointer to the temporary
--   storage must /not/ be used after this.
--