ulib String tokenization with line continuation

This commit is contained in:
2025-10-18 10:36:08 +02:00
parent 76faf0581d
commit 9212ff0e97
2 changed files with 71 additions and 0 deletions

View File

@ -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;

View File

@ -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);