zig/test/cases/const_slice_child.zig
Andrew Kelley 3671582c15 syntax: functions require return type. remove ->
The purpose of this is:

 * Only one way to do things
 * Changing a function with void return type to return a possible
   error becomes a 1 character change, subtly encouraging
   people to use errors.

See #632

Here are some imperfect sed commands for performing this update:

remove arrow:

```
sed -i 's/\(\bfn\b.*\)-> /\1/g' $(find . -name "*.zig")
```

add void:

```
sed -i 's/\(\bfn\b.*\))\s*{/\1) void {/g' $(find ../ -name "*.zig")
```

Some cleanup may be necessary, but this should do the bulk of the work.
2018-01-25 04:10:11 -05:00

45 lines
976 B
Zig

const debug = @import("std").debug;
const assert = debug.assert;
var argv: &const &const u8 = undefined;
test "const slice child" {
const strs = ([]&const u8) {
c"one",
c"two",
c"three",
};
argv = &strs[0];
bar(strs.len);
}
fn foo(args: [][]const u8) void {
assert(args.len == 3);
assert(streql(args[0], "one"));
assert(streql(args[1], "two"));
assert(streql(args[2], "three"));
}
fn bar(argc: usize) void {
const args = debug.global_allocator.alloc([]const u8, argc) catch unreachable;
for (args) |_, i| {
const ptr = argv[i];
args[i] = ptr[0..strlen(ptr)];
}
foo(args);
}
fn strlen(ptr: &const u8) usize {
var count: usize = 0;
while (ptr[count] != 0) : (count += 1) {}
return count;
}
fn streql(a: []const u8, b: []const u8) bool {
if (a.len != b.len) return false;
for (a) |item, index| {
if (b[index] != item) return false;
}
return true;
}