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

+4 -2
View File
@@ -1,6 +1,6 @@
# Aji standard library
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.
Implementation in ordinary safe `.aji`: bytes, math, UTF-8, hex and Base64, with 35 functions and 13 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
@@ -23,6 +23,8 @@ Endian APIs read/write u16/u32/u64 in LE/BE order. Reads return `std.bytes.ReadU
`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.
`std.utf8.next/validate` use Unicode scalar values and explicit byte offsets. `std.encoding.hex` and `std.encoding.base64` encode/decode into caller-provided buffers; malformed input and short capacity are enum results, with no destination mutation on returned errors. Hex emits lowercase and accepts mixed case. Base64 is canonical, padded and uses the standard alphabet. The [text example](../examples/std/text.aji) returns 8364 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) · [Bounded operations RFC](../docs/rfcs/0018-bounded-standard-operations.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) · [Text RFC](../docs/rfcs/0019-standard-text-encodings.md)
+76
View File
@@ -0,0 +1,76 @@
module std.encoding.base64
enum EncodeResult { ok(usize), short_output }
enum DecodeResult { ok(usize), invalid_input, short_output }
fn encode(dst: slice_mut<u8>, src: slice<u8>) -> std.encoding.base64.EncodeResult
effects []
{
let groups = src.len / 3;
let rest = src.len % 3;
if groups > dst.len / 4 { return std.encoding.base64.EncodeResult::short_output; }
let full = groups * 4;
if rest != 0 and dst.len - full < 4 { return std.encoding.base64.EncodeResult::short_output; }
let count = full + if rest == 0 { 0usize } else { 4usize };
let alphabet = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var i: usize = 0;
var out: usize = 0;
while src.len - i >= 3 {
let a = src[i]; let b = src[i + 1]; let c = src[i + 2];
dst[out] = alphabet[(a >> 2) as! usize];
dst[out + 1] = alphabet[(((a & 3) << 4) | (b >> 4)) as! usize];
dst[out + 2] = alphabet[(((b & 15) << 2) | (c >> 6)) as! usize];
dst[out + 3] = alphabet[(c & 63) as! usize];
i = i + 3; out = out + 4;
}
if rest != 0 {
let a = src[i];
let b: u8 = if rest == 2 { src[i + 1] } else { 0u8 };
dst[out] = alphabet[(a >> 2) as! usize];
dst[out + 1] = alphabet[(((a & 3) << 4) | (b >> 4)) as! usize];
dst[out + 2] = if rest == 2 { alphabet[((b & 15) << 2) as! usize] } else { 61u8 };
dst[out + 3] = 61;
}
return std.encoding.base64.EncodeResult::ok(count);
}
fn decode(dst: slice_mut<u8>, src: slice<u8>) -> std.encoding.base64.DecodeResult
effects []
{
if src.len % 4 != 0 { return std.encoding.base64.DecodeResult::invalid_input; }
if src.len == 0 { return std.encoding.base64.DecodeResult::ok(0); }
let alphabet = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var values: [u8; 256] = [255; 256];
for i in 0..alphabet.len { values[alphabet[i] as! usize] = i as! u8; }
var padding: usize = 0;
if src[src.len - 1] == 61 {
padding = 1;
if src[src.len - 2] == 61 { padding = 2; }
}
for i in 0..src.len - padding {
if values[src[i] as! usize] == 255 { return std.encoding.base64.DecodeResult::invalid_input; }
}
if padding == 2 and (values[src[src.len - 3] as! usize] & 15) != 0 {
return std.encoding.base64.DecodeResult::invalid_input;
}
if padding == 1 and (values[src[src.len - 2] as! usize] & 3) != 0 {
return std.encoding.base64.DecodeResult::invalid_input;
}
let groups = src.len / 4;
let count = groups * 3 - padding;
if count > dst.len { return std.encoding.base64.DecodeResult::short_output; }
for group in 0..groups {
let i = group * 4; let out = group * 3;
let a = values[src[i] as! usize]; let b = values[src[i + 1] as! usize];
dst[out] = (a << 2) | (b >> 4);
if out + 1 < count {
let c = values[src[i + 2] as! usize];
dst[out + 1] = (b << 4) | (c >> 2);
if out + 2 < count {
let d = values[src[i + 3] as! usize];
dst[out + 2] = (c << 6) | d;
}
}
}
return std.encoding.base64.DecodeResult::ok(count);
}
+41
View File
@@ -0,0 +1,41 @@
module std.encoding.hex
enum EncodeResult { ok(usize), short_output }
enum DecodeResult { ok(usize), invalid_input, short_output }
fn encode(dst: slice_mut<u8>, src: slice<u8>) -> std.encoding.hex.EncodeResult
effects []
{
if src.len > dst.len / 2 { return std.encoding.hex.EncodeResult::short_output; }
let count = src.len * 2;
let alphabet = b"0123456789abcdef";
for i in 0..src.len {
let byte = src[i];
dst[i * 2] = alphabet[(byte >> 4) as! usize];
dst[i * 2 + 1] = alphabet[(byte & 15) as! usize];
}
return std.encoding.hex.EncodeResult::ok(count);
}
fn decode(dst: slice_mut<u8>, src: slice<u8>) -> std.encoding.hex.DecodeResult
effects []
{
if src.len % 2 != 0 { return std.encoding.hex.DecodeResult::invalid_input; }
var digits: [u8; 256] = [255; 256];
for value in 0u8..10u8 { digits[(value + 48) as! usize] = value; }
for value in 0u8..6u8 {
digits[(value + 65) as! usize] = value + 10;
digits[(value + 97) as! usize] = value + 10;
}
for i in 0..src.len {
if digits[src[i] as! usize] == 255 { return std.encoding.hex.DecodeResult::invalid_input; }
}
let count = src.len / 2;
if count > dst.len { return std.encoding.hex.DecodeResult::short_output; }
for i in 0..count {
let high = digits[src[i * 2] as! usize];
let low = digits[src[i * 2 + 1] as! usize];
dst[i] = (high << 4) | low;
}
return std.encoding.hex.DecodeResult::ok(count);
}
+49
View File
@@ -0,0 +1,49 @@
module std.utf8
enum NextResult { ok(u32, usize), end, invalid_input }
enum ValidateResult { ok, invalid_input(usize) }
fn next(src: slice<u8>) -> std.utf8.NextResult
effects []
{
if src.len == 0 { return std.utf8.NextResult::end; }
let lead = src[0];
if lead < 128 { return std.utf8.NextResult::ok(lead as u32, 1); }
var count: usize = 0;
var value: u32 = 0;
var lower: u8 = 128;
var upper: u8 = 191;
if lead >= 194 and lead <= 223 {
count = 2; value = (lead & 31) as u32;
} else if lead >= 224 and lead <= 239 {
count = 3; value = (lead & 15) as u32;
if lead == 224 { lower = 160; }
if lead == 237 { upper = 159; }
} else if lead >= 240 and lead <= 244 {
count = 4; value = (lead & 7) as u32;
if lead == 240 { lower = 144; }
if lead == 244 { upper = 143; }
} else { return std.utf8.NextResult::invalid_input; }
if src.len < count { return std.utf8.NextResult::invalid_input; }
if src[1] < lower or src[1] > upper { return std.utf8.NextResult::invalid_input; }
for i in 1usize..count {
let byte = src[i];
if byte < 128 or byte > 191 { return std.utf8.NextResult::invalid_input; }
value = (value << 6) | ((byte & 63) as u32);
}
return std.utf8.NextResult::ok(value, count);
}
fn validate(src: slice<u8>) -> std.utf8.ValidateResult
effects []
{
var offset: usize = 0;
while offset < src.len {
match std.utf8.next(ref src[offset..src.len]) {
std.utf8.NextResult::ok(value, count) => { offset = offset + count; }
std.utf8.NextResult::end => { return std.utf8.ValidateResult::ok; }
std.utf8.NextResult::invalid_input => { return std.utf8.ValidateResult::invalid_input(offset); }
}
}
return std.utf8.ValidateResult::ok;
}