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 -> UInt8bytesAppend
bytesAppend : Bytes -> Bytes -> BytesbytesFromList
bytesFromList : List UInt8 -> BytesbytesFromString
bytesFromString : String -> BytesbytesAsString
bytesAsString : Bytes -> Stringlength
length : Bytes -> IntHow many.
toList
toList : Bytes -> List UInt8fromList
fromList : List UInt8 -> Bytes>>> toList (fromList [104, 105, 255])[104, 105, 255]empty
empty : BytesNone.
>>> length empty0fromString
fromString : String -> BytesA string’s bytes, as they are: UTF-8, as a literal writes it.
>>> toList (fromString "hé")[104, 195, 169]>>> length (fromString "hé")3asString
asString : Bytes -> StringThe 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"Trueindex
index : Bytes -> Int -> Maybe UInt8The 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 -> BytesThe 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 -> BytesThe bytes of a, then those of b.
>>> show (append (fromString "he") (fromString "ff"))"heff"concat
concat : List Bytes -> BytesEach 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 -> aEach byte, from the first, into z, as List.foldl goes.
>>> foldl (\n b -> n * 256 + UInt8.toInt b) 0 (fromList [1, 2, 3])66051map
map : (UInt8 -> UInt8) -> Bytes -> BytesEach byte f of it: as many bytes.
>>> toList (map (\x -> x + 1) (fromList [1, 2, 255]))[2, 3, 0]eq
eq : Bytes -> Bytes -> BoolWhether they are the same bytes, as == says (the prelude’s
instanceEqualityBytes).
>>> eq (fromString "hi") (fromList [104, 105])True