util: precompute funcname in name_func_link

This will improve search a bit.

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-11-12 15:07:24 +01:00
parent ccc6452d49
commit f188c10fa4
6 changed files with 18 additions and 15 deletions

View File

@ -16,9 +16,9 @@ do
grep '^layout_t layout_' $file | cut -d' ' -f2 | cut -d\; -f1 | while read layout grep '^layout_t layout_' $file | cut -d' ' -f2 | cut -d\; -f1 | while read layout
do do
shortname=`echo $layout | cut -d _ -f2-` shortname=`echo $layout | cut -d _ -f2-`
echo " {\"$shortname\", $layout}," echo " {\"$shortname\", sizeof(\"$shortname\") - 1, $layout},"
done done
done done
echo " {NULL, NULL}" echo " {NULL, 0, NULL}"
echo "};" echo "};"

View File

@ -10,9 +10,9 @@ do
grep '^widget_constructor_t ' "$file" | cut -d' ' -f2 | cut -d\; -f1 | while read widget grep '^widget_constructor_t ' "$file" | cut -d' ' -f2 | cut -d\; -f1 | while read widget
do do
shortname=`echo $widget | cut -d_ -f2` shortname=`echo $widget | cut -d_ -f2`
echo " {\"$shortname\", $widget}," echo " {\"$shortname\", sizeof(\"$shortname\") - 1, $widget},"
done done
done done
echo " {NULL, NULL}" echo " {NULL, 0, NULL}"
echo "};" echo "};"

View File

@ -59,17 +59,18 @@ _warn(int line, const char *fct, const char *fmt, ...)
/** Lookup for a function pointer from its name /** Lookup for a function pointer from its name
* in the given name_func_link_t list. * in the given name_func_link_t list.
* \param funcname Function name. * \param funcname Function name.
* \param len The function name length.
* \param list Function and name link list. * \param list Function and name link list.
* \return Function pointer. * \return Function pointer.
*/ */
void * void *
name_func_lookup(const char *funcname, const name_func_link_t *list) name_func_lookup(const char *funcname, size_t len, const name_func_link_t *list)
{ {
int i; int i;
if(funcname && list) if(funcname && list)
for(i = 0; list[i].name; i++) for(i = 0; list[i].name; i++)
if(!a_strcmp(funcname, list[i].name)) if(len == list[i].len && !a_strcmp(funcname, list[i].name))
return list[i].func; return list[i].func;
return NULL; return NULL;

View File

@ -60,6 +60,7 @@ typedef enum
typedef struct typedef struct
{ {
const char *name; const char *name;
size_t len;
void *func; void *func;
} name_func_link_t; } name_func_link_t;
@ -351,7 +352,7 @@ position_t position_fromstr(const char *, ssize_t);
const char * position_tostr(position_t); const char * position_tostr(position_t);
orientation_t orientation_fromstr(const char *, ssize_t); orientation_t orientation_fromstr(const char *, ssize_t);
const char * orientation_tostr(orientation_t); const char * orientation_tostr(orientation_t);
void *name_func_lookup(const char *, const name_func_link_t *); void *name_func_lookup(const char *, size_t, const name_func_link_t *);
const char * name_func_rlookup(void *, const name_func_link_t *); const char * name_func_rlookup(void *, const name_func_link_t *);
void a_exec(const char *); void a_exec(const char *);
char ** a_strsplit(const char *, ssize_t, char); char ** a_strsplit(const char *, ssize_t, char);

10
tag.c
View File

@ -271,7 +271,7 @@ tag_view_only_byindex(int screen, int dindex)
static int static int
luaA_tag_new(lua_State *L) luaA_tag_new(lua_State *L)
{ {
size_t len; size_t len, laylen;
tag_t *tag; tag_t *tag;
int ncol, nmaster; int ncol, nmaster;
const char *name, *lay; const char *name, *lay;
@ -286,9 +286,9 @@ luaA_tag_new(lua_State *L)
mwfact = luaA_getopt_number(L, 2, "mwfact", 0.5); mwfact = luaA_getopt_number(L, 2, "mwfact", 0.5);
ncol = luaA_getopt_number(L, 2, "ncol", 1); ncol = luaA_getopt_number(L, 2, "ncol", 1);
nmaster = luaA_getopt_number(L, 2, "nmaster", 1); nmaster = luaA_getopt_number(L, 2, "nmaster", 1);
lay = luaA_getopt_string(L, 2, "layout", "tile"); lay = luaA_getopt_lstring(L, 2, "layout", "tile", &laylen);
layout = name_func_lookup(lay, LayoutList); layout = name_func_lookup(lay, laylen, LayoutList);
tag = tag_new(name, len, tag = tag_new(name, len,
layout, layout,
@ -430,8 +430,8 @@ luaA_tag_newindex(lua_State *L)
tag_append_to_screen(*tag, &globalconf.screens[screen]); tag_append_to_screen(*tag, &globalconf.screens[screen]);
break; break;
case A_TK_LAYOUT: case A_TK_LAYOUT:
buf = luaL_checkstring(L, 3); buf = luaL_checklstring(L, 3, &len);
l = name_func_lookup(buf, LayoutList); l = name_func_lookup(buf, len, LayoutList);
if(l) if(l)
(*tag)->layout = l; (*tag)->layout = l;
else else

View File

@ -329,10 +329,9 @@ luaA_widget_new(lua_State *L)
luaA_checktable(L, 2); luaA_checktable(L, 2);
align = luaA_getopt_lstring(L, 2, "align", "left", &len); type = luaA_getopt_lstring(L, 2, "type", NULL, &len);
type = luaA_getopt_string(L, 2, "type", NULL);
if((wc = name_func_lookup(type, WidgetList))) if((wc = name_func_lookup(type, len, WidgetList)))
{ {
w = p_new(widget_t, 1); w = p_new(widget_t, 1);
wc(w); wc(w);
@ -344,6 +343,8 @@ luaA_widget_new(lua_State *L)
} }
w->type = wc; w->type = wc;
align = luaA_getopt_lstring(L, 2, "align", "left", &len);
w->align_supported |= AlignLeft | AlignRight; w->align_supported |= AlignLeft | AlignRight;
w->align = draw_align_fromstr(align, len); w->align = draw_align_fromstr(align, len);