0
0
mirror of https://github.com/netwide-assembler/nasm.git synced 2025-07-24 10:25:42 -04:00

nasmlib: add nasm_strappend()

nasm_strappend() is similar to nasm_strcat() for the case where the
first string isn't useful and should be freed.  Furthermore, one or
both of the arguments are allowed to be NULL.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin (Intel) 2018-06-25 22:13:42 -07:00
parent d2cdb72e88
commit a227b43b8a
2 changed files with 20 additions and 0 deletions

View File

@ -80,6 +80,7 @@ void nasm_free(void *);
char * safe_alloc nasm_strdup(const char *); char * safe_alloc nasm_strdup(const char *);
char * safe_alloc nasm_strndup(const char *, size_t); char * safe_alloc nasm_strndup(const char *, size_t);
char * safe_alloc nasm_strcat(const char *one, const char *two); char * safe_alloc nasm_strcat(const char *one, const char *two);
char * never_null nasm_strappend(char *one, const char *two);
char * safe_alloc end_with_null nasm_strcatn(const char *one, ...); char * safe_alloc end_with_null nasm_strcatn(const char *one, ...);
/* Assert the argument is a pointer without evaluating it */ /* Assert the argument is a pointer without evaluating it */

View File

@ -110,6 +110,25 @@ char *nasm_strcat(const char *one, const char *two)
return rslt; return rslt;
} }
/* Unlike nasm_strcat() this frees the initial string, which can be NULL */
char *nasm_strappend(char *one, const char *two)
{
size_t l1, l2;
if (!two)
return one;
if (!one)
return nasm_strdup(two);
l1 = strlen(one);
l2 = strlen(two) + 1;
one = nasm_realloc(one, l1+l2);
memcpy(one + l1, two, l2);
return one;
}
char *nasm_strcatn(const char *str1, ...) char *nasm_strcatn(const char *str1, ...)
{ {
va_list ap; va_list ap;