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:
+16
-4
@@ -35,6 +35,18 @@ struct DStr {
|
||||
*/
|
||||
void uc_dstr_init(struct DStr *str);
|
||||
|
||||
/** Initialize a DStr from an existing string
|
||||
*
|
||||
* @return 0 on success (!= 0 if an allocation fail)
|
||||
*/
|
||||
int uc_dstr_init_str(struct DStr *str, const char *init);
|
||||
|
||||
/** Initialize a DStr from an existing string with a given length
|
||||
*
|
||||
* @return 0 on success (!= 0 if an allocation fail)
|
||||
*/
|
||||
int uc_dstr_init_str_sz(struct DStr *str, const char *init, size_t len);
|
||||
|
||||
/** Destroy/free the string.
|
||||
* (Frees str->str, not str itself)
|
||||
*/
|
||||
@@ -95,7 +107,7 @@ char *uc_dstr_put(struct DStr *str, size_t len);
|
||||
* @param is_x ctype.h compatible function to use as a filter.
|
||||
* @return number of chars appended
|
||||
*/
|
||||
size_t uc_dstr_put_str_filter(struct DStr *str, const char *restrict s, int (*is_x)(int c));
|
||||
size_t uc_dstr_append_str_filter(struct DStr *str, const char *restrict s, int (*is_x)(int c));
|
||||
|
||||
/* Append a string of the give size.
|
||||
* (The nul terminator is not appended)
|
||||
@@ -104,20 +116,20 @@ size_t uc_dstr_put_str_filter(struct DStr *str, const char *restrict s, int (*is
|
||||
* @param len bytes to append (not including the nul terminator)
|
||||
* @return 0 on success (!= 0 if an allocation fail)
|
||||
*/
|
||||
int uc_dstr_put_str_sz(struct DStr *str, const char *restrict s, size_t len);
|
||||
int uc_dstr_append_str_sz(struct DStr *str, const char *restrict s, size_t len);
|
||||
|
||||
/* Append a string.
|
||||
* (The nul terminator is not appended)
|
||||
*
|
||||
* @return 0 on success (!= 0 if an allocation fail)
|
||||
*/
|
||||
int uc_dstr_put_str(struct DStr *str, const char *restrict s);
|
||||
int uc_dstr_append_str(struct DStr *str, const char *restrict s);
|
||||
|
||||
/* Append a single char.
|
||||
*
|
||||
* @return 0 on success (!= 0 if an allocation fail)
|
||||
*/
|
||||
int uc_dstr_put_char(struct DStr *str, char c);
|
||||
int uc_dstr_append_char(struct DStr *str, char c);
|
||||
|
||||
/* Make the DStr take ownership of the given dynamically allocated string.
|
||||
* Any existing string in the DStr is freed.
|
||||
|
||||
+20
-7
@@ -12,6 +12,18 @@ void uc_dstr_init(struct DStr *str)
|
||||
str->str = NULL;
|
||||
}
|
||||
|
||||
int uc_dstr_init_str(struct DStr *str, const char *init)
|
||||
{
|
||||
uc_dstr_init(str);
|
||||
return uc_dstr_append_str(str, init);
|
||||
}
|
||||
|
||||
int uc_dstr_init_str_sz(struct DStr *str, const char *init, size_t len)
|
||||
{
|
||||
uc_dstr_init(str);
|
||||
return uc_dstr_append_str_sz(str, init, len);
|
||||
}
|
||||
|
||||
void uc_dstr_destroy(struct DStr *str)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
@@ -106,7 +118,7 @@ char *uc_dstr_put(struct DStr *str, size_t len)
|
||||
return start;
|
||||
}
|
||||
|
||||
size_t uc_dstr_put_str_filter(struct DStr *str, const char *restrict s, int (*is_x)(int c))
|
||||
size_t uc_dstr_append_str_filter(struct DStr *str, const char *restrict s, int (*is_x)(int c))
|
||||
{
|
||||
size_t len = strlen(s);
|
||||
size_t i;
|
||||
@@ -128,7 +140,7 @@ size_t uc_dstr_put_str_filter(struct DStr *str, const char *restrict s, int (*is
|
||||
return idx;
|
||||
}
|
||||
|
||||
int uc_dstr_put_str_sz(struct DStr *str, const char *restrict s, size_t len)
|
||||
int uc_dstr_append_str_sz(struct DStr *str, const char *restrict s, size_t len)
|
||||
{
|
||||
char *start = uc_dstr_put(str, len);
|
||||
|
||||
@@ -140,14 +152,14 @@ int uc_dstr_put_str_sz(struct DStr *str, const char *restrict s, size_t len)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uc_dstr_put_str(struct DStr *str, const char *restrict s)
|
||||
int uc_dstr_append_str(struct DStr *str, const char *restrict s)
|
||||
{
|
||||
size_t len = strlen(s);
|
||||
|
||||
return uc_dstr_put_str_sz(str, s ,len);
|
||||
return uc_dstr_append_str_sz(str, s ,len);
|
||||
}
|
||||
|
||||
int uc_dstr_put_char(struct DStr *str, char c)
|
||||
int uc_dstr_append_char(struct DStr *str, char c)
|
||||
{
|
||||
char *start = uc_dstr_put(str, 1);
|
||||
|
||||
@@ -212,11 +224,12 @@ size_t uc_dstr_replace(struct DStr *str, char f, char r)
|
||||
void uc_dstr_trim(struct DStr *str)
|
||||
{
|
||||
size_t spaces;
|
||||
static const char whitespace[] = " \t\n\v\f\r";
|
||||
|
||||
uc_dstr_terminate(str);
|
||||
|
||||
//spaces at the start
|
||||
spaces = strspn(str->str, " \t\n\v\f\r");
|
||||
spaces = strspn(str->str, whitespace);
|
||||
if (spaces > 0) {
|
||||
memmove(str->str, str->str + spaces, str->len - spaces);
|
||||
str->len -= spaces;
|
||||
@@ -226,7 +239,7 @@ void uc_dstr_trim(struct DStr *str)
|
||||
if (str->len > 0) {
|
||||
size_t idx = str->len - 1;
|
||||
do {
|
||||
if (strchr(" \t\n\v\f\r", str->str[idx]) == NULL) {
|
||||
if (strchr(whitespace, str->str[idx]) == NULL) {
|
||||
break;
|
||||
} else {
|
||||
str->len--;
|
||||
|
||||
+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