From 9b435e69cb9f1572728b38457fabc9636bc47143 Mon Sep 17 00:00:00 2001 From: Jose Colon Rodriguez Date: Fri, 1 Mar 2024 19:28:41 -0400 Subject: Changes when case folded check; 20ms faster --- src/FoldData.zig | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/FoldData.zig') diff --git a/src/FoldData.zig b/src/FoldData.zig index 139c677..2a9a1f5 100644 --- a/src/FoldData.zig +++ b/src/FoldData.zig @@ -5,6 +5,7 @@ const mem = std.mem; allocator: mem.Allocator, fold: [][]u21 = undefined, +cwcf: []bool = undefined, const Self = @This(); @@ -20,18 +21,21 @@ pub fn init(allocator: mem.Allocator) !Self { var self = Self{ .allocator = allocator, .fold = try allocator.alloc([]u21, 0x110000), + .cwcf = try allocator.alloc(bool, 0x110000), }; @memset(self.fold, &.{}); + @memset(self.cwcf, false); while (true) { const len: u8 = try reader.readInt(u8, endian); if (len == 0) break; const cp = try reader.readInt(u24, endian); - self.fold[cp] = try allocator.alloc(u21, len - 1); + self.fold[cp >> 1] = try allocator.alloc(u21, len - 1); for (0..len - 1) |i| { - self.fold[cp][i] = @intCast(try reader.readInt(u24, endian)); + self.fold[cp >> 1][i] = @intCast(try reader.readInt(u24, endian)); } + self.cwcf[cp >> 1] = cp & 1 == 1; } return self; @@ -40,9 +44,15 @@ pub fn init(allocator: mem.Allocator) !Self { pub fn deinit(self: *Self) void { for (self.fold) |slice| self.allocator.free(slice); self.allocator.free(self.fold); + self.allocator.free(self.cwcf); } /// Returns the case fold for `cp`. pub inline fn caseFold(self: Self, cp: u21) []const u21 { return self.fold[cp]; } + +/// Returns true when caseFold(NFD(`cp`)) != NFD(`cp`). +pub inline fn changesWhenCaseFolded(self: Self, cp: u21) bool { + return self.cwcf[cp]; +} -- cgit v1.2.3