summaryrefslogtreecommitdiff
path: root/src/Canonical.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/Canonical.zig')
-rw-r--r--src/Canonical.zig13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/Canonical.zig b/src/Canonical.zig
index d54e828..81d3eec 100644
--- a/src/Canonical.zig
+++ b/src/Canonical.zig
@@ -4,6 +4,7 @@ const compress = std.compress;
4const mem = std.mem; 4const mem = std.mem;
5 5
6allocator: mem.Allocator, 6allocator: mem.Allocator,
7nfc: std.AutoHashMap([2]u21, u21),
7nfd: [][2]u21 = undefined, 8nfd: [][2]u21 = undefined,
8 9
9const Self = @This(); 10const Self = @This();
@@ -19,6 +20,7 @@ pub fn init(allocator: mem.Allocator) !Self {
19 const endian = builtin.cpu.arch.endian(); 20 const endian = builtin.cpu.arch.endian();
20 var self = Self{ 21 var self = Self{
21 .allocator = allocator, 22 .allocator = allocator,
23 .nfc = std.AutoHashMap([2]u21, u21).init(allocator),
22 .nfd = try allocator.alloc([2]u21, 0x110000), 24 .nfd = try allocator.alloc([2]u21, 0x110000),
23 }; 25 };
24 26
@@ -29,13 +31,17 @@ pub fn init(allocator: mem.Allocator) !Self {
29 if (len == 0) break; 31 if (len == 0) break;
30 const cp = try reader.readInt(u24, endian); 32 const cp = try reader.readInt(u24, endian);
31 self.nfd[cp][0] = @intCast(try reader.readInt(u24, endian)); 33 self.nfd[cp][0] = @intCast(try reader.readInt(u24, endian));
32 if (len == 3) self.nfd[cp][1] = @intCast(try reader.readInt(u24, endian)); 34 if (len == 3) {
35 self.nfd[cp][1] = @intCast(try reader.readInt(u24, endian));
36 try self.nfc.put(self.nfd[cp], @intCast(cp));
37 }
33 } 38 }
34 39
35 return self; 40 return self;
36} 41}
37 42
38pub fn deinit(self: *Self) void { 43pub fn deinit(self: *Self) void {
44 self.nfc.deinit();
39 self.allocator.free(self.nfd); 45 self.allocator.free(self.nfd);
40} 46}
41 47
@@ -43,3 +49,8 @@ pub fn deinit(self: *Self) void {
43pub inline fn toNfd(self: Self, cp: u21) [2]u21 { 49pub inline fn toNfd(self: Self, cp: u21) [2]u21 {
44 return self.nfd[cp]; 50 return self.nfd[cp];
45} 51}
52
53// Returns the primary composite for the codepoints in `cp`.
54pub inline fn toNfc(self: Self, cps: [2]u21) ?u21 {
55 return self.nfc.get(cps);
56}