HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Modulebyteslice-0.2.15.0Haskell2010

Data.Bytes

If you are interested in sub-arrays of ByteArrays (e.g. writing a binary search), it would be grossly inefficient to make a copy of the sub-array. On the other hand, it'd be really annoying to track limit indices by hand.

This module defines the Bytes type which exposes a standard array interface for a sub-arrays without copying and without manual index manipulation. -- For mutable arrays, see Data.Bytes.Mutable.

Contents
  1. Types
  2. Constants
  3. Properties
  4. Decompose
  5. Predicates
  6. Create
  7. Sliced
  8. Unsliced
  9. Filtering
  10. Traversals
  11. Folds
  12. Folds with Indices
  13. Monadic Folds
  14. Common Folds
  15. Splitting
  16. Unlimited
  17. Fixed from Beginning
  18. Fixed from End
  19. Combining
  20. Searching
  21. Counting
  22. Prefix and Suffix
  23. Byte Sequence
  24. C Strings
  25. Single Byte
  26. Equality
  27. Fixed Characters
  28. C Strings
  29. Hashing
  30. Unsafe Slicing
  31. Copying
  32. Pointers
  33. Conversion
  34. I/O with Handles
  35. Unlifted Types
  36. Length Indexed
Synopsis

Types

datadata Bytes

A slice of a ByteArray.

Instances7IsList, Eq, Ord, Show, Semigroup, Monoid, …
  • IsList BytesDefined in byteslice-0.2.15.0 · Data.Bytes.Internal
  • Eq BytesDefined in byteslice-0.2.15.0 · Data.Bytes.Internal
  • Ord BytesDefined in byteslice-0.2.15.0 · Data.Bytes.Internal
  • Show BytesDefined in byteslice-0.2.15.0 · Data.Bytes.Internal
  • Semigroup BytesDefined in byteslice-0.2.15.0 · Data.Bytes.Internal
  • Monoid BytesDefined in byteslice-0.2.15.0 · Data.Bytes.Internal
  • type Item Bytes = Word8Defined in byteslice-0.2.15.0 · Data.Bytes.Internal

Constants

Properties

Decompose

Predicates

Create

Sliced

Unsliced

Filtering

Traversals

Folds

valuefoldl :: (a -> Word8 -> a) -> a -> Bytes -> a

Left fold over bytes, non-strict in the accumulator.

valuefoldr :: (Word8 -> a -> a) -> a -> Bytes -> a

Right fold over bytes, non-strict in the accumulator.

Folds with Indices

valueifoldl' :: (a -> Int -> Word8 -> a) -> a -> Bytes -> a

Left fold over bytes, strict in the accumulator. The reduction function is applied to each element along with its index.

Monadic Folds

Common Folds

Splitting

Unlimited

valuesplit :: Word8 -> Bytes -> [Bytes]

Break a byte sequence into pieces separated by the byte argument, consuming the delimiter. This function is a good producer for list fusion. It is common to immidiately consume the results of split with foldl', traverse_, foldlM, and being a good producer helps in this situation.

Note: this function differs from its counterpart in bytestring. If the byte sequence is empty, this returns a singleton list with the empty byte sequence.

Variant of split that returns an array of unsliced byte sequences. Unlike split, this is not a good producer for list fusion. (It does not return a list, so it could not be.) Prefer split if the result is going to be consumed exactly once by a good consumer. Prefer splitU if the result of the split is going to be around for a while and inspected multiple times.

valuesplitInit :: Word8 -> Bytes -> [Bytes]

Variant of split that drops the trailing element. This behaves correctly even if the byte sequence is empty. This is a good producer for list fusion. This is useful when splitting a text file into lines. POSIX mandates that text files end with a newline, so the list resulting from split always has an empty byte sequence as its last element. With splitInit, that unwanted element is discarded.

Fixed from Beginning

valuesplit1 :: Word8 -> Bytes -> Maybe (Bytes, Bytes)

Split a byte sequence on the first occurrence of the target byte. The target is removed from the result. For example:

Example1 expression
split1 0xA [0x1,0x2,0xA,0xB]Just ([0x1,0x2],[0xB])
valuesplit2 :: Word8 -> Bytes -> Maybe (Bytes, Bytes, Bytes)

Split a byte sequence on the first and second occurrences of the target byte. The target is removed from the result. For example:

Example1 expression
split2 0xA [0x1,0x2,0xA,0xB,0xA,0xA,0xA]Just ([0x1,0x2],[0xB],[0xA,0xA])

Split a byte sequence on the first, second, and third occurrences of the target byte. The target is removed from the result. For example:

