2017-04-03 15:11:57 -07:00
|
|
|
const debug = @import("std").debug;
|
|
|
|
const assert = debug.assert;
|
2017-01-05 00:57:48 -08:00
|
|
|
|
2018-06-03 22:09:15 -07:00
|
|
|
var argv: [*]const [*]const u8 = undefined;
|
2016-09-15 11:07:35 -07:00
|
|
|
|
2017-05-23 18:38:31 -07:00
|
|
|
test "const slice child" {
|
2018-06-03 22:09:15 -07:00
|
|
|
const strs = ([][*]const u8){
|
2016-09-15 11:07:35 -07:00
|
|
|
c"one",
|
|
|
|
c"two",
|
|
|
|
c"three",
|
|
|
|
};
|
2018-06-03 22:09:15 -07:00
|
|
|
// TODO this should implicitly cast
|
|
|
|
argv = @ptrCast([*]const [*]const u8, &strs);
|
2016-09-15 11:07:35 -07:00
|
|
|
bar(strs.len);
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn foo(args: [][]const u8) void {
|
2016-09-15 11:07:35 -07:00
|
|
|
assert(args.len == 3);
|
|
|
|
assert(streql(args[0], "one"));
|
|
|
|
assert(streql(args[1], "two"));
|
|
|
|
assert(streql(args[2], "three"));
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn bar(argc: usize) void {
|
2018-01-08 21:07:01 -08:00
|
|
|
const args = debug.global_allocator.alloc([]const u8, argc) catch unreachable;
|
2016-09-15 11:07:35 -07:00
|
|
|
for (args) |_, i| {
|
|
|
|
const ptr = argv[i];
|
2017-05-19 07:39:59 -07:00
|
|
|
args[i] = ptr[0..strlen(ptr)];
|
2016-09-15 11:07:35 -07:00
|
|
|
}
|
|
|
|
foo(args);
|
|
|
|
}
|
|
|
|
|
2018-06-03 22:09:15 -07:00
|
|
|
fn strlen(ptr: [*]const u8) usize {
|
2016-09-15 11:07:35 -07:00
|
|
|
var count: usize = 0;
|
2017-05-03 15:12:07 -07:00
|
|
|
while (ptr[count] != 0) : (count += 1) {}
|
2016-09-15 11:07:35 -07:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn streql(a: []const u8, b: []const u8) bool {
|
2016-09-15 11:07:35 -07:00
|
|
|
if (a.len != b.len) return false;
|
|
|
|
for (a) |item, index| {
|
|
|
|
if (b[index] != item) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|