blob: 6a424e3e232ac773d89ec06e63f2aaa9f76f4cbf (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
/// An example of what methods should be implemented on an arg iterator.
pub const ExampleArgIterator = struct {
pub fn next(iter: *ExampleArgIterator) ?[]const u8 {
_ = iter;
return "2";
}
};
/// An argument iterator which iterates over a slice of arguments.
/// This implementation does not allocate.
pub const SliceIterator = struct {
args: []const []const u8,
index: usize = 0,
pub fn next(iter: *SliceIterator) ?[]const u8 {
if (iter.args.len <= iter.index)
return null;
defer iter.index += 1;
return iter.args[iter.index];
}
};
test "SliceIterator" {
const args = [_][]const u8{ "A", "BB", "CCC" };
var iter = SliceIterator{ .args = &args };
for (args) |a|
try std.testing.expectEqualStrings(a, iter.next().?);
try std.testing.expectEqual(@as(?[]const u8, null), iter.next());
}
const std = @import("std");
|