From: Michael Tremer Date: Sat, 11 Jan 2025 13:52:23 +0000 (+0000) Subject: parser: Write a better implementation to replace newlines X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0c26648f0c170a0ed5cb749be3670d9605a351a2;p=people%2Fric9%2Fpakfire.git parser: Write a better implementation to replace newlines This performs the operating on the existing buffer and should be more straight forward than the previous one. Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/parser/scanner.l b/src/pakfire/parser/scanner.l index f90b5b525..e9473beb8 100644 --- a/src/pakfire/parser/scanner.l +++ b/src/pakfire/parser/scanner.l @@ -82,33 +82,8 @@ static char* copy_string(const char* s) { // Strip any leading or trailing whitespace pakfire_string_strip(buffer); - // Pointer to the start of the string - char* p = buffer; - - // Determine the length of the string - const size_t l = strlen(s); - - char* linebreak = strstr(p, "\\\n"); - while (linebreak) { - // Move p to the beginning of the linebreak - p = linebreak; - - // Skip \\\n - linebreak += strlen("\\\n"); - - // Find any whitespace after the linebreak - while (*linebreak && isspace(*linebreak)) - linebreak++; - - if (!linebreak) - break; - - // Splice together - memmove(p, linebreak, buffer + l - linebreak + 1); - - // Find another linebreak - linebreak = strstr(p, "\\\n"); - } + // Remove any linebreaks + pakfire_string_remove_linebreaks(buffer); return buffer; } diff --git a/src/pakfire/string.c b/src/pakfire/string.c index fba9dc00f..a5de360d7 100644 --- a/src/pakfire/string.c +++ b/src/pakfire/string.c @@ -270,6 +270,29 @@ ERROR: return NULL; } +void pakfire_string_remove_linebreaks(char* src) { + char* dst = src; + + while (*src) { + // Check for "\" at the end of a line + if (src[0] == '\\' && src[1] == '\n') { + // Skip the string + src += strlen("\\\n"); + + // Consume any following whitespace + while (*src && isspace(*src)) + src++; + + // Otherwise copy the entire string + } else { + *dst++ = *src++; + } + } + + // Ensure the string is terminated + *dst = '\0'; +} + char* pakfire_string_join(const char** list, const char* delim) { // Validate input if (!list || !delim) { diff --git a/src/pakfire/string.h b/src/pakfire/string.h index 632fda1dd..481b6b278 100644 --- a/src/pakfire/string.h +++ b/src/pakfire/string.h @@ -64,6 +64,7 @@ void pakfire_string_unquote(char* s); int pakfire_string_partition(const char* s, const char* delim, char** s1, char** s2); char* pakfire_string_replace(const char* s, const char* pattern, const char* repl); +void pakfire_string_remove_linebreaks(char* src); char* pakfire_string_join(const char** list, const char* delim); inline int pakfire_string_equals(const char* s1, const char* s2) {