Skip to content

Instantly share code, notes, and snippets.

@ryohji
Last active February 10, 2021 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryohji/2138a7e11ec66556769450a1bbc1d2f3 to your computer and use it in GitHub Desktop.
Save ryohji/2138a7e11ec66556769450a1bbc1d2f3 to your computer and use it in GitHub Desktop.
umoria roff
static vtype roffbuf; // Line buffer.
static char *roffp = roffbuf; // Pointer into line buffer.
static int roffpline = 0; // Place to print line now being loaded.
static uint8_t count_previous_non_blank_chars(const char *from);
// Print out strings, filling up lines as we go.
static void roff(const char *p) {
uint8_t count;
loop:
switch (*p) {
case '\0':
return;
case '\n':
p += 1;
count = 0;
goto flush;
default:
if (roffp + 1 == END_OF(roffbuf)) {
*roffp = *p++;
count = count_previous_non_blank_chars(roffp);
flush:
roffp[-count] = '\0';
prt(roffbuf, roffpline, 0);
roffpline += 1;
memcpy(roffbuf, roffp - count + 1, count);
roffp = roffbuf + count;
} else {
*roffp++ = *p++;
}
goto loop;
}
}
inline static uint8_t count_previous_non_blank_chars(const char *from) {
uint8_t offset = 0;
while (from[-offset] != ' ') {
offset += 1;
}
return offset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment