]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
string-util: modernize strextendn() a bit 36343/head
authorMike Yuan <me@yhndnzj.com>
Mon, 10 Feb 2025 18:14:49 +0000 (19:14 +0100)
committerMike Yuan <me@yhndnzj.com>
Mon, 10 Feb 2025 18:39:22 +0000 (19:39 +0100)
l == SIZE_MAX requires no special handling, since we assert
on (s || l == 0) above.

src/basic/string-util.c
src/basic/string-util.h

index 196f3ff1a12829840a1be3dddf77f7eaed1b7601..7f4be13b62af41beaa8fb2d0af85bc48daf9b7b4 100644 (file)
@@ -60,6 +60,30 @@ char* strprepend(char **x, const char *s) {
         return *x;
 }
 
+char* strextendn(char **x, const char *s, size_t l) {
+        assert(x);
+        assert(s || l == 0);
+
+        if (l > 0)
+                l = strnlen(s, l); /* ignore trailing noise */
+
+        if (l > 0 || !*x) {
+                size_t q;
+                char *m;
+
+                q = strlen_ptr(*x);
+                m = realloc(*x, q + l + 1);
+                if (!m)
+                        return NULL;
+
+                *mempcpy_typesafe(m + q, s, l) = 0;
+
+                *x = m;
+        }
+
+        return *x;
+}
+
 char* strstrip(char *s) {
         if (!s)
                 return NULL;
@@ -958,33 +982,6 @@ oom:
         return -ENOMEM;
 }
 
-char* strextendn(char **x, const char *s, size_t l) {
-        assert(x);
-        assert(s || l == 0);
-
-        if (l == SIZE_MAX)
-                l = strlen_ptr(s);
-        else if (l > 0)
-                l = strnlen(s, l); /* ignore trailing noise */
-
-        if (l > 0 || !*x) {
-                size_t q;
-                char *m;
-
-                q = strlen_ptr(*x);
-                m = realloc(*x, q + l + 1);
-                if (!m)
-                        return NULL;
-
-                memcpy_safe(m + q, s, l);
-                m[q + l] = 0;
-
-                *x = m;
-        }
-
-        return *x;
-}
-
 char* strrep(const char *s, unsigned n) {
         char *r, *p;
         size_t l;
index 6a30193746f9d2c97d96214d69f6c1d011ddd794..8a5df741cc0293ac413bb29d422dc5a8fbe313fe 100644 (file)
@@ -107,6 +107,7 @@ static inline const char* empty_or_dash_to_null(const char *p) {
 char* first_word(const char *s, const char *word) _pure_;
 
 char* strprepend(char **x, const char *s);
+char* strextendn(char **x, const char *s, size_t l);
 
 #define strjoin(a, ...) strextend_with_separator_internal(NULL, NULL, a, __VA_ARGS__, NULL)
 
@@ -193,8 +194,6 @@ char* strextend_with_separator_internal(char **x, const char *separator, ...) _s
 #define strextend_with_separator(x, separator, ...) strextend_with_separator_internal(x, separator, __VA_ARGS__, NULL)
 #define strextend(x, ...) strextend_with_separator_internal(x, NULL, __VA_ARGS__, NULL)
 
-char* strextendn(char **x, const char *s, size_t l);
-
 int strextendf_with_separator(char **x, const char *separator, const char *format, ...) _printf_(3,4);
 #define strextendf(x, ...) strextendf_with_separator(x, NULL, __VA_ARGS__)