~subsetpark/erasmus

c797c26e3066a21eab8ef7e2e13549318fa0f3fe — Zach Smith 2 months ago ff3012b
Better memory management
2 files changed, 10 insertions(+), 11 deletions(-)

M src/main.zig
M src/note.zig
M src/main.zig => src/main.zig +9 -10
@@ 115,11 115,13 @@ pub fn main() !void {
        try note.populateNoteData(notes, allocator);
    }

    // Assemble new note bodies.
    var note_bodies = std.ArrayList(struct { name: []const u8, body: util.CharBuffer }).init(allocator);
    // Assemble and write new note bodies.
    for (notes.items) |*note| {
        var buf = util.CharBuffer.init(allocator);
        defer buf.deinit();

        try note.generateBody(&buf, allocator);
        note.deinit();

        // Optional debugging.
        if (debug_mode) {


@@ 129,16 131,13 @@ pub fn main() !void {
            });
        }

        try note_bodies.append(.{ .name = note.name, .body = buf });
        note.deinit();
    }

    // Write to disk.
    for (note_bodies.items) |new_note| {
        const path = try fs.path.join(allocator, &[_][]const u8{
            directory_name,
            new_note.name,
            note.name,
        });
        try fs.cwd().writeFile(path, new_note.body.items);
        defer allocator.free(path);
        allocator.free(note.name);

        try fs.cwd().writeFile(path, buf.items);
    }
}

M src/note.zig => src/note.zig +1 -1
@@ 109,6 109,7 @@ pub const Note = struct {

    pub fn deinit(self: @This()) void {
        self.body.deinit();
        self.backlinks.deinit();
        self.references.deinit();
    }



@@ 129,7 130,6 @@ pub const Note = struct {

                if (mem.eql(u8, reference_lowered, self_lowered)) {
                    var escaped = try EscapedString.init(other_note.name, allocator);
                    defer escaped.escaped.deinit();

                    escaped.escapeAndLower(other_note.name);
                    escaped.handleErrors();