cli: add bounded std copy/search and checked arithmetic

This commit is contained in:
dev committed 2026-10-06 15:40:57 +03:00
1 parent a2ca293506
commit 6397a317e2
36 files changed
+1533 -7

No files matched your search

+41
View File
@@ -71,3 +71,44 @@ effects []
Missing enum arm: `E7004`; duplicate/wildcard enum arm: `E7006`; unknown type/variant or missing import: `E7002`; wrong width/payload, immutable destination or variant from another enum: `E7005`; conflicting destination borrows: `E2101`. Existing `or trap` only applies to fallible built-in operations, so it is not used with these enums.
The example is checked in `source/quickref/std_endian.aji`; fixtures for every width/order and all short lengths are in `source/std/endian_u*.aji`.
## Copy and substring search
| Function | Result and contract |
|---|---|
| `std.bytes.copy(dst: slice_mut<u8>, src: slice<u8>) -> std.bytes.WriteResult` | `ok` copies the whole input into the output prefix, preserving the suffix; `short_output` leaves the whole output unchanged. Empty input succeeds. Overlap rejects (`E2101`), including on the error path. |
| `std.bytes.find_slice(data: slice<u8>, needle: slice<u8>) -> std.bytes.FindResult` | `found(index)` for the first match, or `not_found`. Empty needle is `found(0)`, including empty data. Offset is relative to the passed slice; it need not be a readable element index. |
Search allocates no heap and uses constant auxiliary storage; the reference algorithm has O(data length × needle length) worst-case comparisons. Interpreter fuel bounds execution. For splitting at the first separator, match the search result, then take `data[0..index]` and `data[index + separator.len..data.len]`, or split at the index and borrow past the separator in the right half. An empty separator leaves the whole data in the right half; repeated splitting needs an explicit progress rule. There is no split iterator or returned pair of borrowed slices.
## Recoverable arithmetic
| Operand type | Functions | Result |
|---|---|---|
| `u64` | `std.math.checked_add_u64`, `std.math.checked_sub_u64`, `std.math.checked_mul_u64` | `std.math.U64Result::ok(u64)` / `overflow` |
| `usize` | `std.math.checked_add_usize`, `std.math.checked_sub_usize`, `std.math.checked_mul_usize` | `std.math.UsizeResult::ok(usize)` / `overflow` |
Every function takes two operands of the indicated type. A representable mathematical result succeeds; unsigned subtraction underflow also returns `overflow`. Multiplication by zero always succeeds with zero. `usize` follows target pointer width. Normal arithmetic operators still trap on overflow; wrapping uses `+%`, `-%`, `*%`, with no library wrappers. These monomorphic APIs do not provide generic or signed arithmetic. Result enums use the same ordinary match rules as the endian results.
For a zero-terminated byte record, check the complete output size before copying:
```aji
fn encode(dst: slice_mut<u8>, data: slice<u8>) -> usize
effects []
{
match std.math.checked_add_usize(data.len, 1) {
std.math.UsizeResult::ok(total) => {
if total > dst.len { return 0; }
match std.bytes.copy(mut dst, ref data) {
std.bytes.WriteResult::ok => { dst[data.len] = 0; return total; }
std.bytes.WriteResult::short_output => { return 0; }
}
}
std.math.UsizeResult::overflow => { return 0; }
}
}
```
This helper returns the encoded byte count or zero on failure, and leaves the destination unchanged on failure. Import both `std.math` and `std.bytes`. Its zero sentinel is unambiguous because a successful record includes the terminator.
The checked [bounded example](../../examples/std/bounded.aji) combines overflow-aware output sizing, whole-input copy and delimiter search; it returns 10. Copy/search/split and independent arithmetic fixtures are in `source/std/bounded_*.aji`. Wrong operands/payloads are `E7005`; absent imports/types are `E7002`; missing result arms are `E7004`. No new diagnostic code, syntax, verifier state or runtime primitive is introduced.