[Haskell-cafe] Quickest way to pass Text to C code
yves.pares at gmail.com
Wed Mar 21 21:45:54 CET 2012
Okay, eventually it boils down to this:
import Data.Text.Encoding (encodeUtf32LE)
import Data.ByteString.Unsafe (unsafeUseAsCString)
textAsPtrW32 :: Text -> (Ptr Word32 -> IO a) -> IO a
textAsPtrW32 t = unsafeUseAsCString (encodeUtf32LE $ t `snoc` '\0') . (.
As the function passed copies or at least does not store the pointer, I can
use unsafeUseAsCString, but then I have to manually append the
Le 21 mars 2012 13:09, Antoine Latter <aslatter at gmail.com> a écrit :
> On Wed, Mar 21, 2012 at 3:35 AM, Yves Parès <yves.pares at gmail.com> wrote:
> > Hello,
> > I have to interact with a C++ library that accepts as string types
> > c++ strings aside) pointers of wchar_t (CWString in Haskell) or unsigned
> > 32-bit int (Ptr Word32 for UTF-32 codepoints).
> > I have read what text, bytestring and base provide, but Text can only be
> > directly converted to (Ptr Word16), and if I use encodeUTF32 to get a
> > ByteString, then I only get useAsCString, no direct conversion to
> > or Ptr WordXX is possible.
> A CString is a (Ptr CChar). You can then use castPtr to get whichever
> pointer type you need, if you believe the underlying buffer has the
> representation you want (in this case, UTF-32).
> It still won't be null-terminated, however.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe