From 0f23ce88cbef8eb124135557e874ed382d2f296a Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Wed, 25 Jun 2008 11:42:06 +0200 Subject: [PATCH] Divert lua string.len to a multibyte aware implementation. Signed-off-by: Pierre Habouzit --- lua.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lua.c b/lua.c index 36de23aaa..a48f3a64e 100644 --- a/lua.c +++ b/lua.c @@ -453,6 +453,23 @@ luaA_openlib(lua_State *L, const char *name, luaL_register(L, name, methods); } +static int +luaA_mbstrlen(lua_State *L) +{ + const char *cmd = luaL_checkstring(L, 1); + lua_pushnumber(L, mbstowcs(NULL, NONULL(cmd), 0)); + return 1; +} + +static void +luaA_fixups(lua_State *L) +{ + lua_getglobal(L, "string"); + lua_pushcfunction(L, luaA_mbstrlen); + lua_setfield(L, -2, "len"); + lua_pop(L, 1); +} + /** Initialize the Lua VM */ void @@ -497,6 +514,8 @@ luaA_init(void) luaL_openlibs(L); + luaA_fixups(L); + /* Export awesome lib */ luaL_register(L, "awesome", awesome_lib);