len package:bytestring

O(1) length returns the length of a ByteString as an Int.
O(c) length returns the length of a ByteString as an Int64
O(1) The length of a ShortByteString.
O(n). Construct a new ByteString from a CStringLen. The resulting ByteString is an immutable copy of the original CStringLen. The ByteString is a normal Haskell value and will be managed on the Haskell heap.
O(n) construction Use a ByteString with a function requiring a CStringLen. As for useAsCString this function makes a copy of the original ByteString. It must not be stored or used after the subcomputation finishes. Beware that this function is not required to add a terminating NUL byte at the end of the CStringLen it provides. If you need to construct a pointer to a null-terminated sequence, use useAsCString (and measure length independently if desired).
findIndexOrLength is a variant of findIndex, that returns the length of the string if no element is found, rather than Nothing.
See unsafePackAddress. This function is similar, but takes an additional length argument rather then computing it with strlen. Therefore embedding '\0' characters is possible.
See unsafePackLiteral. This function is similar, but takes an additional length argument rather then computing it with strlen. Therefore embedding '\0' characters is possible.
O(c) compareLength compares the length of a ByteString to an Int64
O(n). Construct a new ShortByteString from a CStringLen. The resulting ShortByteString is an immutable copy of the original CStringLen. The ShortByteString is a normal Haskell value and will be managed on the Haskell heap.
O(n) construction. Use a ShortByteString with a function requiring a CStringLen. As for useAsCString this function makes a copy of the original ShortByteString. It must not be stored or used after the subcomputation finishes. Beware that this function does not add a terminating NUL byte at the end of CStringLen. If you need to construct a pointer to a null-terminated sequence, use useAsCString (and measure length independently if desired).
O(1) unsafePackAddressLen provides constant-time construction of ByteStrings, which is ideal for string literals. It packs a sequence of bytes into a ByteString, given a raw Addr# to the string, and the length of the string. This function is unsafe in two ways:
  • the length argument is assumed to be correct. If the length argument is incorrect, it is possible to overstep the end of the byte array.
  • if the underlying Addr# is later modified, this change will be reflected in the resulting ByteString, breaking referential transparency.
If in doubt, don't use this function.
O(1) Build a ByteString from a CStringLen. This value will have no finalizer associated with it, and will not be garbage collected by Haskell. This operation has O(1) complexity as we already know the final size, so no strlen(3) is required. This function is unsafe. If the original CStringLen is later modified, this change will be reflected in the resulting ByteString, breaking referential transparency.
O(1) Build a ByteString from a malloced CStringLen. This value will have a free(3) finalizer associated to it. This function is unsafe. If the original CString is later modified, this change will be reflected in the resulting ByteString, breaking referential transparency. This function is also unsafe if you call its finalizer twice, which will result in a double free error, or if you pass it a CString not allocated with malloc.
O(1) construction Use a ByteString with a function requiring a CStringLen. This function does zero copying, and merely unwraps a ByteString to appear as a CStringLen. It is unsafe:
  • After calling this function the CStringLen shares the underlying byte buffer with the original ByteString. Thus modifying the CStringLen, either in C, or using poke, will cause the contents of the ByteString to change, breaking referential transparency. Other ByteStrings created by sharing (such as those produced via take or drop) will also reflect these changes. Modifying the CStringLen will break referential transparency. To avoid this, use useAsCStringLen, which makes a copy of the original ByteString.
If empty is given, it will pass (nullPtr, 0).