~subsetpark/erasmus

a0aba0b824dccb9c1b1668c3b56819ebc376da67 — Zach Smith 3 months ago b374388
Use a set to catch forward references
1 files changed, 10 insertions(+), 6 deletions(-)

M src/main.zig
M src/main.zig => src/main.zig +10 -6
@@ 22,7 22,7 @@ const end_brackets = '}';
const CharBuffer = std.ArrayList(u8);

const Brackets = struct {
    brackets: std.ArrayList([]const u8),
    brackets: std.StringHashMap(bool),
    error_slice: ?[]const u8,
};



@@ 126,7 126,8 @@ fn findBracketedStrings(note_body: []const u8, brackets: *Brackets) !bool {
                    // If we are closing an existing reference and the brackets
                    // aren't empty, push a new slice.
                    found = true;
                    try brackets.brackets.append(note_body[slice_start + 1 .. i]);
                    const slice = note_body[slice_start + 1 .. i];
                    try brackets.brackets.put(slice, true);
                }
                current_slice = null;
            }


@@ 189,7 190,8 @@ fn appendBacklinks(

    for (notes.items) |other_note| {
        const items = other_note.body.items;
        if (mem.indexOf(u8, items, link_here) != null) {
        const lower_items = try std.ascii.allocLowerString(allocator, items);
        if (mem.indexOf(u8, lower_items, link_here) != null) {
            found_one = true;

            var escaped = EscapedString{


@@ 206,6 208,7 @@ fn appendBacklinks(
            };
            try appendRef(note, escaped.escaped.items, allocator);
        }
        allocator.free(lower_items);
    }

    // Backlinks padding.


@@ 220,7 223,7 @@ fn appendReferences(
    allocator: mem.Allocator,
) !void {
    var brackets = Brackets{
        .brackets = std.ArrayList([]const u8).init(allocator),
        .brackets = std.StringHashMap(bool).init(allocator),
        .error_slice = undefined,
    };
    defer brackets.brackets.deinit();


@@ 241,14 244,15 @@ fn appendReferences(
        try note.append('\n');
    }
    // Escape and add forward references to note.
    for (brackets.brackets.items) |phrase| {
    var keys = brackets.brackets.keyIterator();
    while (keys.next()) |phrase| {
        var escaped = EscapedString{
            .escaped = try CharBuffer.initCapacity(allocator, phrase.len * 2),
            .error_slice = undefined,
        };
        defer escaped.escaped.deinit();

        escape(phrase, &escaped) catch {
        escape(phrase.*, &escaped) catch {
            std.debug.print("Found illegal character in brackets: `{s}`\n", .{
                escaped.error_slice,
            });