Skip to content
heff

bytes

Bytes: bytes, not changed once made — a body, what a socket reads, a file’s content where it is no text — a type of its own beside String, which is text (though any bytes may be in one). A byte is a UInt8. A C program keeps them as a buffer of bytes, a JavaScript one as a Uint8Array. Import it by a name, import bytes as Bytes, as its names are the prelude’s lists’ too: Bytes.length, Bytes.fromString. A binary writes them, b["hi", 255], and takes them apart; their equality, instanceEqualityBytes, is the prelude’s, as == is, and so are their Debug, instanceDebugBytes, which writes them as a binary does, and their Show, instanceShowBytes, which reads them as UTF-8 text.

bytesAt

bytesAt : Bytes -> Int -> UInt8

bytesAppend

bytesAppend : Bytes -> Bytes -> Bytes

bytesFromList

bytesFromList : List UInt8 -> Bytes

bytesFromString

bytesFromString : String -> Bytes

bytesAsString

bytesAsString : Bytes -> String

length

length : Bytes -> Int

How many.

toList

toList : Bytes -> List UInt8

fromList

fromList : List UInt8 -> Bytes
>>> toList (fromList [104, 105, 255])
[104, 105, 255]

empty

empty : Bytes

None.

>>> length empty
0

fromString

fromString : String -> Bytes

A string’s bytes, as they are: UTF-8, as a literal writes it.

>>> toList (fromString "hé")
[104, 195, 169]
>>> length (fromString "hé")
3

asString

asString : Bytes -> String

The bytes as a string, as they are, UTF-8 or not: fromString undone. (show reads them as text, what is not UTF-8 U+FFFD.)

>>> asString (fromList [104, 105, 255]) == "hi\xff"
True

index

index : Bytes -> Int -> Maybe UInt8

The byte at i, if there is one: out of range, Nothing.

>>> List.map (index (fromString "hi")) [1, 2, -1]
[Just 105, Nothing, Nothing]

slice

slice : Bytes -> Int -> Int -> Bytes

The bytes from i up to j, j not among them, each where it is clamped to them, and a negative one counted from the end, as a string’s slice is (strSlice, JavaScript’s slice).

>>> List.map show [slice (fromString "hello") 1 3, slice (fromString "hello") 3 9, slice (fromString "hello") -2 5]
["el", "lo", "lo"]

append

append : Bytes -> Bytes -> Bytes

The bytes of a, then those of b.

>>> show (append (fromString "he") (fromString "ff"))
"heff"

concat

concat : List Bytes -> Bytes

Each one’s bytes, one after another, made at once: each copied once, however many there are.

>>> toList (concat [fromList [1], empty, fromList [2, 3]])
[1, 2, 3]

foldl

foldl : {a : Type} -> (a -> UInt8 -> a) -> a -> Bytes -> a

Each byte, from the first, into z, as List.foldl goes.

>>> foldl (\n b -> n * 256 + UInt8.toInt b) 0 (fromList [1, 2, 3])
66051

map

map : (UInt8 -> UInt8) -> Bytes -> Bytes

Each byte f of it: as many bytes.

>>> toList (map (\x -> x + 1) (fromList [1, 2, 255]))
[2, 3, 0]

eq

eq : Bytes -> Bytes -> Bool

Whether they are the same bytes, as == says (the prelude’s instanceEqualityBytes).

>>> eq (fromString "hi") (fromList [104, 105])
True