From: Michael Tremer Date: Mon, 27 Jan 2025 17:51:52 +0000 (+0000) Subject: string: Make setting a string faster X-Git-Tag: 0.9.30~331 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=756ab66138293ca00d1ac8f890aea197c272c08e;p=pakfire.git string: Make setting a string faster This avoids calling a lot of libc functions that we actually don't need. Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/string.c b/src/pakfire/string.c index edd02eaf..3061da75 100644 --- a/src/pakfire/string.c +++ b/src/pakfire/string.c @@ -59,14 +59,24 @@ int __pakfire_string_vformat(char* s, const size_t length, const char* format, v int __pakfire_string_set(char* s, const size_t length, const char* value) { // If value is NULL or an empty, we will overwrite the buffer with just zeros - if (!value || !*value) { + if (!value) { *s = '\0'; - return 0; } - // Otherwise just copy - return __pakfire_string_format(s, length, "%s", value); + // Copy everything until we hit the end of the input string + for (unsigned int i = 0; i < length; i++) { + s[i] = value[i]; + + // Done + if (value[i] == '\0') + return 0; + } + + // It seems that the buffer was not large enough. Terminate and return an error. + s[length - 1] = '\0'; + + return -ENOBUFS; } int __pakfire_string_setn(char* s, const size_t length, const char* value, const size_t l) {