cstring

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

commit 1f1d53a300efbdeb73f56545976590d50a1a3357
parent 285c910b3462c1b4aa2b88b7a528205725529c9e
Author: Christos Margiolis <christos@margiolis.net>
Date:   Tue, 15 Mar 2022 01:20:07 +0200

minor changes

Diffstat:
MREADME | 5++++-
Mconfig.mk | 2++
Mcstring.c | 46+++++++++++++++++++++++++---------------------
Mcstring.h | 11++++-------
Mtests/test_basic.c | 2+-
Mtests/test_insert.c | 2+-
6 files changed, 37 insertions(+), 31 deletions(-)

diff --git a/README b/README @@ -1,5 +1,6 @@ cstring ======= + A simple and lightweight string library for C inspired by C++'s STL `std::string` (don't laugh) class, but with a few additions/improvements. @@ -21,7 +22,7 @@ In order to make a distribution do: If you want to uninstall the library do: - # sudo make uninstall + # make uninstall A file using `cstring` has to be linked using the `-lcstring` linker flag. @@ -123,3 +124,5 @@ See the test programs in `tests` for more. return 0; } + +Report any bugs to <christos@margiolis.net>. diff --git a/config.mk b/config.mk @@ -5,6 +5,8 @@ VERSION = 0.1 # paths PREFIX = /usr/local MANPREFIX = ${PREFIX}/share/man +# OpenBSD +#MANPREFIX = ${PREFIX}/man INCDIR = ${PREFIX}/include LIBDIR = ${PREFIX}/lib diff --git a/cstring.c b/cstring.c @@ -1,4 +1,8 @@ /* See LICENSE file for copyright and license details. */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + #include "cstring.h" #define CSTRING_EXCEEDS_CAPACITY(len, cap) ((len) >= (cap)) @@ -21,8 +25,8 @@ cstring_is_one_of(char c, const char *s) { for (; *s; s++) if (*s == c) - return 1; - return 0; + return (1); + return (0); } static void * @@ -34,7 +38,7 @@ emalloc(size_t nb) perror("malloc"); exit(1); } - return p; + return (p); } static void @@ -47,25 +51,25 @@ csfree(cstring *cs) static inline int cstring_cmp_greater(const void *lhs, const void *rhs) { - return cstring_greater((cstring *)lhs, (cstring *)rhs); + return (cstring_greater((cstring *)lhs, (cstring *)rhs)); } static inline int cstring_cmp_less(const void *lhs, const void *rhs) { - return cstring_less((cstring *)lhs, (cstring *)rhs); + return (cstring_less((cstring *)lhs, (cstring *)rhs)); } static inline int cstring_cmp_char_greater(const void *lhs, const void *rhs) { - return (*(char *)lhs - *(char *)rhs); + return ((*(char *)lhs - *(char *)rhs)); } static inline int cstring_cmp_char_less(const void *lhs, const void *rhs) { - return -(*(char *)lhs - *(char *)rhs); + return (-(*(char *)lhs - *(char *)rhs)); } cstring @@ -75,7 +79,7 @@ cstring_create(const char *s) cs.len = strlen(s); cs.str = cstring_copy(s); cstring_resize(&cs, cs.len << 1); - return cs; + return (cs); } void @@ -207,12 +211,12 @@ cstring_substr(const cstring *cs, size_t pos, size_t len) { if (CSTRING_OUT_OF_BOUNDS(cs->len, pos) || CSTRING_OUT_OF_BOUNDS(cs->len, len)) - return cstring_create(""); + return (cstring_create("")); cstring substr = cstring_create(&cs->str[pos]); substr.len = len; substr.str[len] = '\0'; cstring_shrink_to_fit(&substr); - return substr; + return (substr); } void @@ -265,14 +269,14 @@ cstring_clear(cstring *cs) #define CSTRING_CHECK(cs, s) \ if (cstring_empty(cs) || !*(s)) \ - return CSTRING_NPOS + return (CSTRING_NPOS) size_t cstring_find(const cstring *cs, const char *s) { CSTRING_CHECK(cs, s); FIND_OCC(cs, s, strstr); - return CSTRING_NPOS; + return (CSTRING_NPOS); } /*SIMPLIFY */ @@ -306,7 +310,7 @@ cstring_find_first_of(const cstring *cs, const char *s) for (; *s; s++) { FIND_OCC(cs, *s, strchr); } - return CSTRING_NPOS; + return (CSTRING_NPOS); } size_t @@ -317,8 +321,8 @@ cstring_find_first_not_of(const cstring *cs, const char *s) CSTRING_CHECK(cs, s); for (; i < cs->len; i++) if (!cstring_is_one_of(cs->str[i], s)) - return i; - return CSTRING_NPOS; + return (i); + return (CSTRING_NPOS); } size_t @@ -331,7 +335,7 @@ cstring_find_last_of(const cstring *cs, const char *s) for (; i >= 0; i--) { FIND_OCC(cs, s[i], strrchr); } - return CSTRING_NPOS; + return (CSTRING_NPOS); } size_t @@ -342,8 +346,8 @@ cstring_find_last_not_of(const cstring *cs, const char *s) CSTRING_CHECK(cs, s); for (; i >= 0; i--) if (!cstring_is_one_of(cs->str[i], s)) - return i; - return CSTRING_NPOS; + return (i); + return (CSTRING_NPOS); } #undef CSTR_CHECK @@ -360,7 +364,7 @@ cstring_ends_with_str(const cstring *cs, const char *s) sub = cstring_substr(cs, cs->len - slen, slen); found = !strcmp(sub.str, s); cstring_delete(&sub); - return found; + return (found); } char * @@ -373,7 +377,7 @@ cstring_copy(const char *s) tmp = emalloc(len + 1); memcpy(tmp, s, len + 1); tmp[len] = '\0'; /* Add \0 in case `s` didn't have it */ - return tmp; + return (tmp); } void @@ -401,5 +405,5 @@ cstring_getline(FILE *fd, cstring *cs, char delim) else cstring_push_back(cs, c); } - return (c == EOF) ? NULL : cs; + return (c == EOF ? NULL : cs); } diff --git a/cstring.h b/cstring.h @@ -2,10 +2,6 @@ #ifndef _CSTRING_H_ #define _CSTRING_H_ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - #ifdef __cplusplus "C" { #endif /* __cplusplus */ @@ -114,7 +110,7 @@ cstring_empty(const cstring *cs) static inline char cstring_front(const cstring *cs) { - return cs->str[0]; + return (cs->str[0]); } static inline char @@ -131,7 +127,8 @@ cstring_starts_with_str(const cstring *cs, const char *s) static inline int cstring_starts_with_char(const cstring *cs, char c) -{ return (cs->str[0] == c); +{ + return (cs->str[0] == c); } static inline int @@ -143,7 +140,7 @@ cstring_ends_with_char(const cstring *cs, char c) static inline void * cstring_data(const cstring *cs) { - return (void *)cs->str; + return ((void *)cs->str); } static inline int diff --git a/tests/test_basic.c b/tests/test_basic.c @@ -57,5 +57,5 @@ main(int argc, char *argv[]) if (cstring_empty(&s)) puts("cstring_delete: Deleted string."); - return 0; + return (0); } diff --git a/tests/test_insert.c b/tests/test_insert.c @@ -8,5 +8,5 @@ main(int argc, char *argv[]) cstring s = cstring_create("HHHHHHEEEEEEEEEEEEEEEEEEEEEYYYYYYYYYYYYYY"); printf("%s\n", s.str); cstring_delete(&s); - return 0; + return (0); }