Skip to content

Commit

Permalink
Add support for shortened mnemonic elements
Browse files Browse the repository at this point in the history
  • Loading branch information
mword committed Feb 28, 2019
1 parent f277405 commit 20f77ef
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/wordlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ static int bstrcmp(const void *l, const void *r)
return strcmp(l, (*(const char **)r));
}

static int bstr4cmp(const void *l, const void *r) {
return strncmp(l, (*(const char **)r), 4);
}

/* https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious */
static int get_bits(size_t n)
{
Expand Down Expand Up @@ -72,12 +76,12 @@ size_t wordlist_lookup_word(const struct words *w, const char *word)
const size_t size = sizeof(const char *);
const char **found = NULL;

if (w->sorted)
found = (const char **)bsearch(word, w->indices, w->len, size, bstrcmp);
else {
if (w->sorted) {
found = (const char **)bsearch(word, w->indices, w->len, size, bstr4cmp);
} else {
size_t i;
for (i = 0; i < w->len && !found; ++i)
if (!strcmp(word, w->indices[i]))
if (!strncmp(word, w->indices[i], 4))
found = w->indices + i;
}
return found ? found - w->indices + 1u : 0u;
Expand Down

0 comments on commit 20f77ef

Please sign in to comment.