diff options
| author | 2025-04-30 12:58:26 -0400 | |
|---|---|---|
| committer | 2025-04-30 13:01:37 -0400 | |
| commit | 3c2c30bfbe861c6c48acd8d7507886787197a788 (patch) | |
| tree | 875ba35c1954b201207452b18a189ebd70c0b596 /src/HangulData.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/HangulData.zig')
| -rw-r--r-- | src/HangulData.zig | 42 |
1 files changed, 22 insertions, 20 deletions
diff --git a/src/HangulData.zig b/src/HangulData.zig index 4bccbe6..8c5f3ad 100644 --- a/src/HangulData.zig +++ b/src/HangulData.zig | |||
| @@ -1,8 +1,4 @@ | |||
| 1 | const std = @import("std"); | 1 | //! Hangul Data |
| 2 | const builtin = @import("builtin"); | ||
| 3 | const compress = std.compress; | ||
| 4 | const mem = std.mem; | ||
| 5 | const testing = std.testing; | ||
| 6 | 2 | ||
| 7 | pub const Syllable = enum { | 3 | pub const Syllable = enum { |
| 8 | none, | 4 | none, |
| @@ -16,9 +12,9 @@ pub const Syllable = enum { | |||
| 16 | s1: []u16 = undefined, | 12 | s1: []u16 = undefined, |
| 17 | s2: []u3 = undefined, | 13 | s2: []u3 = undefined, |
| 18 | 14 | ||
| 19 | const Self = @This(); | 15 | const Hangul = @This(); |
| 20 | 16 | ||
| 21 | pub fn init(allocator: mem.Allocator) !Self { | 17 | pub fn init(allocator: mem.Allocator) !Hangul { |
| 22 | const decompressor = compress.flate.inflate.decompressor; | 18 | const decompressor = compress.flate.inflate.decompressor; |
| 23 | const in_bytes = @embedFile("hangul"); | 19 | const in_bytes = @embedFile("hangul"); |
| 24 | var in_fbs = std.io.fixedBufferStream(in_bytes); | 20 | var in_fbs = std.io.fixedBufferStream(in_bytes); |
| @@ -26,27 +22,33 @@ pub fn init(allocator: mem.Allocator) !Self { | |||
| 26 | var reader = in_decomp.reader(); | 22 | var reader = in_decomp.reader(); |
| 27 | 23 | ||
| 28 | const endian = builtin.cpu.arch.endian(); | 24 | const endian = builtin.cpu.arch.endian(); |
| 29 | var self = Self{}; | 25 | var hangul = Hangul{}; |
| 30 | 26 | ||
| 31 | const stage_1_len: u16 = try reader.readInt(u16, endian); | 27 | const stage_1_len: u16 = try reader.readInt(u16, endian); |
| 32 | self.s1 = try allocator.alloc(u16, stage_1_len); | 28 | hangul.s1 = try allocator.alloc(u16, stage_1_len); |
| 33 | errdefer allocator.free(self.s1); | 29 | errdefer allocator.free(hangul.s1); |
| 34 | for (0..stage_1_len) |i| self.s1[i] = try reader.readInt(u16, endian); | 30 | for (0..stage_1_len) |i| hangul.s1[i] = try reader.readInt(u16, endian); |
| 35 | 31 | ||
| 36 | const stage_2_len: u16 = try reader.readInt(u16, endian); | 32 | const stage_2_len: u16 = try reader.readInt(u16, endian); |
| 37 | self.s2 = try allocator.alloc(u3, stage_2_len); | 33 | hangul.s2 = try allocator.alloc(u3, stage_2_len); |
| 38 | errdefer allocator.free(self.s2); | 34 | errdefer allocator.free(hangul.s2); |
| 39 | for (0..stage_2_len) |i| self.s2[i] = @intCast(try reader.readInt(u8, endian)); | 35 | for (0..stage_2_len) |i| hangul.s2[i] = @intCast(try reader.readInt(u8, endian)); |
| 40 | 36 | ||
| 41 | return self; | 37 | return hangul; |
| 42 | } | 38 | } |
| 43 | 39 | ||
| 44 | pub fn deinit(self: *const Self, allocator: mem.Allocator) void { | 40 | pub fn deinit(hangul: *const Hangul, allocator: mem.Allocator) void { |
| 45 | allocator.free(self.s1); | 41 | allocator.free(hangul.s1); |
| 46 | allocator.free(self.s2); | 42 | allocator.free(hangul.s2); |
| 47 | } | 43 | } |
| 48 | 44 | ||
| 49 | /// Returns the Hangul syllable type for `cp`. | 45 | /// Returns the Hangul syllable type for `cp`. |
| 50 | pub fn syllable(self: Self, cp: u21) Syllable { | 46 | pub fn syllable(hangul: *const Hangul, cp: u21) Syllable { |
| 51 | return @enumFromInt(self.s2[self.s1[cp >> 8] + (cp & 0xff)]); | 47 | return @enumFromInt(hangul.s2[hangul.s1[cp >> 8] + (cp & 0xff)]); |
| 52 | } | 48 | } |
| 49 | |||
| 50 | const std = @import("std"); | ||
| 51 | const builtin = @import("builtin"); | ||
| 52 | const compress = std.compress; | ||
| 53 | const mem = std.mem; | ||
| 54 | const testing = std.testing; | ||