diff options
author | 2023-02-03 18:40:59 +0100 | |
---|---|---|
committer | 2023-02-03 18:40:59 +0100 | |
commit | 51c05fe7b5d6518bfb8a07ee3ec3c072bb21163e (patch) | |
tree | 92a52dc4849fbeb2566f8e87a88ff3ec02e5f75e | |
download | string2-51c05fe7b5d6518bfb8a07ee3ec3c072bb21163e.tar.gz string2-51c05fe7b5d6518bfb8a07ee3ec3c072bb21163e.zip |
Initial commit
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | LICENSE | 26 | ||||
-rw-r--r-- | Makefile | 33 | ||||
-rw-r--r-- | src/string2.c | 46 | ||||
-rw-r--r-- | src/string2.h | 11 | ||||
-rw-r--r-- | test/Makefile | 21 | ||||
-rw-r--r-- | test/main.c | 12 | ||||
-rw-r--r-- | test/test.c | 14 | ||||
-rw-r--r-- | test/test.h | 8 | ||||
-rw-r--r-- | test/test1.c | 32 | ||||
-rw-r--r-- | test/test1.h | 9 |
11 files changed, 216 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1c56290 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.a +*.o +*.so +test-app @@ -0,0 +1,26 @@ +Copyright 2023 Alessandro Iezzi + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors +may be used to endorse or promote products derived from this software without +specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b3f85e3 --- /dev/null +++ b/Makefile @@ -0,0 +1,33 @@ +# See LICENSE file for copyright and license details. + +CC = cc +SRC != find src -name '*.c' +OBJ = ${SRC:.c=.o} + +LIBNAME = libstr + +CFLAGS = -Wall -ansi --std=c89 -pedantic ${OPT} +LDFLAGS = + +dist: + @make OPT='-O2 -pipe -Werror' all + +debug: + @make OPT=-g all + +all: ${LIBNAME:=.so} ${LIBNAME:=.a} + +${LIBNAME:=.so}: ${OBJ} + ${CC} ${LDFLAGS} -fPIC -shared ${OBJ} -o $@ + +${LIBNAME:=.a}: ${OBJ} + ar rcs $@ ${OBJ} + +.c.o: + ${CC} ${CFLAGS} -fPIC -c $< -o $@ + +clean: + rm -f ${OBJ} ${LIBNAME}.* *.core + +tests: all + cd test && make clean tests diff --git a/src/string2.c b/src/string2.c new file mode 100644 index 0000000..332dbf7 --- /dev/null +++ b/src/string2.c @@ -0,0 +1,46 @@ +/* See LICENSE file for copyright and license details. */ + +#include <stdio.h> +#include <string.h> + +int +strends(const char *str, const char *end) +{ + int size_s1 = strlen(str); + int size_s2 = strlen(end); + + int i = size_s1 - 1, j = size_s2 - 1; + + if (size_s2 > size_s1) { + return 1; + } + + for (; i >= 0 && j >= 0; i--, j--) { + if (str[i] != end[j]) { + return 1; + } + } + + return 0; +} + +int +strstarts(const char *str, const char *end) +{ + int size_s1 = strlen(str); + int size_s2 = strlen(end); + + int i = 0; + + if (size_s2 > size_s1) { + return 1; + } + + for (; i < size_s2; i++) { + if (str[i] != end[i]) { + return 1; + } + } + + return 0; +} diff --git a/src/string2.h b/src/string2.h new file mode 100644 index 0000000..4d79dfd --- /dev/null +++ b/src/string2.h @@ -0,0 +1,11 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef __STRING2_H__ +#define __STRING2_H__ + +#include <string.h> + +int strends(const char *, const char *); +int strstarts(const char *, const char *); + +#endif /* __STRING2_H__ */ diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..5cc71c6 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,21 @@ +# See LICENSE file for copyright and license details. + +CC = cc +SRC != find * -name '*.c' +OBJ = ${SRC:.c=.o} + +CFLAGS = -Wall -ansi --std=c89 -pedantic\ + -I../src/ +LDFLAGS = -L../ -l:libstr.a + +tests: test-app + ./test-app + +.c.o: + ${CC} ${CFLAGS} -c $< -o $@ + +test-app: ${OBJ} + ${CC} ${LDFLAGS} -o $@ ${OBJ} + +clean: + rm -f ${test-app} ${OBJ} diff --git a/test/main.c b/test/main.c new file mode 100644 index 0000000..f6be903 --- /dev/null +++ b/test/main.c @@ -0,0 +1,12 @@ +/* See LICENSE file for copyright and license details. */ + +#include "test1.h" + +int +main(int argc, char **argv) +{ + test1(); + test2(); + + return 0; +} diff --git a/test/test.c b/test/test.c new file mode 100644 index 0000000..a47adb6 --- /dev/null +++ b/test/test.c @@ -0,0 +1,14 @@ +/* See LICENSE file for copyright and license details. */ + +/* See LICENSE file for copyright and license details. */ + +#include <stdlib.h> +#include <stdio.h> + +void +asserti(int expected, int value) +{ + if (expected != value) { + exit(1); + } +} diff --git a/test/test.h b/test/test.h new file mode 100644 index 0000000..cbd0a84 --- /dev/null +++ b/test/test.h @@ -0,0 +1,8 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef __TEST_H__ +#define __TEST_H__ + +void asserti(int, int); + +#endif /* __TEST_H__ */ diff --git a/test/test1.c b/test/test1.c new file mode 100644 index 0000000..777aa5f --- /dev/null +++ b/test/test1.c @@ -0,0 +1,32 @@ +/* See LICENSE file for copyright and license details. */ + +#include <string2.h> +#include <stdio.h> + +#include "test.h" + +void +test1(void) +{ + printf("executing test1... "); + asserti(0, strends("alexander", "xander")); + asserti(1, strends("alexander", "dander")); + asserti(1, strends("alexander", "alex")); + asserti(0, strends("alexander", "alexander")); + asserti(1, strends("dwm", "dwmdwm")); + + printf("OK\n"); +} + +void +test2(void) +{ + printf("executing test2... "); + asserti(0, strstarts("alexander", "alex")); + asserti(1, strstarts("alexander", "dalex")); + asserti(1, strstarts("alexander", "xander")); + asserti(0, strstarts("alexander", "alexander")); + asserti(1, strstarts("dwm", "dwmdwm")); + + printf("OK\n"); +} diff --git a/test/test1.h b/test/test1.h new file mode 100644 index 0000000..1b88561 --- /dev/null +++ b/test/test1.h @@ -0,0 +1,9 @@ +/* See LICENSE file for copyright and license details. */ + +#ifndef __TEST1_H__ +#define __TEST1_H__ + +void test1(void); +void test2(void); + +#endif /* __TEST1_H__ */ |