HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduletext-2.1.3Haskell2010

Data.Text.Foreign

Support for using Text data with native code via the Haskell foreign function interface.

  • 1 type
  • 12 values
  • Packagetext-2.1.3
  • Exports13
  • LanguageHaskell2010
  • LicenceBSD-2-Clause
  • SourceForeign.hs

Interoperability with native code

1 declaration

The Text type is implemented using arrays that are not guaranteed to have a fixed address in the Haskell heap. All communication with native code must thus occur by copying data back and forth.

The Text type's internal representation is UTF-8. To interoperate with native libraries that use different internal representations, such as UTF-16 or UTF-32, consider using the functions in the Data.Text.Encoding module.

newtypenewtype I8
#

A type representing a number of UTF-8 code units.

Instances9Bounded, Enum, Eq, Integral, Num, Ord, …
  • Bounded I8Defined in text-2.1.3 · Data.Text.Foreign
  • Enum I8Defined in text-2.1.3 · Data.Text.Foreign
  • Eq I8Defined in text-2.1.3 · Data.Text.Foreign
  • Integral I8Defined in text-2.1.3 · Data.Text.Foreign
  • Num I8Defined in text-2.1.3 · Data.Text.Foreign
  • Ord I8Defined in text-2.1.3 · Data.Text.Foreign
  • Read I8Defined in text-2.1.3 · Data.Text.Foreign
  • Real I8Defined in text-2.1.3 · Data.Text.Foreign
  • Show I8Defined in text-2.1.3 · Data.Text.Foreign

Safe conversion functions

4 declarations
valueuseAsPtr :: Text -> (Ptr Word8 -> I8 -> IO a) -> IO a
#

O(n) Perform an action on a temporary, mutable copy of a Text. The copy is freed as soon as the action returns.

Encoding as UTF-8

valuepeekCString :: CString -> IO Text
#

O(n) Decode a null-terminated C string, which is assumed to have been encoded as UTF-8. If decoding fails, a UnicodeException is thrown.

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

Marshal a Text into a C string with a trailing NUL byte, encoded as UTF-8 in temporary storage.

The Text itself must not contain any NUL bytes, this precondition is not checked. Cf. withCStringLen.

The temporary storage 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 function returns.

O(n) Decode a C string with explicit length, which is assumed to have been encoded as UTF-8. If decoding fails, a UnicodeException is thrown.

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

Marshal a Text into a C string encoded as UTF-8 in temporary storage, with explicit length information. The encoded string may contain NUL bytes, and is not followed by a trailing NUL byte.

The temporary storage 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 function returns.

Unsafe conversion code

2 declarations
valuelengthWord8 :: Text -> Int
#

O(1) Return the length of a Text in units of Word8. This is useful for sizing a target array appropriately before using unsafeCopyToPtr.

Low-level manipulation

2 declarations

Foreign functions that use UTF-8 internally may return indices in units of Word8 instead of characters. These functions may safely be used with such indices, as they will adjust offsets if necessary to preserve the validity of a Unicode string.

valuedropWord8 :: I8 -> Text -> Text
#

O(1) Return the suffix of the Text, with n Word8 units dropped from its beginning.

If n would cause the Text to begin inside a code point, the beginning of the suffix will be advanced by several additional Word8 unit to maintain its validity.

valuetakeWord8 :: I8 -> Text -> Text
#

O(1) Return the prefix of the Text of n Word8 units in length.

If n would cause the Text to end inside a code point, the end of the prefix will be advanced by several additional Word8 units to maintain its validity.