summaryrefslogtreecommitdiff
path: root/src/FoldData.zig
diff options
context:
space:
mode:
authorGravatar Jose Colon Rodriguez2024-03-01 18:51:43 -0400
committerGravatar Jose Colon Rodriguez2024-03-01 18:51:43 -0400
commit9a0fb96c0c28540493a205b85d1b89d2c9b50f2b (patch)
tree723760b45ef8ef604b235d10c3c60edfadd0bb70 /src/FoldData.zig
parentRemoved dupe tombstone check in Normalizer (diff)
downloadzg-9a0fb96c0c28540493a205b85d1b89d2c9b50f2b.tar.gz
zg-9a0fb96c0c28540493a205b85d1b89d2c9b50f2b.tar.xz
zg-9a0fb96c0c28540493a205b85d1b89d2c9b50f2b.zip
Normalizer.eqlIgnoreCase compatibility caseless matching
Diffstat (limited to 'src/FoldData.zig')
-rw-r--r--src/FoldData.zig48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/FoldData.zig b/src/FoldData.zig
new file mode 100644
index 0000000..139c677
--- /dev/null
+++ b/src/FoldData.zig
@@ -0,0 +1,48 @@
1const std = @import("std");
2const builtin = @import("builtin");
3const compress = std.compress;
4const mem = std.mem;
5
6allocator: mem.Allocator,
7fold: [][]u21 = undefined,
8
9const Self = @This();
10
11pub fn init(allocator: mem.Allocator) !Self {
12 const decompressor = compress.deflate.decompressor;
13 const in_bytes = @embedFile("fold");
14 var in_fbs = std.io.fixedBufferStream(in_bytes);
15 var in_decomp = try decompressor(allocator, in_fbs.reader(), null);
16 defer in_decomp.deinit();
17 var reader = in_decomp.reader();
18
19 const endian = builtin.cpu.arch.endian();
20 var self = Self{
21 .allocator = allocator,
22 .fold = try allocator.alloc([]u21, 0x110000),
23 };
24
25 @memset(self.fold, &.{});
26
27 while (true) {
28 const len: u8 = try reader.readInt(u8, endian);
29 if (len == 0) break;
30 const cp = try reader.readInt(u24, endian);
31 self.fold[cp] = try allocator.alloc(u21, len - 1);
32 for (0..len - 1) |i| {
33 self.fold[cp][i] = @intCast(try reader.readInt(u24, endian));
34 }
35 }
36
37 return self;
38}
39
40pub fn deinit(self: *Self) void {
41 for (self.fold) |slice| self.allocator.free(slice);
42 self.allocator.free(self.fold);
43}
44
45/// Returns the case fold for `cp`.
46pub inline fn caseFold(self: Self, cp: u21) []const u21 {
47 return self.fold[cp];
48}