commit d8ef8bfb7516d14d9e6f5545807a5f71fe470189
parent a8e77e831152868e1a5d0aa0ed363795e6cb2a3f
Author: Christos Margiolis <christos@margiolis.net>
Date: Wed, 26 Aug 2020 00:17:44 +0300
shortened function names
Diffstat:
M | README.md | | | 52 | ++++++++++++++++++++++++++-------------------------- |
M | cstring.c | | | 72 | ++++++++++++++++++++++++++++++++++++------------------------------------ |
M | cstring.h | | | 42 | +++++++++++++++++++++--------------------- |
M | test.c | | | 48 | ++++++++++++++++++++++++------------------------ |
4 files changed, 107 insertions(+), 107 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 `cstring_init` and `cstring_delete`
+When using this library, you must to **always** call the `cstr_init` and `cstr_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("")`.
+The recommended way of initializing an empty string is by doing `cstring foo = cstr_init("")`.
## Functions
-* `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`: 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
+* `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`: 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
## Example
@@ -48,10 +48,10 @@ See `test.c` for more.
int
main(int argc, char **argv)
{
- cstring s = cstring_init("Foo");
- cstring_append(&s, "Bar.");
+ cstring s = cstr_init("Foo");
+ cstr_append(&s, "Bar.");
printf("%s\n", s.str);
- cstring_delete(&s);
+ cstr_delete(&s);
return 0;
}
diff --git a/cstring.c b/cstring.c
@@ -1,50 +1,50 @@
#include "cstring.h"
cstring
-cstring_init(const char *s)
+cstr_init(const char *s)
{
cstring cs;
- cs.str = cstring_copy(s);
+ cs.str = cstr_copy(s);
cs.len = strlen(s);
- cstring_resize(&cs, cs.len << 1);
+ cstr_resize(&cs, cs.len << 1);
return cs;
}
void
-cstring_delete(cstring *cs)
+cstr_delete(cstring *cs)
{
- if (!cstring_empty(cs)) free(cs->str);
+ if (!cstr_empty(cs)) free(cs->str);
cs->str = NULL;
cs->len = 0;
cs->capacity = 0;
}
void
-cstring_assign(cstring *cs, const char *s)
+cstr_assign(cstring *cs, const char *s)
{
size_t newlen = strlen(s);
- if (!cstring_empty(cs)) free(cs->str);
- cs->str = cstring_copy(s);
+ if (!cstr_empty(cs)) free(cs->str);
+ cs->str = cstr_copy(s);
cs->len = newlen;
- if (newlen >= cs->capacity) cstring_resize(cs, newlen << 1);
+ if (newlen >= cs->capacity) cstr_resize(cs, newlen << 1);
}
void
-cstring_append(cstring *cs, const char *s)
+cstr_append(cstring *cs, const char *s)
{
- if (!cstring_empty(cs))
+ if (!cstr_empty(cs))
{
size_t newlen = cs->len + strlen(s);
- if (newlen >= cs->capacity) cstring_resize(cs, newlen << 1);
+ if (newlen >= cs->capacity) cstr_resize(cs, newlen << 1);
strcat(cs->str, s);
cs->len = newlen;
}
}
void
-cstring_insert(cstring *cs, const char *s, size_t i)
+cstr_insert(cstring *cs, const char *s, size_t i)
{
- if (!cstring_empty(cs) && i < cs->len)
+ if (!cstr_empty(cs) && i < cs->len)
{
size_t slen = strlen(s);
size_t newlen = cs->len + slen;
@@ -52,7 +52,7 @@ cstring_insert(cstring *cs, const char *s, size_t i)
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) cstring_resize(cs, newlen << 1);
+ if (newlen >= cs->capacity) cstr_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);
@@ -64,15 +64,15 @@ cstring_insert(cstring *cs, const char *s, size_t i)
}
void
-cstring_push_back(cstring *cs, char c)
+cstr_push_back(cstring *cs, char c)
{
- if (cs->len + 1 >= cs->capacity) cstring_resize(cs, (cs->len + 1) << 1);
+ if (cs->len + 1 >= cs->capacity) cstr_resize(cs, (cs->len + 1) << 1);
cs->str[cs->len] = c;
cs->str[++cs->len] = '\0';
}
void
-cstring_pop_back(cstring *cs)
+cstr_pop_back(cstring *cs)
{
if (cs->len > 0)
{
@@ -85,46 +85,46 @@ cstring_pop_back(cstring *cs)
}
void
-cstring_replace(cstring *cs, size_t i, char c)
+cstr_replace(cstring *cs, size_t i, char c)
{
if (i < cs->len) cs->str[i] = c;
}
void
-cstring_clear(cstring *cs)
+cstr_clear(cstring *cs)
{
- if (!cstring_empty(cs)) free(cs->str);
+ if (!cstr_empty(cs)) free(cs->str);
cs->str = (char *)malloc(1);
cs->str[0] = '\0';
cs->len = 0;
}
int
-cstring_exists(const cstring *cs, const char *s)
+cstr_exists(const cstring *cs, const char *s)
{
return (strstr(cs->str, s) != NULL);
}
char
-cstring_front(const cstring *cs)
+cstr_front(const cstring *cs)
{
return cs->str[0];
}
char
-cstring_back(const cstring *cs)
+cstr_back(const cstring *cs)
{
- return (!cstring_empty(cs) ? cs->str[cs->len-1] : cs->str[0]);
+ return (!cstr_empty(cs) ? cs->str[cs->len-1] : cs->str[0]);
}
int
-cstring_empty(const cstring *cs)
+cstr_empty(const cstring *cs)
{
return (cs->str == NULL && cs->len == 0);
}
char *
-cstring_copy(const char *s)
+cstr_copy(const char *s)
{
size_t len = strlen(s);
char *tmp = (char *)malloc(len + 1);
@@ -134,45 +134,45 @@ cstring_copy(const char *s)
}
void
-cstring_resize(cstring *cs, size_t new_capacity)
+cstr_resize(cstring *cs, size_t newcapacity)
{
- if (!cstring_empty(cs))
+ if (!cstr_empty(cs))
{
- char *tmp = (char *)malloc(new_capacity + 1);
+ char *tmp = (char *)malloc(newcapacity + 1);
memcpy(tmp, cs->str, cs->len + 1);
free(cs->str);
tmp[cs->len] = '\0';
cs->str = tmp;
- cs->capacity = new_capacity;
+ cs->capacity = newcapacity;
}
}
int
-cstring_equals(const cstring *lhs, const cstring *rhs)
+cstr_equals(const cstring *lhs, const cstring *rhs)
{
return (strcmp(lhs->str, rhs->str) == 0);
}
int
-cstring_greater(const cstring *lhs, const cstring *rhs)
+cstr_greater(const cstring *lhs, const cstring *rhs)
{
return (strcmp(lhs->str, rhs->str) > 0);
}
int
-cstring_greater_or_equals(const cstring *lhs, const cstring *rhs)
+cstr_greater_or_equals(const cstring *lhs, const cstring *rhs)
{
return (strcmp(lhs->str, rhs->str) >= 0);
}
int
-cstring_less(const cstring *lhs, const cstring *rhs)
+cstr_less(const cstring *lhs, const cstring *rhs)
{
return (strcmp(lhs->str, rhs->str) < 0);
}
int
-cstring_less_or_equals(const cstring *lhs, const cstring *rhs)
+cstr_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 cstring_init(const char *s);
-extern void cstring_delete(cstring *cs);
-extern void cstring_assign(cstring *cs, const char *s);
-extern void cstring_append(cstring *cs, const char *s);
-extern void cstring_insert(cstring *cs, const char *s, size_t i);
-extern void cstring_push_back(cstring *cs, char c);
-extern void cstring_pop_back(cstring *cs);
-extern void cstring_replace(cstring *cs, size_t i, char c);
-extern void cstring_clear(cstring *cs);
-extern int cstring_exists(const cstring *cs, const char *s);
-extern char cstring_front(const cstring *cs);
-extern char cstring_back(const cstring *cs);
-extern int cstring_empty(const cstring *cs);
-extern char *cstring_copy(const char *s);
-extern void cstring_resize(cstring *cs, size_t n);
+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);
/* might be useless */
-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);
-extern int cstring_greater_or_equals(const cstring *lhs, const cstring *rhs);
-extern int cstring_less(const cstring *lhs, const cstring *rhs);
-extern int cstring_less_or_equals(const cstring *lhs, const cstring *rhs);
+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);
#endif /* CSTRING_H */
diff --git a/test.c b/test.c
@@ -4,40 +4,40 @@
int
main(int argc, char **argv)
{
- cstring s = cstring_init("Hello world");
- printf("cstring_init: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
+ cstring s = cstr_init("Hello world");
+ printf("cstr_init: %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_append(&s, "Append");
+ printf("cstr_append: %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);
+ cstr_assign(&s, "New string");
+ printf("cstr_assign: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
- if (cstring_exists(&s, "string"))
- printf("cstring_exists: \"string\" exists!\n");
+ if (cstr_exists(&s, "string"))
+ printf("cstr_exists: \"string\" exists!\n");
- cstring_push_back(&s, 'c');
- printf("cstring_push_back: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
+ cstr_push_back(&s, 'c');
+ printf("cstr_push_back: %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_insert(&s, "Inserted text", 4);
+ printf("cstr_insert: %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_pop_back(&s);
+ printf("cstr_pop_back: %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_clear(&s);
+ printf("cstr_clear: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
- 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_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_replace(&s, 3, 'x');
- printf("cstring_replace: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
+ cstr_replace(&s, 3, 'x');
+ printf("cstr_replace: %s (Len: %ld, Capacity: %ld)\n", s.str, s.len, s.capacity);
- cstring_delete(&s);
- if (cstring_empty(&s)) printf("cstring_delete: Deleted string.\n");
+ cstr_delete(&s);
+ if (cstr_empty(&s)) printf("cstr_delete: Deleted string.\n");
return 0;
}