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 ++++++++++++-- src/Normalizer.zig | 30 ++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 6 deletions(-) (limited to 'src') 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]; +} diff --git a/src/Normalizer.zig b/src/Normalizer.zig index c68b2ec..5a26dfa 100644 --- a/src/Normalizer.zig +++ b/src/Normalizer.zig @@ -389,6 +389,12 @@ fn nfkdCodePoints( return try dcp_list.toOwnedSlice(); } +fn changesWhenCaseFolded(self: Self, cps: []const u21) bool { + return for (cps) |cp| { + if (self.norm_data.fold_data.changesWhenCaseFolded(cp)) break true; + } else false; +} + pub fn eqlIgnoreCase( self: Self, allocator: mem.Allocator, @@ -397,10 +403,18 @@ pub fn eqlIgnoreCase( ) !bool { if (ascii.isAsciiOnly(a) and ascii.isAsciiOnly(b)) return std.ascii.eqlIgnoreCase(a, b); + // Process a const nfd_a = try self.nfxdCodePoints(allocator, a, .nfd); defer allocator.free(nfd_a); - const cf_nfd_a = try self.caseFold(allocator, nfd_a); - defer allocator.free(cf_nfd_a); + + var need_frr_cf_nfd_a = false; + var cf_nfd_a: []const u21 = nfd_a; + if (self.changesWhenCaseFolded(nfd_a)) { + cf_nfd_a = try self.caseFold(allocator, nfd_a); + need_frr_cf_nfd_a = true; + } + defer if (need_frr_cf_nfd_a) allocator.free(cf_nfd_a); + const nfkd_cf_nfd_a = try self.nfkdCodePoints(allocator, cf_nfd_a); defer allocator.free(nfkd_cf_nfd_a); const cf_nfkd_cf_nfd_a = try self.caseFold(allocator, nfkd_cf_nfd_a); @@ -408,10 +422,18 @@ pub fn eqlIgnoreCase( const nfkd_cf_nfkd_cf_nfd_a = try self.nfkdCodePoints(allocator, cf_nfkd_cf_nfd_a); defer allocator.free(nfkd_cf_nfkd_cf_nfd_a); + // Process b const nfd_b = try self.nfxdCodePoints(allocator, b, .nfd); defer allocator.free(nfd_b); - const cf_nfd_b = try self.caseFold(allocator, nfd_b); - defer allocator.free(cf_nfd_b); + + var need_frr_cf_nfd_b = false; + var cf_nfd_b: []const u21 = nfd_b; + if (self.changesWhenCaseFolded(nfd_b)) { + cf_nfd_b = try self.caseFold(allocator, nfd_b); + need_frr_cf_nfd_b = true; + } + defer if (need_frr_cf_nfd_b) allocator.free(cf_nfd_b); + const nfkd_cf_nfd_b = try self.nfkdCodePoints(allocator, cf_nfd_b); defer allocator.free(nfkd_cf_nfd_b); const cf_nfkd_cf_nfd_b = try self.caseFold(allocator, nfkd_cf_nfd_b); -- cgit v1.2.3