diff options
| author | Nic Gaffney <gaffney_nic@protonmail.com> | 2026-02-18 16:24:32 -0600 |
|---|---|---|
| committer | Nic Gaffney <gaffney_nic@protonmail.com> | 2026-02-18 16:24:32 -0600 |
| commit | 83889f7f2d72004414f02f88e978b62bed885bfd (patch) | |
| tree | 8b197c61e17e98b26946d97dc2286e042e7e5f8c /src/iterator.zig | |
| download | sitter-main.tar.gz | |
Diffstat (limited to 'src/iterator.zig')
| -rw-r--r-- | src/iterator.zig | 37 |
1 files changed, 37 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]); + } +} |
