diff options
Diffstat (limited to 'src/Graphemes.zig')
| -rw-r--r-- | src/Graphemes.zig | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/src/Graphemes.zig b/src/Graphemes.zig index 79cd2c6..7bf328a 100644 --- a/src/Graphemes.zig +++ b/src/Graphemes.zig | |||
| @@ -14,7 +14,13 @@ s3: []u8 = undefined, | |||
| 14 | 14 | ||
| 15 | const Graphemes = @This(); | 15 | const Graphemes = @This(); |
| 16 | 16 | ||
| 17 | pub inline fn init(allocator: mem.Allocator) mem.Allocator.Error!Graphemes { | 17 | pub fn init(allocator: Allocator) Allocator.Error!Graphemes { |
| 18 | var graphemes = Graphemes{}; | ||
| 19 | try graphemes.setup(allocator); | ||
| 20 | return graphemes; | ||
| 21 | } | ||
| 22 | |||
| 23 | pub fn setup(graphemes: *Graphemes, allocator: Allocator) Allocator.Error!void { | ||
| 18 | const decompressor = compress.flate.inflate.decompressor; | 24 | const decompressor = compress.flate.inflate.decompressor; |
| 19 | const in_bytes = @embedFile("gbp"); | 25 | const in_bytes = @embedFile("gbp"); |
| 20 | var in_fbs = std.io.fixedBufferStream(in_bytes); | 26 | var in_fbs = std.io.fixedBufferStream(in_bytes); |
| @@ -23,27 +29,23 @@ pub inline fn init(allocator: mem.Allocator) mem.Allocator.Error!Graphemes { | |||
| 23 | 29 | ||
| 24 | const endian = builtin.cpu.arch.endian(); | 30 | const endian = builtin.cpu.arch.endian(); |
| 25 | 31 | ||
| 26 | var self = Graphemes{}; | ||
| 27 | |||
| 28 | const s1_len: u16 = reader.readInt(u16, endian) catch unreachable; | 32 | const s1_len: u16 = reader.readInt(u16, endian) catch unreachable; |
| 29 | self.s1 = try allocator.alloc(u16, s1_len); | 33 | graphemes.s1 = try allocator.alloc(u16, s1_len); |
| 30 | errdefer allocator.free(self.s1); | 34 | errdefer allocator.free(graphemes.s1); |
| 31 | for (0..s1_len) |i| self.s1[i] = reader.readInt(u16, endian) catch unreachable; | 35 | for (0..s1_len) |i| graphemes.s1[i] = reader.readInt(u16, endian) catch unreachable; |
| 32 | 36 | ||
| 33 | const s2_len: u16 = reader.readInt(u16, endian) catch unreachable; | 37 | const s2_len: u16 = reader.readInt(u16, endian) catch unreachable; |
| 34 | self.s2 = try allocator.alloc(u16, s2_len); | 38 | graphemes.s2 = try allocator.alloc(u16, s2_len); |
| 35 | errdefer allocator.free(self.s2); | 39 | errdefer allocator.free(graphemes.s2); |
| 36 | for (0..s2_len) |i| self.s2[i] = reader.readInt(u16, endian) catch unreachable; | 40 | for (0..s2_len) |i| graphemes.s2[i] = reader.readInt(u16, endian) catch unreachable; |
| 37 | 41 | ||
| 38 | const s3_len: u16 = reader.readInt(u16, endian) catch unreachable; | 42 | const s3_len: u16 = reader.readInt(u16, endian) catch unreachable; |
| 39 | self.s3 = try allocator.alloc(u8, s3_len); | 43 | graphemes.s3 = try allocator.alloc(u8, s3_len); |
| 40 | errdefer allocator.free(self.s3); | 44 | errdefer allocator.free(graphemes.s3); |
| 41 | _ = reader.readAll(self.s3) catch unreachable; | 45 | _ = reader.readAll(graphemes.s3) catch unreachable; |
| 42 | |||
| 43 | return self; | ||
| 44 | } | 46 | } |
| 45 | 47 | ||
| 46 | pub fn deinit(graphemes: *const Graphemes, allocator: mem.Allocator) void { | 48 | pub fn deinit(graphemes: *const Graphemes, allocator: Allocator) void { |
| 47 | allocator.free(graphemes.s1); | 49 | allocator.free(graphemes.s1); |
| 48 | allocator.free(graphemes.s2); | 50 | allocator.free(graphemes.s2); |
| 49 | allocator.free(graphemes.s3); | 51 | allocator.free(graphemes.s3); |