Add uc_dstr_copy
This commit is contained in:
+21
@@ -17,6 +17,27 @@ void uc_dstr_destroy(struct DStr *str)
|
||||
free(str->str);
|
||||
}
|
||||
|
||||
int uc_dstr_copy(struct DStr *dest, const struct DStr *src)
|
||||
{
|
||||
char *s;
|
||||
|
||||
uc_dstr_init(dest);
|
||||
if (src->allocated == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
s = uc_dstr_put(dest, src->len);
|
||||
if (s == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
//+1 to copy a potential nul terminator, uc_dstr_put
|
||||
//guarantees we have room for it.
|
||||
memcpy(s, src->str, UC_MIN(src->len + 1, src->allocated));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t uc_dstr_available(const struct DStr *str)
|
||||
{
|
||||
return str->allocated - str->len;
|
||||
|
||||
Reference in New Issue
Block a user