cli: add supplied-buffer decimal integer formatting to std
This commit is contained in:
30 files changed
+3609
-11
No files matched your search
+5
-1
@@ -1,6 +1,6 @@
|
||||
# Aji standard library
|
||||
|
||||
Implementation in ordinary safe `.aji`: bytes, math, UTF-8, hex and Base64, with 35 functions and 13 public result enums. They are embedded with the compiler and compiled through the normal frontend/verifier when imported. They are library modules, not standalone `main` programs.
|
||||
Implementation in ordinary safe `.aji`: bytes, math, UTF-8, hex, Base64 and integer formatting, with 39 functions and 14 public result enums. They are embedded with the compiler and compiled through the normal frontend/verifier when imported. They are library modules, not standalone `main` programs.
|
||||
|
||||
```aji
|
||||
module example
|
||||
@@ -25,6 +25,10 @@ Endian APIs read/write u16/u32/u64 in LE/BE order. Reads return `std.bytes.ReadU
|
||||
|
||||
`std.utf8.next/validate` use Unicode scalar values and explicit byte offsets. `std.encoding.hex` and `std.encoding.base64` encode/decode into caller-provided buffers; malformed input and short capacity are enum results, with no destination mutation on returned errors. Hex emits lowercase and accepts mixed case. Base64 is canonical, padded and uses the standard alphabet. The [text example](../examples/std/text.aji) returns 8364 without grants.
|
||||
|
||||
`std.fmt.write_u64/i64/usize/isize` write decimal ASCII into supplied buffers with `WriteResult::ok(count)` / `short_output`. Counts include a negative sign; short output stays unchanged, suffixes stay unchanged on success and no terminator is written. The [formatting example](../examples/std/format.aji) composes `value=-42 count=7` with explicit offsets and returns 17 without grants.
|
||||
|
||||
The compiler uses its embedded copy, so rebuild after editing these files. An installed compiler needs neither this checkout nor a runtime std search path. Standard enum types use fully qualified names in signatures, constructors and matches. General user imports, imported struct literals, generics and owning collections are still future work.
|
||||
|
||||
[API reference](../docs/reference/std.md) · [Specification](../docs/spec/16-standard-library.md) · [Plan](../docs/plan/standard-library.md) · [Import RFC](../docs/rfcs/0016-standard-library-bootstrap.md) · [Endian RFC](../docs/rfcs/0017-standard-endian-results.md) · [Bounded operations RFC](../docs/rfcs/0018-bounded-standard-operations.md) · [Text RFC](../docs/rfcs/0019-standard-text-encodings.md)
|
||||
|
||||
[Formatting RFC](../docs/rfcs/0020-supplied-buffer-integer-formatting.md)
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
module std.fmt
|
||||
|
||||
enum WriteResult { ok(usize), short_output }
|
||||
|
||||
fn write_u64(dst: slice_mut<u8>, value: u64) -> std.fmt.WriteResult
|
||||
effects []
|
||||
{
|
||||
var magnitude: u64 = value;
|
||||
var count: usize = 1;
|
||||
var remaining: u64 = magnitude;
|
||||
while remaining >= 10 {
|
||||
remaining = remaining / 10;
|
||||
count = count + 1;
|
||||
}
|
||||
if dst.len < count { return std.fmt.WriteResult::short_output; }
|
||||
var position: usize = count;
|
||||
loop {
|
||||
position = position - 1;
|
||||
dst[position] = (magnitude % 10) as! u8 + 48;
|
||||
magnitude = magnitude / 10;
|
||||
if magnitude == 0 { break; }
|
||||
}
|
||||
return std.fmt.WriteResult::ok(count);
|
||||
}
|
||||
|
||||
fn write_i64(dst: slice_mut<u8>, value: i64) -> std.fmt.WriteResult
|
||||
effects []
|
||||
{
|
||||
let negative = value < 0;
|
||||
var magnitude: u64 = 0;
|
||||
if negative {
|
||||
magnitude = (-(value + 1)) as! u64 + 1;
|
||||
} else {
|
||||
magnitude = value as! u64;
|
||||
}
|
||||
var count: usize = 1;
|
||||
var remaining: u64 = magnitude;
|
||||
while remaining >= 10 {
|
||||
remaining = remaining / 10;
|
||||
count = count + 1;
|
||||
}
|
||||
if negative { count = count + 1; }
|
||||
if dst.len < count { return std.fmt.WriteResult::short_output; }
|
||||
if negative { dst[0] = 45; }
|
||||
var position: usize = count;
|
||||
loop {
|
||||
position = position - 1;
|
||||
dst[position] = (magnitude % 10) as! u8 + 48;
|
||||
magnitude = magnitude / 10;
|
||||
if magnitude == 0 { break; }
|
||||
}
|
||||
return std.fmt.WriteResult::ok(count);
|
||||
}
|
||||
|
||||
fn write_usize(dst: slice_mut<u8>, value: usize) -> std.fmt.WriteResult
|
||||
effects []
|
||||
{
|
||||
var magnitude: usize = value;
|
||||
var count: usize = 1;
|
||||
var remaining: usize = magnitude;
|
||||
while remaining >= 10 {
|
||||
remaining = remaining / 10;
|
||||
count = count + 1;
|
||||
}
|
||||
if dst.len < count { return std.fmt.WriteResult::short_output; }
|
||||
var position: usize = count;
|
||||
loop {
|
||||
position = position - 1;
|
||||
dst[position] = (magnitude % 10) as! u8 + 48;
|
||||
magnitude = magnitude / 10;
|
||||
if magnitude == 0 { break; }
|
||||
}
|
||||
return std.fmt.WriteResult::ok(count);
|
||||
}
|
||||
|
||||
fn write_isize(dst: slice_mut<u8>, value: isize) -> std.fmt.WriteResult
|
||||
effects []
|
||||
{
|
||||
let negative = value < 0;
|
||||
var magnitude: usize = 0;
|
||||
if negative {
|
||||
magnitude = (-(value + 1)) as! usize + 1;
|
||||
} else {
|
||||
magnitude = value as! usize;
|
||||
}
|
||||
var count: usize = 1;
|
||||
var remaining: usize = magnitude;
|
||||
while remaining >= 10 {
|
||||
remaining = remaining / 10;
|
||||
count = count + 1;
|
||||
}
|
||||
if negative { count = count + 1; }
|
||||
if dst.len < count { return std.fmt.WriteResult::short_output; }
|
||||
if negative { dst[0] = 45; }
|
||||
var position: usize = count;
|
||||
loop {
|
||||
position = position - 1;
|
||||
dst[position] = (magnitude % 10) as! u8 + 48;
|
||||
magnitude = magnitude / 10;
|
||||
if magnitude == 0 { break; }
|
||||
}
|
||||
return std.fmt.WriteResult::ok(count);
|
||||
}
|
||||
Reference in new issue
Block a user