all repos — hex @ 3cea92f9abf820e9d35830782ea2e02854c20b63

A tiny, minimalist, slightly-esoteric concatenative programming lannguage.

Improving string processing.
h3rald h3rald@h3rald.com
Wed, 20 Nov 2024 15:31:08 +0100
commit

3cea92f9abf820e9d35830782ea2e02854c20b63

parent

b6011edaae35414ecc9bf0a99c85d7c77064406a

1 files changed, 56 insertions(+), 1 deletions(-)

jump to
M hex.chex.c

@@ -291,7 +291,62 @@ }

int hex_push_string(const char *value) { - HEX_StackElement element = {.type = HEX_TYPE_STRING, .data.strValue = strdup(value)}; + size_t len = strlen(value); + char *processedStr = (char *)malloc(len + 1); + if (!processedStr) + { + hex_error("Memory allocation failed"); + return 1; + } + + char *dst = processedStr; + const char *src = value; + while (*src) + { + if (*src == '\\' && *(src + 1)) + { + src++; + switch (*src) + { + case 'n': + *dst++ = '\n'; + break; + case 't': + *dst++ = '\t'; + break; + case 'r': + *dst++ = '\r'; + break; + case 'b': + *dst++ = '\b'; + break; + case 'f': + *dst++ = '\f'; + break; + case 'v': + *dst++ = '\v'; + break; + case '\\': + *dst++ = '\\'; + break; + case '\"': + *dst++ = '\"'; + break; + default: + *dst++ = '\\'; + *dst++ = *src; + break; + } + } + else + { + *dst++ = *src; + } + src++; + } + *dst = '\0'; + + HEX_StackElement element = {.type = HEX_TYPE_STRING, .data.strValue = processedStr}; return hex_push(element); }