refactor: index preprocessed process arguments directly

This commit is contained in:
2026-10-23 12:07:23 +02:00
parent c1e03632ed
commit 0821b84bc3
5 changed files with 10 additions and 54 deletions
-12
View File
@@ -1,12 +0,0 @@
argument_count proc(args Args) usize {
return args.values.len
}
#! returns a borrowed, zero-terminated slice, valid while the supplied argument storage lives.
argument proc(args Args, index usize) ?[;0]u8 {
if (index >= argument_count(args)) return null
pointer :: args.values[index]
length := usize(0)
while (pointer[length] != 0) length += 1
return pointer[..length;0]
}
-27
View File
@@ -1,27 +0,0 @@
import "@std/testing"
import "@std/mem"
borrows_supplied_arguments test {
first [3;0]mut u8 := ['r', 'u', 'n']
empty [0;0]u8 := []
spaced [9;0]u8 := ['t', 'w', 'o', ' ', 'w', 'o', 'r', 'd', 's']
pointers [3][*;0]u8 :: [(&first).ptr, (&empty).ptr, (&spaced).ptr]
args :: Args{ values = pointers[..] }
try testing.expect_equal(usize(3), argument_count(args))
if (argument(args, 0)) |value| {
first[0] = 'f'
try testing.expect(mem.eql(value, "fun"))
} else { try testing.expect(false) }
if (argument(args, 1)) |value| {
try testing.expect_equal(usize(0), value.len)
} else { try testing.expect(false) }
if (argument(args, 2)) |value| {
try testing.expect(mem.eql(value, "two words"))
} else { try testing.expect(false) }
try testing.expect(argument(args, 3) == null)
try testing.expect(argument(args, maxval!(usize)) == null)
none :: Args{ values = pointers[0..0] }
try testing.expect_equal(usize(0), argument_count(none))
try testing.expect(argument(none, 0) == null)
}
+2 -2
View File
@@ -5,7 +5,7 @@ Init :: struct {
args Args
}
#! borrows the argument pointer array and its zero-terminated strings; owns no storage.
#! borrows argument slices and their zero-terminated strings, valid until main returns.
Args :: struct {
values [][*;0]u8
values [][;0]u8
}