summaryrefslogtreecommitdiffhomepage
path: root/lib.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-08-23 15:38:26 +0200
committerJo-Philipp Wich <jo@mein.io>2022-08-24 15:33:18 +0200
commit8e8dae0eb0f90dea3cb4b244c79f7fa855219f92 (patch)
tree9adbcdd3197f27039a76e0f8cd0411946bd39272 /lib.c
parent476f02bfa397959ba8a553a36cf5eaffc82b8f07 (diff)
lib: introduce helper function for indenting error messages
Factor out the nested syntax error message indentation logic into a separate helper procedure for reuse in other places. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib.c b/lib.c
index 4c9ff81..fcdd17b 100644
--- a/lib.c
+++ b/lib.c
@@ -208,6 +208,40 @@ uc_error_context_format(uc_stringbuf_t *buf, uc_source_t *src, uc_value_t *stack
return uc_source_context_format(buf, src, off, false);
}
+void
+uc_error_message_indent(char **msg) {
+ uc_stringbuf_t *buf = xprintbuf_new();
+ char *s, *p, *nl;
+ size_t len;
+
+ if (!msg || !*msg)
+ return;
+
+ s = *msg;
+ len = strlen(s);
+
+ while (len > 0 && s[len-1] == '\n')
+ s[--len] = 0;
+
+ for (p = s, nl = strchr(p, '\n'); p != NULL;
+ p = nl ? nl + 1 : NULL, nl = p ? strchr(p, '\n') : NULL)
+ {
+ if (!nl)
+ ucv_stringbuf_printf(buf, " | %s", p);
+ else if (nl != p)
+ ucv_stringbuf_printf(buf, " | %.*s\n", (int)(nl - p), p);
+ else
+ ucv_stringbuf_append(buf, " |\n");
+ }
+
+ ucv_stringbuf_append(buf, "\n");
+
+ *msg = buf->buf;
+
+ free(buf);
+ free(s);
+}
+
static char *uc_cast_string(uc_vm_t *vm, uc_value_t **v, bool *freeable) {
if (ucv_type(*v) == UC_STRING) {
*freeable = false;