diff options
| author | 2024-02-26 20:22:41 -0400 | |
|---|---|---|
| committer | 2024-02-26 20:22:41 -0400 | |
| commit | ad306724ae574b1a22abbcb6de37e65a69db82e4 (patch) | |
| tree | 4b78d89006a2524b63c0ca7530e4c56a111d6eca /src/CompatData.zig | |
| parent | Using NormData nfc and nfd (diff) | |
| download | zg-ad306724ae574b1a22abbcb6de37e65a69db82e4.tar.gz zg-ad306724ae574b1a22abbcb6de37e65a69db82e4.tar.xz zg-ad306724ae574b1a22abbcb6de37e65a69db82e4.zip | |
Using NormData nfkd
Diffstat (limited to 'src/CompatData.zig')
| -rw-r--r-- | src/CompatData.zig | 50 |
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 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const builtin = @import("builtin"); | ||
| 3 | const compress = std.compress; | ||
| 4 | const mem = std.mem; | ||
| 5 | |||
| 6 | allocator: mem.Allocator, | ||
| 7 | nfkd: [][]u21 = undefined, | ||
| 8 | |||
| 9 | const Self = @This(); | ||
| 10 | |||
| 11 | pub 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 | |||
| 40 | pub 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`. | ||
| 48 | pub inline fn toNfkd(self: Self, cp: u21) []u21 { | ||
| 49 | return self.nfkd[cp]; | ||
| 50 | } | ||