From ad306724ae574b1a22abbcb6de37e65a69db82e4 Mon Sep 17 00:00:00 2001 From: Jose Colon Rodriguez Date: Mon, 26 Feb 2024 20:22:41 -0400 Subject: Using NormData nfkd --- src/CombiningData.zig | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/CombiningData.zig (limited to 'src/CombiningData.zig') diff --git a/src/CombiningData.zig b/src/CombiningData.zig new file mode 100644 index 0000000..95c947d --- /dev/null +++ b/src/CombiningData.zig @@ -0,0 +1,48 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const compress = std.compress; +const mem = std.mem; + +allocator: mem.Allocator, +s1: []u16 = undefined, +s2: []u8 = undefined, + +const Self = @This(); + +pub fn init(allocator: mem.Allocator) !Self { + const decompressor = compress.deflate.decompressor; + const in_bytes = @embedFile("ccc"); + 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(u8, stage_2_len); + _ = try reader.readAll(self.s2); + + return self; +} + +pub fn deinit(self: *Self) void { + self.allocator.free(self.s1); + self.allocator.free(self.s2); +} + +/// Returns the canonical combining class for a code point. +pub inline fn ccc(self: Self, cp: u21) u8 { + return self.s2[self.s1[cp >> 8] + (cp & 0xff)]; +} + +/// True if `cp` is a starter code point, not a combining character. +pub inline fn isStarter(self: Self, cp: u21) bool { + return self.s2[self.s1[cp >> 8] + (cp & 0xff)] == 0; +} -- cgit v1.2.3