From 32c68059a05dde8a57a330db6d14a32506081516 Mon Sep 17 00:00:00 2001 From: Jose Colon Rodriguez Date: Tue, 27 Feb 2024 09:26:40 -0400 Subject: Using HangulData in NormData --- src/HangulData.zig | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/NormData.zig | 4 ++++ src/Normalizer.zig | 23 +++++++++++------------ 3 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 src/HangulData.zig (limited to 'src') diff --git a/src/HangulData.zig b/src/HangulData.zig new file mode 100644 index 0000000..4d80c99 --- /dev/null +++ b/src/HangulData.zig @@ -0,0 +1,52 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const compress = std.compress; +const mem = std.mem; +const testing = std.testing; + +pub const Syllable = enum { + none, + L, + LV, + LVT, + V, + T, +}; + +allocator: mem.Allocator, +s1: []u16 = undefined, +s2: []Syllable = undefined, + +const Self = @This(); + +pub fn init(allocator: mem.Allocator) !Self { + const decompressor = compress.deflate.decompressor; + const in_bytes = @embedFile("hangul"); + var in_fbs = std.io.fixedBufferStream(in_bytes); + var in_decomp = try decompressor(allocator, in_fbs.reader(), null); + defer in_decomp.deinit(); + var reader = in_decomp.reader(); + + const endian = builtin.cpu.arch.endian(); + var self = Self{ .allocator = allocator }; + + const stage_1_len: u16 = try reader.readInt(u16, endian); + self.s1 = try allocator.alloc(u16, stage_1_len); + for (0..stage_1_len) |i| self.s1[i] = try reader.readInt(u16, endian); + + const stage_2_len: u16 = try reader.readInt(u16, endian); + self.s2 = try allocator.alloc(Syllable, stage_2_len); + for (0..stage_2_len) |i| self.s2[i] = @enumFromInt(try reader.readInt(u8, endian)); + + return self; +} + +pub fn deinit(self: *Self) void { + self.allocator.free(self.s1); + self.allocator.free(self.s2); +} + +/// Returns the Hangul syllable type for `cp`. +pub inline fn syllable(self: Self, cp: u21) Syllable { + return self.s2[self.s1[cp >> 8] + (cp & 0xff)]; +} diff --git a/src/NormData.zig b/src/NormData.zig index 83110f0..8923382 100644 --- a/src/NormData.zig +++ b/src/NormData.zig @@ -4,10 +4,12 @@ const mem = std.mem; const CanonData = @import("CanonData"); const CccData = @import("CombiningData"); const CompatData = @import("CompatData"); +const HangulData = @import("HangulData"); canon_data: CanonData, ccc_data: CccData, compat_data: CompatData, +hangul_data: HangulData, const Self = @This(); @@ -16,6 +18,7 @@ pub fn init(allocator: std.mem.Allocator) !Self { .canon_data = try CanonData.init(allocator), .ccc_data = try CccData.init(allocator), .compat_data = try CompatData.init(allocator), + .hangul_data = try HangulData.init(allocator), }; } @@ -23,4 +26,5 @@ pub fn deinit(self: *Self) void { self.canon_data.deinit(); self.ccc_data.deinit(); self.compat_data.deinit(); + self.hangul_data.deinit(); } diff --git a/src/Normalizer.zig b/src/Normalizer.zig index 1434043..0670cae 100644 --- a/src/Normalizer.zig +++ b/src/Normalizer.zig @@ -7,7 +7,6 @@ const testing = std.testing; const CodePointIterator = @import("code_point").Iterator; const case_fold_map = @import("ziglyph").case_folding; -const hangul_map = @import("ziglyph").hangul; const norm_props = @import("ziglyph").normalization_props; pub const NormData = @import("NormData"); @@ -17,9 +16,9 @@ norm_data: *NormData, const Self = @This(); // Hangul processing utilities. -fn isHangulPrecomposed(cp: u21) bool { - if (hangul_map.syllableType(cp)) |kind| return kind == .LV or kind == .LVT; - return false; +fn isHangulPrecomposed(self: Self, cp: u21) bool { + const kind = self.norm_data.hangul_data.syllable(cp); + return kind == .LV or kind == .LVT; } const SBase: u21 = 0xAC00; @@ -117,7 +116,7 @@ pub fn decompose(self: Self, cp: u21, form: Form) Decomp { } // Hangul precomposed syllable full decomposition. - if (isHangulPrecomposed(cp)) { + if (self.isHangulPrecomposed(cp)) { const cps = decomposeHangul(cp); @memcpy(dc.cps[0..cps.len], &cps); return dc; @@ -335,12 +334,12 @@ test "nfkd !ASCII / alloc" { // Composition utilities. -fn isHangul(cp: u21) bool { - return cp >= 0x1100 and hangul_map.syllableType(cp) != null; +fn isHangul(self: Self, cp: u21) bool { + return cp >= 0x1100 and self.norm_data.hangul_data.syllable(cp) != .none; } fn isNonHangulStarter(self: Self, cp: u21) bool { - return !isHangul(cp) and self.norm_data.ccc_data.isStarter(cp); + return !self.isHangul(cp) and self.norm_data.ccc_data.isStarter(cp); } /// Normalizes `str` to NFC. @@ -395,7 +394,7 @@ fn nfxc(self: Self, allocator: std.mem.Allocator, str: []const u8, form: Form) ! for (d_list.items[(j + 1)..i]) |B| { const cc_B = self.norm_data.ccc_data.ccc(B); // Check for blocking conditions. - if (isHangul(C)) { + if (self.isHangul(C)) { if (cc_B != 0 or self.isNonHangulStarter(B)) continue :block_check; } if (cc_B >= cc_C) continue :block_check; @@ -414,9 +413,9 @@ fn nfxc(self: Self, allocator: std.mem.Allocator, str: []const u8, form: Form) ! const L = d_list.items[sidx]; var processed_hangul = false; - if (isHangul(L) and isHangul(C)) { - const l_stype = hangul_map.syllableType(L).?; - const c_stype = hangul_map.syllableType(C).?; + if (self.isHangul(L) and self.isHangul(C)) { + const l_stype = self.norm_data.hangul_data.syllable(L); + const c_stype = self.norm_data.hangul_data.syllable(C); if (l_stype == .LV and c_stype == .T) { // LV, T -- cgit v1.2.3