~subsetpark/erasmus

4f85fb3ce909749d7a471fec1ce2e067d082a7fd — Zach Smith 2 months ago 1f1084e
Smarter appendRef
1 files changed, 12 insertions(+), 17 deletions(-)

M src/note.zig
M src/note.zig => src/note.zig +12 -17
@@ 77,15 77,14 @@ const begin_braces = '{';
const end_braces = '}';

// Create a reference line and add it to the note.
fn appendRef(buf: *util.CharBuffer, referent: []const u8, allocator: mem.Allocator) !void {
    const ref = try fmt.allocPrint(allocator, "{s}:{s}", .{
        util.ref_prefix,
        referent,
    });
    defer allocator.free(ref);

    try buf.appendSlice(ref);
    try buf.append('\n');
fn appendRef(buf: *util.CharBuffer, referent: []const u8) !void {
    const capacity = util.ref_prefix.len + 1 + referent.len + 1;
    try buf.ensureUnusedCapacity(capacity);

    buf.appendSliceAssumeCapacity(util.ref_prefix);
    buf.appendAssumeCapacity(':');
    buf.appendSliceAssumeCapacity(referent);
    buf.appendAssumeCapacity('\n');
}

pub const Note = struct {


@@ 200,16 199,12 @@ pub const Note = struct {

    // Backlinks: if any other note body refers to this one, add a reference to
    // that body's name.
    fn appendBacklinks(
        self: *@This(),
        buf: *util.CharBuffer,
        allocator: mem.Allocator,
    ) !void {
    fn appendBacklinks(self: *@This(), buf: *util.CharBuffer) !void {
        var has_a_backlink = false;

        for (self.backlinks.items) |backlink| {
            has_a_backlink = true;
            try appendRef(buf, backlink, allocator);
            try appendRef(buf, backlink);
        }

        // Backlinks padding.


@@ 249,7 244,7 @@ pub const Note = struct {
            const str = escaped.items;
            if (!seen.contains(str)) {
                try seen.insert(str);
                try appendRef(buf, str, allocator);
                try appendRef(buf, str);
            }
        }
    }


@@ 259,7 254,7 @@ pub const Note = struct {
        buf: *util.CharBuffer,
        allocator: mem.Allocator,
    ) !void {
        try self.appendBacklinks(buf, allocator);
        try self.appendBacklinks(buf);
        try self.appendBody(buf);
        try self.appendReferences(buf, allocator);
    }