Add uc_dstr_copy
This commit is contained in:
@@ -40,6 +40,15 @@ void uc_dstr_init(struct DStr *str);
|
|||||||
*/
|
*/
|
||||||
void uc_dstr_destroy(struct DStr *str);
|
void uc_dstr_destroy(struct DStr *str);
|
||||||
|
|
||||||
|
/** Copy a DStr
|
||||||
|
*
|
||||||
|
* @param dest destination string to copy to, must not contain an existing string
|
||||||
|
* @param src DStr to copy
|
||||||
|
*
|
||||||
|
* @return 0 if success, != 0 if allocation fails
|
||||||
|
*/
|
||||||
|
int uc_dstr_copy(struct DStr *dest, const struct DStr *src);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the available/unused tail bytes in the string.
|
* @return the available/unused tail bytes in the string.
|
||||||
*/
|
*/
|
||||||
|
|||||||
+21
@@ -17,6 +17,27 @@ void uc_dstr_destroy(struct DStr *str)
|
|||||||
free(str->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)
|
size_t uc_dstr_available(const struct DStr *str)
|
||||||
{
|
{
|
||||||
return str->allocated - str->len;
|
return str->allocated - str->len;
|
||||||
|
|||||||
@@ -140,6 +140,33 @@ START_TEST (test_dstr_sprintf)
|
|||||||
|
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
|
START_TEST (test_dstr_copy)
|
||||||
|
struct DStr str;
|
||||||
|
struct DStr copy;
|
||||||
|
size_t rc;
|
||||||
|
|
||||||
|
uc_dstr_init(&str);
|
||||||
|
rc = uc_dstr_put_str(&str, "123");
|
||||||
|
ck_assert_int_eq(rc, 0);
|
||||||
|
|
||||||
|
rc = uc_dstr_copy(©, &str);
|
||||||
|
ck_assert_int_eq(rc, 0);
|
||||||
|
ck_assert_str_eq(uc_dstr_str(©), uc_dstr_str(&str));
|
||||||
|
|
||||||
|
uc_dstr_destroy(&str);
|
||||||
|
uc_dstr_destroy(©);
|
||||||
|
uc_dstr_init(&str);
|
||||||
|
uc_dstr_init(©);
|
||||||
|
|
||||||
|
rc = uc_dstr_copy(©, &str);
|
||||||
|
ck_assert_int_eq(rc, 0);
|
||||||
|
ck_assert_str_eq(uc_dstr_str(©), "");
|
||||||
|
|
||||||
|
uc_dstr_destroy(&str);
|
||||||
|
uc_dstr_destroy(©);
|
||||||
|
|
||||||
|
END_TEST
|
||||||
|
|
||||||
Suite *dstr_suite(void)
|
Suite *dstr_suite(void)
|
||||||
{
|
{
|
||||||
Suite *s = suite_create("dstr");
|
Suite *s = suite_create("dstr");
|
||||||
@@ -150,6 +177,7 @@ Suite *dstr_suite(void)
|
|||||||
tcase_add_test(tc, test_dstr_replace);
|
tcase_add_test(tc, test_dstr_replace);
|
||||||
tcase_add_test(tc, test_dstr_filter);
|
tcase_add_test(tc, test_dstr_filter);
|
||||||
tcase_add_test(tc, test_dstr_sprintf);
|
tcase_add_test(tc, test_dstr_sprintf);
|
||||||
|
tcase_add_test(tc, test_dstr_copy);
|
||||||
|
|
||||||
suite_add_tcase(s, tc);
|
suite_add_tcase(s, tc);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user