2019-02-08 23:18:47 +00:00
|
|
|
const std = @import("std");
|
|
|
|
const debug = std.debug;
|
2020-01-29 21:30:13 +00:00
|
|
|
const testing = std.testing;
|
|
|
|
const expect = testing.expect;
|
2017-01-05 08:57:48 +00:00
|
|
|
|
2018-06-04 06:09:15 +01:00
|
|
|
var argv: [*]const [*]const u8 = undefined;
|
2016-09-15 19:07:35 +01:00
|
|
|
|
2017-05-24 02:38:31 +01:00
|
|
|
test "const slice child" {
|
2021-10-03 04:15:03 +01:00
|
|
|
const strs = [_][*]const u8{ "one", "two", "three" };
|
2019-11-20 01:29:08 +00:00
|
|
|
argv = &strs;
|
2021-05-04 19:23:22 +01:00
|
|
|
try bar(strs.len);
|
2016-09-15 19:07:35 +01:00
|
|
|
}
|
|
|
|
|
2021-05-04 19:23:22 +01:00
|
|
|
fn foo(args: [][]const u8) !void {
|
|
|
|
try expect(args.len == 3);
|
|
|
|
try expect(streql(args[0], "one"));
|
|
|
|
try expect(streql(args[1], "two"));
|
|
|
|
try expect(streql(args[2], "three"));
|
2016-09-15 19:07:35 +01:00
|
|
|
}
|
|
|
|
|
2021-05-04 19:23:22 +01:00
|
|
|
fn bar(argc: usize) !void {
|
2020-01-30 03:22:01 +00:00
|
|
|
const args = testing.allocator.alloc([]const u8, argc) catch unreachable;
|
|
|
|
defer testing.allocator.free(args);
|
2016-09-15 19:07:35 +01:00
|
|
|
for (args) |_, i| {
|
|
|
|
const ptr = argv[i];
|
2017-05-19 15:39:59 +01:00
|
|
|
args[i] = ptr[0..strlen(ptr)];
|
2016-09-15 19:07:35 +01:00
|
|
|
}
|
2021-05-04 19:23:22 +01:00
|
|
|
try foo(args);
|
2016-09-15 19:07:35 +01:00
|
|
|
}
|
|
|
|
|
2018-06-04 06:09:15 +01:00
|
|
|
fn strlen(ptr: [*]const u8) usize {
|
2016-09-15 19:07:35 +01:00
|
|
|
var count: usize = 0;
|
2017-05-03 23:12:07 +01:00
|
|
|
while (ptr[count] != 0) : (count += 1) {}
|
2016-09-15 19:07:35 +01:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn streql(a: []const u8, b: []const u8) bool {
|
2016-09-15 19:07:35 +01:00
|
|
|
if (a.len != b.len) return false;
|
|
|
|
for (a) |item, index| {
|
|
|
|
if (b[index] != item) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|