From d38f82f6462af4e5aad6a2c776f5c00ce5b13c87 Mon Sep 17 00:00:00 2001 From: Linnnus Date: Thu, 1 Feb 2024 22:59:38 +0100 Subject: feat: initial commit Here is a small overview of the state of the project at this first commit. I have basic Git Repo -> HTML working, and a plan for how setting up an actual server would work (mainly, NGINX + a git hook to rebuild). The main thing I'm working on right now is parsing WikiCreole, though I am starting to wonder if this is the right langauge. WikiCreole is pretty irregular and has a lot of edge cases (e.g. around emphasis). --- src/strutil.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/strutil.c (limited to 'src/strutil.c') diff --git a/src/strutil.c b/src/strutil.c new file mode 100644 index 0000000..9192989 --- /dev/null +++ b/src/strutil.c @@ -0,0 +1,58 @@ +#include "strutil.h" + +#include "arena.h" // struct arena, new +#include // assert +#include // va_* +#include // bool, false +#include // vsnprintf +#include // strlen, strncmp + +int aprintf(struct arena *a, char **out, const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + int ret = vaprintf(a, out, fmt, ap); + va_end(ap); + return ret; +} + +int vaprintf(struct arena *a, char **out, const char *fmt, va_list args) { + // Calculate size. + va_list tmp; + va_copy(tmp, args); + int size = vsnprintf(NULL, 0, fmt, args); + va_end(tmp); + + // If e.g. the format string was broken, we cannot continue. + if (size < 0) { + return -1; + } + + // Arena allocation cannot fail. + *out = new(a, char, size + 1); + + int t = vsnprintf(*out, size + 1, fmt, args); + assert(t == size); + + return size; +} + +char *joinpath(struct arena *a, const char *path_a, const char *path_b) { + char *out; + int ret = aprintf(a, &out, "%s/%s", path_a, path_b); + assert(ret > 0 && "should be infallible"); + return out; +} + +bool endswith(const char *haystack, const char *needle) { + assert(haystack != NULL); + assert(needle != NULL); + + size_t haystack_len = strlen(haystack); + size_t needle_len = strlen(needle); + + if (needle_len > haystack_len) { + return false; + } + + return strncmp(haystack + (haystack_len - needle_len), needle, needle_len) == 0; +} -- cgit v1.2.3