From 7d8f330db2bfb625a054eb7e21d397ff696c0b3f Mon Sep 17 00:00:00 2001 From: Jose Colon Rodriguez Date: Mon, 26 Feb 2024 18:54:11 -0400 Subject: Using NormData nfc and nfd --- src/Canonical.zig | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/Canonical.zig') 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; const mem = std.mem; allocator: mem.Allocator, +nfc: std.AutoHashMap([2]u21, u21), nfd: [][2]u21 = undefined, const Self = @This(); @@ -19,6 +20,7 @@ pub fn init(allocator: mem.Allocator) !Self { const endian = builtin.cpu.arch.endian(); var self = Self{ .allocator = allocator, + .nfc = std.AutoHashMap([2]u21, u21).init(allocator), .nfd = try allocator.alloc([2]u21, 0x110000), }; @@ -29,13 +31,17 @@ pub fn init(allocator: mem.Allocator) !Self { if (len == 0) break; const cp = try reader.readInt(u24, endian); self.nfd[cp][0] = @intCast(try reader.readInt(u24, endian)); - if (len == 3) self.nfd[cp][1] = @intCast(try reader.readInt(u24, endian)); + if (len == 3) { + self.nfd[cp][1] = @intCast(try reader.readInt(u24, endian)); + try self.nfc.put(self.nfd[cp], @intCast(cp)); + } } return self; } pub fn deinit(self: *Self) void { + self.nfc.deinit(); self.allocator.free(self.nfd); } @@ -43,3 +49,8 @@ pub fn deinit(self: *Self) void { pub inline fn toNfd(self: Self, cp: u21) [2]u21 { return self.nfd[cp]; } + +// Returns the primary composite for the codepoints in `cp`. +pub inline fn toNfc(self: Self, cps: [2]u21) ?u21 { + return self.nfc.get(cps); +} -- cgit v1.2.3