From ef27c51b8e46f3909a27fd137429b717797f1fd9 Mon Sep 17 00:00:00 2001 From: Sam Atman Date: Fri, 23 May 2025 16:48:55 -0400 Subject: Add iterateBefore and iterateAfter These create reverse or forward iterators before or after a Word. So this way, the user can get the word at an index, then iterate forward or back from that word. Also: Fixes #59 Which was fixed awhile back, but I don't feel like doing repo surgery to tag the fix where it happened. We have blame for that kind of thing. --- src/unicode_tests.zig | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/unicode_tests.zig') diff --git a/src/unicode_tests.zig b/src/unicode_tests.zig index 18f1814..195fdcb 100644 --- a/src/unicode_tests.zig +++ b/src/unicode_tests.zig @@ -287,6 +287,25 @@ test "Segmentation Word Iterator" { } else { try testing.expect(false); } + var peek_iter = wb.iterateAfter(this_str, got_word); + const peek_1 = peek_iter.next(); + if (peek_1) |p1| { + const peek_2 = iter.peek(); + if (peek_2) |p2| { + std.testing.expectEqualSlices( + u8, + p1.bytes(this_str), + p2.bytes(this_str), + ) catch |err| { + debug.print("Bad peek on line {d} #{d} offset {d}\n", .{ line_iter.line, idx + 1, idx }); + return err; + }; + } else { + try testing.expect(false); + } + } else { + try testing.expectEqual(null, iter.peek()); + } for (got_word.offset..got_word.offset + got_word.len) |i| { const this_word = wb.wordAtIndex(this_str, i); std.testing.expectEqualSlices( @@ -337,6 +356,25 @@ test "Segmentation Word Iterator" { } else { try testing.expect(false); } + var peek_iter = wb.iterateBefore(this_str, got_word); + const peek_1 = peek_iter.prev(); + if (peek_1) |p1| { + const peek_2 = r_iter.peek(); + if (peek_2) |p2| { + std.testing.expectEqualSlices( + u8, + p1.bytes(this_str), + p2.bytes(this_str), + ) catch |err| { + debug.print("Bad peek on line {d} #{d} offset {d}\n", .{ line_iter.line, idx + 1, idx }); + return err; + }; + } else { + try testing.expect(false); + } + } else { + try testing.expectEqual(null, r_iter.peek()); + } for (got_word.offset..got_word.offset + got_word.len) |i| { const this_word = wb.wordAtIndex(this_str, i); std.testing.expectEqualSlices( -- cgit v1.2.3