~crm/cstring

9497790140b5c75524157d3379a8a5d9faaa1ae3 — Christos Margiolis 3 years ago 5479902
fixed cstring_replace_str and cstring_sort_partial, still pending cstring_erase fix
5 files changed, 34 insertions(+), 28 deletions(-)

M Makefile
M README.md
M cstring.3
M cstring.c
M cstring.h
M Makefile => Makefile +1 -0
@@ 15,6 15,7 @@ AR = ar
ARFLAGS = rs
CC = gcc
CPPFLAGS += -Iinclude -DCSTRING_DBG -DVERSION=\"${VERSION}\"
#CPPFLAGS += -Iinclude -DVERSION=\"${VERSION}\"
CFLAGS += -Wall -std=c99 -pedantic -O3
LDFLAGS += -Llib
#LDLIBS += 

M README.md => README.md +7 -7
@@ 1,6 1,7 @@
# cstring

A simple and lightweight string library for C inspired by C++'s STL `string` class.
A simple and lightweight string library for C inspired by C++'s STL `string` class,
but with a lot of additions.

## Building



@@ 9,7 10,6 @@ the library file in `/usr/local/lib`. In order to install it do the following

```shell
$ cd /path/to/cstring
$ make
$ sudo make install
$ make clean
```


@@ 22,12 22,11 @@ $ sudo make uninstall
```

In order to link `cstring` to your project use the `-lcstring` flag during compilation.  
In case you want to run your project in debug mode, compile every file using `cstring` with the
`-DCSTRING_DBG` option.
In case you want to run your project in debug mode, compile it using the `-DCSTRING_DBG` option.

## Usage

When using this library, you must to **always** call the `cstring_create` and `cstring_delete` 
When using this library, you must **always** call the `cstring_create` 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.  



@@ 102,17 101,18 @@ The following macros can only be used in debug mode

## Example

See `test.c` for more.
See the test programs in `tests` for more.

```c
#include <stdio.h>
#include <cstring.h>

/* outputs "Foobar" to the screen */
int
main(int argc, char **argv)
{
    cstring s = cstring_create("Foo");
    cstring_append(&s, "Bar.");
    cstring_append(&s, "bar.");
    printf("%s\n", s.str);
    cstring_delete(&s);


M cstring.3 => cstring.3 +9 -3
@@ 90,8 90,12 @@ Remove the last character in the string.
.BR void\ cstring_replace_char(cstring\ *cs,\ size_t\ i,\ char\ c)
Replace character at a specific index.
.TP
.BR void\ cstring_replace_str(cstring\ *cs,\ const\ char\ *s,\ size_t\ pos,\ size_t\ len)
.BR void\ cstring_replace_str(cstring\ *cs,\ const\ char\ *s,\ size_t\ pos,\ size_t\ olen)
Replace portion of the string.
.I olen
is the length of the old string. An example use could be:
.br
.B cstring_replace_str(&string, new_word, cstring_find(&s, old_word), old_word_len)
.TP
.BR cstring\ cstring_substr(cstring\ *cs,\ size_t\ pos,\ size_t\ len)
Extract a substring from current string.


@@ 99,14 103,15 @@ Extract a substring from current string.
.BR void\ cstring_swap(cstring\ *lhs,\ cstring\ *rhs)
Swap contents of two strings.
.TP
.BR void\ cstring_sort(cstring\ **cs,\ size_t\ len,\ enum\ cstring_sort_flags\ flags,\ cstring_sort_callback\ callback)
.BR void\ cstring_sort(cstring\ *cs,\ size_t\ len,\ enum\ cstring_sort_flags\ flags,\ cstring_sort_callback\ callback)
Sort an array of cstrings. If you want to use the builtin comparison pass
.I NULL
in the last argument. In case you want to use your own callback use the
.I CSTRING_SORT_CALLBACK
flag and pass your own callback function in the last argument.
You can also combine flags using the bitwise OR operator.
.TP
.BR void\ cstring_sort_partial(cstring\ **cs,\ size_t\ pos,\ size_t\ len,\ enum\ cstring_sort_flags\ flags,\ cstring_sort_callback\ callback)
.BR void\ cstring_sort_partial(cstring\ *cs,\ size_t\ pos,\ size_t\ len,\ enum\ cstring_sort_flags\ flags,\ cstring_sort_callback\ callback)
Like
.B cstring_sort
but for specified part of an array.


@@ 117,6 122,7 @@ Sort a cstring's contents. If you want to use the builtin comparison pass
in the last argument. In case you want to use your own callback use the
.I CSTRING_SORT_CALLBACK
flag and pass your own callback function in the last argument.
You can also combine flags using the bitwise OR operator.
.TP
.BR void\ cstring_sort_chars_partial(cstring\ *cs,\ size_t\ pos,\ size_t\ len,\ enum\ cstring_sort_flags\ flags,\ cstring_sort_callback\ callback)
Like

M cstring.c => cstring.c +14 -15
@@ 223,15 223,13 @@ cstring_replace_char(cstring *cs, size_t i, char c)
}

