summaryrefslogtreecommitdiff
path: root/src/Graphemes.zig
diff options
context:
space:
mode:
authorGravatar Sam Atman2025-04-30 16:48:07 -0400
committerGravatar Sam Atman2025-04-30 16:48:07 -0400
commitd2d42bf3ef5490f6fdec73508c2493a666ecee41 (patch)
tree377794be59ece4118ca2449b705b8e7cc646abc0 /src/Graphemes.zig
parentUpdate README.md to new API (diff)
downloadzg-d2d42bf3ef5490f6fdec73508c2493a666ecee41.tar.gz
zg-d2d42bf3ef5490f6fdec73508c2493a666ecee41.tar.xz
zg-d2d42bf3ef5490f6fdec73508c2493a666ecee41.zip
Setup variants for all allocating modules
This harmonizes the allocating modules in a couple of ways. All can now be constructed by pointer, and all treat various miscellaneous read failures as `unreachable`, which indeed they should be. The README has been updated to inform users of this option.
Diffstat (limited to 'src/Graphemes.zig')
-rw-r--r--src/Graphemes.zig32
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
15const Graphemes = @This(); 15const Graphemes = @This();
16 16
17pub inline fn init(allocator: mem.Allocator) mem.Allocator.Error!Graphemes { 17pub fn init(allocator: Allocator) Allocator.Error!Graphemes {
18 var graphemes = Graphemes{};
19 try graphemes.setup(allocator);
20 return graphemes;
21}
22
23pub 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
46pub fn deinit(graphemes: *const Graphemes, allocator: mem.Allocator) void { 48pub 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);