Add uc_str_append_all function
This commit is contained in:
@@ -83,6 +83,18 @@ void uc_strv_clear(struct UCStrv *strv);
|
|||||||
*/
|
*/
|
||||||
void uc_strv_destroy(struct UCStrv *strv);
|
void uc_strv_destroy(struct UCStrv *strv);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append all the elments from b to a
|
||||||
|
* If copy is 1, the appended elements will be copied when appended
|
||||||
|
*
|
||||||
|
* @param a destination UCStrv
|
||||||
|
* @param b source UCStrv
|
||||||
|
* @param copy 1 for making a copy of the string when appending it
|
||||||
|
* 0 to just append the same pointer in b to a
|
||||||
|
* @return 0 on success, != 0 if appending/allocation fails
|
||||||
|
*/
|
||||||
|
int uc_strv_append_all(struct UCStrv *a, struct UCStrv *b, int copy);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -105,3 +105,28 @@ void uc_strv_destroy(struct UCStrv *strv)
|
|||||||
strv->strings = NULL;
|
strv->strings = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int uc_strv_append_all(struct UCStrv *a, struct UCStrv *b, int copy)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
rc = uc_strv_grow(a, a->allocated + b->cnt);
|
||||||
|
if (rc != 0) {
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < b->cnt; i++) {
|
||||||
|
if (copy) {
|
||||||
|
rc = uc_strv_append(a, b->strings[i]);
|
||||||
|
} else {
|
||||||
|
rc = uc_strv_append_nocopy(a, b->strings[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rc != 0) {
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include <check.h>
|
#include <check.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <ucore/strvec.h>
|
#include <ucore/strvec.h>
|
||||||
|
|
||||||
|
|
||||||
@@ -73,6 +74,54 @@ START_TEST (test_strv_null_terminate)
|
|||||||
|
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
|
START_TEST (test_strv_append_all_copy)
|
||||||
|
struct UCStrv src;
|
||||||
|
struct UCStrv dst;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
uc_strv_init(&src);
|
||||||
|
uc_strv_init(&dst);
|
||||||
|
|
||||||
|
uc_strv_append(&dst, "string1");
|
||||||
|
uc_strv_append(&src, "string2");
|
||||||
|
uc_strv_append(&src, "string3");
|
||||||
|
|
||||||
|
rc = uc_strv_append_all(&dst, &src, 1);
|
||||||
|
fail_if(rc != 0);
|
||||||
|
fail_if(dst.cnt != 3);
|
||||||
|
ck_assert_str_eq("string1", dst.strings[0]);
|
||||||
|
ck_assert_str_eq("string2", dst.strings[1]);
|
||||||
|
ck_assert_str_eq("string3", dst.strings[2]);
|
||||||
|
|
||||||
|
uc_strv_destroy(&src);
|
||||||
|
uc_strv_destroy(&dst);
|
||||||
|
|
||||||
|
END_TEST
|
||||||
|
|
||||||
|
START_TEST (test_strv_append_all_nocopy)
|
||||||
|
struct UCStrv src;
|
||||||
|
struct UCStrv dst;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
uc_strv_init(&src);
|
||||||
|
uc_strv_init(&dst);
|
||||||
|
|
||||||
|
uc_strv_append(&dst, "string1");
|
||||||
|
uc_strv_append(&src, "string2");
|
||||||
|
uc_strv_append(&src, "string3");
|
||||||
|
|
||||||
|
rc = uc_strv_append_all(&dst, &src, 0);
|
||||||
|
fail_if(rc != 0);
|
||||||
|
fail_if(dst.cnt != 3);
|
||||||
|
ck_assert_str_eq("string1", dst.strings[0]);
|
||||||
|
ck_assert_str_eq("string2", dst.strings[1]);
|
||||||
|
ck_assert_str_eq("string3", dst.strings[2]);
|
||||||
|
|
||||||
|
uc_strv_destroy(&dst);
|
||||||
|
free(src.strings);
|
||||||
|
|
||||||
|
END_TEST
|
||||||
|
|
||||||
|
|
||||||
Suite *strv_suite(void)
|
Suite *strv_suite(void)
|
||||||
{
|
{
|
||||||
@@ -83,6 +132,8 @@ Suite *strv_suite(void)
|
|||||||
tcase_add_test(tc, test_strv_clear);
|
tcase_add_test(tc, test_strv_clear);
|
||||||
tcase_add_test(tc, test_strv_nocopy);
|
tcase_add_test(tc, test_strv_nocopy);
|
||||||
tcase_add_test(tc, test_strv_null_terminate);
|
tcase_add_test(tc, test_strv_null_terminate);
|
||||||
|
tcase_add_test(tc, test_strv_append_all_copy);
|
||||||
|
tcase_add_test(tc, test_strv_append_all_nocopy);
|
||||||
|
|
||||||
suite_add_tcase(s, tc);
|
suite_add_tcase(s, tc);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user