From 9212ff0e97423b33432dc84133ebaeeaa1d6f217 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Sat, 18 Oct 2025 10:36:08 +0200 Subject: [PATCH] ulib String tokenization with line continuation --- ulib/string/string.c | 70 ++++++++++++++++++++++++++++++++++++++++++++ ulib/string/string.h | 1 + 2 files changed, 71 insertions(+) diff --git a/ulib/string/string.c b/ulib/string/string.c index d2277ea..e88385a 100644 --- a/ulib/string/string.c +++ b/ulib/string/string.c @@ -173,6 +173,76 @@ char *string_tokenizealloc(char *s, char *delim) { return w; } +char *string_tokenizealloc_linecontinue(char *s, char *delim) { + static char *saved = NULL; + + if (s) { + saved = s; + } + + if (!delim || !saved) { + return NULL; + } + + while (*saved && string_strchr(delim, *saved)) { + saved++; + } + + if (!*saved) { + return NULL; + } + + char *w = (char *)umalloc(sizeof(char) * STRING_TOKENIZEALLOC_TOK_SIZE); + string_memset(w, 0, sizeof(char) * STRING_TOKENIZEALLOC_TOK_SIZE); + + int k = 0; + + while (*saved) { + if (*saved == '\\') { + char *p = saved; + int bs_count = 0; + + while (*p == '\\') { + bs_count++; + p++; + } + + if ((*p == '\n' || (*p == '\r' && (*p + 1) == '\n')) && (bs_count % 2 == 1)) { + int tocopy = bs_count / 2; + while (tocopy-- > 0 && k < STRING_TOKENIZEALLOC_TOK_SIZE - 1) { + w[k++] = '\\'; + } + + if (*p == '\r' & *(p + 1) == '\n') { + saved = p + 2; + } else { + saved = p + 1; + } + + continue; + } + } + + if (string_strchr(delim, *saved)) { + break; + } + + if (k >= STRING_TOKENIZEALLOC_TOK_SIZE - 1) { + break; + } + + w[k++] = *saved++; + } + + w[k] = '\0'; + + if (*saved) { + saved++; + } + + return w; +} + // https://stackoverflow.com/questions/2488563/strcat-implementation char *string_combine(char *dest, const char *src) { size_t i, j; diff --git a/ulib/string/string.h b/ulib/string/string.h index b10d495..daee531 100644 --- a/ulib/string/string.h +++ b/ulib/string/string.h @@ -17,6 +17,7 @@ char *string_strcpy(char *dest, const char *src); char *string_strchr(const char *s, int c); int string_strncmp(const char * s1, const char * s2, size_t n); char *string_tokenizealloc(char *s, char *delim); +char *string_tokenizealloc_linecontinue(char *s, char *delim); char *string_combine(char *dest, const char *src); void * string_memmove(void* dest, const void* src, unsigned int n);