summaryrefslogtreecommitdiff
path: root/src/HangulData.zig
diff options
context:
space:
mode:
authorGravatar Sam Atman2025-04-30 12:58:26 -0400
committerGravatar Sam Atman2025-04-30 13:01:37 -0400
commit3c2c30bfbe861c6c48acd8d7507886787197a788 (patch)
tree875ba35c1954b201207452b18a189ebd70c0b596 /src/HangulData.zig
parentgrapheme now Graphemes, Data files gone (diff)
downloadzg-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.zig42
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 @@
1const std = @import("std"); 1//! Hangul Data
2const builtin = @import("builtin");
3const compress = std.compress;
4const mem = std.mem;
5const testing = std.testing;
6 2
7pub const Syllable = enum { 3pub const Syllable = enum {
8 none, 4 none,
@@ -16,9 +12,9 @@ pub const Syllable = enum {
16s1: []u16 = undefined, 12s1: []u16 = undefined,
17s2: []u3 = undefined, 13s2: []u3 = undefined,
18 14
19const Self = @This(); 15const Hangul = @This();
20 16
21pub fn init(allocator: mem.Allocator) !Self { 17pub 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
44pub fn deinit(self: *const Self, allocator: mem.Allocator) void { 40pub 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`.
50pub fn syllable(self: Self, cp: u21) Syllable { 46pub 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
50const std = @import("std");
51const builtin = @import("builtin");
52const compress = std.compress;
53const mem = std.mem;
54const testing = std.testing;