CString package:streamly-core

MutByteArray representing null terminated c strings. All APIs in this module are unsafe and caution must be used when using them. Completely experimental. Everything is subject to change without notice.
Convert an array of any element type into a null terminated CString Ptr. The array is copied to pinned memory. Unsafe O(n) Time: (creates a copy of the array) Pre-release
Copy a null terminated immutable Addr# Word8 sequence into an array. Unsafe: The caller is responsible for safe addressing. Note that this is completely safe when reading from Haskell string literals because they are guaranteed to be NULL terminated: Note, you can use lazy unsafePerformIO _only if_ the pointer is immutable.
>>> Array.toList $ unsafePerformIO $ Array.fromCString# "\1\2\3\0"#
[1,2,3]
Copy a C string consisting of 16-bit wide chars and terminated by a 16-bit null char, into a Word16 array. The null character is not copied. Useful for copying UTF16 strings on Windows.
Copy a C string consisting of 16-bit wide chars and terminated by a 16-bit null char, into a Word16 array. The null character is not copied. Useful for copying UTF16 strings on Windows.
putCString dst dstOffset cstr writes the cstring cstr at dstOffset in the dst MutByteArray. The result is terminated by a null byte.
Join a null terminated cstring MutByteByteArray with a null terminated cstring Ptr.
The array is grown only by the required amount of space.
fromCString# addr copies a C string consisting of bytes and terminated by a null byte, into a Word8 array. The null byte is not copied.
>>> MutArray.fromCString# "hello"#
Unsafe: The caller has to ensure that:
  1. the addr is pinned and alive during the call.
  2. the pointer passed is valid up to the point where null byte is found.
fromW16CString# addr copies a C string consisting of 16-bit wide chars and terminated by a 16-bit null char, into a Word16 array. The null character is not copied. Useful for copying UTF16 strings on Windows. Unsafe: The caller has to ensure that:
  1. the addr is pinned and alive during the call.
  2. the pointer passed is valid up to the point where null Word16 is found.
Read bytes from an immutable Addr# until a 0 byte is encountered, the 0 byte is not included in the stream.
>>> :set -XMagicHash

>>> fromCString# addr = Stream.takeWhile (/= 0) $ Stream.fromPtr $ (Ptr addr :: Ptr Word8)
Unsafe: The caller is responsible for safe addressing. Note that this is completely safe when reading from Haskell string literals because they are guaranteed to be NULL terminated:
>>> Stream.toList $ Stream.fromCString# "\1\2\3\0"#
[1,2,3]
Read Word16 from an immutable Addr# until a 0 Word16 is encountered, the 0 Word16 is not included in the stream.
>>> :set -XMagicHash

>>> fromW16CString# addr = Stream.takeWhile (/= 0) $ Stream.fromPtr $ (Ptr addr :: Ptr Word16)
Unsafe: The caller is responsible for safe addressing.
Use the path as a pinned CString. Useful for using a PosixPath in system calls on Posix.
The CString must be pinned.
Use the path as a pinned CString. Useful for using a PosixPath in system calls on Posix.