From 5ce8a5206d8778a6513b74ccf3a3421cc86b6d4c Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Mon, 15 Mar 2010 15:54:45 +0800 Subject: add ime.trim*. --- lua/lua-plugin-init.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) (limited to 'lua/lua-plugin-init.c') diff --git a/lua/lua-plugin-init.c b/lua/lua-plugin-init.c index 0ee2677..3965452 100644 --- a/lua/lua-plugin-init.c +++ b/lua/lua-plugin-init.c @@ -1,4 +1,5 @@ #include +#include #include #include "lua.h" @@ -53,6 +54,71 @@ static int ime_get_version(lua_State* L){ return 1; } +static gboolean ime_is_white_space(const char c){ + const char * white_space = " \t\n\r\v\f"; + int i; + size_t len = strlen(white_space); + + for ( i = 0; i < len; ++i){ + if ( white_space[i] == c ) + return TRUE; + } + return FALSE; +} + +static int ime_trim_string_left(lua_State* L){ + size_t l; int start, end; + const char * s = luaL_checklstring(L, 1, &l); + if (NULL == s){ + lua_pushliteral(L, ""); + return 1; + } + start = 0; end = strlen(s); + while( ime_is_white_space(s[start])){ + start++; + } + + lua_pushlstring(L, s + start, end - start); + return 1; +} + +static int ime_trim_string_right(lua_State* L){ + size_t l; int start, end; + const char * s = luaL_checklstring(L, 1, &l); + if ( NULL == s){ + lua_pushliteral(L, ""); + return 1; + } + start = 0; end = strlen(s); + while( ime_is_white_space(s[end - 1])){ + end--; + } + lua_pushlstring(L, s + start, end -start); + return 1; +} + +static int ime_trim_string(lua_State* L){ + size_t l; int start, end; + const char * s = luaL_checklstring(L, 1, &l); + if ( NULL == s){ + lua_pushliteral(L, ""); + return 1; + } + start = 0; end = strlen(s); + while( ime_is_white_space(s[start])){ + start++; + } + while( ime_is_white_space(s[end - 1])){ + end--; + } + if (start >= end ){ + lua_pushliteral(L, ""); + return 1; + } + lua_pushlstring(L, s + start, end -start); + return 1; +} + static const luaL_Reg imelib[] = { {"get_last_commit", ime_get_last_commit}, @@ -64,10 +130,10 @@ static const luaL_Reg imelib[] = { /* Note: the register_trigger function is dropped for ibus-pinyin. */ {"register_trigger", ime_register_trigger}, {"split_string", ime_split_string}, +#endif {"trim_string_left", ime_trim_string_left}, {"trim_string_right", ime_trim_string_right}, {"trim_string", ime_trim_string}, -#endif {NULL, NULL} }; -- cgit