From bfee085f2be86e3bd16a96c8dd70c3cc937852b1 Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Tue, 6 Apr 2010 12:53:36 +0800 Subject: [PATCH] change tolstring to checklstring. --- lua/lua-plugin-init.c | 52 ++++++++++----------------------------------------- lua/test-lua-plugin.c | 28 ++++++++++++++++++++++++++- lua/test.lua | 8 -------- 3 files changed, 37 insertions(+), 51 deletions(-) diff --git a/lua/lua-plugin-init.c b/lua/lua-plugin-init.c index eaa7ad0..a59e6f2 100644 --- a/lua/lua-plugin-init.c +++ b/lua/lua-plugin-init.c @@ -65,7 +65,7 @@ static int ime_join_string(lua_State* L){ if ( !lua_istable(L, 1) ) return 0; - sep = lua_tolstring(L, 2, NULL); + sep = luaL_checklstring(L, 2, NULL); vec_len = lua_objlen(L, 1); if ( 0 == vec_len ){ @@ -105,18 +105,10 @@ static int ime_parse_mapping(lua_State * L){ gchar** key_value = NULL; const char * key = NULL; gchar** values = NULL; size_t values_no = 0; const char * value = NULL; - src_string = lua_tolstring(L, 1, NULL); - if ( NULL == src_string || '\0' == src_string[0]) - goto failed; - line_sep = lua_tolstring(L, 2, NULL); - if ( NULL == line_sep || '\0' == line_sep[0]) - goto failed; - key_value_sep = lua_tolstring(L, 3, NULL); - if ( NULL == key_value_sep || '\0' == key_value_sep[0]) - goto failed; - values_sep = lua_tolstring(L, 4, NULL); - if ( NULL == values_sep || '\0' == values_sep[0]) - goto failed; + src_string = luaL_checklstring(L, 1, NULL); + line_sep = luaL_checklstring(L, 2, NULL); + key_value_sep = luaL_checklstring(L, 3, NULL); + values_sep = luaL_checklstring(L, 4, NULL); lines = g_strsplit(src_string, line_sep, 0); lines_no = g_strv_length(lines); @@ -157,29 +149,15 @@ static int ime_parse_mapping(lua_State * L){ lua_remove(L, 2); lua_remove(L, 1); return 1; - -failed: - lua_createtable(L, 0, 0); - return 1; } static int ime_split_string(lua_State * L){ gchar ** str_vec; guint str_vec_len = 0; int i; const char * sep; - const char * str = lua_tolstring(L, 1, NULL); + const char * str = luaL_checklstring(L, 1, NULL); - if ( NULL == str || '\0' == str[0]){ - lua_newtable(L); - return 1; - } - - sep = lua_tolstring(L, 2, NULL); - - if ( NULL == sep || '\0' == sep[0]){ - lua_newtable(L); - return 1; - } + sep = luaL_checklstring(L, 2, NULL); str_vec = g_strsplit(str, sep, 0); str_vec_len = g_strv_length(str_vec); @@ -209,13 +187,6 @@ static gboolean ime_is_white_space(const char c){ return FALSE; } -#define IME_TRIM_PRECHECK \ - if (NULL == s || '\0' == s[0]){ \ - lua_pushliteral(L, ""); \ - return 1; \ - } - - static int ime_push_string(lua_State* L, const char * s, int start, int end){ if (start >= end ){ @@ -229,9 +200,8 @@ static int ime_push_string(lua_State* L, const char * s, static int ime_trim_string_left(lua_State* L){ size_t l; int start, end; - const char * s = lua_tolstring(L, 1, &l); + const char * s = luaL_checklstring(L, 1, &l); - IME_TRIM_PRECHECK; start = 0; end = l; while( ime_is_white_space(s[start])){ start++; @@ -242,9 +212,8 @@ static int ime_trim_string_left(lua_State* L){ static int ime_trim_string_right(lua_State* L){ size_t l; int start, end; - const char * s = lua_tolstring(L, 1, &l); + const char * s = luaL_checklstring(L, 1, &l); - IME_TRIM_PRECHECK; start = 0; end = l; while( ime_is_white_space(s[end - 1]) && end > 0){ end--; @@ -255,9 +224,8 @@ static int ime_trim_string_right(lua_State* L){ static int ime_trim_string(lua_State* L){ size_t l; int start, end; - const char * s = lua_tolstring(L, 1, &l); + const char * s = luaL_checklstring(L, 1, &l); - IME_TRIM_PRECHECK; start = 0; end = l; while( ime_is_white_space(s[start])){ start++; diff --git a/lua/test-lua-plugin.c b/lua/test-lua-plugin.c index c2de597..34bbef5 100644 --- a/lua/test-lua-plugin.c +++ b/lua/test-lua-plugin.c @@ -7,8 +7,32 @@ #include "lua-plugin.h" + +static const char * progname = "test-lua-plugin"; static lua_State * L = NULL; +static void l_message (const char *pname, const char *msg) { + if (pname) fprintf(stderr, "%s: ", pname); + fprintf(stderr, "%s\n", msg); + fflush(stderr); +} + +static int report (lua_State *L, int status) { + if (status && !lua_isnil(L, -1)) { + const char *msg = lua_tostring(L, -1); + if (msg == NULL) msg = "(error object is not a string)"; + l_message(progname, msg); + lua_pop(L, 1); + } + return status; +} + +static int run_test(lua_State *L, const char * filename){ + int status = luaL_dofile(L, filename); + fprintf(stderr, "%s done.\n", filename); + return report(L, status); +} + int main(int argc, char * argv[]){ printf("starting test...\n"); @@ -16,7 +40,9 @@ int main(int argc, char * argv[]){ L = lua_open(); lua_plugin_init(L); - luaL_dofile(L, "test.lua"); + + run_test(L, "test.lua"); + lua_plugin_fini(L); return 0; } diff --git a/lua/test.lua b/lua/test.lua index 166bcb3..b0ace98 100644 --- a/lua/test.lua +++ b/lua/test.lua @@ -1,23 +1,15 @@ print(ime.get_version()) print(ime.trim_string(" Hello World! ")) -print(ime.trim_string(nil)) print(ime.trim_string_right(" Hello World! ")) tab = ime.split_string("aa..bb..cc", ".."); for i,v in ipairs(tab) do print(i, v) end -tab = ime.split_string(nil, nil); -for i,v in ipairs(tab) do print(i, v) end - -tab = ime.split_string("aa..bb..cc", nil); -for i,v in ipairs(tab) do print(i, v) end - tab = ime.split_string("aa..bb..cc", ".."); print(ime.join_string(tab, ", ")); -print(ime.join_string(nil, "..")); print(ime.join_string({}, "..")); _MAPPING_TABLE = [[ -- 2.7.4