bytes.bend checks
raw source on the hub · import bend-kit-bytes@0.3.0.0/bytes.bend as Bytes
Byte buffers packed four bytes to a U32, with bounds-checked access. Source: https://github.com/paymog/bend-kit/tree/main/bytes
1 import
import Base
Types
type Bytes source · line 9 · raw
Type
Bytes@len:U32 -> @buf:Array<U32> -> Bytes
Definitions
def b8 source · line 12 · raw
@+x:U32 -> U32
def shift source · line 15 · raw
@+i:U32 -> Nat
def words source · line 18 · raw
@+n:U32 -> U32
def depth.go source · line 22 · raw
@f:Nat -> @more:Bool -> @+w:U32 -> @+d:Nat -> @+cap:U32 -> Nat
The fewest d with 2^d >= w.
def depth source · line 33 · raw
@+w:U32 -> Nat
def alloc source · line 36 · raw
@+n:U32 -> Array<U32>
def peek.of source · line 40 · raw
@+sh:Nat -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, U32)
Unchecked byte read and write. Callers keep i below len.
def peek source · line 44 · raw
@a:Array<U32> -> @+i:U32 -> Pair(Array<U32>, U32)
def poke.of source · line 47 · raw
@+i:U32 -> @+v:U32 -> @r:Pair(Array<U32>, U32) -> Array<U32>
def poke source · line 52 · raw
@a:Array<U32> -> @+i:U32 -> @+v:U32 -> Array<U32>
def copy.go source · line 56 · raw
@n:Nat -> @r:Pair(Array<U32>, U32) -> @dst:Array<U32> -> @+s:U32 -> @+d:U32 -> Pair(Array<U32>, Array<U32>)
n bytes from src[s..] to dst[d..], one byte at a time.
def copy.bytes source · line 65 · raw
@+m:U32 -> @src:Array<U32> -> @dst:Array<U32> -> @+s:U32 -> @+d:U32 -> Pair(Array<U32>, Array<U32>)
def copy.words source · line 69 · raw
@n:Nat -> @r:Pair(Array<U32>, U32) -> @dst:Array<U32> -> @+s:U32 -> @+d:U32 -> Pair(Array<U32>, Array<U32>)
n words from src[s..] to dst[d..] (word indexes).
def copy.shift source · line 80 · raw
@n:Nat -> @+lo:Nat -> @+hi:Nat -> @+prev:U32 -> @r:Pair(Array<U32>, U32) -> @dst:Array<U32> -> @+k:U32 -> @+d:U32 -> Pair(Array<U32>, Array<U32>)
m bytes to a word-aligned d from an unaligned s: each out word joins two source words. lo = 8*(s%4) and hi = 32 - lo; r holds source word k, prev the word before it.
def copy.shift.at source · line 89 · raw
@+w:U32 -> @+lo:U32 -> @+k:U32 -> @+d:U32 -> @r:Pair(Array<U32>, U32) -> @dst:Array<U32> -> Pair(Array<U32>, Array<U32>)
def copy.tail source · line 93 · raw
@+m:U32 -> @+s:U32 -> @+d:U32 -> @r:Pair(Array<U32>, Array<U32>) -> Pair(Array<U32>, Array<U32>)
def copy.pick source · line 97 · raw
@aligned:Bool -> @+m:U32 -> @src:Array<U32> -> @dst:Array<U32> -> @+s:U32 -> @+d:U32 -> Pair(Array<U32>, Array<U32>)
def copy.dst source · line 108 · raw
@daligned:Bool -> @+m:U32 -> @src:Array<U32> -> @dst:Array<U32> -> @+s:U32 -> @+d:U32 -> Pair(Array<U32>, Array<U32>)
def copy source · line 117 · raw
@+m:U32 -> @src:Array<U32> -> @dst:Array<U32> -> @+s:U32 -> @+d:U32 -> Pair(Array<U32>, Array<U32>)
m bytes from src[s..] to dst[d..]. Whole words when d is word-aligned, shifted when s is not. ponytail: an unaligned d (append after an odd length) copies bytes; merge into d's first word if that gets hot.
def new source · line 121 · raw
@+n:U32 -> Bytes
n zero bytes.
def length source · line 124 · raw
@b:Bytes -> Pair(Bytes, U32)
def get.some source · line 128 · raw
@+len:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Bytes, Maybe<&2, U32>)
def get.if source · line 132 · raw
@ok:Bool -> @+len:U32 -> @buf:Array<U32> -> @+i:U32 -> Pair(Bytes, Maybe<&2, U32>)
def get source · line 140 · raw
@b:Bytes -> @+i:U32 -> Pair(Bytes, Maybe<&2, U32>)
Byte i, or None when i >= len.
def set.if source · line 144 · raw
@ok:Bool -> @+len:U32 -> @buf:Array<U32> -> @+i:U32 -> @+v:U32 -> Bytes
def set source · line 152 · raw
@b:Bytes -> @+i:U32 -> @+v:U32 -> Bytes
Byte i becomes v & 255. Nothing changes when i >= len.
def step source · line 157 · raw
@+up:Bool -> @+i:U32 -> U32
The next index: one forward when up, one back when not.
def fits source · line 161 · raw
@+len:U32 -> @+i:U32 -> @+n:U32 -> Bool
Do n bytes from i fit in len?
def uint.go source · line 165 · raw
@n:Nat -> @r:Pair(Array<U32>, U32) -> @+j:U32 -> @+up:Bool -> @+acc:U32 -> Pair(Array<U32>, U32)
n bytes from j, each shifted in below the ones before: the first byte read is the most significant.
def uint.if source · line 175 · raw
@ok:Bool -> @+len:U32 -> @buf:Array<U32> -> @+i:U32 -> @+n:U32 -> @+be:Bool -> Pair(Bytes, Maybe<&2, U32>)
Big-endian reads forward from i; little-endian reads back from the last byte.
def uint source · line 183 · raw
@b:Bytes -> @+i:U32 -> @+n:U32 -> @+be:Bool -> Pair(Bytes, Maybe<&2, U32>)
def get.u16be source · line 189 · raw
@b:Bytes -> @+i:U32 -> Pair(Bytes, Maybe<&2, U32>)
Unsigned integers at byte i, or None when they run past len. be: most significant byte first (network order); le: least significant first.
def get.u16le source · line 192 · raw
@b:Bytes -> @+i:U32 -> Pair(Bytes, Maybe<&2, U32>)
def get.u32be source · line 195 · raw
@b:Bytes -> @+i:U32 -> Pair(Bytes, Maybe<&2, U32>)
def get.u32le source · line 198 · raw
@b:Bytes -> @+i:U32 -> Pair(Bytes, Maybe<&2, U32>)
def put.go source · line 202 · raw
@n:Nat -> @a:Array<U32> -> @+j:U32 -> @+up:Bool -> @+v:U32 -> Array<U32>
n bytes of v from j, least significant first.
def put.if source · line 210 · raw
@ok:Bool -> @+len:U32 -> @buf:Array<U32> -> @+i:U32 -> @+n:U32 -> @+be:Bool -> @+v:U32 -> Bytes
Little-endian writes forward from i; big-endian writes back from the last byte.
def put source · line 217 · raw
@b:Bytes -> @+i:U32 -> @+n:U32 -> @+be:Bool -> @+v:U32 -> Bytes
def set.u16be source · line 222 · raw
@b:Bytes -> @+i:U32 -> @+v:U32 -> Bytes
v's low 16 or 32 bits at byte i. Nothing changes when they would run past len.
def set.u16le source · line 225 · raw
@b:Bytes -> @+i:U32 -> @+v:U32 -> Bytes
def set.u32be source · line 228 · raw
@b:Bytes -> @+i:U32 -> @+v:U32 -> Bytes
def set.u32le source · line 231 · raw
@b:Bytes -> @+i:U32 -> @+v:U32 -> Bytes
def count source · line 235 · raw
@s:String -> @+n:U32 -> U32
String.length counts in Nat, which costs more than the walk itself.
def flush source · line 243 · raw
@full:Bool -> @a:Array<U32> -> @+k:U32 -> @+w:U32 -> Array<U32>
Bytes enter at the top of w and shift down, so a full word has byte 0 lowest.
def from.go source · line 250 · raw
@s:String -> @a:Array<U32> -> @+i:U32 -> @+w:U32 -> Array<U32>
def from_string source · line 261 · raw
@+s:String -> Bytes
A byte string (one Char per octet, as Wire and Http use) to Bytes. Each Char keeps its low 8 bits.
def to.go source · line 265 · raw
@n:Nat -> @r:Pair(Array<U32>, U32) -> @+i:U32 -> @acc:String -> String
def to_string source · line 274 · raw
@b:Bytes -> String
Bytes to a byte string, one Char per octet.
def slice.fin source · line 278 · raw
@+len:U32 -> @+m:U32 -> @r:Pair(Array<U32>, Array<U32>) -> Pair(Bytes, Bytes)
def slice.at source · line 282 · raw
@+len:U32 -> @buf:Array<U32> -> @+s:U32 -> @+m:U32 -> Pair(Bytes, Bytes)
def slice source · line 286 · raw
@b:Bytes -> @+start:U32 -> @+n:U32 -> Pair(Bytes, Bytes)
The buffer back, and a copy of up to n bytes from start. Both ends are clamped to len.
def dst source · line 291 · raw
@r:Pair(Array<U32>, Array<U32>) -> Array<U32>
def room source · line 296 · raw
@+len:U32 -> @+n:U32 -> Bool
Does a buffer holding len bytes have room for n? Its 2^d words fit the fewest that hold len.
def grow.if source · line 299 · raw
@fits:Bool -> @+len:U32 -> @buf:Array<U32> -> @+n:U32 -> Array<U32>
def grow source · line 307 · raw
@+len:U32 -> @buf:Array<U32> -> @+n:U32 -> Array<U32>
buf, or a copy of its first len bytes in the fewest 2^d words that hold n.
def append source · line 312 · raw
@a:Bytes -> @b:Bytes -> Bytes
a then b. b is written into a's buffer while it has room, and the buffer at least doubles when it runs out, so building a buffer one piece at a time is O(total).
def concat.total.con source · line 318 · raw
@x:Bytes -> @r:Pair(List<&1, Bytes>, U32) -> Pair(List<&1, Bytes>, U32)
def concat.total source · line 322 · raw
@xs:List<&1, Bytes> -> @+n:U32 -> Pair(List<&1, Bytes>, U32)
def concat.go source · line 330 · raw
@xs:List<&1, Bytes> -> @out:Array<U32> -> @+at:U32 -> Array<U32>
def concat.of source · line 338 · raw
@r:Pair(List<&1, Bytes>, U32) -> Bytes
def concat source · line 343 · raw
@xs:List<&1, Bytes> -> Bytes
The pieces in order, in one new buffer: one copy per byte, however many pieces.
def at.go source · line 347 · raw
@needle:String -> @ok:Bool -> @r:Pair(Array<U32>, U32) -> @+j:U32 -> Pair(Array<U32>, Bool)
Does needle match at j? ok is the previous byte's result; the first mismatch stops the walk.
def at source · line 361 · raw
@a:Array<U32> -> @+needle:String -> @+j:U32 -> Pair(Array<U32>, Bool)
def find.go source · line 366 · raw
@f:Nat -> @r:Pair(Array<U32>, Bool) -> @+needle:String -> @+i:U32 -> @+up:Bool -> Pair(Array<U32>, Maybe<&2, U32>)
ponytail: naive search; a mismatch costs one byte read per position, but a repetitive needle is O(len * needle). Add a SWAR scan or two-way search if find gets hot. f counts the positions left after i.
def find.fin source · line 379 · raw
@+len:U32 -> @r:Pair(Array<U32>, Maybe<&2, U32>) -> Pair(Bytes, Maybe<&2, U32>)
def find.at source · line 383 · raw
@none:Bool -> @+len:U32 -> @buf:Array<U32> -> @+needle:String -> @+i:U32 -> @+f:U32 -> @+up:Bool -> Pair(Bytes, Maybe<&2, U32>)
def find.from source · line 392 · raw
@b:Bytes -> @+needle:String -> @+start:U32 -> Pair(Bytes, Maybe<&2, U32>)
Index of the first match of needle (a byte string) at or after start, or None. An empty needle matches at start when start <= len.
def find source · line 398 · raw
@b:Bytes -> @+needle:String -> Pair(Bytes, Maybe<&2, U32>)
Index of the first match of needle (a byte string), or None. An empty needle matches at 0.
def rfind source · line 402 · raw
@b:Bytes -> @+needle:String -> Pair(Bytes, Maybe<&2, U32>)
Index of the last match of needle, or None. An empty needle matches at len.
def at.fin source · line 407 · raw
@+len:U32 -> @r:Pair(Array<U32>, Bool) -> Pair(Bytes, Bool)
def at.if source · line 411 · raw
@fits:Bool -> @+len:U32 -> @buf:Array<U32> -> @+needle:String -> @+j:U32 -> Pair(Bytes, Bool)
def starts_with source · line 419 · raw
@b:Bytes -> @+prefix:String -> Pair(Bytes, Bool)
Does b begin with prefix (a byte string)?
def ends_with source · line 425 · raw
@b:Bytes -> @+suffix:String -> Pair(Bytes, Bool)
Does b end with suffix (a byte string)?
def split.scan source · line 432 · raw
@f:Nat -> @r:Pair(Array<U32>, Bool) -> @+sep:String -> @+m:U32 -> @+i:U32 -> @+skip:U32 -> @+cuts:List<&2, U32> -> Pair(Array<U32>, List<&2, U32>)
Match positions of sep, latest first, that do not overlap an earlier match. f counts the positions left; skip counts the bytes still inside the last match.
def snd source · line 443 · raw
@r:Pair(Bytes, Bytes) -> Bytes
def split.cut source · line 448 · raw
@cuts:List<&2, U32> -> @r:Pair(Bytes, Bytes) -> @+end:U32 -> @+m:U32 -> @acc:List<&1, Bytes> -> List<&1, Bytes>
r holds the buffer and the piece that ends at end; each earlier cut j starts one at j + m.
def split.of source · line 457 · raw
@cuts:List<&2, U32> -> @b:Bytes -> @+len:U32 -> @+m:U32 -> List<&1, Bytes>
def split.run source · line 464 · raw
@+len:U32 -> @+m:U32 -> @r:Pair(Array<U32>, List<&2, U32>) -> List<&1, Bytes>
def split.if source · line 468 · raw
@whole:Bool -> @+len:U32 -> @buf:Array<U32> -> @+sep:String -> @+m:U32 -> List<&1, Bytes>
def split source · line 477 · raw
@b:Bytes -> @+sep:String -> List<&1, Bytes>
The pieces between matches of sep (a byte string), left to right. n matches give n + 1 pieces, so a match at either end gives an empty piece. An empty sep gives b whole.
def eq.go source · line 483 · raw
@n:Nat -> @same:Bool -> @r:Pair(Array<U32>, U32) -> @s:Pair(Array<U32>, U32) -> @+i:U32 -> Pair(Array<U32>, Pair(Array<U32>, Bool))
Compares whole words; same tells whether the previous pair matched.
def eq.fin source · line 500 · raw
@+la:U32 -> @+lb:U32 -> @r:Pair(Array<U32>, Pair(Array<U32>, Bool)) -> Pair(Bytes, Pair(Bytes, Bool))
def eq.len source · line 504 · raw
@same:Bool -> @+la:U32 -> @xa:Array<U32> -> @+lb:U32 -> @ya:Array<U32> -> Pair(Bytes, Pair(Bytes, Bool))
def eq source · line 512 · raw
@a:Bytes -> @b:Bytes -> Pair(Bytes, Pair(Bytes, Bool))
Both buffers back, and whether they hold the same bytes.
def bswap source · line 518 · raw
@+w:U32 -> U32
Byte 0 of w becomes the most significant, so words compare in byte order.
def cmp.go source · line 522 · raw
@n:Nat -> @c:Cmp -> @r:Pair(Array<U32>, U32) -> @s:Pair(Array<U32>, U32) -> @+i:U32 -> Pair(Array<U32>, Pair(Array<U32>, Cmp))
Compares whole words; c is the previous pair's order.
def cmp.len source · line 543 · raw
@c:Cmp -> @+la:U32 -> @+lb:U32 -> Cmp
def cmp.fin source · line 552 · raw
@+la:U32 -> @+lb:U32 -> @r:Pair(Array<U32>, Pair(Array<U32>, Cmp)) -> Pair(Bytes, Pair(Bytes, Cmp))
def cmp source · line 558 · raw
@a:Bytes -> @b:Bytes -> Pair(Bytes, Pair(Bytes, Cmp))
Both buffers back, and their order byte by byte; a prefix sorts first. The shorter buffer's bytes past its len are 0, so its last word compares low when only the longer one goes on.
def nibble source · line 563 · raw
@+n:U32 -> Char
def hex.go source · line 566 · raw
@n:Nat -> @r:Pair(Array<U32>, U32) -> @+i:U32 -> @acc:String -> String
def to_hex source · line 575 · raw
@b:Bytes -> String
Two lowercase hex digits per byte.
def in source · line 579 · raw
@+lo:U32 -> @+c:U32 -> @+hi:U32 -> Bool
def unnibble source · line 583 · raw
@+c:U32 -> U32
A hex digit's value, or 16.
def poke.when source · line 586 · raw
@write:Bool -> @a:Array<U32> -> @+i:U32 -> @+v:U32 -> Array<U32>
def unhex.go source · line 594 · raw
@s:String -> @a:Array<U32> -> @+i:U32 -> @+hi:U32 -> @+ok:Bool -> Pair(Array<U32>, Bool)
hi is the previous digit; an odd i completes a byte.
def decoded source · line 602 · raw
@+n:U32 -> @r:Pair(Array<U32>, Bool) -> Maybe<&1, Bytes>
def unhex.if source · line 610 · raw
@even:Bool -> @+s:String -> @+n:U32 -> Maybe<&1, Bytes>
def from_hex source · line 618 · raw
@+s:String -> Maybe<&1, Bytes>
Hex digits, either case, to bytes. None for an odd count or a non-hex char.
def b64.char source · line 623 · raw
@+i:U32 -> U32
RFC 4648 §4: the standard alphabet, padded with '='.
def b64.at source · line 627 · raw
@+v:U32 -> @+k:Nat -> Char
The six bits of v at k as a base64 char.
def b64.go source · line 631 · raw
@n:Nat -> @r:Pair(Array<U32>, U32) -> @+i:U32 -> @acc:String -> String
Groups of three bytes, last first; r holds the 24 bits of the group at i.
def b64.tail source · line 641 · raw
@+rem:U32 -> @+t:U32 -> @+q:U32 -> @r:Pair(Array<U32>, U32) -> String
The rem < 3 bytes after the last whole group, then the groups before them.
def to_base64 source · line 649 · raw
@b:Bytes -> String
Base64 (RFC 4648 §4) with '=' padding.
def b64.val source · line 656 · raw
@+c:U32 -> U32
A base64 char's value, 64 for '=', or 65.
def b64.pads source · line 660 · raw
@s:String -> @+run:U32 -> U32
The count of '=' at the end of s.
def b64.put source · line 668 · raw
@full:Bool -> @a:Array<U32> -> @+o:U32 -> @+w:U32 -> @+v:U32 -> Array<U32>
A whole group of four chars writes its bytes at o, as many as fit before len.
def unb64.go source · line 676 · raw
@s:String -> @a:Array<U32> -> @+i:U32 -> @+v:U32 -> @+ok:Bool -> @+len:U32 -> @+body:U32 -> Pair(Array<U32>, Bool)
v collects six bits per char. '=' is allowed only at body and after.
def unb64.if source · line 687 · raw
@ok:Bool -> @+s:String -> @+n:U32 -> @+pads:U32 -> Maybe<&1, Bytes>
def from_base64 source · line 697 · raw
@+s:String -> Maybe<&1, Bytes>
Padded base64 (RFC 4648 §4) to bytes. None for a length that is not a multiple of 4, more than two '=', '=' before the end, or a char outside the alphabet.