Welcome to little lamb

Code » qmdoc » commit c6d756f

Add LF to the input to allow some parser optimization

author Olivier Brunel
2022-12-31 22:03:38 UTC
committer Olivier Brunel
2022-12-31 22:03:38 UTC
parent 703db07a19dd34a93f327aae7ae1e10c0c78d714

Add LF to the input to allow some parser optimization

main.c +6 -1

diff --git a/main.c b/main.c
index 110df56..968ebf3 100644
--- a/main.c
+++ b/main.c
@@ -889,13 +889,18 @@ convert_page(struct ctx *ctx, int fddest)
     if (fd < 0) ret_strerr_warnwu1sys(ERR_IO, "create destination");
 
     size_t salen = ctx->sa.len;
-    if (!stralloc_readyplus(&ctx->sa, p->size))
+    if (!stralloc_readyplus(&ctx->sa, p->size + 1))
         return ERR_MEM;
 
     if (allread(p->fd, ctx->sa.s + salen, p->size) != p->size)
         ret_strerr_warnwu1sys(ERR_IO, "read source file");
     ctx->sa.len += p->size;
 
+    /* ending on a new line allows parser optimization */
+    if (ctx->sa.s[ctx->sa.len - 1] != '\n') {
+        stralloc_catb(&ctx->sa, "\n", 1);
+    }
+
     const MD_PARSER parser = {
         .flags = MD_FLAG_COLLAPSEWHITESPACE | MD_FLAG_PERMISSIVEAUTOLINKS
             | MD_FLAG_NOHTMLBLOCKS | MD_FLAG_STRIKETHROUGH | MD_FLAG_UNDERLINE