blob: 920091a7e5b333c993eb6944bce44876d4e90280 (
plain)
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
35
36
37
|
const std = @import("std");
const iter = @This();
pub fn Iterator(comptime T: type) type {
return struct{
const Self = @This();
items: []const T,
index: usize = 0,
pub fn next(self: *Self) ?T {
if (self.empty()) return null;
defer self.index += 1;
return self.items[self.index];
}
pub fn current(self: Self) ?T {
if (self.empty()) return null;
return self.items[self.index];
}
inline fn empty(self: Self) bool {
if (self.items.len < 1) return true;
return false;
}
};
}
const t = std.testing;
test "initialize iterator" {
var iterator = Iterator(u8){.items = "Hello World!"};
for ("Hello World!", 0..) |c,i| {
try t.expect(c == iterator.current().?);
try t.expect(c == iterator.next().?);
try t.expect(c == iterator.items[i]);
}
}
|