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 ){
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);
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);
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 ){
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++;
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--;
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++;
#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");
L = lua_open();
lua_plugin_init(L);
- luaL_dofile(L, "test.lua");
+
+ run_test(L, "test.lua");
+
lua_plugin_fini(L);
return 0;
}
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 = [[