From 83889f7f2d72004414f02f88e978b62bed885bfd Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Wed, 18 Feb 2026 16:24:32 -0600 Subject: intitial commit --- src/iterator.zig | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/iterator.zig (limited to 'src/iterator.zig') 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]); + } +} -- cgit v1.2.3