]> git.ipfire.org Git - pakfire.git/commitdiff
string: Add a function to append something to a string
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 27 Jan 2025 17:35:45 +0000 (17:35 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 27 Jan 2025 17:35:45 +0000 (17:35 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/string.c
src/pakfire/string.h

index 16fd8c524acdafca6ded26b5e967343b5d01a14e..edd02eaf4a6d7521767b307c9e372c407f926fdf 100644 (file)
@@ -192,7 +192,7 @@ int pakfire_string_partition(const char* s, const char* delim, char** s1, char**
        return 0;
 }
 
-static int pakfire_string_append(char** buffer, const char* s, const size_t l) {
+static int __pakfire_string_replace(char** buffer, const char* s, const size_t l) {
        int r;
 
        // Check how long the string is that we are holding
@@ -237,7 +237,7 @@ char* pakfire_string_replace(const char* s, const char* pattern, const char* rep
 
                if (!m) {
                        // Copy the remaining string
-                       r = pakfire_string_append(&buffer, p, strlen(p));
+                       r = __pakfire_string_replace(&buffer, p, strlen(p));
                        if (r < 0)
                                goto ERROR;
 
@@ -248,11 +248,11 @@ char* pakfire_string_replace(const char* s, const char* pattern, const char* rep
                l = m - p;
 
                // Copy the read string up to pattern
-               r = pakfire_string_append(&buffer, p, l);
+               r = __pakfire_string_replace(&buffer, p, l);
                if (r < 0)
                        goto ERROR;
 
-               r = pakfire_string_append(&buffer, repl, repl_length);
+               r = __pakfire_string_replace(&buffer, repl, repl_length);
                if (r < 0)
                        goto ERROR;
 
@@ -345,6 +345,20 @@ char* pakfire_string_join(const char** list, const char* delim) {
        return string;
 }
 
+/*
+       Append the given string to the buffer
+*/
+int __pakfire_string_append(char* buffer, size_t length, const char* appendix) {
+       // Check inputs
+       if (!buffer || !appendix)
+               return -EINVAL;
+
+       // How long is the existing string?
+       const size_t l = strlen(buffer);
+
+       return __pakfire_string_set(buffer + l, length - l, appendix);
+}
+
 int pakfire_string_search(const char* haystack, ssize_t lhaystack,
                const char* needle, ssize_t lneedle) {
        // Check inputs
index 0757894c6240cd0f7551fdfe8185dc2e33869147..0e7f10913c59e3eaf464027b9e0ce44062a32a91 100644 (file)
@@ -85,6 +85,10 @@ static inline int pakfire_string_contains_whitespace(const char* s) {
        return 0;
 }
 
+#define pakfire_string_append(s, appendix) \
+       __pakfire_string_append(s, sizeof(s), appendix)
+int __pakfire_string_append(char* buffer, size_t length, const char* appendix);
+
 int pakfire_string_search(const char* haystack, ssize_t lhaystack,
        const char* needle, ssize_t lneedle);