Possible macro?:
#define utstring_clone(dst, src) \
do { \
if ((src)->d != NULL) { \
utstring_new(dst); \
(dst)->n = (src)->n; \
(dst)->i = (src)->i; \
memcpy((dst)->d, (src)->d, (src)->n); \
} else { \
(dst) = NULL; \
} \
} while (0)
---
#include <stdio.h>
#include <string.h>
#include "utstring.h"
int main() {
UT_string *s, *c;
utstring_new(s);
utstring_printf(s, "hello world!");
printf("source -> %s\n", utstring_body(s));
utstring_clone(c, s);
printf("cloned -> %s\n", utstring_body(c));
utstring_free(s);
utstring_free(c);
return 0;
}
./a.out
source -> hello world!
cloned -> hello world!
(tested only on GCC)
I'm not sure if MSVC allows macro returning. 🤔 But I'm going to test it in MSVC 15 2017:
#define utstring_clone(src) \
({ \
UT_string *dst = NULL; \
if ((src)->d != NULL) { \
utstring_new(dst); \
(dst)->n = (src)->n; \
(dst)->i = (src)->i; \
memcpy((dst)->d, (src)->d, (src)->n); \
} \
dst; \
})
it allows to use as:
#include <stdio.h>
#include <string.h>
#include "utstring.h"
int main() {
UT_string *s, *c;
utstring_new(s);
utstring_printf(s, "hello world!");
printf("source -> %s\n", utstring_body(s));
c = utstring_clone(s);
printf("cloned -> %s\n", utstring_body(c));
utstring_free(s);
utstring_free(c);
return 0;
}
./a.out
source -> hello world!
cloned -> hello world!