diff options
| author | 2024-03-01 18:51:43 -0400 | |
|---|---|---|
| committer | 2024-03-01 18:51:43 -0400 | |
| commit | 9a0fb96c0c28540493a205b85d1b89d2c9b50f2b (patch) | |
| tree | 723760b45ef8ef604b235d10c3c60edfadd0bb70 /src/FoldData.zig | |
| parent | Removed dupe tombstone check in Normalizer (diff) | |
| download | zg-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.zig | 48 |
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 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const builtin = @import("builtin"); | ||
| 3 | const compress = std.compress; | ||
| 4 | const mem = std.mem; | ||
| 5 | |||
| 6 | allocator: mem.Allocator, | ||
| 7 | fold: [][]u21 = undefined, | ||
| 8 | |||
| 9 | const Self = @This(); | ||
| 10 | |||
| 11 | pub 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 | |||
| 40 | pub 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`. | ||
| 46 | pub inline fn caseFold(self: Self, cp: u21) []const u21 { | ||
| 47 | return self.fold[cp]; | ||
| 48 | } | ||