diff options
Diffstat (limited to 'src/CombiningData.zig')
| -rw-r--r-- | src/CombiningData.zig | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/CombiningData.zig b/src/CombiningData.zig new file mode 100644 index 0000000..95c947d --- /dev/null +++ b/src/CombiningData.zig | |||
| @@ -0,0 +1,48 @@ | |||
| 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 | s1: []u16 = undefined, | ||
| 8 | s2: []u8 = undefined, | ||
| 9 | |||
| 10 | const Self = @This(); | ||
| 11 | |||
| 12 | pub fn init(allocator: mem.Allocator) !Self { | ||
| 13 | const decompressor = compress.deflate.decompressor; | ||
| 14 | const in_bytes = @embedFile("ccc"); | ||
| 15 | var in_fbs = std.io.fixedBufferStream(in_bytes); | ||
| 16 | var in_decomp = try decompressor(allocator, in_fbs.reader(), null); | ||
| 17 | defer in_decomp.deinit(); | ||
| 18 | var reader = in_decomp.reader(); | ||
| 19 | |||
| 20 | const endian = builtin.cpu.arch.endian(); | ||
| 21 | |||
| 22 | var self = Self{ .allocator = allocator }; | ||
| 23 | |||
| 24 | const stage_1_len: u16 = try reader.readInt(u16, endian); | ||
| 25 | self.s1 = try allocator.alloc(u16, stage_1_len); | ||
| 26 | for (0..stage_1_len) |i| self.s1[i] = try reader.readInt(u16, endian); | ||
| 27 | |||
| 28 | const stage_2_len: u16 = try reader.readInt(u16, endian); | ||
| 29 | self.s2 = try allocator.alloc(u8, stage_2_len); | ||
| 30 | _ = try reader.readAll(self.s2); | ||
| 31 | |||
| 32 | return self; | ||
| 33 | } | ||
| 34 | |||
| 35 | pub fn deinit(self: *Self) void { | ||
| 36 | self.allocator.free(self.s1); | ||
| 37 | self.allocator.free(self.s2); | ||
| 38 | } | ||
| 39 | |||
| 40 | /// Returns the canonical combining class for a code point. | ||
| 41 | pub inline fn ccc(self: Self, cp: u21) u8 { | ||
| 42 | return self.s2[self.s1[cp >> 8] + (cp & 0xff)]; | ||
| 43 | } | ||
| 44 | |||
| 45 | /// True if `cp` is a starter code point, not a combining character. | ||
| 46 | pub inline fn isStarter(self: Self, cp: u21) bool { | ||
| 47 | return self.s2[self.s1[cp >> 8] + (cp & 0xff)] == 0; | ||
| 48 | } | ||