💾 Archived View for random-projects.net › blog › 2021-03-05-benchmark.zig captured on 2021-12-05 at 23:47:19.

View Raw

More Information

⬅️ Previous capture (2021-12-03)

➡️ Next capture (2022-06-11)

-=-=-=-=-=-=-

const std = @import("std");
const gemtext = @import("gemtext");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();

    const allocator = &gpa.allocator;

    var stream = std.io.getStdIn().reader();

    var buffer: [1 << 20]u8 = undefined;

    var parser = gemtext.Parser.init(allocator);
    defer parser.deinit();

    while (true) {
        const length = try stream.read(&buffer);
        if (length == 0)
            break;

        var arena = std.heap.ArenaAllocator.init(allocator);
        defer arena.deinit();

        var offset: usize = 0;
        while (offset < length) {
            var result = try parser.feed(&arena.allocator, buffer[offset..length]);
            if (result.fragment) |*frag| {
                defer frag.free(&arena.allocator);

                try gemtext.renderer.gemtext(&[_]gemtext.Fragment{frag.*}, std.io.getStdOut().writer());
            }
            offset += result.consumed;
        }
    }
    if (try parser.finalize(allocator)) |*frag| {
        defer frag.free(allocator);

        try gemtext.renderer.gemtext(&[_]gemtext.Fragment{frag.*}, std.io.getStdOut().writer());
    }
}