nihdoc

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

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

Add experimental home-rolled strcpy in name of performance

The previous implemenation of strcat needs to repeatedly (for every
character in the lnkdes) go to the end of the lnkdes to append to it.

I thought it _might_ be faster to replace it with a persistent pointer
to the end of the buf, and it is, so it's worth checking it in, but I'll
revert this immediately because the code-clarity ain't worth the speed
boost!

Diffstat:
Mnihdoc.c | 25++++++++++++++++++++++---
1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/nihdoc.c b/nihdoc.c @@ -7,7 +7,6 @@ #include <stdio.h> #include <unistd.h> #include <stdbool.h> -#include <string.h> /* Global Constants and Enums */ @@ -17,6 +16,12 @@ char *FMT_STRS[] = { ['`'] = "code", }; +int FMT_LENS[] = { + ['_'] = 2, + ['*'] = 6, + ['`'] = 4, +}; + enum Block { NONE, HEADER, @@ -43,6 +48,7 @@ int hlvl = 0; bool fmts[256] = {false}; /* indexed by _ * ` */ bool escape = false; char lnkdes[2048] = {0}; +char *lnkp = NULL; int indent = 0; int previndent = 0; int listdepth = 0; @@ -143,18 +149,30 @@ 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 == '*') { - snprintf(buf, 12, "<%s%s>", fmts[c] ? "/" : "", FMT_STRS[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]; + } fmts[c] ^= true; } else if (c == '\\') { escape = true; + len = 0; } else { buf[0] = c; } - strcat(lnkdes, buf); + /* copy into lnkdes starting at strp */ + for (int i = 0; i < len; i++) { + *(lnkp++) = buf[i]; + } + *lnkp = '\0'; } int parse() { @@ -255,6 +273,7 @@ int parse() { maybe_startp(); in_link = DESC_PARSE; lnkdes[0] = '\0'; + lnkp = lnkdes; } else { putesc(c); }