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.
This commit is contained in:
CJ van den Berg 2026-02-02 21:35:57 +01:00
parent 152eed7847
commit e37c17452f
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -437,26 +437,24 @@ 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 { pub fn egc_length(self: *const Plane, egcs: []const u8, colcount: *usize, abs_col: usize, tab_width: usize) usize {
if (egcs.len == 0) { if (egcs.len == 0) {
colcount.* = 0; colcount.* = 0;
return 0; return 0;
} }
if (is_control_code(egcs[0])) { switch (egcs[0]) {
0...8,
10...31, //control codes
32...126,
=> { //ascii
colcount.* = 1; colcount.* = 1;
return 1; return 1;
} },
if (egcs[0] == '\t') { '\t' => {
colcount.* = tab_width - (abs_col % tab_width); colcount.* = tab_width - (abs_col % tab_width);
return 1; return 1;
} },
else => {
var iter = vaxis.unicode.graphemeIterator(egcs); var iter = vaxis.unicode.graphemeIterator(egcs);
const grapheme = iter.next() orelse { const grapheme = iter.next() orelse {
colcount.* = 1; colcount.* = 1;
@ -466,6 +464,8 @@ pub fn egc_length(self: *const Plane, egcs: []const u8, colcount: *usize, abs_co
const w = self.window.gwidth(s); const w = self.window.gwidth(s);
colcount.* = w; colcount.* = w;
return s.len; return s.len;
},
}
} }
pub fn egc_chunk_width(self: *const Plane, chunk_: []const u8, abs_col_: usize, tab_width: usize) usize { pub fn egc_chunk_width(self: *const Plane, chunk_: []const u8, abs_col_: usize, tab_width: usize) usize {