From: Brendan Hansen Date: Wed, 8 Mar 2023 23:05:19 +0000 (-0600) Subject: changed: iterator type of `utf8.runes` X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=6fa8a70bf0804a3cc0ad670752f8b5655e78c708;p=onyx.git changed: iterator type of `utf8.runes` --- diff --git a/core/encoding/utf8.onyx b/core/encoding/utf8.onyx index 2bf037ff..57fcc1f0 100644 --- a/core/encoding/utf8.onyx +++ b/core/encoding/utf8.onyx @@ -134,20 +134,27 @@ rune_is_start :: (b: u8) -> bool { return rune_length_from_first_byte(b) > 1; } -runes :: (s: str) -> Iterator(rune) { + +RuneIterValue :: struct { + rune: i32; + bytes: [] u8; +} + +runes :: (s: str) -> Iterator(RuneIterValue) { return iter.generator( &.{ s = s }, ctx => { - if string.empty(ctx.s) do return 0, false; + if string.empty(ctx.s) do return RuneIterValue.{}, false; r, len := decode_rune(ctx.s); - string.advance(&ctx.s, len); + defer string.advance(&ctx.s, len); - return r, true; + return .{ r, ctx.s[0..len] }, true; } ); } + slice :: (s: str, low, high: i32) -> str { advanced := advance_rune(s, low); tmp := advanced; diff --git a/tests/utf8_test.onyx b/tests/utf8_test.onyx index 1449b686..bc1f6ed3 100644 --- a/tests/utf8_test.onyx +++ b/tests/utf8_test.onyx @@ -11,8 +11,7 @@ main :: () { utf8.advance_rune(&tmp, 6); for utf8.runes(tmp) { - buf: [4] u8; - printf("{} ", utf8.encode_rune(buf, it)); + printf("{} ", it.bytes); } print("\n");