luaa: optimize package path computing

This is just for fun.

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2009-04-27 12:22:54 +02:00
parent 14b090e566
commit 9ac5cea397
1 changed files with 15 additions and 6 deletions

21
luaa.c
View File

@ -748,18 +748,27 @@ luaA_init(xdgHandle* xdg)
/* add XDG_CONFIG_DIR as include path */ /* add XDG_CONFIG_DIR as include path */
const char * const *xdgconfigdirs = xdgSearchableConfigDirectories(xdg); const char * const *xdgconfigdirs = xdgSearchableConfigDirectories(xdg);
buffer_t buf; buffer_t buf;
int prev_len = 0;
buffer_init(&buf); buffer_init(&buf);
#define A_LUAA_FIRST_STRING "package.path = package.path .. \";"
#define A_LUAA_SECOND_STRING "/awesome/?.lua;"
buffer_addsl(&buf, A_LUAA_FIRST_STRING);
buffer_addsl(&buf, A_LUAA_SECOND_STRING);
buffer_addsl(&buf, "/awesome/?/init.lua\"");
for(; *xdgconfigdirs; xdgconfigdirs++) for(; *xdgconfigdirs; xdgconfigdirs++)
{ {
size_t len = a_strlen(*xdgconfigdirs); size_t len = a_strlen(*xdgconfigdirs);
buffer_addsl(&buf, "package.path = package.path .. \";"); buffer_splice(&buf, sizeof(A_LUAA_FIRST_STRING) - 1, prev_len, *xdgconfigdirs, len);
buffer_add(&buf, *xdgconfigdirs, len); buffer_splice(&buf,
buffer_addsl(&buf, "/awesome/?.lua;"); sizeof(A_LUAA_FIRST_STRING) - 1
buffer_add(&buf, *xdgconfigdirs, len); + len
buffer_addsl(&buf, "/awesome/?/init.lua\""); + sizeof(A_LUAA_SECOND_STRING) - 1,
prev_len, *xdgconfigdirs, len);
luaA_dostring(L, buf.s); luaA_dostring(L, buf.s);
buf.len = 0; prev_len = len;
} }
#undef A_LUAA_FIRST_STRING
#undef A_LUAA_SECOND_STRING
buffer_wipe(&buf); buffer_wipe(&buf);
} }