HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleghc-internal-9.1003.0Haskell2010

GHC.Internal.Foreign.C.String

Utilities for primitive marshalling of C strings.

The marshalling converts each Haskell character, representing a Unicode code point, to one or more bytes in a manner that, by default, is determined by the current locale. As a consequence, no guarantees can be made about the relative length of a Haskell string and its corresponding C string, and therefore all the marshalling routines include memory allocation. The translation between Unicode and the encoding of the current locale may be lossy.

  • 4 types
  • 25 values

C strings

2 declarations
typetype CString = Ptr CChar
#

A C string is a reference to an array of C characters terminated by NUL.

typetype CStringLen = (Ptr CChar, Int)
#

A string with explicit length information in bytes instead of a terminating NUL (allowing NUL characters in the middle of the string).

Using a locale-dependent encoding

These functions are different from their CAString counterparts in that they will use an encoding determined by the current locale, rather than always assuming ASCII.

valuenewCString :: String -> IO CString
#

Marshal a Haskell string into a NUL terminated C string.

  • the Haskell string may not contain any NUL characters

  • new storage is allocated for the C string and must be explicitly freed using free or finalizerFree.

Marshal a Haskell string into a C string (ie, character array) with explicit length information.

  • new storage is allocated for the C string and must be explicitly freed using free or finalizerFree.

valuewithCString :: String -> (CString -> IO a) -> IO a
#

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.

valuewithCStringLen :: String -> (CStringLen -> IO a) -> IO a
#

Marshal a Haskell string into a C string (ie, character array) in temporary storage, with explicit length information.

  • 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.

Using 8-bit characters

These variants of the above functions are for use with C libraries that are ignorant of Unicode. These functions should be used with care, as a loss of information can occur.

valuecastCharToCChar :: Char -> CChar
#

Convert a Haskell character to a C character. This function is only safe on the first 256 characters.

valuecastCCharToChar :: CChar -> Char
#

Convert a C byte, representing a Latin-1 character, to the corresponding Haskell character.

valuecastCharToCUChar :: Char -> CUChar
#

Convert a Haskell character to a C unsigned char. This function is only safe on the first 256 characters.

valuecastCUCharToChar :: CUChar -> Char
#

Convert a C unsigned char, representing a Latin-1 character, to the corresponding Haskell character.

valuecastCharToCSChar :: Char -> CSChar
#

Convert a Haskell character to a C signed char. This function is only safe on the first 256 characters.

valuecastCSCharToChar :: CSChar -> Char
#

Convert a C signed char, representing a Latin-1 character, to the corresponding Haskell character.

valuenewCAString :: String -> IO CString
#

Marshal a Haskell string into a NUL terminated C string.

  • the Haskell string may not contain any NUL characters

  • new storage is allocated for the C string and must be explicitly freed using free or finalizerFree.

valuewithCAString :: String -> (CString -> IO a) -> IO a
#

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.

valuewithCAStringLen :: String -> (CStringLen -> IO a) -> IO a
#

Marshal a Haskell string into a C string (ie, character array) in temporary storage, with explicit length information.

  • 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.

C wide strings

8 declarations

These variants of the above functions are for use with C libraries that encode Unicode using the C wchar_t type in a system-dependent way. The only encodings supported are

  • UTF-32 (the C compiler defines 201706L), or

  • UTF-16 (as used on Windows systems).

typetype CWString = Ptr CWchar
#

A C wide string is a reference to an array of C wide characters terminated by NUL.

typetype CWStringLen = (Ptr CWchar, Int)
#

A wide character string with explicit length information in CWchars instead of a terminating NUL (allowing NUL characters in the middle of the string).

valuenewCWString :: String -> IO CWString
#

Marshal a Haskell string into a NUL terminated C wide string.

  • the Haskell string may not contain any NUL characters

  • new storage is allocated for the C wide string and must be explicitly freed using free or finalizerFree.

Marshal a Haskell string into a C wide string (ie, wide character array) with explicit length information.

  • new storage is allocated for the C wide string and must be explicitly freed using free or finalizerFree.

valuewithCWString :: String -> (CWString -> IO a) -> IO a
#

Marshal a Haskell string into a NUL terminated C wide 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.

valuewithCWStringLen :: String -> (CWStringLen -> IO a) -> IO a
#

Marshal a Haskell string into a C wide string (i.e. wide character array) in temporary storage, with explicit length information.

  • 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.