cstring

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

commit bfce9e85b2603b71e57ee3c78ee942355385410f
parent da6af4863c09169ebe9134915e97931360669464
Author: Christos Margiolis <christos@margiolis.net>
Date:   Thu, 22 Oct 2020 22:24:37 +0300

fixed cstring_ends_with_string possible bug but cstring_substr should be avoided ideally

Diffstat:
MMakefile | 9++++++---
Mcstring.c | 15+++++++++++++++
Mcstring.h | 10++--------
3 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/Makefile b/Makefile @@ -8,8 +8,9 @@ LIB = cstring DIST = ${LIB}-${VERSION} MAN3 = ${LIB}.3 +EXT = c SRC = cstring.c -OBJ = cstring.o +OBJ = ${SRC:.${EXT}=.o} all: options ${LIB} @@ -17,13 +18,15 @@ options: @echo ${LIB} build options: @echo "CFLAGS = ${CFLAGS}" @echo "LDFLAGS = ${LDFLAGS}" + @echo "ARFLAGS = ${ARFLAGS}" @echo "CC = ${CC}" + @echo "AR = ${AR}" ${LIB}: ${OBJ} ${AR} ${ARFLAGS} lib${LIB}.a ${OBJ} -${OBJ}: ${SRC} cstring.h - ${CC} ${CFLAGS} -c ${SRC} -o $@ +.${EXT}.o: + ${CC} ${CFLAGS} -c $< dist: clean ${MKDIR} ${DIST} diff --git a/cstring.c b/cstring.c @@ -397,6 +397,21 @@ cstring_find_last_not_of(const cstring *cs, const char *s) #undef CSTR_CHECK +int +cstring_ends_with_str(const cstring *cs, const char *s) +{ + /* avoid cstring_substr */ + cstring sub; + size_t slen; + int found; + + slen = strlen(s); + sub = cstring_substr(cs, cs->len - slen, slen); + found = !strcmp(sub.str, s); + cstring_delete(&sub); + return found; +} + char * cstring_copy(const char *s) { diff --git a/cstring.h b/cstring.h @@ -18,7 +18,7 @@ extern "C" { #define CSTRING_FLAG_CHECK(flag, bit) (((flag) & (int)(bit)) == (int)(bit)) #define CSTRING_MALLOC(ptr, size) do { \ - ptr = (char *)malloc((size)); \ + ptr = malloc((size)); \ if (ptr == NULL) \ fputs("CSTRING_MALLOC(): cannot allocate memory\n", stderr); \ } while (0) @@ -83,6 +83,7 @@ extern size_t cstring_find_first_of(const cstring *, const char *); extern size_t cstring_find_first_not_of(const cstring *,const char *); extern size_t cstring_find_last_of(const cstring *, const char *); extern size_t cstring_find_last_not_of(const cstring *, const char *); +extern int cstring_ends_with_str(const cstring *, const char *); extern char *cstring_copy(const char *); extern void cstring_resize(cstring *, size_t); extern cstring *cstring_getline(FILE *, cstring *, char); @@ -100,7 +101,6 @@ static inline int cstring_empty(const cstring *); static inline char cstring_front(const cstring *); static inline char cstring_back(const cstring *); static inline int cstring_starts_with_str(const cstring *, const char *); -static inline int cstring_ends_with_str(const cstring *, const char *); static inline int cstring_starts_with_char(const cstring *, char); static inline int cstring_ends_with_char(const cstring *, char); static inline void *cstring_data(const cstring *); @@ -170,12 +170,6 @@ cstring_starts_with_str(const cstring *cs, const char *s) } static inline int -cstring_ends_with_str(const cstring *cs, const char *s) -{ - return (cstring_find(cs, s) == cs->len - strlen(s)); -} - -static inline int cstring_starts_with_char(const cstring *cs, char c) { return (cs->str[0] == c);