diff options
| author | 2025-04-30 12:58:26 -0400 | |
|---|---|---|
| committer | 2025-04-30 13:01:37 -0400 | |
| commit | 3c2c30bfbe861c6c48acd8d7507886787197a788 (patch) | |
| tree | 875ba35c1954b201207452b18a189ebd70c0b596 /src/CompatData.zig | |
| parent | grapheme now Graphemes, Data files gone (diff) | |
| download | zg-3c2c30bfbe861c6c48acd8d7507886787197a788.tar.gz zg-3c2c30bfbe861c6c48acd8d7507886787197a788.tar.xz zg-3c2c30bfbe861c6c48acd8d7507886787197a788.zip | |
Merge NormData with Normalize
Diffstat (limited to 'src/CompatData.zig')
| -rw-r--r-- | src/CompatData.zig | 36 |
1 files changed, 19 insertions, 17 deletions
diff --git a/src/CompatData.zig b/src/CompatData.zig index ac08048..d787103 100644 --- a/src/CompatData.zig +++ b/src/CompatData.zig | |||
| @@ -1,13 +1,10 @@ | |||
| 1 | const std = @import("std"); | 1 | //! Compatibility Data |
| 2 | const builtin = @import("builtin"); | ||
| 3 | const compress = std.compress; | ||
| 4 | const mem = std.mem; | ||
| 5 | 2 | ||
| 6 | nfkd: [][]u21 = undefined, | 3 | nfkd: [][]u21 = undefined, |
| 7 | 4 | ||
| 8 | const Self = @This(); | 5 | const CompatData = @This(); |
| 9 | 6 | ||
| 10 | pub fn init(allocator: mem.Allocator) !Self { | 7 | pub fn init(allocator: mem.Allocator) !CompatData { |
| 11 | const decompressor = compress.flate.inflate.decompressor; | 8 | const decompressor = compress.flate.inflate.decompressor; |
| 12 | const in_bytes = @embedFile("compat"); | 9 | const in_bytes = @embedFile("compat"); |
| 13 | var in_fbs = std.io.fixedBufferStream(in_bytes); | 10 | var in_fbs = std.io.fixedBufferStream(in_bytes); |
| @@ -15,34 +12,39 @@ pub fn init(allocator: mem.Allocator) !Self { | |||
| 15 | var reader = in_decomp.reader(); | 12 | var reader = in_decomp.reader(); |
| 16 | 13 | ||
| 17 | const endian = builtin.cpu.arch.endian(); | 14 | const endian = builtin.cpu.arch.endian(); |
| 18 | var self = Self{ | 15 | var cpdata = CompatData{ |
| 19 | .nfkd = try allocator.alloc([]u21, 0x110000), | 16 | .nfkd = try allocator.alloc([]u21, 0x110000), |
| 20 | }; | 17 | }; |
| 21 | errdefer self.deinit(allocator); | 18 | errdefer cpdata.deinit(allocator); |
| 22 | 19 | ||
| 23 | @memset(self.nfkd, &.{}); | 20 | @memset(cpdata.nfkd, &.{}); |
| 24 | 21 | ||
| 25 | while (true) { | 22 | while (true) { |
| 26 | const len: u8 = try reader.readInt(u8, endian); | 23 | const len: u8 = try reader.readInt(u8, endian); |
| 27 | if (len == 0) break; | 24 | if (len == 0) break; |
| 28 | const cp = try reader.readInt(u24, endian); | 25 | const cp = try reader.readInt(u24, endian); |
| 29 | self.nfkd[cp] = try allocator.alloc(u21, len - 1); | 26 | cpdata.nfkd[cp] = try allocator.alloc(u21, len - 1); |
| 30 | for (0..len - 1) |i| { | 27 | for (0..len - 1) |i| { |
| 31 | self.nfkd[cp][i] = @intCast(try reader.readInt(u24, endian)); | 28 | cpdata.nfkd[cp][i] = @intCast(try reader.readInt(u24, endian)); |
| 32 | } | 29 | } |
| 33 | } | 30 | } |
| 34 | 31 | ||
| 35 | return self; | 32 | return cpdata; |
| 36 | } | 33 | } |
| 37 | 34 | ||
| 38 | pub fn deinit(self: *const Self, allocator: mem.Allocator) void { | 35 | pub fn deinit(cpdata: *const CompatData, allocator: mem.Allocator) void { |
| 39 | for (self.nfkd) |slice| { | 36 | for (cpdata.nfkd) |slice| { |
| 40 | if (slice.len != 0) allocator.free(slice); | 37 | if (slice.len != 0) allocator.free(slice); |
| 41 | } | 38 | } |
| 42 | allocator.free(self.nfkd); | 39 | allocator.free(cpdata.nfkd); |
| 43 | } | 40 | } |
| 44 | 41 | ||
| 45 | /// Returns compatibility decomposition for `cp`. | 42 | /// Returns compatibility decomposition for `cp`. |
| 46 | pub fn toNfkd(self: Self, cp: u21) []u21 { | 43 | pub fn toNfkd(cpdata: *const CompatData, cp: u21) []u21 { |
| 47 | return self.nfkd[cp]; | 44 | return cpdata.nfkd[cp]; |
| 48 | } | 45 | } |
| 46 | |||
| 47 | const std = @import("std"); | ||
| 48 | const builtin = @import("builtin"); | ||
| 49 | const compress = std.compress; | ||
| 50 | const mem = std.mem; | ||