From: Mark Wooding Date: Sun, 30 Mar 2025 20:33:52 +0000 (+0100) Subject: chain.c (compare_words): Receive separate pointer and length arguments. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/wordchain/commitdiff_plain/5bae2a13df4265effc536a794363020866819c67?ds=inline chain.c (compare_words): Receive separate pointer and length arguments. This means that we don't have to represent the key as a literal `struct word' if that's inconvenient. --- diff --git a/chain.c b/chain.c index 1b53ea7..94dc4ae 100644 --- a/chain.c +++ b/chain.c @@ -216,12 +216,12 @@ static void bail(const char *msg, ...) #if USE_XYLA || TREE == SGT_TREE234 || USE_LIBAVL -static int compare_words(const struct word *a, const struct word *b) +static int compare_words(const char *a, size_t alen, + const char *b, size_t blen) { - size_t alen = a->n, blen = b->n; int ord; - ord = memcmp(a->p, b->p, alen <= blen ? alen : blen); + ord = memcmp(a, b, alen <= blen ? alen : blen); if (!ord) ord = alen < blen ? -1 : alen > blen ? +1 : 0; return (ord); } @@ -238,7 +238,7 @@ static int word_nav(struct xyla_bt_nodecls *cls, const struct word *k = arg; const struct node *node = (struct node *)nn; - return (compare_words(k, &node->w)); + return (compare_words(k->p, k->n, WORDPTR(node), WORDLEN(node))); } # define DECLS \ @@ -449,14 +449,19 @@ static bool my_Tnextl(Tbl *t, const char **k_inout, size_t *sz_inout, #elif TREE == SGT_TREE234 static int node_cmp(void *a, void *b) - { const struct word *wa = a, *wb = b; return (compare_words(wa, wb)); } +{ + const struct node *na = a, *nb = b; + + return (compare_words(WORDPTR(na), WORDLEN(na), + WORDPTR(nb), WORDLEN(nb))); +} static int word_cmp(void *k, void *n) { const struct word *word = k; const struct node *node = n; - return (compare_words(word, &node->w)); + return (compare_words(word->p, word->n, WORDPTR(node), WORDLEN(node))); } # define DECLS \ @@ -488,7 +493,11 @@ static int word_cmp(void *k, void *n) #elif USE_LIBAVL static int node_cmp(const void *a, const void *b, void *arg) - { const struct word *wa = a, *wb = b; return (compare_words(wa, wb)); } +{ + const struct word *wa = a, *wb = b; + + return (compare_words(wa->p, wa->n, wb->p, wb->n)); +} static void free_node(void *n, void *arg) { free(n); }