string-util: modernize strextendn() a bit

l == SIZE_MAX requires no special handling, since we assert
on (s || l == 0) above.
This commit is contained in:
Mike Yuan
2025-02-10 19:14:49 +01:00
parent 146b2ed6a6
commit 63ed611579
2 changed files with 25 additions and 29 deletions

View 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;