+
+-- isCString is useful when passing PackedStrings to the
+-- outside world, and need to figure out whether you can
+-- pass it as an Addr or ByteArray.
+--
+isCString :: PackedString -> Bool
+isCString (CPS _ _ ) = True
+isCString _ = False
+
+psToCString :: PackedString -> Addr
+psToCString (CPS addr _) = (A# addr)
+psToCString (PS bytes n# has_null) =
+ unsafePerformIO $ do
+ stuff <- _ccall_ malloc ((I# n#) * (``sizeof(char)''))
+ let
+ fill_in n# i#
+ | n# ==# 0# = return ()
+ | otherwise = do
+ let ch# = indexCharArray# bytes i#
+ writeCharOffAddr stuff (I# i#) (C# ch#)
+ fill_in (n# -# 1#) (i# +# 1#)
+ fill_in n# 0#
+ return stuff
+