cli: add safe UTF-8 and supplied-buffer hex/base64 std modules

This commit is contained in:
dev committed 2026-10-06 17:06:39 +03:00
1 parent 6397a317e2
commit 3bb11e03b2
42 files changed
+2807 -14

No files matched your search

+53
View File
@@ -112,3 +112,56 @@ effects []
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.
## UTF-8 and encodings
| Function | Result and contract |
|---|---|
| `std.utf8.next(src: slice<u8>) -> std.utf8.NextResult` | `ok(scalar: u32, consumed: usize)` for the first scalar, `end` for empty input, or `invalid_input` for a bad/truncated first sequence; trailing bytes do not affect a successful first decode. |
| `std.utf8.validate(src: slice<u8>) -> std.utf8.ValidateResult` | `ok` for wholly valid bytes, including empty input; `invalid_input(offset)` at the beginning of the first bad/truncated sequence. |
| `std.encoding.hex.encode(dst: slice_mut<u8>, src: slice<u8>) -> std.encoding.hex.EncodeResult` | lowercase ASCII hex; `ok(count)` or `short_output`. |
| `std.encoding.hex.decode(dst: slice_mut<u8>, src: slice<u8>) -> std.encoding.hex.DecodeResult` | even-length ASCII digits, upper/lower/mixed case; `ok(count)`, `invalid_input` or `short_output`. |
| `std.encoding.base64.encode(dst: slice_mut<u8>, src: slice<u8>) -> std.encoding.base64.EncodeResult` | standard alphabet with canonical padding and zero unused bits; `ok(count)` or `short_output`. |
| `std.encoding.base64.decode(dst: slice_mut<u8>, src: slice<u8>) -> std.encoding.base64.DecodeResult` | canonical standard Base64 only; `ok(count)`, `invalid_input` or `short_output`. |
UTF-8 uses shortest scalar encodings, excluding surrogates and values above U+10FFFF. NUL, BOM, noncharacters and unassigned scalars are accepted; validation does not normalize or replace bytes. Iteration repeatedly calls `next(ref data[offset..data.len])` and advances offset by the returned consumed count. `next` validates its first sequence only, so validate the whole slice when that is the required contract. A malformed final prefix is `invalid_input`, not a streaming "need more" result. [UTF-8 rules: RFC 3629 §3/§4](https://www.rfc-editor.org/rfc/rfc3629.html#section-3).
Encoders check complete capacity before writing; decoders check the entire input first, then capacity. Invalid input takes precedence over short output. Every returned error preserves the whole destination. Success writes a prefix, preserves remaining output and appends no terminator. Empty encoding/decoding succeeds with `ok(0)`, including zero capacity. Mathematical lengths too large for usize/output are `short_output`, checked without overflowing an intermediate. Input/output cannot overlap; disjoint `split_at` siblings from the same allocation are allowed. These guarantees do not cover interpreter fuel interruption.
Hex decoding rejects whitespace, prefixes, separators, odd length and non-hex bytes. Base64 uses the standard `+/` alphabet; incomplete final groups require `=` padding, and complete groups omit it. Decoding rejects whitespace, URL-safe symbols, incomplete groups, misplaced/excess padding and nonzero unused bits. This is a deliberate strict policy permitted by [RFC 4648 §3.5/§4](https://www.rfc-editor.org/rfc/rfc4648.html#section-3.5); no permissive or URL-safe mode is implied.
```aji
module demo.text
import std.bytes;
import std.encoding.hex;
import std.encoding.base64;
import std.utf8;
fn main() -> u32
effects []
{
var utf8: [u8; 3] = zero;
match std.encoding.hex.decode(mut utf8, b"E282AC") {
std.encoding.hex.DecodeResult::ok(count) => { assert(count == 3); }
std.encoding.hex.DecodeResult::invalid_input => { return 0; }
std.encoding.hex.DecodeResult::short_output => { return 0; }
}
match std.utf8.validate(ref utf8) {
std.utf8.ValidateResult::ok => {}
std.utf8.ValidateResult::invalid_input(offset) => { return 0; }
}
var encoded: [u8; 4] = zero;
match std.encoding.base64.encode(mut encoded, ref utf8) {
std.encoding.base64.EncodeResult::ok(count) => { assert(count == 4); }
std.encoding.base64.EncodeResult::short_output => { return 0; }
}
assert(std.bytes.equal(ref encoded, b"4oKs"));
match std.utf8.next(ref utf8) {
std.utf8.NextResult::ok(scalar, count) => { assert(count == 3); return scalar; }
std.utf8.NextResult::end => { return 0; }
std.utf8.NextResult::invalid_input => { return 0; }
}
}
```
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.