aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-05-17 08:41:55 +0000
committerJoursoir <chat@joursoir.net>2021-05-17 08:41:55 +0000
commitbbf29455ed723ef4e9be95033e7aca2ea6a72a91 (patch)
tree7c49780ea6da6fe45c0167cccbcbc06a8edd04ee
parent58241bc88020e972b7719753c3838a5557ac4d84 (diff)
downloadctimeline-bbf29455ed723ef4e9be95033e7aca2ea6a72a91.tar.gz
ctimeline-bbf29455ed723ef4e9be95033e7aca2ea6a72a91.tar.bz2
ctimeline-bbf29455ed723ef4e9be95033e7aca2ea6a72a91.zip
xstring: add char* concatenation, use func for realloc memory
-rw-r--r--xstring.c25
-rw-r--r--xstring.h1
2 files changed, 15 insertions, 11 deletions
diff --git a/xstring.c b/xstring.c
index d540828..17804f3 100644
--- a/xstring.c
+++ b/xstring.c
@@ -58,19 +58,22 @@ static void string_realloc(string *str, int cap)
void string_addch(string *str, int ch)
{
- if(str->capacity <= (str->len + 1)) {
- int i;
- int updated_cap = str->capacity + INIT_LEN_STRING;
- char *tmp_s = str->s;
- str->s = malloc(sizeof(char) * (updated_cap + 1));
- for(i = 0; i < updated_cap; i++)
- str->s[i] = (i >= str->capacity) ? '\0' : tmp_s[i];
-
- str->capacity = updated_cap;
- free(tmp_s);
- }
+ if((str->len + 1) >= str->capacity)
+ string_realloc(str, str->capacity + INIT_LEN_STRING);
str->s[str->len] = ch;
str->len++;
str->s[str->len] = '\0';
}
+
+void string_addstr(string *str, const char *src, size_t src_len)
+{
+ if((str->len + src_len) >= str->capacity)
+ string_realloc(str, str->capacity + src_len);
+
+ while(src_len > 0 && (str->s[str->len] = *src++)) {
+ src_len--;
+ str->len++;
+ }
+ str->s[str->len] = '\0';
+}
diff --git a/xstring.h b/xstring.h
index a0426d6..4a33e99 100644
--- a/xstring.h
+++ b/xstring.h
@@ -14,5 +14,6 @@ string *string_nalloc(const char *text, size_t text_len);
void string_release(string *str);
void string_reset(string *str);
void string_addch(string *str, int ch);
+void string_addstr(string *str, const char *src, size_t src_len);
#endif /* CTIMELINE_XSTRING_H */