chiark / gitweb /
chain.c (compare_words): Receive separate pointer and length arguments.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 30 Mar 2025 20:33:52 +0000 (21:33 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 30 Mar 2025 21:48:44 +0000 (22:48 +0100)
This means that we don't have to represent the key as a literal `struct
word' if that's inconvenient.

chain.c

diff --git a/chain.c b/chain.c
index 1b53ea7a157cad67c6b1859644a7f2760152b285..94dc4ae6ceedd1e9aa27d29874b92ed5ba3e1361 100644 (file)
--- 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); }