diff options
| author | 2024-02-18 09:20:19 -0400 | |
|---|---|---|
| committer | 2024-02-18 09:20:19 -0400 | |
| commit | f913551d27e07f0a7c7e201ba3141fd3a6cbb47c (patch) | |
| tree | 74bfe0fb0aa98d053b5ab76beec6fdc733026017 /src/Grapheme.zig | |
| parent | Code point and grapheme are now namespaces. (diff) | |
| download | zg-f913551d27e07f0a7c7e201ba3141fd3a6cbb47c.tar.gz zg-f913551d27e07f0a7c7e201ba3141fd3a6cbb47c.tar.xz zg-f913551d27e07f0a7c7e201ba3141fd3a6cbb47c.zip | |
Code point code is now a method not a field.
Diffstat (limited to 'src/Grapheme.zig')
| -rw-r--r-- | src/Grapheme.zig | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/src/Grapheme.zig b/src/Grapheme.zig index f013aba..6981753 100644 --- a/src/Grapheme.zig +++ b/src/Grapheme.zig | |||
| @@ -1,7 +1,6 @@ | |||
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const unicode = std.unicode; | 2 | const unicode = std.unicode; |
| 3 | 3 | ||
| 4 | const CodePoint = @import("code_point").CodePoint; | ||
| 5 | const CodePointIterator = @import("code_point").Iterator; | 4 | const CodePointIterator = @import("code_point").Iterator; |
| 6 | const gbp = @import("gbp"); | 5 | const gbp = @import("gbp"); |
| 7 | 6 | ||
| @@ -17,6 +16,13 @@ pub const Grapheme = struct { | |||
| 17 | } | 16 | } |
| 18 | }; | 17 | }; |
| 19 | 18 | ||
| 19 | // We need the code as a u21. | ||
| 20 | const CodePoint = struct { | ||
| 21 | code: u21, | ||
| 22 | len: u3, | ||
| 23 | offset: u32, | ||
| 24 | }; | ||
| 25 | |||
| 20 | /// `Iterator` iterates a sting of UTF-8 encoded bytes one grapheme cluster at-a-time. | 26 | /// `Iterator` iterates a sting of UTF-8 encoded bytes one grapheme cluster at-a-time. |
| 21 | pub const Iterator = struct { | 27 | pub const Iterator = struct { |
| 22 | buf: [2]?CodePoint = .{ null, null }, | 28 | buf: [2]?CodePoint = .{ null, null }, |
| @@ -33,7 +39,13 @@ pub const Iterator = struct { | |||
| 33 | 39 | ||
| 34 | fn advance(self: *Self) void { | 40 | fn advance(self: *Self) void { |
| 35 | self.buf[0] = self.buf[1]; | 41 | self.buf[0] = self.buf[1]; |
| 36 | self.buf[1] = self.cp_iter.next(); | 42 | |
| 43 | const maybe_cp = self.cp_iter.next(); | ||
| 44 | self.buf[1] = if (maybe_cp) |cp| .{ | ||
| 45 | .code = cp.code(self.cp_iter.bytes), | ||
| 46 | .len = cp.len, | ||
| 47 | .offset = cp.offset, | ||
| 48 | } else null; | ||
| 37 | } | 49 | } |
| 38 | 50 | ||
| 39 | pub fn next(self: *Self) ?Grapheme { | 51 | pub fn next(self: *Self) ?Grapheme { |