cepheid

An Aurora 4X clone
Log | Files | Refs | README

commit ad401e75c8e8e3d4a8b5cc84e0e867e46e122c5d
parent e2b64b8fe8a8ea161c376bd22ae4a14f1550863f
Author: hhvn <dev@hhvn.uk>
Date:   Sat, 17 Dec 2022 14:07:42 +0000

Functions for checking values in arrays

Diffstat:
Msrc/main.h | 3+++
Msrc/str.c | 43+++++++++++++++++++++++++++++++++++++++++++
Atests/str.test | 36++++++++++++++++++++++++++++++++++++
3 files changed, 82 insertions(+), 0 deletions(-)

diff --git a/src/main.h b/src/main.h @@ -66,6 +66,9 @@ size_t strsplit(char *str, char *sep, char **list, size_t len); void strjoinl(char *sep, char **ret, char *append); char * strjoin(char *sep, char **list, size_t len); char * strtrunc(char *str, int w); +int memeq(void *m1, void *m2, size_t size); +void * memval(void *mem, void *val, size_t nmemb, size_t size); /* search for value in array */ +void * memcval(void *mem, void *val, size_t nmemb, size_t size); /* search for complement of value */ void edittrunc(wchar_t *str, int *len, int *cur); void editrm(wchar_t *str, int *len, int *cur); void editins(wchar_t *str, int *len, int *cur, int size, wchar_t c); diff --git a/src/str.c b/src/str.c @@ -270,6 +270,49 @@ end: return fstrdup(buf); } +/* Yeah, they're called 'mem' and there is a mem.c, + * but mem* functions in libc are in string.h */ +int +memeq(void *m1, void *m2, size_t size) { + if (m1 == m2) + return 1; + else if (!m1 || !m2) + return 0; + else if (memcmp(m1, m2, size) == 0) + return 1; + return 0; +} + +void * +memval(void *mem, void *val, size_t nmemb, size_t size) { + int i; + size_t pos; + + for (i = 0; i < size; i++) { + pos = i * size; + + if (memeq(mem + pos, val, size)) + return mem + pos; + } + + return NULL; +} + +void * +memcval(void *mem, void *val, size_t nmemb, size_t size) { + int i; + size_t pos; + + for (i = 0; i < size; i++) { + pos = i * size; + + if (!memeq(mem + pos, val, size)) + return mem + pos; + } + + return NULL; +} + void edittrunc(wchar_t *str, int *len, int *cur) { *len = *cur = 0; diff --git a/tests/str.test b/tests/str.test @@ -0,0 +1,36 @@ +/* vim: set filetype=c : */ +#include "../src/main.h" + +%{ + char *s1 = "test"; + char *s2 = 1; + int m1[4] = {5, 5, 7, 8}; + int m2[4] = {5, 5, 7, 9}; + long m3[4] = {5, 5, 7, 8}; + int v1 = 5; + int v2 = 64; + long v3 = 5; + long v4 = 64; + + ck_assert(streq(s1, s1)); + ck_assert(streq(s1, "test")); + ck_assert(!streq(NULL, s2)); + ck_assert(streq(NULL, NULL)); + ck_assert(!streq(s1, "testing")); + + ck_assert(memeq(m1, m1, 3 * sizeof(int))); + ck_assert(memeq(m1, m2, 3 * sizeof(int))); + ck_assert(!memeq(NULL, m2, 50000)); + ck_assert(memeq(NULL, NULL, 50000)); + ck_assert(!memeq(m1, m2, 4 * sizeof(int))); + + ck_assert(memval(m1, &v1, 4, sizeof(int)) == m1); + ck_assert(memval(m1, &v2, 4, sizeof(int)) == NULL); + ck_assert(memcval(m1, &v1, 4, sizeof(int)) == &m1[2]); + ck_assert(memcval(m1, &v2, 4, sizeof(int)) == m1); + + ck_assert(memval(m3, &v3, 4, sizeof(long)) == m3); + ck_assert(memval(m3, &v4, 4, sizeof(long)) == NULL); + ck_assert(memcval(m3, &v3, 4, sizeof(long)) == &m3[2]); + ck_assert(memcval(m3, &v4, 4, sizeof(long)) == m3); +}