Module

Data.ByteString

Package
purescript-bytestrings
Repository
f-f/purescript-bytestrings

This module defines a type of immutable byte strings, and associated helper functions and type class instances. String conversions live in Data.ByteString.Encoding, mutation in Data.ByteString.ST.

#ByteString Source

data ByteString

An immutable sequence of bytes.

Eq and Ord compare bytewise, ordering lexicographically. show renders the bytes as lowercase hex, e.g. ByteString "6869"

Instances

#Byte Source

newtype Byte

A byte value

Instances

#byte Source

byte :: Int -> Byte

Construct a Byte Inputs outside of the 0-255 are masked.

#fromByte Source

fromByte :: Byte -> Int

Unwrap a Byte to its underlying Int.

#byteToHex Source

byteToHex :: Byte -> String

Render a single byte as exactly two lowercase hex digits.

byteToHex (byte 255) == "ff"
byteToHex (byte 7) == "07"

#empty Source

empty :: ByteString

The empty byte string.

#pack Source

pack :: Array Byte -> ByteString

Create a byte string from an array of byte values.

Running time: O(n)

#bytes Source

bytes :: Array Int -> ByteString

Create a byte string from an array of Ints

Running time: O(n)

#length Source

length :: ByteString -> Int

Get the length of a byte string in bytes.

Running time: O(1)

#index Source

index :: ByteString -> Int -> Maybe Byte

Read the byte at the given index, returning Nothing if the index is out of range.

Running time: O(1)

#(!!) Source

Operator alias for Data.ByteString.index (left-associative / precedence 8)

#unsafeIndex Source

unsafeIndex :: ByteString -> Int -> Byte

Read the byte at the given index, throwing an exception if the index is out of range. It is the caller's responsibility to ensure the index is in bounds; use index for safe access.

#uncons Source

uncons :: ByteString -> Maybe { head :: Byte, tail :: ByteString }

Split off the first byte, returning it together with the remaining bytes, or Nothing if the byte string is empty.

Running time: O(n)

#unsnoc Source

unsnoc :: ByteString -> Maybe { init :: ByteString, last :: Byte }

Split off the last byte, returning the leading bytes together with it, or Nothing if the byte string is empty.

Running time: O(n)

#head Source

head :: ByteString -> Maybe Byte

Get the first byte, or Nothing if the byte string is empty.

Running time: O(1)

#last Source

last :: ByteString -> Maybe Byte

Get the last byte, or Nothing if the byte string is empty.

Running time: O(1)

#foldl Source

foldl :: forall a. (a -> Byte -> a) -> a -> ByteString -> a

Fold over the bytes left-to-right (first byte first).

#foldr Source

foldr :: forall a. (Byte -> a -> a) -> a -> ByteString -> a

Fold over the bytes right-to-left (last byte first).

#foldMap Source

foldMap :: forall m. Monoid m => (Byte -> m) -> ByteString -> m

Map each byte to a monoid and combine the results in byte order (left-to-right, matching foldl).

#slice Source

slice :: Int -> Int -> ByteString -> ByteString

Extract the sub-range [start, end) as a new byte string. Indices are clamped into range, so this never fails.

slice 1 3 (bytes [ 1, 2, 3 ]) == bytes [ 2, 3 ]

Running time: O(n)

#take Source

take :: Int -> ByteString -> ByteString

Take the first n bytes as a new byte string. Does not fail.

Running time: O(n)

#drop Source

drop :: Int -> ByteString -> ByteString

Drop the first n bytes, returning the rest as a new byte string. Does not fail.

Running time: O(n)

#unpack Source

unpack :: ByteString -> Array Byte

Convert to an array of byte values.

Running time: O(n)