From 9085cb5bfdd0b78ff3a62c0c71fc231dd7b5052a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 9 Aug 2026 10:41:33 -0300 Subject: replace ArrayLists with bounded storage --- src/gui/fonts.zig | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src/gui/fonts.zig') diff --git a/src/gui/fonts.zig b/src/gui/fonts.zig index e21f4674..1ad7dd84 100644 --- a/src/gui/fonts.zig +++ b/src/gui/fonts.zig @@ -65,7 +65,8 @@ pub var want: ?[]const u8 = null; /// four in vendor/stb/font.c plus a UTF-16BE decode, and this file is where it /// would land. pub fn list(arena: std.mem.Allocator, want_name: ?[]const u8) []const Font { - var out: std.ArrayList(Font) = .empty; + var found: [max_fonts]Font = undefined; + var found_len: usize = 0; // Zig 0.16 moved the filesystem behind std.Io; the blocking // single-threaded implementation is the synchronous walk a sans-IO core // wants, the same one look.find uses. @@ -87,7 +88,7 @@ pub fn list(arena: std.mem.Allocator, want_name: ?[]const u8) []const Font { var w = dir.walkSelectively(arena) catch continue; defer w.deinit(); var steps: usize = 0; - while (steps < max_steps and out.items.len < max_fonts) { + while (steps < max_steps and found_len < found.len) { steps += 1; // an unreadable dir burns a step too, so it cannot spin const e = (w.next(io) catch continue) orelse break; if (e.kind == .directory) { @@ -103,21 +104,22 @@ pub fn list(arena: std.mem.Allocator, want_name: ?[]const u8) []const Font { if (want_name) |wn| { if (!std.mem.eql(u8, wn, name)) continue; } else if (!monospaced(path)) continue; - out.append(arena, .{ + found[found_len] = .{ .name = arena.dupe(u8, name) catch break, .path = arena.dupe(u8, path) catch break, - }) catch break; - if (want_name != null) return out.items; // asked for one, found it + }; + found_len += 1; + if (want_name != null) return arena.dupe(Font, found[0..found_len]) catch &.{}; // asked for one, found it } } // readdir order is undefined; sort so the picker is the same list twice // running and n/N walks a font's own variants in a row - std.mem.sort(Font, out.items, {}, struct { + std.mem.sort(Font, found[0..found_len], {}, struct { fn lt(_: void, a: Font, b: Font) bool { return std.mem.lessThan(u8, a.name, b.name); } }.lt); - return out.items; + return arena.dupe(Font, found[0..found_len]) catch &.{}; } /// Is every glyph in this font the same width? The terminal grid IS a -- cgit v1.3