1 import Base 2 import bend-osdev-word64@0.1.0.0/word64.bend as W 3 import ./bytes.bend as B 4 5 def show_u32(m: Maybe<&2, U32>) -> String: 6 match m: 7 case Some{v}: 8 U32.show(v) 9 case None{}: 10 "none" 11 12 def show_w64(m: Maybe<&2, W.W64>) -> String: 13 match m: 14 case Some{v}: 15 W.W64.show_hex(v) 16 case None{}: 17 "none" 18 19 # n bytes 0, 1, 2, ... (mod 256), built tail-recursively 20 def ramp(n: Nat, acc: List<&2, U32>) -> List<&2, U32>: 21 match n: 22 case 0n: 23 acc 24 case 1n+(+p): 25 ramp(p, Con{U32.and(U32.from_nat(p), 255), acc}) 26 27 # the fast Octets forms against the list layer, on a string short enough for 28 # the structural list functions 29 def same_as_list(+o: B.Octets) -> Bool: 30 Bool.and(String.eq(B.Octets.hex(o), B.hex(B.Octets.to_list(o))), String.eq(B.Octets.to_string(o), B.to_string(B.Octets.to_list(o)))) 31 32 def main() -> IO(Unit): 33 do IO: 34 IO.print(B.hex(B.be32(305419896))) 35 IO.print(B.hex(B.le32(305419896))) 36 IO.print(B.hex(B.le64(W.W64.from_parts(16909060, 84281096)))) 37 IO.print(show_u32(B.be16_at([0, 1, 2, 3], 1n))) 38 IO.print(show_u32(B.le32_at(B.of_string("Bend"), 0n))) 39 IO.print(show_w64(B.be64_at([0, 1, 2, 3, 4, 5, 6, 7, 8], 1n))) 40 IO.print(show_u32(B.le32_at([1, 2, 3], 0n))) 41 IO.print(B.hex(B.append(B.take(2n, [1, 2, 3, 4]), B.zeros(2n)))) 42 IO.print(B.to_string(B.drop(2n, B.of_string("osdev")))) 43 IO.print(Bool.show(same_as_list(B.Octets.of_list(ramp(3000n, Nil{}))))) 44 IO.print(Nat.show(B.Octets.len(B.Octets.append(B.Octets.of_list(ramp(60000n, Nil{})), B.Octets.zeros(40000n))))) 45 IO.print(String.take(B.Octets.hex(B.Octets.drop(59998n, B.Octets.of_list(ramp(60000n, Nil{})))), 4n)) 46 47 #|12345678 48 #|78563412 49 #|0807060504030201 50 #|258 51 #|1684956482 52 #|0102030405060708 53 #|none 54 #|01020000 55 #|dev 56 #|True 57 #|100000 58 #|5e5f