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

Modulebytestring-0.12.2.0Haskell2010

Data.ByteString.Internal

A module containing semi-public ByteString internals. This exposes the ByteString representation and low level construction functions. As such all the functions in this module are unsafe. The API is also not stable.

Where possible application should instead use the functions from the normal public interface modules, such as Data.ByteString.Unsafe. Packages that extend the ByteString system at a low level will need to use this module.

  • 3 types
  • 56 values

The ByteString type and representation

3 declarations
datadata ByteString
#

A space-efficient representation of a Word8 vector, supporting many efficient operations.

A ByteString contains 8-bit bytes, or by using the operations from Data.ByteString.Char8 it can be interpreted as containing 8-bit characters.

Constructors

Instances12IsList, Eq, Data, Ord, Read, Show, …
  • IsList ByteStringDefined in bytestring-0.12.2.0 · Data.ByteString.Internal.Type
  • Eq ByteStringDefined in bytestring-0.12.2.0 · Data.ByteString.Internal.Type
  • Data ByteStringDefined in bytestring-0.12.2.0 · Data.ByteString.Internal.Type
  • Ord ByteStringDefined in bytestring-0.12.2.0 · Data.ByteString.Internal.Type
  • Read ByteStringDefined in bytestring-0.12.2.0 · Data.ByteString.Internal.Type
  • Show ByteStringDefined in bytestring-0.12.2.0 · Data.ByteString.Internal.Type
  • IsString ByteStringDefined in bytestring-0.12.2.0 · Data.ByteString.Internal.Type

    Beware: fromString truncates multi-byte characters to octets. e.g. "枯朶に烏のとまりけり秋の暮" becomes �6k�nh~�Q��n�

  • Semigroup ByteStringDefined in bytestring-0.12.2.0 · Data.ByteString.Internal.Type
  • Monoid ByteStringDefined in bytestring-0.12.2.0 · Data.ByteString.Internal.Type
  • NFData ByteStringDefined in bytestring-0.12.2.0 · Data.ByteString.Internal.Type
  • Lift ByteStringDefined in bytestring-0.12.2.0 · Data.ByteString.Internal.Type
  • type Item ByteString = Word8Defined in bytestring-0.12.2.0 · Data.ByteString.Internal.Type
patternpattern PS :: ForeignPtr Word8 -> Int -> Int -> ByteString
#

PS foreignPtr offset length represents a ByteString with data backed by a given foreignPtr, starting at a given offset in bytes and of a specified length.

This pattern is used to emulate the legacy ByteString data constructor, so that pre-existing code generally doesn't need to change to benefit from the simplified BS constructor and can continue to function unchanged.

Note: Matching with this constructor will always be given a 0 offset, as the base will be manipulated by plusForeignPtr instead.

Internal indexing

1 declaration

Conversion with lists: packing and unpacking

16 declarations

O(n) Pack a null-terminated sequence of bytes, pointed to by an Addr# (an arbitrary machine address assumed to point outside the garbage-collected heap) into a ByteString. A much faster way to create an Addr# is with an unboxed string literal, than to pack a boxed string. A unboxed string literal is compiled to a static char [] by GHC. Establishing the length of the string requires a call to strlen(3), so the Addr# must point to a null-terminated buffer (as is the case with "string"# literals in GHC). Use unsafePackAddressLen if you know the length of the string statically.

An example:

literalFS = unsafePackAddress "literal"#

This function is unsafe. If you modify the buffer pointed to by the original Addr# this modification will be reflected in the resulting ByteString, breaking referential transparency.

Note this also won't work if your Addr# has embedded '\0' characters in the string, as strlen will return too short a length.

See unsafePackAddress. This function has similar behavior. Prefer this function when the address in known to be an Addr# literal. In that context, there is no need for the sequencing guarantees that IO provides. On GHC 9.0 and up, this function uses the FinalPtr data constructor for ForeignPtrContents.

Low level imperative construction

10 declarations
valuecreateAndTrim :: Int -> (Ptr Word8 -> IO Int) -> IO ByteString
#

Given the maximum size needed and a function to make the contents of a ByteString, createAndTrim makes the ByteString. The generating function is required to return the actual final size (<= the maximum size), and the resulting byte array is reallocated to this size.

createAndTrim is the main mechanism for creating custom, efficient ByteString functions, using Haskell or C functions to fill the space.

Conversion to and from ForeignPtrs

5 declarations

Utilities

6 declarations

Most operations on a ByteString need to read from the buffer given by its ForeignPtr Word8 field. But since most operations on ByteString are (nominally) pure, their implementations cannot see the IO state thread that was used to initialize the contents of that buffer. This means that under some circumstances, these buffer-reads may be executed before the writes used to initialize the buffer are executed, with unpredictable results.

deferForeignPtrAvailability exists to help solve this problem. At runtime, a call deferForeignPtrAvailability x is equivalent to pure $! x, but the former is more opaque to the simplifier, so that reads from the pointer in its result cannot be executed until the deferForeignPtrAvailability x call is complete.

The opaque bits evaporate during CorePrep, so using deferForeignPtrAvailability incurs no direct overhead.

Standard C Functions

6 declarations
valuememcpy :: Ptr Word8 -> Ptr Word8 -> Int -> IO ()
#

Deprecated. Use Foreign.Marshal.Utils.copyBytes instead

deprecated since bytestring-0.11.5.0

cbits functions

6 declarations

Chars

4 declarations
valuec2w :: Char -> Word8
#

Unsafe conversion between Char and Word8. This is a no-op and silently truncates to 8 bits Chars > '255'. It is provided as convenience for ByteString construction.

valueisSpaceWord8 :: Word8 -> Bool
#

Selects words corresponding to white-space characters in the Latin-1 range

Deprecated and unmentionable

1 declaration
valueaccursedUnutterablePerformIO :: IO a -> a
#

This "function" has a superficial similarity to unsafePerformIO but it is in fact a malevolent agent of chaos. It unpicks the seams of reality (and the IO monad) so that the normal rules no longer apply. It lulls you into thinking it is reasonable, but when you are not looking it stabs you in the back and aliases all of your mutable buffers. The carcass of many a seasoned Haskell programmer lie strewn at its feet.

Witness the trail of destruction:

Do not talk about "safe"! You do not know what is safe!

Yield not to its blasphemous call! Flee traveller! Flee or you will be corrupted and devoured!

Exported compatibility shim

2 declarations
valueplusForeignPtr :: ForeignPtr a -> Int -> ForeignPtr b
#

Advances the given address by the given offset in bytes.

The new ForeignPtr shares the finalizer of the original, equivalent from a finalization standpoint to just creating another reference to the original. That is, the finalizer will not be called before the new ForeignPtr is unreachable, nor will it be called an additional time due to this call, and the finalizer will be called with the same address that it would have had this call not happened, *not* the new address.

valueunsafeWithForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
#

This is similar to withForeignPtr but comes with an important caveat: the user must guarantee that the continuation does not diverge (e.g. loop or throw an exception). In exchange for this loss of generality, this function offers the ability of GHC to optimise more aggressively.

Specifically, applications of the form: unsafeWithForeignPtr fptr (forever something)

See GHC issue #17760 for more information about the unsoundness behavior that this function can result in.