From ca17f34d941d6697b1322802a2f2b1516e13c3b2 Mon Sep 17 00:00:00 2001 From: Jacob Sandlund Date: Fri, 4 Jul 2025 15:23:34 -0400 Subject: [PATCH] use Context instead of props. only import DisplayWidth when needed --- src/build/SharedDeps.zig | 5 +- src/unicode/main.zig | 2 +- src/unicode/props.zig | 99 +++++++++++++++++++++------------------- 3 files changed, 55 insertions(+), 51 deletions(-) diff --git a/src/build/SharedDeps.zig b/src/build/SharedDeps.zig index 909f727d3..ce70e0a03 100644 --- a/src/build/SharedDeps.zig +++ b/src/build/SharedDeps.zig @@ -416,8 +416,9 @@ pub fn add( .optimize = optimize, })) |dep| { step.root_module.addImport("CaseFolding", dep.module("CaseFolding")); - // Only needed for table gen, but still include to have build succeeed - step.root_module.addImport("DisplayWidth", dep.module("DisplayWidth")); + if (self.config.emit_unicode_test) { + step.root_module.addImport("DisplayWidth", dep.module("DisplayWidth")); + } step.root_module.addImport("Emoji", dep.module("Emoji")); step.root_module.addImport("GeneralCategories", dep.module("GeneralCategories")); step.root_module.addImport("Graphemes", dep.module("Graphemes")); diff --git a/src/unicode/main.zig b/src/unicode/main.zig index a3868f3c8..03544e310 100644 --- a/src/unicode/main.zig +++ b/src/unicode/main.zig @@ -41,7 +41,7 @@ pub fn main() !void { const args = try std.process.argsAlloc(alloc); defer std.process.argsFree(alloc, args); - var zg = try props.init(alloc); + var zg = try props.Context.init(alloc); defer zg.deinit(alloc); const ziglyph = @import("ziglyph"); diff --git a/src/unicode/props.zig b/src/unicode/props.zig index 3b8364235..bfdcb3533 100644 --- a/src/unicode/props.zig +++ b/src/unicode/props.zig @@ -1,30 +1,55 @@ -const props = @This(); const std = @import("std"); const assert = std.debug.assert; -const Graphemes = @import("Graphemes"); -const DisplayWidth = @import("DisplayWidth"); const lut = @import("lut.zig"); +const Graphemes = @import("Graphemes"); -graphemes: Graphemes, -display_width: DisplayWidth, +/// The context needed for lut generation. +pub const Context = struct { + graphemes: Graphemes, + display_width: DisplayWidth, -// Whether to use the old implementation based on ziglyph. -old: bool = false, + // Whether to use the old implementation based on ziglyph. + old: bool = false, -// Public only for unicode-test -pub fn init(alloc: std.mem.Allocator) !props { - const graphemes = try Graphemes.init(alloc); - return .{ - .graphemes = graphemes, - .display_width = try DisplayWidth.initWithGraphemes(alloc, graphemes), - }; -} + const DisplayWidth = @import("DisplayWidth"); -// Public only for unicode-test -pub fn deinit(self: *props, alloc: std.mem.Allocator) void { - self.graphemes.deinit(alloc); - self.display_width.deinit(alloc); -} + // Public only for unicode-test + pub fn init(alloc: std.mem.Allocator) !Context { + const graphemes = try Graphemes.init(alloc); + return .{ + .graphemes = graphemes, + .display_width = try DisplayWidth.initWithGraphemes(alloc, graphemes), + }; + } + + // Public only for unicode-test + pub fn deinit(self: *Context, alloc: std.mem.Allocator) void { + self.graphemes.deinit(alloc); + self.display_width.deinit(alloc); + } + + pub fn get(self: Context, cp: u21) !Properties { + if (cp > 0x10FFFF) { + return .{ + .width = 0, + .grapheme_boundary_class = .invalid, + }; + } else { + const zg_width = DisplayWidth.codePointWidth(self.display_width, cp); + + return .{ + .width = @intCast(@min(2, @max(0, zg_width))), + //.grapheme_boundary_class = .init(self, cp), + .grapheme_boundary_class = if (self.old) .initOld(cp) else .init(self, cp), + }; + } + } + + pub fn eql(self: Context, a: Properties, b: Properties) bool { + _ = self; + return a.eql(b); + } +}; /// The lookup tables for Ghostty. pub const table = table: { @@ -114,7 +139,7 @@ pub const GraphemeBoundaryClass = enum(u4) { /// Gets the grapheme boundary class for a codepoint. This is VERY /// SLOW. The use case for this is only in generating lookup tables. - pub fn init(ctx: props, cp: u21) GraphemeBoundaryClass { + pub fn init(ctx: Context, cp: u21) GraphemeBoundaryClass { return switch (Graphemes.gbp(ctx.graphemes, cp)) { .Emoji_Modifier_Base => .extended_pictographic_base, .Emoji_Modifier => .emoji_modifier, @@ -180,28 +205,6 @@ pub const GraphemeBoundaryClass = enum(u4) { } }; -pub fn get(ctx: props, cp: u21) !Properties { - if (cp > 0x10FFFF) { - return .{ - .width = 0, - .grapheme_boundary_class = .invalid, - }; - } else { - const zg_width = DisplayWidth.codePointWidth(ctx.display_width, cp); - - return .{ - .width = @intCast(@min(2, @max(0, zg_width))), - //.grapheme_boundary_class = .init(ctx, cp), - .grapheme_boundary_class = if (ctx.old) .initOld(cp) else .init(ctx, cp), - }; - } -} - -pub fn eql(ctx: props, a: Properties, b: Properties) bool { - _ = ctx; - return a.eql(b); -} - /// Runnable binary to generate the lookup tables and output to stdout. pub fn main() !void { var arena_state = std.heap.ArenaAllocator.init(std.heap.page_allocator); @@ -211,17 +214,17 @@ pub fn main() !void { const args = try std.process.argsAlloc(alloc); defer std.process.argsFree(alloc, args); - var self = try init(alloc); - defer self.deinit(alloc); + var ctx = try Context.init(alloc); + defer ctx.deinit(alloc); if (args.len > 1 and std.mem.eql(u8, args[1], "old")) { - self.old = true; + ctx.old = true; } const gen: lut.Generator( Properties, - props, - ) = .{ .ctx = self }; + Context, + ) = .{ .ctx = ctx }; const t = try gen.generate(alloc); defer alloc.free(t.stage1);