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

Modulebyteslice-0.2.13.2Haskell2010

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.

  • 1 type
  • 111 values

Types

1 declaration
datadata Bytes
#

A slice of a ByteArray.

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

Constants

3 declarations

Properties

2 declarations

Decompose

2 declarations

Predicates

2 declarations
valueany :: (Word8 -> Bool) -> Bytes -> Bool
#

O(n) Returns true if any byte in the sequence satisfies the predicate.

valueall :: (Word8 -> Bool) -> Bytes -> Bool
#

O(n) Returns true if all bytes in the sequence satisfy the predicate.

Create

0 declarations

Sliced

Unsliced

Filtering

4 declarations

Traversals

2 declarations
valuemap :: (Word8 -> Word8) -> Bytes -> Bytes
#

Map over bytes in a sequence. The result has the same length as the argument.

Folds

4 declarations
valuefoldl :: (a -> Word8 -> a) -> a -> Bytes -> a
#

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

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

Left fold over bytes, strict in the accumulator.

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

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

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

Right fold over bytes, strict in the accumulator.

Folds with Indices

1 declaration
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

2 declarations
valuefoldlM :: Monad m => (a -> Word8 -> m a) -> a -> Bytes -> m a
#

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

valuefoldrM :: Monad m => (Word8 -> a -> m a) -> a -> Bytes -> m a
#

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

Common Folds

1 declaration

Splitting

0 declarations

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])
valuesplit3 :: Word8 -> Bytes -> Maybe (Bytes, Bytes, Bytes, Bytes)
#

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])
valuesplit4 :: Word8 -> Bytes -> Maybe (Bytes, Bytes, Bytes, Bytes, Bytes)
#

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

valuesplitEnd1 :: Word8 -> Bytes -> Maybe (Bytes, Bytes)
#

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

4 declarations
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.

valueintercalateByte2
  1. :: Word8

    Separator

  2. -> Bytes

    First byte sequence

  3. -> Bytes

    Second byte sequence

  4. -> Bytes
#

Specialization of intercalate where the separator is a single byte and there are exactly two byte sequences that are being concatenated.

Searching

3 declarations
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

1 declaration
valuecount :: Word8 -> Bytes -> Int
#

Count the number of times the byte appears in the sequence.

Prefix and Suffix

0 declarations

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

O(n) Return the suffix of the second string if its prefix matches the entire first string. Otherwise, return the second string unchanged.

O(n) Return the prefix of the second string if its suffix matches the entire first string. Otherwise, return the second string unchanged.

C Strings

Single Byte

Equality

0 declarations

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?

valueequalsLatin6
  1. :: Char
  2. -> Char
  3. -> Char
  4. -> Char
  5. -> Char
  6. -> Char
  7. -> Bytes
  8. -> Bool
#

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

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

valueequalsLatin7
  1. :: Char
  2. -> Char
  3. -> Char
  4. -> Char
  5. -> Char
  6. -> Char
  7. -> Char
  8. -> Bytes
  9. -> Bool
#

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

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

C Strings

Hashing

2 declarations

Unsafe Slicing

4 declarations
valueunsafeIndex :: Bytes -> Int -> Word8
#

Index into the byte sequence at the given position. This index must be less than the length.

Copying

1 declaration
valueunsafeCopy
  1. :: PrimMonad m
  2. => MutableByteArray (PrimState m)

    Destination

  3. -> Int

    Destination Offset

  4. -> Bytes

    Source

  5. -> m ()
#

Copy the byte sequence into a mutable buffer. The buffer must have enough space to accomodate the byte sequence, but this this is not checked.

Pointers

3 declarations
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

19 declarations
valuetoByteArray :: Bytes -> ByteArray
#

Convert the sliced Bytes to an unsliced ByteArray. This reuses the array backing the sliced Bytes if the slicing metadata implies that all of the bytes are used. Otherwise, it makes a copy.

Convert the sliced Bytes to an unsliced ByteArray. This reuses the array backing the sliced Bytes if the slicing metadata implies that all of the bytes are used and they are already pinned. Otherwise, it makes a copy.

valuefromAsciiString :: String -> Bytes
#

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.

valuefromLatinString :: String -> Bytes
#

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.

valuetoLatinString :: Bytes -> String
#

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

3 declarations

Unlifted Types

2 declarations

Length Indexed

2 declarations