Source/Packages

Std.Natural

packages/foundation/standard/src/Std/Natural.alpha

335 lines48 declarations13.5 KiBSHA-256 0a0af2339e6a

def · lines 301–331

naturalDecimalBuilder

Full file
The decimal digits of a natural, most significant first ("0" for zero): the text form of a counter, a sequence number or a tick in an AOS-JSON/1 stream (Observe.Value), and of a token cache path's ranges (Data.TokenCachePath). `fuel` is the caller's bound on the number of digits LESS ONE: a natural with more than `fuel + 1` digits keeps only its last `fuel + 1`, so every caller names a bound its values cannot exceed (a u64 has at most 20 digits).
301def naturalDecimalBuilder =
302  (lambda unrestricted fuel : Nat .
303    nat-eliminate
304      (lambda unrestricted current : Nat . pi unrestricted value : Nat . BytesBuilder)
305      (lambda unrestricted value : Nat .
306        then bytes-builder-chunk bytes nat-to-byte (naturalAdd 48) (naturalModuloUnchecked value) 10)
307      (lambda unrestricted predecessor : Nat .
308        unrestricted induction : (pi unrestricted value : Nat . BytesBuilder) .
309        unrestricted value : Nat .
310        let unrestricted quotient =
311          (naturalDivideUnchecked value 10)
312          in
313          nat-eliminate
314            (lambda unrestricted nonzero : Nat . BytesBuilder)
315            (then bytes-builder-chunk
316              bytes
317              nat-to-byte
318              (naturalAdd 48)
319              (naturalModuloUnchecked value)
320              10)
321            (lambda unrestricted quotientPredecessor : Nat .
322              unrestricted quotientInduction : BytesBuilder .
323              then (bytes-builder-append (induction quotient))
324                bytes-builder-chunk
325                bytes
326                nat-to-byte
327                (naturalAdd 48)
328                (naturalModuloUnchecked value)
329                10)
330            (naturalNonzero quotient))
331      fuel)

The compiler supplied declaration spans and resolved links from this source snapshot. This page does not assert that this file belongs to a checked closure.