cli: add bounded std copy/search and checked arithmetic
This commit is contained in:
36 files changed
+1533
-7
No files matched your search
+4
-2
@@ -1,6 +1,6 @@
|
||||
# Aji standard library
|
||||
|
||||
Implementation in ordinary safe `.aji`: `math.aji` and `bytes.aji`, with 21 functions and four 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`: `math.aji` and `bytes.aji`, with 29 functions and seven 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
|
||||
@@ -21,6 +21,8 @@ Run `zig build`, then `zig-out/bin/aji run --fuel 1000000 examples/std/bytes.aji
|
||||
|
||||
Endian APIs read/write u16/u32/u64 in LE/BE order. Reads return `std.bytes.ReadU16`, `ReadU32` or `ReadU64` (`ok(value)` / `short_input`); writes return `std.bytes.WriteResult` (`ok` / `short_output`). A short write leaves the entire destination unchanged. Pass a sub-slice for an offset. [Endian example](../examples/std/endian.aji) returns 305419896 and needs no grants.
|
||||
|
||||
`std.bytes.copy` reuses `WriteResult` and preserves the whole destination on short output. `find_slice` returns `FindResult::found(index)` / `not_found`; explicit slices compose a split without returned borrows. `std.math.checked_add/sub/mul_u64` and the `_usize` forms return typed `ok(value)` / `overflow` enums; wrapping stays with `+%`, `-%`, `*%`. The [bounded example](../examples/std/bounded.aji) returns 10 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)
|
||||
[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)
|
||||
@@ -183,3 +183,30 @@ effects []
|
||||
dst[7] = value as% u8;
|
||||
return std.bytes.WriteResult::ok;
|
||||
}
|
||||
|
||||
fn copy(dst: slice_mut<u8>, src: slice<u8>) -> std.bytes.WriteResult
|
||||
effects []
|
||||
{
|
||||
if dst.len < src.len { return std.bytes.WriteResult::short_output; }
|
||||
slice.copy(mut dst, ref src);
|
||||
return std.bytes.WriteResult::ok;
|
||||
}
|
||||
|
||||
enum FindResult { found(usize), not_found }
|
||||
|
||||
fn find_slice(data: slice<u8>, needle: slice<u8>) -> std.bytes.FindResult
|
||||
effects []
|
||||
{
|
||||
if needle.len == 0 { return std.bytes.FindResult::found(0); }
|
||||
if needle.len > data.len { return std.bytes.FindResult::not_found; }
|
||||
let last = data.len - needle.len;
|
||||
var start: usize = 0;
|
||||
loop {
|
||||
if std.bytes.equal(ref data[start..start + needle.len], ref needle) {
|
||||
return std.bytes.FindResult::found(start);
|
||||
}
|
||||
if start == last { break; }
|
||||
start = start + 1;
|
||||
}
|
||||
return std.bytes.FindResult::not_found;
|
||||
}
|
||||
@@ -20,3 +20,52 @@ effects []
|
||||
assert(lower <= upper);
|
||||
return std.math.min_u64(std.math.max_u64(value, lower), upper);
|
||||
}
|
||||
|
||||
enum U64Result { ok(u64), overflow }
|
||||
enum UsizeResult { ok(usize), overflow }
|
||||
|
||||
fn checked_add_u64(a: u64, b: u64) -> std.math.U64Result
|
||||
effects []
|
||||
{
|
||||
let maximum = 0u64 -% 1u64;
|
||||
if b > maximum - a { return std.math.U64Result::overflow; }
|
||||
return std.math.U64Result::ok(a + b);
|
||||
}
|
||||
|
||||
fn checked_sub_u64(a: u64, b: u64) -> std.math.U64Result
|
||||
effects []
|
||||
{
|
||||
if b > a { return std.math.U64Result::overflow; }
|
||||
return std.math.U64Result::ok(a - b);
|
||||
}
|
||||
|
||||
fn checked_mul_u64(a: u64, b: u64) -> std.math.U64Result
|
||||
effects []
|
||||
{
|
||||
let maximum = 0u64 -% 1u64;
|
||||
if b != 0 and a > maximum / b { return std.math.U64Result::overflow; }
|
||||
return std.math.U64Result::ok(a * b);
|
||||
}
|
||||
|
||||
fn checked_add_usize(a: usize, b: usize) -> std.math.UsizeResult
|
||||
effects []
|
||||
{
|
||||
let maximum = 0usize -% 1usize;
|
||||
if b > maximum - a { return std.math.UsizeResult::overflow; }
|
||||
return std.math.UsizeResult::ok(a + b);
|
||||
}
|
||||
|
||||
fn checked_sub_usize(a: usize, b: usize) -> std.math.UsizeResult
|
||||
effects []
|
||||
{
|
||||
if b > a { return std.math.UsizeResult::overflow; }
|
||||
return std.math.UsizeResult::ok(a - b);
|
||||
}
|
||||
|
||||
fn checked_mul_usize(a: usize, b: usize) -> std.math.UsizeResult
|
||||
effects []
|
||||
{
|
||||
let maximum = 0usize -% 1usize;
|
||||
if b != 0 and a > maximum / b { return std.math.UsizeResult::overflow; }
|
||||
return std.math.UsizeResult::ok(a * b);
|
||||
}
|
||||
Reference in new issue
Block a user