From e37c17452f9cfa1eb14ef8f5cca5d2eaa37f4658 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Mon, 2 Feb 2026 21:35:57 +0100 Subject: [PATCH] refactor: add ascii optimization to Plane.egc_length This gives a huge performance boost to pos_to_width which is used by find results and many other things. --- src/renderer/vaxis/Plane.zig | 46 ++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/renderer/vaxis/Plane.zig b/src/renderer/vaxis/Plane.zig index 23ba7db..acddf01 100644 --- a/src/renderer/vaxis/Plane.zig +++ b/src/renderer/vaxis/Plane.zig @@ -437,35 +437,35 @@ inline fn set_font_style(style: *vaxis.Cell.Style, fs: FontStyle) void { } } -inline fn is_control_code(c: u8) bool { - return switch (c) { - 0...8, 10...31 => true, - else => false, - }; -} - pub fn egc_length(self: *const Plane, egcs: []const u8, colcount: *usize, abs_col: usize, tab_width: usize) usize { if (egcs.len == 0) { colcount.* = 0; return 0; } - if (is_control_code(egcs[0])) { - colcount.* = 1; - return 1; + switch (egcs[0]) { + 0...8, + 10...31, //control codes + 32...126, + => { //ascii + colcount.* = 1; + return 1; + }, + '\t' => { + colcount.* = tab_width - (abs_col % tab_width); + return 1; + }, + else => { + var iter = vaxis.unicode.graphemeIterator(egcs); + const grapheme = iter.next() orelse { + colcount.* = 1; + return 1; + }; + const s = grapheme.bytes(egcs); + const w = self.window.gwidth(s); + colcount.* = w; + return s.len; + }, } - if (egcs[0] == '\t') { - colcount.* = tab_width - (abs_col % tab_width); - return 1; - } - var iter = vaxis.unicode.graphemeIterator(egcs); - const grapheme = iter.next() orelse { - colcount.* = 1; - return 1; - }; - const s = grapheme.bytes(egcs); - const w = self.window.gwidth(s); - colcount.* = w; - return s.len; } pub fn egc_chunk_width(self: *const Plane, chunk_: []const u8, abs_col_: usize, tab_width: usize) usize {