summaryrefslogtreecommitdiff
path: root/src/Grapheme.zig
diff options
context:
space:
mode:
authorGravatar Jose Colon Rodriguez2024-02-18 09:20:19 -0400
committerGravatar Jose Colon Rodriguez2024-02-18 09:20:19 -0400
commitf913551d27e07f0a7c7e201ba3141fd3a6cbb47c (patch)
tree74bfe0fb0aa98d053b5ab76beec6fdc733026017 /src/Grapheme.zig
parentCode point and grapheme are now namespaces. (diff)
downloadzg-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.zig16
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 @@
1const std = @import("std"); 1const std = @import("std");
2const unicode = std.unicode; 2const unicode = std.unicode;
3 3
4const CodePoint = @import("code_point").CodePoint;
5const CodePointIterator = @import("code_point").Iterator; 4const CodePointIterator = @import("code_point").Iterator;
6const gbp = @import("gbp"); 5const 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.
20const 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.
21pub const Iterator = struct { 27pub 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 {