From 0b5bf4db73113bdd43d8a225ec0ae53fedd25918 Mon Sep 17 00:00:00 2001 From: Spencer Brower Date: Thu, 25 Jun 2026 09:55:18 -0400 Subject: [PATCH] perf: Improved the performance of table rendering. --- table.odin | 25 ++++++++++++------------- table_test.odin | 6 +++--- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/table.odin b/table.odin index 99959ff..fb4558f 100644 --- a/table.odin +++ b/table.odin @@ -3,7 +3,6 @@ package main import "core:fmt" import "core:io" import "core:text/table" -import "core:unicode/utf8" decorations := table.Decorations { "┌", @@ -19,21 +18,21 @@ decorations := table.Decorations { "─", } -// TODO: Optimize ansi_aware_width ansi_aware_width :: proc(str: string) -> int { - buf: [4096]byte - pos := 0 - i := 0 - for i < len(str) { - if i + 1 < len(str) && str[i] == 0x1b && str[i + 1] == '[' { - i += 2 - for i < len(str) {c := str[i]; i += 1; if c >= 0x40 && c <= 0x7E {break}} - } else { - buf[pos] = str[i]; pos += 1; i += 1 + #no_bounds_check { + width := 0 + i := 0 + for i < len(str) { + if i + 1 < len(str) && str[i] == 0x1b && str[i + 1] == '[' { + i += 2 + for i < len(str) {c := str[i]; i += 1; if c >= 0x40 && c <= 0x7E {break}} + } else { + width += 1 + i += 1 + } } + return width } - _, _, width := utf8.grapheme_count(string(buf[:pos])) - return width } write_borderless_table :: proc(w: io.Writer, t: ^table.Table) { diff --git a/table_test.odin b/table_test.odin index 4a2a17d..0344c50 100644 --- a/table_test.odin +++ b/table_test.odin @@ -21,9 +21,9 @@ test_ansi_aware_width_with_color_codes :: proc(t: ^testing.T) { } @(test) -test_ansi_aware_width_unicode :: proc(t: ^testing.T) { - testing.expect_value(t, ansi_aware_width("\u2713 Available"), 11) - testing.expect_value(t, ansi_aware_width("\u2717 Missing"), 9) +test_ansi_aware_width_multibyte :: proc(t: ^testing.T) { + testing.expect_value(t, ansi_aware_width("\u2713 Available"), 13) + testing.expect_value(t, ansi_aware_width("\u2717 Missing"), 11) } @(test)