~/bend-docscommunity

number.bend checks

raw source on the hub · import 0xeb25eff332574f4f1d48668b83cdda30/number.bend as Number

bend-osdev-asn1, numbers in octets: the digits of a natural number in a base, read back by Horner's rule; the base-128 form with bit 8 set on every octet but the last (X.690 8.1.2.4.2 for high tag numbers, 8.19.2 for object identifier subidentifiers); and the unsigned big-endian form of the long length octets (8.1.3.5). Internal: the public readers and writers are in tag.bend, length.bend and the typed files.

Bend evaluates arguments before a call, so a recursive def that must stop takes the test for stopping as a parameter, computed by its caller (a1.digits.go's lt, a1.r128's more).

2 imports
import Base
import bend-osdev-bytes@0.1.0.0/bytes.bend as B

Types

type A1Num source · line 15 · raw

Data

A number read from octets: its value, the octets it took, and the rest

Definitions

def a1.b source · line 19 · raw

@n:Nat -> U32

the byte of a value below 256 (the encoder's form of a byte: masked)

def a1.digits.go source · line 24 · raw

@fuel:Nat -> @lt:Bool -> @+b:Nat -> @+n:Nat -> List<&2, Nat>

the digits of n in base b (b at least 2), most significant first; [n] when n < b (lt says whether n < b; fuel bounds the digits)

def a1.digits source · line 34 · raw

@+b:Nat -> @+n:Nat -> List<&2, Nat>

def a1.horner source · line 38 · raw

@ds:List<&2, Nat> -> @+b:Nat -> @acc:Nat -> Nat

Horner's rule: the value of digits ds in base b, after acc

def a1.bytes_of source · line 46 · raw

@ds:List<&2, Nat> -> List<&2, U32>

each digit as a byte

def a1.enc128.c source · line 53 · raw

@d:Nat -> @t:List<&2, Nat> -> @rest:List<&2, U32> -> List<&2, U32>

def a1.enc128 source · line 61 · raw

@ds:List<&2, Nat> -> List<&2, U32>

base-128 digits as octets: bit 8 set on all but the last

def a1.b128 source · line 69 · raw

@+n:Nat -> List<&2, U32>

n in base-128 octets (a high tag number, a subidentifier)

def a1.be source · line 73 · raw

@+n:Nat -> List<&2, U32>

n as unsigned big-endian octets, as few as there can be (one for 0)

def a1.num_cons source · line 76 · raw

@x:U32 -> @m:Maybe<&2, A1Num> -> Maybe<&2, A1Num>

def a1.r128 source · line 86 · raw

@xs:List<&2, U32> -> @more:Bool -> @k:Nat -> @+acc:Nat -> Maybe<&2, A1Num>

reading base-128 octets while the last had bit 8 set (more), at most k more of them; acc is the value so far. None if the octets end first or there are more than k.

def a1.max_octets source · line 103 · raw

Nat

The implementation's bound on numbers read from octets: Bend's natural numbers are immediate values below 2^48 at run time, so a base-128 number (a high tag number, a subidentifier) has at most six octets (42 bits) and a length at most six octets (48 bits)

def a1.fold256 source · line 107 · raw

@xs:List<&2, U32> -> @acc:Nat -> Nat

the value of big-endian octets, after acc

def a1.rbe.pick source · line 114 · raw

@ok:Bool -> @+k:Nat -> @+xs:List<&2, U32> -> Maybe<&2, A1Num>

def a1.rbe source · line 123 · raw

@+k:Nat -> @+xs:List<&2, U32> -> Maybe<&2, A1Num>

k big-endian octets; None if there are fewer

def a1.leq source · line 127 · raw

@xs:List<&2, U32> -> @ys:List<&2, U32> -> Bool

element-wise equality of byte lists