This module defines the ByteSlice
structure. It's a modified version of the SubArray
code,
with some functions specific to ByteSlice
.
Internal representation of ByteSlice
, which is an abbreviation for Slice ByteSliceData
.
- byteArray : ByteArray
The underlying byte array.
- start : Nat
The starting index of the region of interest (inclusive).
- stop : Nat
The ending index of the region of interest (exclusive).
The starting index is no later than the ending index.
The ending index is exclusive. If the starting and ending indices are equal, then the byte slice is empty.
The stopping index is no later than the end of the byte array.
The ending index is exclusive. If it is equal to the size of the byte array, then the last byte of the byte array is in the byte slice.
Instances For
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
.
Instances For
The underlying byte array.
Equations
Instances For
The starting index of the region of interest (inclusive).
Equations
- xs.start = xs.internalRepresentation.start
Instances For
The ending index of the region of interest (exclusive).
Equations
- xs.stop = xs.internalRepresentation.stop
Instances For
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.
Instances For
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.
Instances For
The empty byte slice.
This empty byte slice is backed by an empty byte array.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- ByteSlice.instEmptyCollection = { emptyCollection := ByteSlice.empty }
Equations
- ByteSlice.instInhabited = { default := ∅ }
Comparison function
Equations
- a.beq b = (a.toByteArray == b.toByteArray)
Instances For
Equations
- ByteSlice.instBEq = { beq := ByteSlice.beq }
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
Equations
- ByteSlice.foldrM f init as = ByteSlice.foldrM.loop✝ f as 0 init
Instances For
Runs a monadic action on each byte of a byte slice.
The bytes are processed starting at the lowest index and moving up.
Equations
- ByteSlice.forM f as = ByteSlice.forM.loop✝ f as 0
Instances For
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
Equations
- ByteSlice.foldr f init as = (ByteSlice.foldrM (fun (x1 : UInt8) (x2 : β) => pure (f x1 x2)) init as).run
Instances For
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.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Checks if the byte slice contains a specific byte value.
Returns true
if any byte in the slice equals the given value, false
otherwise.
Equations
- s.contains byte = ByteSlice.contains.loop✝ s byte 0
Instances For
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.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.