diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-04 23:28:55 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 01:01:28 -0300 |
| commit | d093e439614d325573e771e7b9e24ea95c284104 (patch) | |
| tree | cb2b99278952feaa44b9966f32c72e336a858a45 /src | |
| parent | 55b842a1be818562a0d70bad3477405a5dc57afa (diff) | |
| download | pardes-d093e439614d325573e771e7b9e24ea95c284104.tar.gz pardes-d093e439614d325573e771e7b9e24ea95c284104.zip | |
pdf_view: a PDF's sections are rendered in one pass through an Allocating writer (SectionRows.writeRows), where render counted every row and then wrote them into an exact allocation, cleanTitle with a null buffer as its counting mode; the rows are the same and a failure is still OutOfMemory
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src')
| -rw-r--r-- | src/pdf_view.zig | 74 |
1 files changed, 25 insertions, 49 deletions
diff --git a/src/pdf_view.zig b/src/pdf_view.zig index ddc9c896..d5c92fb6 100644 --- a/src/pdf_view.zig +++ b/src/pdf_view.zig @@ -3725,12 +3725,11 @@ pub const SectionRows = if (enabled) struct { return ordinals; } - fn cleanTitle(out: ?[]u8, title: ?[]const u8) usize { - const raw = title orelse { - if (out) |buf| @memcpy(buf[0..10], "[untitled]"); - return 10; - }; - var at: usize = 0; + /// A title on one row: blanks, controls and broken bytes as one space + /// between words and none at its ends; `[untitled]` for none, `[empty + /// title]` for one with nothing to print. + fn cleanTitle(w: *std.Io.Writer, title: ?[]const u8) std.Io.Writer.Error!void { + const raw = title orelse return w.writeAll("[untitled]"); var i: usize = 0; var wrote = false; var pending_space = false; @@ -3754,62 +3753,39 @@ pub const SectionRows = if (enabled) struct { if (whitespace_or_control) { pending_space = wrote; } else { - if (pending_space) { - if (out) |buf| buf[at] = ' '; - at += 1; - } - if (out) |buf| @memcpy(buf[at..][0..n], raw[i .. i + n]); - at += n; + if (pending_space) try w.writeByte(' '); + try w.writeAll(raw[i .. i + n]); wrote = true; pending_space = false; } i += n; } - if (!wrote) { - if (out) |buf| @memcpy(buf[0..13], "[empty title]"); - return 13; - } - return at; + if (!wrote) try w.writeAll("[empty title]"); } pub fn render(gpa: std.mem.Allocator, path: []const u8, entries: []const pdf.OutlineEntry) ![]u8 { - const target = std.fs.path.basename(path); const ordinals = try resolveOrdinals(gpa, entries); defer gpa.free(ordinals); - var total: usize = 0; - for (entries, 0..) |entry, ordinal| { - const resolved = ordinals[ordinal]; - if (resolved == std.math.maxInt(usize)) continue; - // unreachable: resolveOrdinals resolves only to usable destinations - const destination = usableDestination(entries[resolved].destination) orelse unreachable; - total += switch (destination) { - .internal => |internal| std.fmt.count("{s}:{d}:{d} ", .{ target, internal.page + 1, ordinal + 1 }), - .external => |uri| std.fmt.count("{s} ", .{uri}), - .none => unreachable, - }; - total += @as(usize, entry.depth) * 2 + cleanTitle(null, entry.title) + 1; - } - const out = try gpa.alloc(u8, total); - errdefer gpa.free(out); - var at: usize = 0; - for (entries, 0..) |entry, ordinal| { - const resolved = ordinals[ordinal]; + var out: std.Io.Writer.Allocating = .init(gpa); + errdefer out.deinit(); + writeRows(&out.writer, std.fs.path.basename(path), entries, ordinals) catch return error.OutOfMemory; + return out.toOwnedSlice(); + } + + /// A row an entry with somewhere to go: where (`file:PAGE:ORDINAL`, or + /// its URI), indented by depth, its title. + fn writeRows(w: *std.Io.Writer, target: []const u8, entries: []const pdf.OutlineEntry, ordinals: []const usize) std.Io.Writer.Error!void { + for (entries, ordinals, 0..) |entry, resolved, ordinal| { if (resolved == std.math.maxInt(usize)) continue; // unreachable: resolveOrdinals resolves only to usable destinations - const destination = usableDestination(entries[resolved].destination) orelse unreachable; - const prefix = switch (destination) { - .internal => |internal| try std.fmt.bufPrint(out[at..], "{s}:{d}:{d} ", .{ target, internal.page + 1, ordinal + 1 }), - .external => |uri| try std.fmt.bufPrint(out[at..], "{s} ", .{uri}), + switch (usableDestination(entries[resolved].destination) orelse unreachable) { + .internal => |internal| try w.print("{s}:{d}:{d} ", .{ target, internal.page + 1, ordinal + 1 }), + .external => |uri| try w.print("{s} ", .{uri}), .none => unreachable, - }; - at += prefix.len; - const indent = @as(usize, entry.depth) * 2; - @memset(out[at..][0..indent], ' '); - at += indent; - at += cleanTitle(out[at..], entry.title); - out[at] = '\n'; - at += 1; + } + try w.splatByteAll(' ', @as(usize, entry.depth) * 2); + try cleanTitle(w, entry.title); + try w.writeByte('\n'); } - return out; } } else struct {}; |
