diff options
Diffstat (limited to 'src/WidthData.zig')
| -rw-r--r-- | src/WidthData.zig | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/src/WidthData.zig b/src/WidthData.zig index b07a679..ca7eaf0 100644 --- a/src/WidthData.zig +++ b/src/WidthData.zig | |||
| @@ -4,15 +4,36 @@ const compress = std.compress; | |||
| 4 | const mem = std.mem; | 4 | const mem = std.mem; |
| 5 | const testing = std.testing; | 5 | const testing = std.testing; |
| 6 | 6 | ||
| 7 | const GraphemeData = @import("GraphemeData"); | 7 | const Graphemes = @import("Graphemes"); |
| 8 | 8 | ||
| 9 | g_data: GraphemeData, | 9 | g_data: Graphemes, |
| 10 | s1: []u16 = undefined, | 10 | s1: []u16 = undefined, |
| 11 | s2: []i4 = undefined, | 11 | s2: []i4 = undefined, |
| 12 | owns_gdata: bool, | ||
| 12 | 13 | ||
| 13 | const Self = @This(); | 14 | const Self = @This(); |
| 14 | 15 | ||
| 15 | pub fn init(allocator: mem.Allocator) mem.Allocator.Error!Self { | 16 | pub fn init(allocator: mem.Allocator) mem.Allocator.Error!Self { |
| 17 | var self: Self = try Self.setup(allocator); | ||
| 18 | errdefer { | ||
| 19 | allocator.free(self.s1); | ||
| 20 | allocator.free(self.s2); | ||
| 21 | } | ||
| 22 | self.owns_gdata = true; | ||
| 23 | self.g_data = try Graphemes.init(allocator); | ||
| 24 | errdefer self.g_data.deinit(allocator); | ||
| 25 | return self; | ||
| 26 | } | ||
| 27 | |||
| 28 | pub fn initWithGraphemeData(allocator: mem.Allocator, g_data: Graphemes) mem.Allocator.Error!Self { | ||
| 29 | var self = try Self.setup(allocator); | ||
| 30 | self.g_data = g_data; | ||
| 31 | self.owns_gdata = false; | ||
| 32 | return self; | ||
| 33 | } | ||
| 34 | |||
| 35 | // Sets up the DisplayWidthData, leaving the GraphemeData undefined. | ||
| 36 | fn setup(allocator: mem.Allocator) mem.Allocator.Error!Self { | ||
| 16 | const decompressor = compress.flate.inflate.decompressor; | 37 | const decompressor = compress.flate.inflate.decompressor; |
| 17 | const in_bytes = @embedFile("dwp"); | 38 | const in_bytes = @embedFile("dwp"); |
| 18 | var in_fbs = std.io.fixedBufferStream(in_bytes); | 39 | var in_fbs = std.io.fixedBufferStream(in_bytes); |
| @@ -21,10 +42,7 @@ pub fn init(allocator: mem.Allocator) mem.Allocator.Error!Self { | |||
| 21 | 42 | ||
| 22 | const endian = builtin.cpu.arch.endian(); | 43 | const endian = builtin.cpu.arch.endian(); |
| 23 | 44 | ||
| 24 | var self = Self{ | 45 | var self: Self = undefined; |
| 25 | .g_data = try GraphemeData.init(allocator), | ||
| 26 | }; | ||
| 27 | errdefer self.g_data.deinit(allocator); | ||
| 28 | 46 | ||
| 29 | const stage_1_len: u16 = reader.readInt(u16, endian) catch unreachable; | 47 | const stage_1_len: u16 = reader.readInt(u16, endian) catch unreachable; |
| 30 | self.s1 = try allocator.alloc(u16, stage_1_len); | 48 | self.s1 = try allocator.alloc(u16, stage_1_len); |
| @@ -42,7 +60,7 @@ pub fn init(allocator: mem.Allocator) mem.Allocator.Error!Self { | |||
| 42 | pub fn deinit(self: *const Self, allocator: mem.Allocator) void { | 60 | pub fn deinit(self: *const Self, allocator: mem.Allocator) void { |
| 43 | allocator.free(self.s1); | 61 | allocator.free(self.s1); |
| 44 | allocator.free(self.s2); | 62 | allocator.free(self.s2); |
| 45 | self.g_data.deinit(allocator); | 63 | if (self.owns_gdata) self.g_data.deinit(allocator); |
| 46 | } | 64 | } |
| 47 | 65 | ||
| 48 | /// codePointWidth returns the number of cells `cp` requires when rendered | 66 | /// codePointWidth returns the number of cells `cp` requires when rendered |