cli: add caller-buffer lexical path APIs

Add pure std.path queries and failure-preserving normalization/join using
safe reverse measurement/emission and caller-provided output storage.
Component queries return explicit Copy ranges; filesystem authority is
checked independently. Complete the STD-7 implementation gate.

Validation: 1079 conformance cases, 18 path cases with per-step collection
and 12-seed schedule stress, independent 132-path/49-join capacity oracle,
68 CLI checks, fourteen previous source/std CLI suites, unit/Quick Reference,
formatting, hosted smoke, 117/117 source coverage and reference budgets.
This commit is contained in:
dev committed 2026-10-07 13:41:45 +03:00
1 parent 8d8bef8f3e
commit 8edec5b831
35 files changed
+1189 -8

No files matched your search

+15 -2
View File
@@ -1,6 +1,17 @@
# Aji standard library
Implementation in ordinary safe `.aji`: bytes, math, UTF-8, hex, Base64 and integer formatting, with 39 monomorphic functions, 14 monomorphic result enums and option/result templates with two Copy classification helpers. They are embedded with the compiler and compiled through the normal frontend/verifier when imported. They are library modules, not standalone `main` programs.
Nineteen compiler-pinned modules implemented in ordinary safe `.aji`. Imports compile through the normal frontend and verifier. Modules are embedded with the compiler; an installed compiler needs no runtime std search path.
| Modules | Purpose |
|---|---|
| `std.math`, `std.bytes`, `std.hash` | checked arithmetic, byte operations, endian reads/writes, non-cryptographic hashes |
| `std.utf8`, `std.encoding.hex`, `std.encoding.base64`, `std.fmt` | validation, caller-buffer encoding and decimal formatting |
| `std.option`, `std.result` | explicit generic result values |
| `std.vec`, `std.deque`, `std.map`, `std.hashmap`, `std.hashset`, `std.heap` | owning collections with explicit allocation, growth and draining |
| `std.io`, `std.io.reader`, `std.io.writer` | bounded I/O and owning buffered file adapters |
| `std.path` | pure lexical paths and caller-buffer normalization/join |
Owning collections support resources such as files and channel endpoints. Allocation requires explicit authority; resources must be consumed explicitly. Importing a module grants no capabilities. STD-0..7 is the intermediate foundation; remaining Std 0.1 system APIs and integration examples are tracked in the [plan](../docs/plan/standard-library.md).
```aji
module example
@@ -41,4 +52,6 @@ STD-5 supports whole-value owning structs/enums/fixed arrays and individual own<
`std.deque` provides eleven generic functions for a bounded owning ring, including push/pop at both ends. Allocation is explicit in new/reserve; failed growth, full push and nonempty free preserve the queue. Slots hold Option<T> and include its tag in the charge. Users drain/consume their elements before free. [Deque example](../examples/std/deque.aji) and [RFC 0025](../docs/rfcs/0025-owning-deque.md).
`std.hash` provides FNV-1a 64 and Castagnoli CRC-32C, with one fresh and one continuation function per algorithm. It reads byte slices with effects [], no allocation or authority; numeric results and chunking are deterministic. These are non-cryptographic algorithms and hashes never establish key equality. [Hash example](../examples/std/hash.aji) returns 42 without grants. The next owning map contract is a [proposal](../docs/design/hash-map.md), not an available std module.
`std.hash` provides FNV-1a 64 and Castagnoli CRC-32C, with one fresh and one continuation function per algorithm. It reads byte slices with effects [], no allocation or authority; numeric results and chunking are deterministic. These are non-cryptographic algorithms and hashes never establish key equality. [Hash example](../examples/std/hash.aji) returns 42 without grants. `std.map` supports numeric keys; `std.hashmap` and `std.hashset` use explicit static hash/equality operations, and `std.heap` uses an explicit static order. Complete ownership, failure and cleanup contracts are in the [API reference](../docs/reference/std.md).
`std.io` composes exact/bounded reads and limited copies into caller storage. `std.io.reader`/`std.io.writer` own a file and a fixed buffer, with explicit finish/discard; a failed writer preserves its first error and never retries. `std.path` uses slash-separated bytes in every profile; lexical success grants no filesystem authority. [Path example](../examples/std/path.aji) returns 42 without grants.
+128
View File
@@ -0,0 +1,128 @@
module std.path
struct Range { start: usize, end: usize }
enum WriteResult { ok(usize), invalid_input, size_overflow, short_output }
fn is_absolute(path: slice<u8>) -> bool effects [] {
return path.len != 0usize and path[0] == 47u8;
}
fn basename(path: slice<u8>) -> Range effects [] {
var end: usize = path.len;
while end != 0usize and path[end - 1usize] == 47u8 { end = end - 1usize; }
if end == 0usize { return Range { start: 0usize, end: 0usize }; }
var start: usize = end;
while start != 0usize and path[start - 1usize] != 47u8 { start = start - 1usize; }
return Range { start: start, end: end };
}
fn parent(path: slice<u8>) -> Range effects [] {
let component = basename(path); var end: usize = component.start;
while end != 0usize and path[end - 1usize] == 47u8 { end = end - 1usize; }
if end == 0usize and is_absolute(path) { end = 1usize; }
return Range { start: 0usize, end: end };
}
fn extension(path: slice<u8>) -> Range effects [] {
let component = basename(path); var cursor: usize = component.end;
while cursor > component.start {
cursor = cursor - 1usize;
if path[cursor] == 46u8 {
if cursor == component.start or cursor + 1usize == component.end { break; }
return Range { start: cursor + 1usize, end: component.end };
}
}
return Range { start: component.end, end: component.end };
}
// Implementation state for reverse measurement/emission; not an additional
// supported path API. No borrow is stored in it.
struct Measurement { pending: usize, length: usize, count: usize, overflow: bool }
struct Emission { pending: usize, cursor: usize }
fn valid(path: slice<u8>) -> bool effects [] {
for i in 0usize..path.len { if path[i] == 0u8 { return false; } }
return true;
}
fn measure(path: slice<u8>,initial: Measurement) -> Measurement effects [] {
if initial.overflow { return initial; }
var pending: usize = initial.pending; var length: usize = initial.length;
var count: usize = initial.count; var cursor: usize = path.len;
while cursor != 0usize {
while cursor != 0usize and path[cursor - 1usize] == 47u8 { cursor = cursor - 1usize; }
let end = cursor;
while cursor != 0usize and path[cursor - 1usize] != 47u8 { cursor = cursor - 1usize; }
let width = end - cursor;
if width == 0usize { break; }
if width == 1usize and path[cursor] == 46u8 { continue; }
if width == 2usize and path[cursor] == 46u8 and path[cursor + 1usize] == 46u8 {
if pending == 18446744073709551615usize { return Measurement { pending: pending, length: length, count: count, overflow: true }; }
pending = pending + 1usize; continue;
}
if pending != 0usize { pending = pending - 1usize; continue; }
if width > 18446744073709551615usize - length { return Measurement { pending: pending, length: length, count: count, overflow: true }; }
length = length + width;
if count != 0usize {
if length == 18446744073709551615usize { return Measurement { pending: pending, length: length, count: count, overflow: true }; }
length = length + 1usize;
}
count = count + 1usize;
}
return Measurement { pending: pending, length: length, count: count, overflow: false };
}
fn emit(destination: slice_mut<u8>,path: slice<u8>,initial: Emission) -> Emission effects [] {
var pending: usize = initial.pending; var output: usize = initial.cursor; var cursor: usize = path.len;
while cursor != 0usize {
while cursor != 0usize and path[cursor - 1usize] == 47u8 { cursor = cursor - 1usize; }
let end = cursor;
while cursor != 0usize and path[cursor - 1usize] != 47u8 { cursor = cursor - 1usize; }
let width = end - cursor;
if width == 0usize { break; }
if width == 1usize and path[cursor] == 46u8 { continue; }
if width == 2usize and path[cursor] == 46u8 and path[cursor + 1usize] == 46u8 { pending = pending + 1usize; continue; }
if pending != 0usize { pending = pending - 1usize; continue; }
assert(width <= output); output = output - width;
for i in 0usize..width { destination[output + i] = path[cursor + i]; }
if output != 0usize { output = output - 1usize; destination[output] = 47u8; }
}
return Emission { pending: pending, cursor: output };
}
fn join(destination: slice_mut<u8>,base: slice<u8>,child: slice<u8>) -> WriteResult effects [] {
if not valid(base) or not valid(child) { return WriteResult::invalid_input; }
let replace = is_absolute(child); let rooted = replace or is_absolute(base);
var measured: Measurement = Measurement { pending: 0usize, length: 0usize, count: 0usize, overflow: false };
measured = measure(child,measured); if not replace { measured = measure(base,measured); }
if measured.overflow { return WriteResult::size_overflow; }
var length: usize = measured.length; var count: usize = measured.count;
if rooted {
if length == 18446744073709551615usize { return WriteResult::size_overflow; } length = length + 1usize;
} else {
for i in 0usize..measured.pending {
let added = if count == 0usize { 2usize } else { 3usize };
if added > 18446744073709551615usize - length { return WriteResult::size_overflow; }
length = length + added; count = count + 1usize;
}
if length == 0usize { length = 1usize; }
}
if length > destination.len { return WriteResult::short_output; }
var emitted: Emission = Emission { pending: 0usize, cursor: length };
emitted = emit(destination,child,emitted); if not replace { emitted = emit(destination,base,emitted); }
if rooted {
if measured.count == 0usize { destination[0usize] = 47u8; } else { assert(emitted.cursor == 0usize); }
} else {
var cursor: usize = emitted.cursor;
for i in 0usize..emitted.pending {
assert(cursor >= 2usize); cursor = cursor - 2usize; destination[cursor] = 46u8; destination[cursor + 1usize] = 46u8;
if cursor != 0usize { cursor = cursor - 1usize; destination[cursor] = 47u8; }
}
if count == 0usize { destination[0usize] = 46u8; } else { assert(cursor == 0usize); }
}
return WriteResult::ok(length);
}
fn normalize(destination: slice_mut<u8>,path: slice<u8>) -> WriteResult effects [] {
let blank: [u8;1] = [0u8;1]; return join(destination,ref blank[0usize..0usize],path);
}