From d2bb0b9409d8cac3ca26147dfe745e5e5fb317aa Mon Sep 17 00:00:00 2001 From: Asherah Connor Date: Sat, 15 Aug 2020 12:21:27 +1000 Subject: [PATCH] used owned slices now that we know how they work --- Makefile | 3 +++ src/html.zig | 4 ++-- src/main.zig | 51 +++++++++++++++------------------------------------ 3 files changed, 20 insertions(+), 38 deletions(-) diff --git a/Makefile b/Makefile index bcd942d..8040915 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,5 @@ all: zig build run + +test: + zig build test diff --git a/src/html.zig b/src/html.zig index d5b232a..401ae74 100644 --- a/src/html.zig +++ b/src/html.zig @@ -5,7 +5,7 @@ const mem = std.mem; const nodes = @import("nodes.zig"); -pub fn print(allocator: *mem.Allocator, root: *nodes.AstNode) !std.ArrayList(u8) { +pub fn print(allocator: *mem.Allocator, root: *nodes.AstNode) ![]u8 { var buffer = std.ArrayList(u8).init(allocator); var formatter = HtmlFormatter{ @@ -14,7 +14,7 @@ pub fn print(allocator: *mem.Allocator, root: *nodes.AstNode) !std.ArrayList(u8) }; try formatter.format(root, false); - return buffer; + return buffer.toOwnedSlice(); } const HtmlFormatter = struct { diff --git a/src/main.zig b/src/main.zig index c96721a..712c96a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -6,37 +6,19 @@ const nodes = @import("nodes.zig"); const html = @import("html.zig"); pub fn main() !void { - var allocator = std.heap.GeneralPurposeAllocator(.{}){}; - defer _ = allocator.deinit(); + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); - var in_buffer = try std.ArrayList(u8).initCapacity(&allocator.allocator, 8192); - in_buffer.expandToCapacity(); + var markdown = try std.io.getStdIn().reader().readAllAlloc(&gpa.allocator, 1024 * 1024 * 1024); + defer gpa.allocator.free(markdown); - var length: usize = 0; - var reader = std.io.getStdIn().reader(); + var output = try markdownToHtml(&gpa.allocator, markdown); + defer gpa.allocator.free(output); - while (true) { - const capacity = in_buffer.capacity - length; - const read = try reader.readAll(in_buffer.items[length..]); - length += read; - if (read < capacity) - break; - - try in_buffer.ensureCapacity(length + 8192); - in_buffer.expandToCapacity(); - } - - in_buffer.items.len = length; - - defer in_buffer.deinit(); - - var buffer = try markdownToHtml(&allocator.allocator, in_buffer.span()); - defer buffer.deinit(); - - try std.io.getStdOut().writer().writeAll(buffer.span()); + try std.io.getStdOut().writer().writeAll(output); } -fn markdownToHtml(allocator: *std.mem.Allocator, markdown: []const u8) !std.ArrayList(u8) { +fn markdownToHtml(allocator: *std.mem.Allocator, markdown: []const u8) ![]u8 { var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); @@ -52,16 +34,13 @@ fn markdownToHtml(allocator: *std.mem.Allocator, markdown: []const u8) !std.Arra }; try parser.feed(markdown); var doc = try parser.finish(); + defer doc.deinit(); var noisy_env = std.process.getEnvVarOwned(&arena.allocator, "KOINO_NOISY") catch ""; const noisy = noisy_env.len > 0; doc.validate(noisy); - var buffer = try html.print(allocator, doc); - - doc.deinit(); - - return buffer; + return try html.print(allocator, doc); } test "" { @@ -69,11 +48,11 @@ test "" { } test "convert simple emphases" { - var allocator = std.heap.GeneralPurposeAllocator(.{}){}; - defer _ = allocator.deinit(); + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); - var buffer = try markdownToHtml(&allocator.allocator, "hello, _world_ __world__ ___world___ *_world_*\n\nthis is `yummy`\n"); - defer buffer.deinit(); + var output = try markdownToHtml(&gpa.allocator, "hello, _world_ __world__ ___world___ *_world_*\n\nthis is `yummy`\n"); + defer gpa.allocator.free(output); - std.testing.expectEqualStrings("

hello, world world world world\n

this is yummy

\n", buffer.span()); + std.testing.expectEqualStrings("

hello, world world world world\n

this is yummy

\n", output); }