aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-06-17 14:00:33 +0530
committerRaghuram Subramani <raghus2247@gmail.com>2025-06-17 14:00:33 +0530
commit71d481b4559e85b57ba92fe7b06b7c074f1cafdd (patch)
tree5053ea42c2859652582e77ff40c8d4de7f43913a
parentf27b864591814a9e4630ef0ccc5349bf5bcd1366 (diff)
(find_next_key): manually find start and length instead of using regex
-rw-r--r--Makefile2
-rw-r--r--msg.c40
2 files changed, 21 insertions, 21 deletions
diff --git a/Makefile b/Makefile
index 6a13991..f84ab7c 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ CFLAGS += -lmarkdown
all: CFLAGS += -O3
all: clean msg
-debug: CFLAGS += -Og -g3 -glldb
+debug: CFLAGS += -O0 -g3 -glldb
debug: clean msg
msg: msg.c
diff --git a/msg.c b/msg.c
index fd91faf..badf6f8 100644
--- a/msg.c
+++ b/msg.c
@@ -3,7 +3,6 @@
#include <ctype.h>
#include <libgen.h>
#include <mkdio.h>
-#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -39,28 +38,26 @@ char *base_post;
key_match_t *
find_next_key(char *buffer)
{
- regex_t regex;
- regcomp(&regex, "{{[^}]*}}", 0);
-
- regmatch_t pmatch[1];
- regoff_t offset, length;
+ key_match_t *match = calloc(1, sizeof(key_match_t));
- int file_offset = 0;
- int ret;
- ret = regexec(&regex, buffer, ARRAY_SIZE(pmatch), pmatch, 0);
- if (ret == REG_NOMATCH)
- return NULL;
+ for (size_t i = 0; i < strlen(buffer) - 1; i++) {
+ if (buffer[i] == '{' && buffer[i + 1] == '{')
+ match->offset = i;
- key_match_t *match = calloc(1, sizeof(key_match_t));
- offset = pmatch[0].rm_so;
- length = pmatch[0].rm_eo - pmatch[0].rm_so;
+ if (i == strlen(buffer) - 1)
+ return NULL;
+ }
- match->length = length;
- match->offset = file_offset + offset;
+ char *subbuffer = buffer + match->offset;
+ for (size_t i = 0; i < strlen(buffer) - 1; i++) {
+ if (subbuffer[i] == '}' && subbuffer[i + 1] == '}')
+ match->length = i + 2;
- file_offset = offset + length;
- buffer += file_offset;
- regfree(&regex);
+ if (i == strlen(buffer) - 1) {
+ printf("Unterminated Key\n");
+ return NULL;
+ }
+ }
return match;
}
@@ -120,6 +117,8 @@ ingest(char **buffer)
break;
directive_t *directive = find_directive(*buffer, match);
+ if (directive == NULL)
+ break;
if (directive->type == INCLUDE) {
char *operand = (char *) directive->operands;
@@ -224,7 +223,8 @@ handle_file(const char *path)
markdown(doc, out, 0);
fprintf(out, "%s", base_post);
} else {
- ingest(&buffer);
+ if (strlen(buffer) != 0)
+ ingest(&buffer);
fprintf(out, "%s%s%s", base_pre, buffer, base_post);
}