summaryrefslogtreecommitdiff
path: root/src/CaseFolding.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/CaseFolding.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/CaseFolding.zig')
-rw-r--r--src/CaseFolding.zig18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/CaseFolding.zig b/src/CaseFolding.zig
index 162e82f..2e53bfa 100644
--- a/src/CaseFolding.zig
+++ b/src/CaseFolding.zig
@@ -11,20 +11,21 @@ owns_normalize: bool,
11 11
12const CaseFolding = @This(); 12const CaseFolding = @This();
13 13
14pub fn init(allocator: Allocator) !CaseFolding { 14pub fn init(allocator: Allocator) Allocator.Error!CaseFolding {
15 var case_fold: CaseFolding = undefined; 15 var case_fold: CaseFolding = undefined;
16 try case_fold.setup(allocator); 16 try case_fold.setup(allocator);
17 return case_fold; 17 return case_fold;
18} 18}
19 19
20pub fn initWithNormalize(allocator: Allocator, norm: Normalize) !CaseFolding { 20pub fn initWithNormalize(allocator: Allocator, norm: Normalize) Allocator.Error!CaseFolding {
21 var casefold: CaseFolding = undefined; 21 var casefold: CaseFolding = undefined;
22 try casefold.setupWithNormalize(allocator, norm); 22 try casefold.setupWithNormalize(allocator, norm);
23 return casefold; 23 return casefold;
24} 24}
25 25
26pub fn setup(casefold: *CaseFolding, allocator: Allocator) !void { 26pub fn setup(casefold: *CaseFolding, allocator: Allocator) Allocator.Error!void {
27 try casefold.setupImpl(allocator); 27 try casefold.setupImpl(allocator);
28 // Handle normalize memory separately during setup:
28 casefold.owns_normalize = false; 29 casefold.owns_normalize = false;
29 errdefer casefold.deinit(allocator); 30 errdefer casefold.deinit(allocator);
30 try casefold.normalize.setup(allocator); 31 try casefold.normalize.setup(allocator);
@@ -37,7 +38,16 @@ pub fn setupWithNormalize(casefold: *CaseFolding, allocator: Allocator, norm: No
37 casefold.owns_normalize = false; 38 casefold.owns_normalize = false;
38} 39}
39 40
40fn setupImpl(casefold: *CaseFolding, allocator: Allocator) !void { 41fn setupImpl(casefold: *CaseFolding, allocator: Allocator) Allocator.Error!void {
42 casefold.setupImplInner(allocator) catch |err| {
43 switch (err) {
44 error.OutOfMemory => |e| return e,
45 else => unreachable,
46 }
47 };
48}
49
50inline fn setupImplInner(casefold: *CaseFolding, allocator: Allocator) !void {
41 const decompressor = compress.flate.inflate.decompressor; 51 const decompressor = compress.flate.inflate.decompressor;
42 const in_bytes = @embedFile("fold"); 52 const in_bytes = @embedFile("fold");
43 var in_fbs = std.io.fixedBufferStream(in_bytes); 53 var in_fbs = std.io.fixedBufferStream(in_bytes);