void
cstring_replace_str(cstring *cs, const char *s, size_t pos, size_t len)
cstring_replace_str(cstring *cs, const char *s, size_t pos, size_t olen)
{
    if (!CSTRING_OUT_OF_BOUNDS(cs, pos)
    &&  !CSTRING_OUT_OF_BOUNDS(cs, len)
    &&  !CSTRING_OUT_OF_BOUNDS(cs, pos + len))
    &&  !CSTRING_OUT_OF_BOUNDS(cs, olen))
    {
        int i = pos;
        for (; i < len && *s; s++, i++)
            cs->str[i] = *s;
        cstring_erase(cs, pos, olen);
        cstring_insert(cs, s, pos);
    }
}



@@ 256,23 254,24 @@ cstring_swap(cstring *lhs, cstring *rhs)
    *rhs = tmp;
}

// FIX
void
cstring_sort_partial(cstring **cs,
cstring_sort_partial(cstring *cs,
                     size_t pos,
                     size_t len,
                     enum cstring_sort_flags flags,
                     cstring_sort_callback callback)
{
    if (CSTRING_FLAG_CHECK(flags, CSTRING_SORT_REST)
    ||  pos + len > len) /* maybe chanage out of bounds macro */
    /* maybe chanage out of bounds macro */
    if (CSTRING_FLAG_CHECK(flags, CSTRING_SORT_REST) ||  pos + len > len)
        len -= pos;

    if (CSTRING_FLAG_CHECK(flags, CSTRING_SORT_ASCENDING))
        qsort(cs + pos, len, sizeof(cstring *), cstring_cmp_greater);
        qsort(cs + pos, len, sizeof(cstring), cstring_cmp_greater);
    else if (CSTRING_FLAG_CHECK(flags, CSTRING_SORT_DESCENDING))
        qsort(cs + pos, len, sizeof(cstring *), cstring_cmp_less);
        qsort(cs + pos, len, sizeof(cstring), cstring_cmp_less);
    else if (CSTRING_FLAG_CHECK(flags, CSTRING_SORT_CALLBACK))
        qsort(cs + pos, len, sizeof(cstring *), callback);
        qsort(cs + pos, len, sizeof(cstring), callback);
}

void


@@ 324,8 323,8 @@ cstring_rfind(const cstring *cs, const char *s)
    int found;
    size_t idx = -1;
    size_t slen = strlen(s);
    size_t  i = 0, j;
    for (; i <= cs->len - slen; i++) {
    size_t  i, j;
    for (i = 0; i <= cs->len - slen; i++) {
        found = 1;
        for (j = 0; j < slen; j++) {
            if (cs->str[i + j] != s[j]) {


@@ 400,7 399,7 @@ cstring_resize(cstring *cs, size_t newcapacity)
{
#ifdef CSTRING_DBG
    CSTRING_DBG_LOG("Old capacity: %ld | New capacity: %ld\n",
            cs->capacity, newcapacity);
                    cs->capacity, newcapacity);
#endif /* CSTRING_DBG */
    char *tmp;
    CSTRING_MALLOC(tmp, newcapacity + 1); /* no +1? */

M cstring.h => cstring.h +3 -3
@@ 68,7 68,7 @@ extern void       cstring_replace_char(cstring *, size_t, char);
extern void       cstring_replace_str(cstring *, const char *, size_t, size_t);
extern cstring    cstring_substr(const cstring *, size_t, size_t);
extern void       cstring_swap(cstring *, cstring *);
extern void       cstring_sort_partial(cstring **, size_t, size_t,
extern void       cstring_sort_partial(cstring *, size_t, size_t,
                                       enum cstring_sort_flags,
                                       cstring_sort_callback);
extern void       cstring_sort_chars_partial(cstring *cs, size_t, size_t,


@@ 89,7 89,7 @@ extern cstring   *cstring_getline(FILE *, cstring *, char);
/* static inlines */
static inline void    cstring_prepend(cstring *, const char *);
static inline void    cstring_append(cstring *, const char *);
static inline void    cstring_sort(cstring **, size_t, enum cstring_sort_flags,
static inline void    cstring_sort(cstring *, size_t, enum cstring_sort_flags,
                                   cstring_sort_callback);
static inline void    cstring_sort_chars(cstring *, enum cstring_sort_flags,
                                         cstring_sort_callback);


@@ 121,7 121,7 @@ cstring_append(cstring *cs, const char *s)
}

static inline void
cstring_sort(cstring **cs,
cstring_sort(cstring *cs,
             size_t len,
             enum cstring_sort_flags flags,
             cstring_sort_callback callback)