From 3c2c30bfbe861c6c48acd8d7507886787197a788 Mon Sep 17 00:00:00 2001 From: Sam Atman Date: Wed, 30 Apr 2025 12:58:26 -0400 Subject: Merge NormData with Normalize --- src/FoldData.zig | 78 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 39 deletions(-) (limited to 'src/FoldData.zig') diff --git a/src/FoldData.zig b/src/FoldData.zig index e44e714..b7fdceb 100644 --- a/src/FoldData.zig +++ b/src/FoldData.zig @@ -12,9 +12,9 @@ stage1: []u8 = undefined, stage2: []u8 = undefined, stage3: []i24 = undefined, -const Self = @This(); +const FoldData = @This(); -pub fn init(allocator: mem.Allocator) !Self { +pub fn init(allocator: mem.Allocator) !FoldData { const decompressor = compress.flate.inflate.decompressor; const in_bytes = @embedFile("fold"); var in_fbs = std.io.fixedBufferStream(in_bytes); @@ -23,61 +23,61 @@ pub fn init(allocator: mem.Allocator) !Self { const endian = builtin.cpu.arch.endian(); - var self = Self{}; - self.cutoff = @intCast(try reader.readInt(u24, endian)); - self.multiple_start = @intCast(try reader.readInt(u24, endian)); + var fdata = FoldData{}; + fdata.cutoff = @intCast(try reader.readInt(u24, endian)); + fdata.multiple_start = @intCast(try reader.readInt(u24, endian)); var len = try reader.readInt(u16, endian); - self.stage1 = try allocator.alloc(u8, len); - errdefer allocator.free(self.stage1); - for (0..len) |i| self.stage1[i] = try reader.readInt(u8, endian); + fdata.stage1 = try allocator.alloc(u8, len); + errdefer allocator.free(fdata.stage1); + for (0..len) |i| fdata.stage1[i] = try reader.readInt(u8, endian); len = try reader.readInt(u16, endian); - self.stage2 = try allocator.alloc(u8, len); - errdefer allocator.free(self.stage2); - for (0..len) |i| self.stage2[i] = try reader.readInt(u8, endian); + fdata.stage2 = try allocator.alloc(u8, len); + errdefer allocator.free(fdata.stage2); + for (0..len) |i| fdata.stage2[i] = try reader.readInt(u8, endian); len = try reader.readInt(u16, endian); - self.stage3 = try allocator.alloc(i24, len); - errdefer allocator.free(self.stage3); - for (0..len) |i| self.stage3[i] = try reader.readInt(i24, endian); + fdata.stage3 = try allocator.alloc(i24, len); + errdefer allocator.free(fdata.stage3); + for (0..len) |i| fdata.stage3[i] = try reader.readInt(i24, endian); - self.cwcf_exceptions_min = @intCast(try reader.readInt(u24, endian)); - self.cwcf_exceptions_max = @intCast(try reader.readInt(u24, endian)); + fdata.cwcf_exceptions_min = @intCast(try reader.readInt(u24, endian)); + fdata.cwcf_exceptions_max = @intCast(try reader.readInt(u24, endian)); len = try reader.readInt(u16, endian); - self.cwcf_exceptions = try allocator.alloc(u21, len); - errdefer allocator.free(self.cwcf_exceptions); - for (0..len) |i| self.cwcf_exceptions[i] = @intCast(try reader.readInt(u24, endian)); + fdata.cwcf_exceptions = try allocator.alloc(u21, len); + errdefer allocator.free(fdata.cwcf_exceptions); + for (0..len) |i| fdata.cwcf_exceptions[i] = @intCast(try reader.readInt(u24, endian)); - return self; + return fdata; } -pub fn deinit(self: *const Self, allocator: mem.Allocator) void { - allocator.free(self.stage1); - allocator.free(self.stage2); - allocator.free(self.stage3); - allocator.free(self.cwcf_exceptions); +pub fn deinit(fdata: *const FoldData, allocator: mem.Allocator) void { + allocator.free(fdata.stage1); + allocator.free(fdata.stage2); + allocator.free(fdata.stage3); + allocator.free(fdata.cwcf_exceptions); } /// Returns the case fold for `cp`. -pub fn caseFold(self: Self, cp: u21, buf: []u21) []const u21 { - if (cp >= self.cutoff) return &.{}; +pub fn caseFold(fdata: *const FoldData, cp: u21, buf: []u21) []const u21 { + if (cp >= fdata.cutoff) return &.{}; - const stage1_val = self.stage1[cp >> 8]; + const stage1_val = fdata.stage1[cp >> 8]; if (stage1_val == 0) return &.{}; const stage2_index = @as(usize, stage1_val) * 256 + (cp & 0xFF); - const stage3_index = self.stage2[stage2_index]; + const stage3_index = fdata.stage2[stage2_index]; if (stage3_index & 0x80 != 0) { - const real_index = @as(usize, self.multiple_start) + (stage3_index ^ 0x80) * 3; - const mapping = mem.sliceTo(self.stage3[real_index..][0..3], 0); + const real_index = @as(usize, fdata.multiple_start) + (stage3_index ^ 0x80) * 3; + const mapping = mem.sliceTo(fdata.stage3[real_index..][0..3], 0); for (mapping, 0..) |c, i| buf[i] = @intCast(c); return buf[0..mapping.len]; } - const offset = self.stage3[stage3_index]; + const offset = fdata.stage3[stage3_index]; if (offset == 0) return &.{}; buf[0] = @intCast(@as(i32, cp) + offset); @@ -86,14 +86,14 @@ pub fn caseFold(self: Self, cp: u21, buf: []u21) []const u21 { } /// Returns true when caseFold(NFD(`cp`)) != NFD(`cp`). -pub fn changesWhenCaseFolded(self: Self, cp: u21) bool { +pub fn changesWhenCaseFolded(fdata: *const FoldData, cp: u21) bool { var buf: [3]u21 = undefined; - const has_mapping = self.caseFold(cp, &buf).len != 0; - return has_mapping and !self.isCwcfException(cp); + const has_mapping = fdata.caseFold(cp, &buf).len != 0; + return has_mapping and !fdata.isCwcfException(cp); } -fn isCwcfException(self: Self, cp: u21) bool { - return cp >= self.cwcf_exceptions_min and - cp <= self.cwcf_exceptions_max and - std.mem.indexOfScalar(u21, self.cwcf_exceptions, cp) != null; +fn isCwcfException(fdata: *const FoldData, cp: u21) bool { + return cp >= fdata.cwcf_exceptions_min and + cp <= fdata.cwcf_exceptions_max and + std.mem.indexOfScalar(u21, fdata.cwcf_exceptions, cp) != null; } -- cgit v1.2.3