summaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorPeng Wu <epico@dhcp-65-116.nay.redhat.com>2010-03-15 15:54:45 +0800
committerPeng Wu <alexepico@gmail.com>2010-05-19 10:09:31 +0800
commit5ce8a5206d8778a6513b74ccf3a3421cc86b6d4c (patch)
tree836a306b7d4c96bb2e78d3e584d056a375b6b96d /lua
parentea36603015ab31469aa59eff8ee09880d006f83d (diff)
downloadibus-libpinyin-5ce8a5206d8778a6513b74ccf3a3421cc86b6d4c.tar.gz
ibus-libpinyin-5ce8a5206d8778a6513b74ccf3a3421cc86b6d4c.tar.xz
ibus-libpinyin-5ce8a5206d8778a6513b74ccf3a3421cc86b6d4c.zip
add ime.trim*.
Diffstat (limited to 'lua')
-rw-r--r--lua/lua-plugin-init.c68
1 files changed, 67 insertions, 1 deletions
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 <stdio.h>
+#include <string.h>
#include <glib.h>
#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}
};