]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
parser: Write a better implementation to replace newlines
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 13:52:23 +0000 (13:52 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 13:52:23 +0000 (13:52 +0000)
This performs the operating on the existing buffer and should be more
straight forward than the previous one.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/parser/scanner.l
src/pakfire/string.c
src/pakfire/string.h

index f90b5b5250c08b2ec0dcbb51aeb2161857a81533..e9473beb862682f5aab372ce9516924994ec337c 100644 (file)
@@ -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;
 }
index fba9dc00f7e873f84b01149ece27ef7e30f3999c..a5de360d72fead7da81eabcd7645ce26b70940fc 100644 (file)
@@ -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) {
index 632fda1dd7981af1470cce3f27797a081559c3ab..481b6b278444d3af121afba310c26f8ff3b0063c 100644 (file)
@@ -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) {