Example1 expression
split3 0xA [0x1,0x2,0xA,0xB,0xA,0xA,0xA]Just ([0x1,0x2],[0xB],[],[0xA])

Split a byte sequence on the first, second, third, and fourth occurrences of the target byte. The target is removed from the result. For example:

Example1 expression
split4 0xA [0x1,0x2,0xA,0xB,0xA,0xA,0xA]Just ([0x1,0x2],[0xB],[],[],[])

Fixed from End

Split a byte sequence on the last occurrence of the target byte. The target is removed from the result. For example:

Example1 expression
split1 0xA [0x1,0x2,0xA,0xB,0xA,0xC]Just ([0x1,0x2,0xA,0xB],[0xC])

Combining

valueintercalate
  1. :: Bytes

    Separator (interspersed into the list)

  2. -> [Bytes]

    List

  3. -> Bytes

O(n) The intercalate function takes a separator Bytes and a list of Bytes and concatenates the list elements by interspersing the separator between each element.

Searching

valuereplace
  1. :: Bytes

    needle, must not be empty

  2. -> Bytes

    replacement

  3. -> Bytes

    haystack

  4. -> Bytes

Replace every non-overlapping occurrence of needle in haystack with replacement.

Counting

Prefix and Suffix

Byte Sequence

valueisInfixOf
  1. :: Bytes

    String to search for

  2. -> Bytes

    String to search in

  3. -> Bool

Is the first argument an infix of the second argument?

Uses the Rabin-Karp algorithm: expected time O(n+m), worst-case O(nm).

C Strings

Single Byte

Equality

Fixed Characters

valueequalsLatin1 :: Char -> Bytes -> Bool

Deprecated. use Data.Bytes.Text.Latin1.equals1 instead

Is the byte sequence, when interpreted as ISO-8859-1-encoded text, a singleton whose element matches the character?

valueequalsLatin2 :: Char -> Char -> Bytes -> Bool

Deprecated. use Data.Bytes.Text.Latin1.equals2 instead

Is the byte sequence, when interpreted as ISO-8859-1-encoded text, a doubleton whose elements match the characters?

valueequalsLatin3 :: Char -> Char -> Char -> Bytes -> Bool

Deprecated. use Data.Bytes.Text.Latin1.equals3 instead

Is the byte sequence, when interpreted as ISO-8859-1-encoded text, a tripleton whose elements match the characters?

valueequalsLatin4 :: Char -> Char -> Char -> Char -> Bytes -> Bool

Deprecated. use Data.Bytes.Text.Latin1.equals4 instead

Is the byte sequence, when interpreted as ISO-8859-1-encoded text, a quadrupleton whose elements match the characters?

valueequalsLatin5 :: Char -> Char -> Char -> Char -> Char -> Bytes -> Bool

Deprecated. use Data.Bytes.Text.Latin1.equals5 instead

Is the byte sequence, when interpreted as ISO-8859-1-encoded text, a quintupleton whose elements match the characters?

C Strings

Hashing

Unsafe Slicing

Copying

Pointers

valuepin :: Bytes -> Bytes

Yields a pinned byte sequence whose contents are identical to those of the original byte sequence. If the ByteArray backing the argument was already pinned, this simply aliases the argument and does not perform any copying.

valuecontents :: Bytes -> Ptr Word8

Yields a pointer to the beginning of the byte sequence. It is only safe to call this on a Bytes backed by a pinned ByteArray.

valuetouch :: PrimMonad m => Bytes -> m ()

Touch the byte array backing the byte sequence. This sometimes needed after calling contents so that the ByteArray does not get garbage collected.

Conversion

Deprecated. use Data.Bytes.Text.Ascii.fromString instead

Convert a String consisting of only characters in the ASCII block to a byte sequence. Any character with a codepoint above U+007F is replaced by U+0000.

Deprecated. use Data.Bytes.Text.Latin1.fromString instead

Convert a String consisting of only characters representable by ISO-8859-1. These are encoded with ISO-8859-1. Any character with a codepoint above U+00FF is replaced by an unspecified byte.

Deprecated. use Data.Bytes.Text.Latin1.toString instead

Interpret a byte sequence as text encoded by ISO-8859-1.

Deprecated. use Data.BytesTextAsciiExt.toLowerU

O(n) Interpreting the bytes an ASCII-encoded characters, convert the string to lowercase. This adds 0x20 to bytes in the range [0x41,0x5A] and leaves all other bytes alone. Unconditionally copies the bytes.

I/O with Handles

Unlifted Types

Length Indexed