diff options
| author | 2024-02-27 09:26:40 -0400 | |
|---|---|---|
| committer | 2024-02-27 09:26:40 -0400 | |
| commit | 32c68059a05dde8a57a330db6d14a32506081516 (patch) | |
| tree | c2b3b9bbbf48330db3570135d371cb92b552f1cb /src/HangulData.zig | |
| parent | Using NormData nfkd (diff) | |
| download | zg-32c68059a05dde8a57a330db6d14a32506081516.tar.gz zg-32c68059a05dde8a57a330db6d14a32506081516.tar.xz zg-32c68059a05dde8a57a330db6d14a32506081516.zip | |
Using HangulData in NormData
Diffstat (limited to 'src/HangulData.zig')
| -rw-r--r-- | src/HangulData.zig | 52 |
1 files changed, 52 insertions, 0 deletions
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 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const builtin = @import("builtin"); | ||
| 3 | const compress = std.compress; | ||
| 4 | const mem = std.mem; | ||
| 5 | const testing = std.testing; | ||
| 6 | |||
| 7 | pub const Syllable = enum { | ||
| 8 | none, | ||
| 9 | L, | ||
| 10 | LV, | ||
| 11 | LVT, | ||
| 12 | V, | ||
| 13 | T, | ||
| 14 | }; | ||
| 15 | |||
| 16 | allocator: mem.Allocator, | ||
| 17 | s1: []u16 = undefined, | ||
| 18 | s2: []Syllable = undefined, | ||
| 19 | |||
| 20 | const Self = @This(); | ||
| 21 | |||
| 22 | pub fn init(allocator: mem.Allocator) !Self { | ||
| 23 | const decompressor = compress.deflate.decompressor; | ||
| 24 | const in_bytes = @embedFile("hangul"); | ||
| 25 | var in_fbs = std.io.fixedBufferStream(in_bytes); | ||
| 26 | var in_decomp = try decompressor(allocator, in_fbs.reader(), null); | ||
| 27 | defer in_decomp.deinit(); | ||
| 28 | var reader = in_decomp.reader(); | ||
| 29 | |||
| 30 | const endian = builtin.cpu.arch.endian(); | ||
| 31 | var self = Self{ .allocator = allocator }; | ||
| 32 | |||
| 33 | const stage_1_len: u16 = try reader.readInt(u16, endian); | ||
| 34 | self.s1 = try allocator.alloc(u16, stage_1_len); | ||
| 35 | for (0..stage_1_len) |i| self.s1[i] = try reader.readInt(u16, endian); | ||
| 36 | |||
| 37 | const stage_2_len: u16 = try reader.readInt(u16, endian); | ||
| 38 | self.s2 = try allocator.alloc(Syllable, stage_2_len); | ||
| 39 | for (0..stage_2_len) |i| self.s2[i] = @enumFromInt(try reader.readInt(u8, endian)); | ||
| 40 | |||
| 41 | return self; | ||
| 42 | } | ||
| 43 | |||
| 44 | pub fn deinit(self: *Self) void { | ||
| 45 | self.allocator.free(self.s1); | ||
| 46 | self.allocator.free(self.s2); | ||
| 47 | } | ||
| 48 | |||
| 49 | /// Returns the Hangul syllable type for `cp`. | ||
| 50 | pub inline fn syllable(self: Self, cp: u21) Syllable { | ||
| 51 | return self.s2[self.s1[cp >> 8] + (cp & 0xff)]; | ||
| 52 | } | ||