diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/iterator.zig | 37 | ||||
| -rw-r--r-- | src/root.zig | 1 |
2 files changed, 38 insertions, 0 deletions
diff --git a/src/iterator.zig b/src/iterator.zig new file mode 100644 index 0000000..920091a --- /dev/null +++ b/src/iterator.zig @@ -0,0 +1,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]); + } +} diff --git a/src/root.zig b/src/root.zig new file mode 100644 index 0000000..10b8720 --- /dev/null +++ b/src/root.zig @@ -0,0 +1 @@ +const iter = @import("iterator.zig"); |
