~crm/cstring

bfce9e85b2603b71e57ee3c78ee942355385410f — Christos Margiolis 3 years ago da6af48
fixed cstring_ends_with_string possible bug but cstring_substr should be avoided ideally
3 files changed, 23 insertions(+), 11 deletions(-)

M Makefile
M cstring.c
M cstring.h
M Makefile => Makefile +6 -3
@@ 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}

M cstring.c => cstring.c +15 -0
@@ 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)
{

M cstring.h => cstring.h +2 -8
@@ 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);