]> git.ipfire.org Git - pakfire.git/commitdiff
string: Make setting a string faster
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 27 Jan 2025 17:51:52 +0000 (17:51 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 27 Jan 2025 17:51:52 +0000 (17:51 +0000)
This avoids calling a lot of libc functions that we actually don't need.

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

index edd02eaf4a6d7521767b307c9e372c407f926fdf..3061da75e2cd13d4ec43057491111febd85d7744 100644 (file)
@@ -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) {