cli: add supplied-buffer decimal integer formatting to std
This commit is contained in:
30 files changed
+3609
-11
No files matched your search
@@ -165,3 +165,52 @@ effects []
|
||||
```
|
||||
|
||||
The [text example](../../examples/std/text.aji) is checked by `source/std/text_example.aji` and returns 8364 (U+20AC). Independent byte/scalar vectors, capacities, invalid-input precedence, namespace collisions, alias rejects and disjoint buffers are under `source/std/text_*.aji`. UTF-8/encoding algorithms allocate no heap and declare `effects []`; fixed local tables are constant auxiliary storage. Wrong operands/payloads: `E7005`; missing result arms: `E7004`; unknown types/imports: `E7002`; conflicting borrows: `E2101`. Results are ordinary Copy enums with no new must-use rule.
|
||||
|
||||
## Integer formatting (`std.fmt`)
|
||||
|
||||
Import `std.fmt`. Its four functions share `std.fmt.WriteResult::ok(count: usize)` / `short_output`:
|
||||
|
||||
| Function | Value type |
|
||||
|---|---|
|
||||
| `write_u64(dst: slice_mut<u8>, value: u64)` | unsigned 64-bit |
|
||||
| `write_i64(dst: slice_mut<u8>, value: i64)` | signed 64-bit |
|
||||
| `write_usize(dst: slice_mut<u8>, value: usize)` | unsigned target pointer width |
|
||||
| `write_isize(dst: slice_mut<u8>, value: isize)` | signed target pointer width |
|
||||
|
||||
Output is decimal ASCII with no leading zeroes, plus sign, padding or terminator; negative values begin with `-` and zero is `0`. Every scalar value is supported, including signed minima. Success writes the prefix and preserves the suffix; short capacity preserves the entire output. Empty output is short even for zero. No heap allocation, cursor or format string exists. Full contracts are in spec 16 §2.8.
|
||||
|
||||
Compose byte literals and integers using explicit offsets, advancing only after a successful operation:
|
||||
|
||||
```aji
|
||||
module demo.format
|
||||
import std.bytes;
|
||||
import std.fmt;
|
||||
|
||||
fn main() -> usize
|
||||
effects []
|
||||
{
|
||||
var record: [u8; 24] = [165; 24];
|
||||
match std.bytes.copy(mut record, b"value=") {
|
||||
std.bytes.WriteResult::ok => {}
|
||||
std.bytes.WriteResult::short_output => { return 0; }
|
||||
}
|
||||
var offset: usize = 6;
|
||||
match std.fmt.write_i64(mut record[offset..record.len], -42) {
|
||||
std.fmt.WriteResult::ok(count) => { offset = offset + count; }
|
||||
std.fmt.WriteResult::short_output => { return 0; }
|
||||
}
|
||||
match std.bytes.copy(mut record[offset..record.len], b" count=") {
|
||||
std.bytes.WriteResult::ok => { offset = offset + 7; }
|
||||
std.bytes.WriteResult::short_output => { return 0; }
|
||||
}
|
||||
match std.fmt.write_usize(mut record[offset..record.len], 7) {
|
||||
std.fmt.WriteResult::ok(count) => { offset = offset + count; }
|
||||
std.fmt.WriteResult::short_output => { return 0; }
|
||||
}
|
||||
assert(std.bytes.equal(ref record[0..offset], b"value=-42 count=7"));
|
||||
for i in offset..record.len { assert(record[i] == 165); }
|
||||
return offset;
|
||||
}
|
||||
```
|
||||
|
||||
This checked program is `examples/std/format.aji` and `tests/conformance/source/std/fmt_example.aji`. It returns 17 without grants. Each operation preserves its own destination on short output; a sequence is not transactional across earlier successful calls. Existing alias checks apply even when a write would return short output. Smaller scalar widths require an explicit widening conversion to an appropriate writer; wider integers, float formatting and parsing are outside this increment.
|
||||
Reference in new issue
Block a user