summaryrefslogtreecommitdiff
path: root/src/CompatData.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/CompatData.zig')
-rw-r--r--src/CompatData.zig50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/CompatData.zig b/src/CompatData.zig
new file mode 100644
index 0000000..a1f5de6
--- /dev/null
+++ b/src/CompatData.zig
@@ -0,0 +1,50 @@
1const std = @import("std");
2const builtin = @import("builtin");
3const compress = std.compress;
4const mem = std.mem;
5
6allocator: mem.Allocator,
7nfkd: [][]u21 = undefined,
8
9const Self = @This();
10
11pub fn init(allocator: mem.Allocator) !Self {
12 const decompressor = compress.deflate.decompressor;
13 const in_bytes = @embedFile("compat");
14 var in_fbs = std.io.fixedBufferStream(in_bytes);
15 var in_decomp = try decompressor(allocator, in_fbs.reader(), null);
16 defer in_decomp.deinit();
17 var reader = in_decomp.reader();
18
19 const endian = builtin.cpu.arch.endian();
20 var self = Self{
21 .allocator = allocator,
22 .nfkd = try allocator.alloc([]u21, 0x110000),
23 };
24
25 for (0..0x110000) |i| self.nfkd[i] = &.{};
26
27 while (true) {
28 const len: u8 = try reader.readInt(u8, endian);
29 if (len == 0) break;
30 const cp = try reader.readInt(u24, endian);
31 self.nfkd[cp] = try allocator.alloc(u21, len - 1);
32 for (0..len - 1) |i| {
33 self.nfkd[cp][i] = @intCast(try reader.readInt(u24, endian));
34 }
35 }
36
37 return self;
38}
39
40pub fn deinit(self: *Self) void {
41 for (self.nfkd) |slice| {
42 if (slice.len != 0) self.allocator.free(slice);
43 }
44 self.allocator.free(self.nfkd);
45}
46
47/// Returns compatibility decomposition for `cp`.
48pub inline fn toNfkd(self: Self, cp: u21) []u21 {
49 return self.nfkd[cp];
50}