nihdoc

WIP markup parser (txt -> html)
git clone git://git.alexkarle.com.com/blag
Log | Files | Refs | README | LICENSE

commit c083e8917360793a1dac9050c04cb3e7e85d6202 (patch)
parent dcc2b5252bfe66a23571ca003716c68c6715b9f6
Author: Alex Karle <alex@alexkarle.com>
Date:   Tue, 28 Dec 2021 12:37:12 -0500

Replace home-rolled strcpy with strlcat

This reverts commit dcc2b5252bfe66a23571ca003716c68c6715b9f6.

The time savings from using a home-rolled strcpy implementation vs
strlcat (which is safer both in that I don't need to manipulate pointers
AND it handles null termination) are negligible (not observable in 1000
runs). I suppose it might have an impact for super long link
descriptions, but I don't expect many of those...

Diffstat:
Mnihdoc.c | 29++++++-----------------------
1 file changed, 6 insertions(+), 23 deletions(-)

diff --git a/nihdoc.c b/nihdoc.c @@ -7,6 +7,9 @@ #include <stdio.h> #include <unistd.h> #include <stdbool.h> +#include <string.h> + +#define LNK_DES_MAX 2048 /* Global Constants and Enums */ @@ -16,12 +19,6 @@ char *FMT_STRS[] = { ['`'] = "code", }; -int FMT_LENS[] = { - ['_'] = 2, - ['*'] = 6, - ['`'] = 4, -}; - enum Block { NONE, HEADER, @@ -47,8 +44,7 @@ enum Link in_link = NOL; int hlvl = 0; bool fmts[256] = {false}; /* indexed by _ * ` */ bool escape = false; -char lnkdes[2048] = {0}; -char *lnkp = NULL; +char lnkdes[LNK_DES_MAX] = {0}; int indent = 0; int previndent = 0; int listdepth = 0; @@ -149,30 +145,18 @@ void toggle_format() { void pushbuf() { /* push description onto lnkdes, with formatting! */ char buf[12] = {0}; - int len = 1; if (escape) { buf[0] = c; escape = false; } else if (c == '`' || c == '_' || c == '*') { - if (fmts[c]) { - snprintf(buf, 12, "</%s>", FMT_STRS[c]); - len = 3 + FMT_LENS[c]; - } else { - snprintf(buf, 12, "<%s>", FMT_STRS[c]); - len = 2 + FMT_LENS[c]; - } + snprintf(buf, 12, "<%s%s>", fmts[c] ? "/" : "", FMT_STRS[c]); fmts[c] ^= true; } else if (c == '\\') { escape = true; - len = 0; } else { buf[0] = c; } - /* copy into lnkdes starting at strp */ - for (int i = 0; i < len; i++) { - *(lnkp++) = buf[i]; - } - *lnkp = '\0'; + escape || strlcat(lnkdes, buf, LNK_DES_MAX); } int parse() { @@ -273,7 +257,6 @@ int parse() { maybe_startp(); in_link = DESC_PARSE; lnkdes[0] = '\0'; - lnkp = lnkdes; } else { putesc(c); }