diff options
| author | 2025-05-23 16:48:55 -0400 | |
|---|---|---|
| committer | 2025-05-23 16:50:01 -0400 | |
| commit | ef27c51b8e46f3909a27fd137429b717797f1fd9 (patch) | |
| tree | 182aa11043370c96942ed10f2c833c715295bc6d /src/unicode_tests.zig | |
| parent | Remove benchmarks, ziglyph references (diff) | |
| download | zg-ef27c51b8e46f3909a27fd137429b717797f1fd9.tar.gz zg-ef27c51b8e46f3909a27fd137429b717797f1fd9.tar.xz zg-ef27c51b8e46f3909a27fd137429b717797f1fd9.zip | |
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.
Diffstat (limited to 'src/unicode_tests.zig')
| -rw-r--r-- | src/unicode_tests.zig | 38 |
1 files changed, 38 insertions, 0 deletions
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" { | |||
| 287 | } else { | 287 | } else { |
| 288 | try testing.expect(false); | 288 | try testing.expect(false); |
| 289 | } | 289 | } |
| 290 | var peek_iter = wb.iterateAfter(this_str, got_word); | ||
| 291 | const peek_1 = peek_iter.next(); | ||
| 292 | if (peek_1) |p1| { | ||
| 293 | const peek_2 = iter.peek(); | ||
| 294 | if (peek_2) |p2| { | ||
| 295 | std.testing.expectEqualSlices( | ||
| 296 | u8, | ||
| 297 | p1.bytes(this_str), | ||
| 298 | p2.bytes(this_str), | ||
| 299 | ) catch |err| { | ||
| 300 | debug.print("Bad peek on line {d} #{d} offset {d}\n", .{ line_iter.line, idx + 1, idx }); | ||
| 301 | return err; | ||
| 302 | }; | ||
| 303 | } else { | ||
| 304 | try testing.expect(false); | ||
| 305 | } | ||
| 306 | } else { | ||
| 307 | try testing.expectEqual(null, iter.peek()); | ||
| 308 | } | ||
| 290 | for (got_word.offset..got_word.offset + got_word.len) |i| { | 309 | for (got_word.offset..got_word.offset + got_word.len) |i| { |
| 291 | const this_word = wb.wordAtIndex(this_str, i); | 310 | const this_word = wb.wordAtIndex(this_str, i); |
| 292 | std.testing.expectEqualSlices( | 311 | std.testing.expectEqualSlices( |
| @@ -337,6 +356,25 @@ test "Segmentation Word Iterator" { | |||
| 337 | } else { | 356 | } else { |
| 338 | try testing.expect(false); | 357 | try testing.expect(false); |
| 339 | } | 358 | } |
| 359 | var peek_iter = wb.iterateBefore(this_str, got_word); | ||
| 360 | const peek_1 = peek_iter.prev(); | ||
| 361 | if (peek_1) |p1| { | ||
| 362 | const peek_2 = r_iter.peek(); | ||
| 363 | if (peek_2) |p2| { | ||
| 364 | std.testing.expectEqualSlices( | ||
| 365 | u8, | ||
| 366 | p1.bytes(this_str), | ||
| 367 | p2.bytes(this_str), | ||
| 368 | ) catch |err| { | ||
| 369 | debug.print("Bad peek on line {d} #{d} offset {d}\n", .{ line_iter.line, idx + 1, idx }); | ||
| 370 | return err; | ||
| 371 | }; | ||
| 372 | } else { | ||
| 373 | try testing.expect(false); | ||
| 374 | } | ||
| 375 | } else { | ||
| 376 | try testing.expectEqual(null, r_iter.peek()); | ||
| 377 | } | ||
| 340 | for (got_word.offset..got_word.offset + got_word.len) |i| { | 378 | for (got_word.offset..got_word.offset + got_word.len) |i| { |
| 341 | const this_word = wb.wordAtIndex(this_str, i); | 379 | const this_word = wb.wordAtIndex(this_str, i); |
| 342 | std.testing.expectEqualSlices( | 380 | std.testing.expectEqualSlices( |