ByteArray
is like Array UInt8
, but with an efficient run-time representation as a packed
byte buffer.
Constructor
ByteArray.mk
Fields
data : Array UInt8
Byte arrays are a specialized array type that can only contain elements of type UInt8
.
Due to this restriction, they can use a much more efficient representation, with no pointer indirections.
Like other arrays, byte arrays are represented in compiled code as dynamic arrays, and the Lean runtime specially optimizes array operations.
The operations that modify byte arrays first check the array's reference count, and if there are no other references to the array, it is modified in place.
There is no literal syntax for byte arrays.
List.toByteArray
can be used to construct an array from a list literal.
ByteArray : TypeByteArray : Type
Constructs a new empty byte array with initial capacity 0
.
Use ByteArray.emptyWithCapacity
to create an array with a greater initial capacity.
Constructs a new empty byte array with initial capacity c
.
Appends two byte arrays.
In compiled code, calls to ByteArray.append
are replaced with the much more efficient
ByteArray.fastAppend
.
Copies the slice at [srcOff, srcOff + len)
in src
to [destOff, destOff + len)
in
dest
, growing dest
if necessary. If exact
is false
, the capacity
will be doubled when grown.
Returns the number of bytes in the byte array.
This is the number of bytes actually in the array, as distinct from its capacity, which is the amount of memory presently allocated for the array.
Retrieves the byte at the indicated index. Callers must prove that the index is in bounds.
Use uget
for a more efficient alternative or get!
for a variant that panics if the
index is out of bounds.
Retrieves the byte at the indicated index. Callers must prove that the index is in bounds. The index is represented by a platform-specific fixed-width integer (either 32 or 64 bits).
Because USize
is big enough to address all memory on every platform that Lean supports, there are
in practice no ByteArray
s for which uget
cannot retrieve all elements.
Retrieves the byte at the indicated index. Panics if the index is out of bounds.
Converts a packed array of bytes to a linked list.
Decodes a sequence of characters from their UTF-8 representation. Returns none
if the bytes are
not a sequence of Unicode scalar values.
Adds an element to the end of an array. The resulting array's size is one greater than the input array. If there are no other references to the array, then it is modified in-place.
This takes amortized O(1)
time because ByteArray
is represented by a dynamic array.
Replaces the byte at the given index.
No bounds check is performed, but the function requires a proof that the index is in bounds. This proof can usually be omitted, and will be synthesized automatically.
The array is modified in-place if there are no other references to it.
Replaces the byte at the given index.
No bounds check is performed, but the function requires a proof that the index is in bounds. This proof can usually be omitted, and will be synthesized automatically.
The array is modified in-place if there are no other references to it.
A left fold on ByteArray
that iterates over an array from low to high indices, computing a
running value.
Each element of the array is combined with the value from the prior elements using a function
f
. The initial value init
is the starting value before any elements have been
processed.
ByteArray.foldlM
is a monadic variant of this function.
A monadic left fold on ByteArray
that iterates over an array from low to high indices,
computing a running value.
Each element of the array is combined with the value from the prior elements using a monadic
function f
. The initial value init
is the starting value before any elements have
been processed.
Creates an iterator at the beginning of an array.
ByteArray.Iterator : TypeByteArray.Iterator : Type
Iterator over the bytes (UInt8
) of a ByteArray
.
Typically created by arr.iter
, where arr
is a ByteArray
.
An iterator is valid if the position i
is valid for the array arr
, meaning 0 β€ i β€ arr.size
Most operations on iterators return arbitrary values if the iterator is not valid. The functions in
the ByteArray.Iterator
API should rule out the creation of invalid iterators, with two exceptions:
Iterator.next iter
is invalid if iter
is already at the end of the array (iter.atEnd
is
true
)
Iterator.forward iter n
/Iterator.nextn iter n
is invalid if n
is strictly greater than the
number of remaining bytes.
ByteArray.Iterator.mk
array : ByteArray
The array the iterator is for.
idx : Nat
True if the iterator is past the array's last byte.
True if the iterator is valid; that is, it is not past the array's last byte.
True if the position is not zero.
The byte at the current position. -
Moves the iterator's position forward by one byte, unconditionally.
It is only valid to call this function if the iterator is not at the end of the array, i.e.
Iterator.atEnd
is false
; otherwise, the resulting iterator will be invalid.
Moves the iterator's position forward by one byte. -
Moves the iterator's position several bytes forward.
The resulting iterator is only valid if the number of bytes to skip is less than or equal to the number of bytes left in the iterator.
Moves the iterator's position several bytes forward.
The resulting iterator is only valid if the number of bytes to skip is less than or equal to the number of bytes left in the iterator.
Decreases the iterator's position.
If the position is zero, this function is the identity.
Moves the iterator's position several bytes back.
If asked to go back more bytes than available, stops at the beginning of the array.
The number of bytes remaining in the iterator.
Moves the iterator's position to the end of the array.
Given i : ByteArray.Iterator
, note that i.toEnd.atEnd
is always true
.
Returns a byte slice of a byte array, with the given bounds.
If start
or stop
are not valid bounds for a byte slice, then they are clamped to byte array's size.
Additionally, the starting index is clamped to the ending index.
ByteSlice : TypeByteSlice : Type
A region of some underlying byte array.
A byte slice contains a byte array together with the start and end indices of a region of interest.
Byte slices can be used to avoid copying or allocating space, while being more convenient than
tracking the bounds by hand. The region of interest consists of every index that is both greater
than or equal to start
and strictly less than stop
.
The underlying byte array.
The empty byte slice.
This empty byte slice is backed by an empty byte array.
Folds an operation from right to left over the bytes in a byte slice.
An accumulator of type Ξ²
is constructed by starting with init
and combining each byte of the
byte slice with the current accumulator value in turn, moving from the end to the start.
Examples:
(ByteArray.mk #[1, 2, 3]).toByteSlice.foldr (Β·.toNat + Β·) 0 = 6
(ByteArray.mk #[1, 2, 3]).toByteSlice.popFront.foldr (Β·.toNat + Β·) 0 = 5
Folds a monadic operation from right to left over the bytes in a byte slice.
An accumulator of type Ξ²
is constructed by starting with init
and monadically combining each
byte of the byte slice with the current accumulator value in turn, moving from the end to the
start. The monad in question may permit early termination or repetition.
Examples:
#eval (ByteArray.mk #[1, 2, 3]).toByteSlice.foldrM (init := 0) fun x acc => some x.toNat + acc
some 6
Runs a monadic action on each byte of a byte slice.
The bytes are processed starting at the lowest index and moving up.
Extracts a byte from the byte slice.
The index is relative to the start of the byte slice, rather than the underlying byte array.
Extracts a byte from the byte slice, or returns a default value when the index is out of bounds.
The index is relative to the start and end of the byte slice, rather than the underlying byte array. The default value is 0.
Extracts a byte from the byte slice, or returns a default value vβ
when the index is out of
bounds.
The index is relative to the start and end of the byte slice, rather than the underlying byte array.
Creates a new ByteSlice of a ByteArray
Computes the size of the byte slice.
Creates a sub-slice of the byte slice with the given bounds.
If start
or stop
are not valid bounds for a sub-slice, then they are clamped to the slice's size.
Additionally, the starting index is clamped to the ending index.
The indices are relative to the current slice, not the underlying byte array.
The starting index of the region of interest (inclusive).
The ending index of the region of interest (exclusive).