Introduce bsearch(3)

This commit is contained in:
Přemysl Eric Janouch 2014-09-23 02:41:40 +02:00
parent 56f7847ce3
commit 75d3388a35
1 changed files with 15 additions and 27 deletions

View File

@ -443,7 +443,7 @@ peekkey (termkey_t *tk, void *info,
return TERMKEY_RES_NONE; return TERMKEY_RES_NONE;
} }
static struct static struct func
{ {
const char *funcname; const char *funcname;
termkey_type_t type; termkey_type_t type;
@ -500,37 +500,25 @@ funcs[] =
{ NULL }, { NULL },
}; };
static int
func_compare (const void *key, const void *element)
{
return strcmp (key, ((struct func *) element)->funcname);
}
static int static int
funcname2keysym (const char *funcname, funcname2keysym (const char *funcname,
termkey_type_t *typep, termkey_sym_t *symp, int *modmaskp, int *modsetp) termkey_type_t *typep, termkey_sym_t *symp, int *modmaskp, int *modsetp)
{ {
// Binary search struct func *func = bsearch (funcname, funcs,
sizeof funcs / sizeof funcs[0], sizeof funcs[0], func_compare);
int start = 0; if (func)
int end = sizeof funcs / sizeof funcs[0];
// is "one past" the end of the range
// XXX: bsearch()?
while (1)
{ {
int i = (start + end) / 2; *typep = func->type;
int cmp = strcmp (funcname, funcs[i].funcname); *symp = func->sym;
*modmaskp = func->mods;
if (cmp == 0) *modsetp = func->mods;
{ return 1;
*typep = funcs[i].type;
*symp = funcs[i].sym;
*modmaskp = funcs[i].mods;
*modsetp = funcs[i].mods;
return 1;
}
else if (end == start + 1)
// That was our last choice and it wasn't it - not found
break;
else if (cmp > 0)
start = i;
else
end = i;
} }
if (funcname[0] == 'f' && isdigit (funcname[1])) if (funcname[0] == 'f' && isdigit (funcname[1]))