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:
Nils O. Selåsdal
2014-06-18 15:57:51 +02:00
parent 5bd23d751f
commit 0e296ce7e5
3 changed files with 64 additions and 20 deletions
+20 -7
View File
@@ -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--;