cstring

Lightweight string library for C
git clone git://git.christosmarg.xyz/cstring.git
Log | Files | Refs | README | LICENSE

commit a8e77e831152868e1a5d0aa0ed363795e6cb2a3f
parent 918f03bbaf5cad59a306b006cab74674014c8fdf
Author: Christos Margiolis <christos@margiolis.net>
Date:   Mon, 17 Aug 2020 22:42:32 +0300

minor optimizations

Diffstat:
MREADME.md | 17+++++++++++------
Mcstring.c | 52++++++++++++++++++++--------------------------------
Mcstring.h | 1-
3 files changed, 31 insertions(+), 39 deletions(-)

diff --git a/README.md b/README.md @@ -1,9 +1,17 @@ # cstring +A simple and lightweight string library for C. + ## Usage Simply include the source files in your projects and compile them -along with your other files. +along with your other files. + +When using this library, you must to **always** call the `cstring_init` and `cstring_delete` +functions whenever you want to make a new instance of `cstring` and stop using it respectively, +in order not to cause any memory leaks, as there's no *constructor* and *destructor* to do it for you. + +The recommended way of initializing an empty string is by doing `cstring foo = cstring_init("")`. ## Functions @@ -22,7 +30,6 @@ along with your other files. * `cstring_empty`: Checks if the string is empty * `cstring_copy`: Makes a copy of a given `const char *` * `cstring_resize`: Resizes the array stored inside the string `struct` -* `cstring_len`: Returns the length of the string * `cstring_equals`: True if the strings are equal * `cstring_not_equals`: True if the strings are not equal * `cstring_greater`: True if the left hand string is greater than the right hand one @@ -32,8 +39,6 @@ along with your other files. ## Example -A recommended way of using this string is to **always** call the `cstring_delete` function when you're done -working with it, in order not to cause any memory leaks, as there's no *destructor* to do it for you. See `test.c` for more. ```c @@ -43,8 +48,8 @@ See `test.c` for more. int main(int argc, char **argv) { - cstring s = cstring_init("Example string"); - cstring_append(&s, "more text..."); + cstring s = cstring_init("Foo"); + cstring_append(&s, "Bar."); printf("%s\n", s.str); cstring_delete(&s); diff --git a/cstring.c b/cstring.c @@ -13,13 +13,10 @@ cstring_init(const char *s) void cstring_delete(cstring *cs) { - if (!cstring_empty(cs)) - { - free(cs->str); - cs->str = NULL; - cs->len = 0; - cs->capacity = 0; - } + if (!cstring_empty(cs)) free(cs->str); + cs->str = NULL; + cs->len = 0; + cs->capacity = 0; } void @@ -35,14 +32,13 @@ cstring_assign(cstring *cs, const char *s) void cstring_append(cstring *cs, const char *s) { - size_t newlen = cs->len + strlen(s); if (!cstring_empty(cs)) { - if (newlen >= cs->capacity) - cstring_resize(cs, newlen << 1); + size_t newlen = cs->len + strlen(s); + if (newlen >= cs->capacity) cstring_resize(cs, newlen << 1); strcat(cs->str, s); + cs->len = newlen; } - cs->len = newlen; } void @@ -50,17 +46,17 @@ cstring_insert(cstring *cs, const char *s, size_t i) { if (!cstring_empty(cs) && i < cs->len) { - /* TODO minimize function calls */ + size_t slen = strlen(s); + size_t newlen = cs->len + slen; char *tmp1 = (char *)malloc(i + 1); char *tmp2 = (char *)malloc(cs->len - i + 1); memcpy(tmp1, cs->str, i + 1); memcpy(tmp2, cs->str + i, cs->len + 1); - if (cs->len + strlen(s) >= cs->capacity) - cstring_resize(cs, (cs->len + strlen(s)) << 1); + if (newlen >= cs->capacity) cstring_resize(cs, newlen << 1); memcpy(cs->str, tmp1, i + 1); - memcpy(cs->str + i, s, strlen(s) + 1); - memcpy(cs->str + strlen(s) + i, tmp2, cs->len - i + 1); - cs->len = cstring_len(cs); + memcpy(cs->str + i, s, slen + 1); + memcpy(cs->str + slen + i, tmp2, cs->len - i + 1); + cs->len = newlen; cs->str[cs->len] = '\0'; free(tmp1); free(tmp2); @@ -70,9 +66,8 @@ cstring_insert(cstring *cs, const char *s, size_t i) void cstring_push_back(cstring *cs, char c) { - if (cs->len + 1 >= cs->capacity) - cstring_resize(cs, (cs->len + 1) << 1); - cs->str[cs->len] = c; + if (cs->len + 1 >= cs->capacity) cstring_resize(cs, (cs->len + 1) << 1); + cs->str[cs->len] = c; cs->str[++cs->len] = '\0'; } @@ -84,9 +79,8 @@ cstring_pop_back(cstring *cs) char *tmp = (char *)malloc(cs->len); memcpy(tmp, cs->str, cs->len); free(cs->str); - tmp[cs->len - 1] = '\0'; + tmp[cs->len--] = '\0'; cs->str = tmp; - cs->len--; } } @@ -100,9 +94,9 @@ void cstring_clear(cstring *cs) { if (!cstring_empty(cs)) free(cs->str); - cs->str = (char *)malloc(1); + cs->str = (char *)malloc(1); cs->str[0] = '\0'; - cs->len = 0; + cs->len = 0; } int @@ -120,13 +114,13 @@ cstring_front(const cstring *cs) char cstring_back(const cstring *cs) { - return (!cstring_empty(cs) ? cs->str[cs->len -1] : cs->str[0]); + return (!cstring_empty(cs) ? cs->str[cs->len-1] : cs->str[0]); } int cstring_empty(const cstring *cs) { - return (cs->len == 0 || cs->str == NULL); + return (cs->str == NULL && cs->len == 0); } char * @@ -153,12 +147,6 @@ cstring_resize(cstring *cs, size_t new_capacity) } } -size_t -cstring_len(const cstring *cs) -{ - return strlen(cs->str); -} - int cstring_equals(const cstring *lhs, const cstring *rhs) { diff --git a/cstring.h b/cstring.h @@ -27,7 +27,6 @@ extern char *cstring_copy(const char *s); extern void cstring_resize(cstring *cs, size_t n); /* might be useless */ -extern size_t cstring_len(const cstring *cs); extern int cstring_equals(const cstring *lhs, const cstring *rhs); extern int cstring_not_equals(const cstring *lhs, const cstring *rhs); extern int cstring_greater(const cstring *lhs, const cstring *rhs);