Add init method to dstr for initializing directyl from a string
Change the api so _put_ functions that copy in data are named "_append_" instead.
This commit is contained in:
+28
-9
@@ -12,17 +12,17 @@ START_TEST (test_dstr_basics)
|
||||
char *s;
|
||||
|
||||
uc_dstr_init(&str);
|
||||
rc = uc_dstr_put_str(&str, "Hello");
|
||||
rc = uc_dstr_append_str(&str, "Hello");
|
||||
ck_assert_int_eq(rc, 0);
|
||||
ck_assert_int_eq(str.len, 5);
|
||||
ck_assert_str_eq(uc_dstr_str(&str), "Hello");
|
||||
|
||||
rc = uc_dstr_put_char(&str, '!');
|
||||
rc = uc_dstr_append_char(&str, '!');
|
||||
ck_assert_int_eq(rc, 0);
|
||||
ck_assert_int_eq(str.len, 6);
|
||||
ck_assert_str_eq(uc_dstr_str(&str), "Hello!");
|
||||
|
||||
rc = uc_dstr_put_str_sz(&str, " World 12345678", 13);
|
||||
rc = uc_dstr_append_str_sz(&str, " World 12345678", 13);
|
||||
ck_assert_int_eq(rc, 0);
|
||||
ck_assert_int_eq(str.len, 19);
|
||||
ck_assert_str_eq(uc_dstr_str(&str), "Hello! World 123456");
|
||||
@@ -48,6 +48,24 @@ START_TEST (test_dstr_basics)
|
||||
uc_dstr_destroy(&str);
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_dstr_init)
|
||||
struct DStr str;
|
||||
int rc;
|
||||
|
||||
rc = uc_dstr_init_str(&str, "Hello");
|
||||
ck_assert_int_eq(rc, 0);
|
||||
ck_assert_int_eq(str.len, 5);
|
||||
ck_assert_str_eq(uc_dstr_str(&str), "Hello");
|
||||
|
||||
uc_dstr_destroy(&str);
|
||||
|
||||
rc = uc_dstr_init_str_sz(&str, "Hello", 2);
|
||||
ck_assert_int_eq(rc, 0);
|
||||
ck_assert_int_eq(str.len, 2);
|
||||
ck_assert_str_eq(uc_dstr_str(&str), "He");
|
||||
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_dstr_own_steal)
|
||||
struct DStr str;
|
||||
char *s;
|
||||
@@ -69,14 +87,14 @@ END_TEST
|
||||
START_TEST (test_dstr_trim)
|
||||
struct DStr str = UC_DSTR_STATIC_INIT;
|
||||
|
||||
uc_dstr_put_str(&str, " \t \nHello !\r\n");
|
||||
uc_dstr_append_str(&str, " \t \nHello !\r\n");
|
||||
uc_dstr_trim(&str);
|
||||
ck_assert_str_eq(uc_dstr_str(&str), "Hello !");
|
||||
|
||||
uc_dstr_destroy(&str);
|
||||
uc_dstr_init(&str);
|
||||
|
||||
uc_dstr_put_str(&str, " ");
|
||||
uc_dstr_append_str(&str, " ");
|
||||
|
||||
uc_dstr_trim(&str);
|
||||
ck_assert_str_eq(uc_dstr_str(&str), "");
|
||||
@@ -96,7 +114,7 @@ START_TEST (test_dstr_replace)
|
||||
size_t rc;
|
||||
|
||||
uc_dstr_init(&str);
|
||||
uc_dstr_put_str(&str, "-1-2-3-4-5-");
|
||||
uc_dstr_append_str(&str, "-1-2-3-4-5-");
|
||||
rc = uc_dstr_replace(&str, '-',' ');
|
||||
ck_assert_int_eq(rc, 6);
|
||||
|
||||
@@ -114,8 +132,8 @@ START_TEST (test_dstr_filter)
|
||||
size_t rc;
|
||||
|
||||
uc_dstr_init(&str);
|
||||
uc_dstr_put_str(&str, "AB:");
|
||||
rc = uc_dstr_put_str_filter(&str, "1:2,3 4 ", isdigit);
|
||||
uc_dstr_append_str(&str, "AB:");
|
||||
rc = uc_dstr_append_str_filter(&str, "1:2,3 4 ", isdigit);
|
||||
ck_assert_int_eq(rc, 4);
|
||||
|
||||
ck_assert_str_eq(uc_dstr_str(&str), "AB:1234");
|
||||
@@ -147,7 +165,7 @@ START_TEST (test_dstr_copy)
|
||||
size_t rc;
|
||||
|
||||
uc_dstr_init(&str);
|
||||
rc = uc_dstr_put_str(&str, "123");
|
||||
rc = uc_dstr_append_str(&str, "123");
|
||||
ck_assert_int_eq(rc, 0);
|
||||
|
||||
rc = uc_dstr_copy(©, &str);
|
||||
@@ -173,6 +191,7 @@ Suite *dstr_suite(void)
|
||||
Suite *s = suite_create("dstr");
|
||||
TCase *tc = tcase_create("dstr tests");
|
||||
tcase_add_test(tc, test_dstr_basics);
|
||||
tcase_add_test(tc, test_dstr_init);
|
||||
tcase_add_test(tc, test_dstr_own_steal);
|
||||
tcase_add_test(tc, test_dstr_trim);
|
||||
tcase_add_test(tc, test_dstr_replace);
|
||||
|
||||
Reference in New Issue
Block a user