-
Notifications
You must be signed in to change notification settings - Fork 55
Description
currently it's just defined as
peekCAText cp = Text.pack <$> peekCAString cpso, it not only calls the more inneficient peekCAString from base, but then it must convert the String into text! This seems unnecessarily expensive. There is an alternative in something similar to Data.Text.Foreign.peekCStringLen, which goes from CStringLen -> IO Text. There is a drawback to this, in that it only supports CStrings that are valid UTF-8, and throws an exception otherwise. Another drawback is that there's only a CStringLen variant, but that's not hard to get around:
peekCString :: CString -> IO Text
peekCStringLen cs = do
bs <- Data.ByteString.Unsafe.unsafePackCString cs
return $! decodeUtf8 bsThis is almost exactly like peekCStringLen, but calls a different function from Data.ByteString.Unsafe, since there's no length information present.
This seems like a good idea, if you're willing to sacrifice support for non-UTF-8. you could always use something like bytestring-encodings.Data.ByteString.Encoding.isUtf8 (https://hackage.haskell.org/package/bytestring-encodings-0.2.0.2/docs/Data-ByteString-Encodings.html#v:isUtf8) to verify that the ByteString is UTF-8 encoded before proceeding, but then you'd probably have to return a 'Maybe Text', which doesn't seem worth it.