cstring

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

commit fffc188e22f56adce2d1d162720e456917d45dc5
parent 719dca735afc8c14364310d23c5e391f31005ac8
Author: Christos Margiolis <christos@margiolis.net>
Date:   Wed,  2 Sep 2020 18:10:38 +0300

mostly naming changes

Diffstat:
MMakefile | 3---
MREADME.md | 2+-
Mcstring.c | 14+++++---------
Mcstring.h | 42+++++++++++++++++++++---------------------
Mtest.c | 2+-
5 files changed, 28 insertions(+), 35 deletions(-)

diff --git a/Makefile b/Makefile @@ -25,8 +25,5 @@ $(TARGET): $(OBJ) run: ./$(TARGET) -install: $(TARGET) - cp $(TARGET) $(INSTALL_PATH) - clean: $(RM) $(OBJ) $(TARGET) diff --git a/README.md b/README.md @@ -22,7 +22,7 @@ The recommended way of initializing an empty string is by doing `cstring foo = c * `cstr_insert`: Inserts a string at a specific index * `cstr_push_back`: Adds a character at the end of the string * `cstr_pop_back`: Removes the last character in the string -* `cstr_replace`: Replaces a character at a specific index +* `cstr_replace_char`: Replaces a character at a specific index * `cstr_clear`: Erases the whole string * `cstr_exists`: Checks to see if a (sub)string exists in the string * `cstr_front`: Returns the first character of the string diff --git a/cstring.c b/cstring.c @@ -32,8 +32,7 @@ cstr_assign(cstring *cs, const char *s) void cstr_append(cstring *cs, const char *s) { - if (!cstr_empty(cs)) - { + if (!cstr_empty(cs)) { size_t newlen = cs->len + strlen(s); if (newlen >= cs->capacity) cstr_resize(cs, newlen << 1); strcat(cs->str, s); @@ -44,8 +43,7 @@ cstr_append(cstring *cs, const char *s) void cstr_insert(cstring *cs, const char *s, size_t i) { - if (!cstr_empty(cs) && i < cs->len) - { + if (!cstr_empty(cs) && i < cs->len) { size_t slen = strlen(s); size_t newlen = cs->len + slen; char *tmp1 = (char *)malloc(i + 1); @@ -74,8 +72,7 @@ cstr_push_back(cstring *cs, char c) void cstr_pop_back(cstring *cs) { - if (cs->len > 0) - { + if (cs->len > 0) { char *tmp = (char *)malloc(cs->len); memcpy(tmp, cs->str, cs->len); free(cs->str); @@ -85,7 +82,7 @@ cstr_pop_back(cstring *cs) } void -cstr_replace(cstring *cs, size_t i, char c) +cstr_replace_char(cstring *cs, size_t i, char c) { if (i < cs->len) cs->str[i] = c; } @@ -136,8 +133,7 @@ cstr_copy(const char *s) void cstr_resize(cstring *cs, size_t newcapacity) { - if (!cstr_empty(cs)) - { + if (!cstr_empty(cs)) { char *tmp = (char *)malloc(newcapacity + 1); memcpy(tmp, cs->str, cs->len + 1); free(cs->str); diff --git a/cstring.h b/cstring.h @@ -10,28 +10,28 @@ typedef struct { size_t capacity; } cstring; -extern cstring cstr_init(const char *s); -extern void cstr_delete(cstring *cs); -extern void cstr_assign(cstring *cs, const char *s); -extern void cstr_append(cstring *cs, const char *s); -extern void cstr_insert(cstring *cs, const char *s, size_t i); -extern void cstr_push_back(cstring *cs, char c); -extern void cstr_pop_back(cstring *cs); -extern void cstr_replace(cstring *cs, size_t i, char c); -extern void cstr_clear(cstring *cs); -extern int cstr_exists(const cstring *cs, const char *s); -extern char cstr_front(const cstring *cs); -extern char cstr_back(const cstring *cs); -extern int cstr_empty(const cstring *cs); -extern char *cstr_copy(const char *s); -extern void cstr_resize(cstring *cs, size_t newcapacity); +extern cstring cstr_init(const char *); +extern void cstr_delete(cstring *); +extern void cstr_assign(cstring *, const char *); +extern void cstr_append(cstring *, const char *); +extern void cstr_insert(cstring *, const char *, size_t); +extern void cstr_push_back(cstring *, char); +extern void cstr_pop_back(cstring *); +extern void cstr_replace_char(cstring *, size_t, char); +extern void cstr_clear(cstring *); +extern int cstr_exists(const cstring *, const char *); +extern char cstr_front(const cstring *); +extern char cstr_back(const cstring *); +extern int cstr_empty(const cstring *); +extern char *cstr_copy(const char *); +extern void cstr_resize(cstring *, size_t); /* might be useless */ -extern int cstr_equals(const cstring *lhs, const cstring *rhs); -extern int cstr_not_equals(const cstring *lhs, const cstring *rhs); -extern int cstr_greater(const cstring *lhs, const cstring *rhs); -extern int cstr_greater_or_equals(const cstring *lhs, const cstring *rhs); -extern int cstr_less(const cstring *lhs, const cstring *rhs); -extern int cstr_less_or_equals(const cstring *lhs, const cstring *rhs); +extern int cstr_equals(const cstring *, const cstring *); +extern int cstr_not_equals(const cstring *, const cstring *); +extern int cstr_greater(const cstring *, const cstring *); +extern int cstr_greater_or_equals(const cstring *, const cstring *); +extern int cstr_less(const cstring *, const cstring *); +extern int cstr_less_or_equals(const cstring *, const cstring *); #endif /* CSTRING_H */ diff --git a/test.c b/test.c @@ -33,7 +33,7 @@ main(int argc, char **argv) printf("cstr_front: %c\n", cstr_front(&s)); printf("cstr_back: %c\n", cstr_back(&s)); - cstr_replace(&s, 3, 'x'); + cstr_replace_char(&s, 3, 'x'); printf("cstr_replace: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity); cstr_delete(&s);