From: Michael Tremer Date: Mon, 27 Jan 2025 17:35:45 +0000 (+0000) Subject: string: Add a function to append something to a string X-Git-Tag: 0.9.30~334 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4db2274e256873867f3b7b1d049248fcab98119d;p=pakfire.git string: Add a function to append something to a string Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/string.c b/src/pakfire/string.c index 16fd8c52..edd02eaf 100644 --- a/src/pakfire/string.c +++ b/src/pakfire/string.c @@ -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 diff --git a/src/pakfire/string.h b/src/pakfire/string.h index 0757894c..0e7f1091 100644 --- a/src/pakfire/string.h +++ b/src/pakfire/string.h @@ -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);