commit 48893e3c5054d0d5760a258bad9213e10be332f2
parent fffc188e22f56adce2d1d162720e456917d45dc5
Author: Christos Margiolis <christos@margiolis.net>
Date: Thu, 3 Sep 2020 01:59:47 +0300
renamed cstr_ to cstring_
Diffstat:
M | README.md | | | 52 | ++++++++++++++++++++++++++-------------------------- |
M | cstring.c | | | 68 | ++++++++++++++++++++++++++++++++++---------------------------------- |
M | cstring.h | | | 42 | +++++++++++++++++++++--------------------- |
M | test.c | | | 48 | ++++++++++++++++++++++++------------------------ |
4 files changed, 105 insertions(+), 105 deletions(-)
diff --git a/README.md b/README.md
@@ -7,35 +7,35 @@ A simple and lightweight string library for C.
Simply include the source files in your projects and compile them
along with your other files.
-When using this library, you must to **always** call the `cstr_init` and `cstr_delete`
+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 = cstr_init("")`.
+The recommended way of initializing an empty string is by doing `cstring foo = cstring_init("")`.
## Functions
-* `cstr_init`: Initiliazes string
-* `cstr_delete`: Deallocates string
-* `cstr_assign`: Assigns a new string to the current string
-* `cstr_append`: Appends a string to the current string
-* `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_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
-* `cstr_back`: Returns the last character of the string
-* `cstr_empty`: Checks if the string is empty
-* `cstr_copy`: Makes a copy of a given `const char *`
-* `cstr_resize`: Resizes the array stored inside the string `struct`
-* `cstr_equals`: True if the strings are equal
-* `cstr_not_equals`: True if the strings are not equal
-* `cstr_greater`: True if the left hand string is greater than the right hand one
-* `cstr_greater_or_equals`: True if the left hand string is greater than or equal to the right hand one
-* `cstr_less`: True if the left hand string is less than the right hand one
-* `cstr_less_or_equals`: True if the left hand string is less than or equal to the right hand one
+* `cstring_init`: Initiliazes string
+* `cstring_delete`: Deallocates string
+* `cstring_assign`: Assigns a new string to the current string
+* `cstring_append`: Appends a string to the current string
+* `cstring_insert`: Inserts a string at a specific index
+* `cstring_push_back`: Adds a character at the end of the string
+* `cstring_pop_back`: Removes the last character in the string
+* `cstring_replace_char`: Replaces a character at a specific index
+* `cstring_clear`: Erases the whole string
+* `cstring_exists`: Checks to see if a (sub)string exists in the string
+* `cstring_front`: Returns the first character of the string
+* `cstring_back`: Returns the last character of the string
+* `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_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
+* `cstring_greater_or_equals`: True if the left hand string is greater than or equal to the right hand one
+* `cstring_less`: True if the left hand string is less than the right hand one
+* `cstring_less_or_equals`: True if the left hand string is less than or equal to the right hand one
## Example
@@ -48,10 +48,10 @@ See `test.c` for more.
int
main(int argc, char **argv)
{
- cstring s = cstr_init("Foo");
- cstr_append(&s, "Bar.");
+ cstring s = cstring_init("Foo");
+ cstring_append(&s, "Bar.");
printf("%s\n", s.str);
- cstr_delete(&s);
+ cstring_delete(&s);
return 0;
}
diff --git a/cstring.c b/cstring.c
@@ -1,56 +1,56 @@
#include "cstring.h"
cstring
-cstr_init(const char *s)
+cstring_init(const char *s)
{
cstring cs;
- cs.str = cstr_copy(s);
+ cs.str = cstring_copy(s);
cs.len = strlen(s);
- cstr_resize(&cs, cs.len << 1);
+ cstring_resize(&cs, cs.len << 1);
return cs;
}
void
-cstr_delete(cstring *cs)
+cstring_delete(cstring *cs)
{
- if (!cstr_empty(cs)) free(cs->str);
+ if (!cstring_empty(cs)) free(cs->str);
cs->str = NULL;
cs->len = 0;
cs->capacity = 0;
}
void
-cstr_assign(cstring *cs, const char *s)
+cstring_assign(cstring *cs, const char *s)
{
size_t newlen = strlen(s);
- if (!cstr_empty(cs)) free(cs->str);
- cs->str = cstr_copy(s);
+ if (!cstring_empty(cs)) free(cs->str);
+ cs->str = cstring_copy(s);
cs->len = newlen;
- if (newlen >= cs->capacity) cstr_resize(cs, newlen << 1);
+ if (newlen >= cs->capacity) cstring_resize(cs, newlen << 1);
}
void
-cstr_append(cstring *cs, const char *s)
+cstring_append(cstring *cs, const char *s)
{
- if (!cstr_empty(cs)) {
+ if (!cstring_empty(cs)) {
size_t newlen = cs->len + strlen(s);
- if (newlen >= cs->capacity) cstr_resize(cs, newlen << 1);
+ if (newlen >= cs->capacity) cstring_resize(cs, newlen << 1);
strcat(cs->str, s);
cs->len = newlen;
}
}
void
-cstr_insert(cstring *cs, const char *s, size_t i)
+cstring_insert(cstring *cs, const char *s, size_t i)
{
- if (!cstr_empty(cs) && i < cs->len) {
+ if (!cstring_empty(cs) && i < cs->len) {
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 (newlen >= cs->capacity) cstr_resize(cs, newlen << 1);
+ if (newlen >= cs->capacity) cstring_resize(cs, newlen << 1);
memcpy(cs->str, tmp1, i + 1);
memcpy(cs->str + i, s, slen + 1);
memcpy(cs->str + slen + i, tmp2, cs->len - i + 1);
@@ -62,15 +62,15 @@ cstr_insert(cstring *cs, const char *s, size_t i)
}
void
-cstr_push_back(cstring *cs, char c)
+cstring_push_back(cstring *cs, char c)
{
- if (cs->len + 1 >= cs->capacity) cstr_resize(cs, (cs->len + 1) << 1);
+ if (cs->len + 1 >= cs->capacity) cstring_resize(cs, (cs->len + 1) << 1);
cs->str[cs->len] = c;
cs->str[++cs->len] = '\0';
}
void
-cstr_pop_back(cstring *cs)
+cstring_pop_back(cstring *cs)
{
if (cs->len > 0) {
char *tmp = (char *)malloc(cs->len);
@@ -82,46 +82,46 @@ cstr_pop_back(cstring *cs)
}
void
-cstr_replace_char(cstring *cs, size_t i, char c)
+cstring_replace_char(cstring *cs, size_t i, char c)
{
if (i < cs->len) cs->str[i] = c;
}
void
-cstr_clear(cstring *cs)
+cstring_clear(cstring *cs)
{
- if (!cstr_empty(cs)) free(cs->str);
+ if (!cstring_empty(cs)) free(cs->str);
cs->str = (char *)malloc(1);
cs->str[0] = '\0';
cs->len = 0;
}
int
-cstr_exists(const cstring *cs, const char *s)
+cstring_exists(const cstring *cs, const char *s)
{
return (strstr(cs->str, s) != NULL);
}
char
-cstr_front(const cstring *cs)
+cstring_front(const cstring *cs)
{
return cs->str[0];
}
char
-cstr_back(const cstring *cs)
+cstring_back(const cstring *cs)
{
- return (!cstr_empty(cs) ? cs->str[cs->len-1] : cs->str[0]);
+ return (!cstring_empty(cs) ? cs->str[cs->len-1] : cs->str[0]);
}
int
-cstr_empty(const cstring *cs)
+cstring_empty(const cstring *cs)
{
return (cs->str == NULL && cs->len == 0);
}
char *
-cstr_copy(const char *s)
+cstring_copy(const char *s)
{
size_t len = strlen(s);
char *tmp = (char *)malloc(len + 1);
@@ -131,9 +131,9 @@ cstr_copy(const char *s)
}
void
-cstr_resize(cstring *cs, size_t newcapacity)
+cstring_resize(cstring *cs, size_t newcapacity)
{
- if (!cstr_empty(cs)) {
+ if (!cstring_empty(cs)) {
char *tmp = (char *)malloc(newcapacity + 1);
memcpy(tmp, cs->str, cs->len + 1);
free(cs->str);
@@ -144,31 +144,31 @@ cstr_resize(cstring *cs, size_t newcapacity)
}
int
-cstr_equals(const cstring *lhs, const cstring *rhs)
+cstring_equals(const cstring *lhs, const cstring *rhs)
{
return (strcmp(lhs->str, rhs->str) == 0);
}
int
-cstr_greater(const cstring *lhs, const cstring *rhs)
+cstring_greater(const cstring *lhs, const cstring *rhs)
{
return (strcmp(lhs->str, rhs->str) > 0);
}
int
-cstr_greater_or_equals(const cstring *lhs, const cstring *rhs)
+cstring_greater_or_equals(const cstring *lhs, const cstring *rhs)
{
return (strcmp(lhs->str, rhs->str) >= 0);
}
int
-cstr_less(const cstring *lhs, const cstring *rhs)
+cstring_less(const cstring *lhs, const cstring *rhs)
{
return (strcmp(lhs->str, rhs->str) < 0);
}
int
-cstr_less_or_equals(const cstring *lhs, const cstring *rhs)
+cstring_less_or_equals(const cstring *lhs, const cstring *rhs)
{
return (strcmp(lhs->str, rhs->str) <= 0);
}
diff --git a/cstring.h b/cstring.h
@@ -10,28 +10,28 @@ typedef struct {
size_t capacity;
} cstring;
-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);
+extern cstring cstring_init(const char *);
+extern void cstring_delete(cstring *);
+extern void cstring_assign(cstring *, const char *);
+extern void cstring_append(cstring *, const char *);
+extern void cstring_insert(cstring *, const char *, size_t);
+extern void cstring_push_back(cstring *, char);
+extern void cstring_pop_back(cstring *);
+extern void cstring_replace_char(cstring *, size_t, char);
+extern void cstring_clear(cstring *);
+extern int cstring_exists(const cstring *, const char *);
+extern char cstring_front(const cstring *);
+extern char cstring_back(const cstring *);
+extern int cstring_empty(const cstring *);
+extern char *cstring_copy(const char *);
+extern void cstring_resize(cstring *, size_t);
/* might be useless */
-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 *);
+extern int cstring_equals(const cstring *, const cstring *);
+extern int cstring_not_equals(const cstring *, const cstring *);
+extern int cstring_greater(const cstring *, const cstring *);
+extern int cstring_greater_or_equals(const cstring *, const cstring *);
+extern int cstring_less(const cstring *, const cstring *);
+extern int cstring_less_or_equals(const cstring *, const cstring *);
#endif /* CSTRING_H */
diff --git a/test.c b/test.c
@@ -4,40 +4,40 @@
int
main(int argc, char **argv)
{
- cstring s = cstr_init("Hello world");
- printf("cstr_init: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
+ cstring s = cstring_init("Hello world");
+ printf("cstring_init: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
- cstr_append(&s, "Append");
- printf("cstr_append: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
+ cstring_append(&s, "Append");
+ printf("cstring_append: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
- cstr_assign(&s, "New string");
- printf("cstr_assign: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
+ cstring_assign(&s, "New string");
+ printf("cstring_assign: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
- if (cstr_exists(&s, "string"))
- printf("cstr_exists: \"string\" exists!\n");
+ if (cstring_exists(&s, "string"))
+ printf("cstring_exists: \"string\" exists!\n");
- cstr_push_back(&s, 'c');
- printf("cstr_push_back: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
+ cstring_push_back(&s, 'c');
+ printf("cstring_push_back: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
- cstr_insert(&s, "Inserted text", 4);
- printf("cstr_insert: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
+ cstring_insert(&s, "Inserted text", 4);
+ printf("cstring_insert: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
- cstr_pop_back(&s);
- printf("cstr_pop_back: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
+ cstring_pop_back(&s);
+ printf("cstring_pop_back: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
- cstr_clear(&s);
- printf("cstr_clear: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
+ cstring_clear(&s);
+ printf("cstring_clear: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
- cstr_assign(&s, "CSTRING");
- printf("cstr_assign: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
- printf("cstr_front: %c\n", cstr_front(&s));
- printf("cstr_back: %c\n", cstr_back(&s));
+ cstring_assign(&s, "CSTRING");
+ printf("cstring_assign: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
+ printf("cstring_front: %c\n", cstring_front(&s));
+ printf("cstring_back: %c\n", cstring_back(&s));
- cstr_replace_char(&s, 3, 'x');
- printf("cstr_replace: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
+ cstring_replace_char(&s, 3, 'x');
+ printf("cstring_replace: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
- cstr_delete(&s);
- if (cstr_empty(&s)) printf("cstr_delete: Deleted string.\n");
+ cstring_delete(&s);
+ if (cstring_empty(&s)) printf("cstring_delete: Deleted string.\n");
return 0;
}