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