From 9ac5cea397567c8edac22a0b53b8f9a56ee9121b Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Mon, 27 Apr 2009 12:22:54 +0200 Subject: [PATCH] luaa: optimize package path computing This is just for fun. Signed-off-by: Julien Danjou --- luaa.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/luaa.c b/luaa.c index c29838402..3e52c0b19 100644 --- a/luaa.c +++ b/luaa.c @@ -748,18 +748,27 @@ luaA_init(xdgHandle* xdg) /* add XDG_CONFIG_DIR as include path */ const char * const *xdgconfigdirs = xdgSearchableConfigDirectories(xdg); buffer_t buf; + int prev_len = 0; 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++) { size_t len = a_strlen(*xdgconfigdirs); - buffer_addsl(&buf, "package.path = package.path .. \";"); - buffer_add(&buf, *xdgconfigdirs, len); - buffer_addsl(&buf, "/awesome/?.lua;"); - buffer_add(&buf, *xdgconfigdirs, len); - buffer_addsl(&buf, "/awesome/?/init.lua\""); + buffer_splice(&buf, sizeof(A_LUAA_FIRST_STRING) - 1, prev_len, *xdgconfigdirs, len); + buffer_splice(&buf, + sizeof(A_LUAA_FIRST_STRING) - 1 + + len + + sizeof(A_LUAA_SECOND_STRING) - 1, + prev_len, *xdgconfigdirs, len); luaA_dostring(L, buf.s); - buf.len = 0; + prev_len = len; } +#undef A_LUAA_FIRST_STRING +#undef A_LUAA_SECOND_STRING buffer_wipe(&buf); }