summaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorPeng Wu <epico@dhcp-65-116.nay.redhat.com>2010-03-15 16:06:18 +0800
committerPeng Wu <alexepico@gmail.com>2010-05-19 10:09:31 +0800
commit38e3513af3026135910647a5b09eee6b51d04a3c (patch)
tree617a8c6dcbcbd39049badf19002ed7929f2d6406 /lua
parent5ce8a5206d8778a6513b74ccf3a3421cc86b6d4c (diff)
downloadibus-libpinyin-38e3513af3026135910647a5b09eee6b51d04a3c.tar.gz
ibus-libpinyin-38e3513af3026135910647a5b09eee6b51d04a3c.tar.xz
ibus-libpinyin-38e3513af3026135910647a5b09eee6b51d04a3c.zip
refactor code for checking condition.
Diffstat (limited to 'lua')
-rw-r--r--lua/lua-plugin-init.c50
1 files changed, 28 insertions, 22 deletions
diff --git a/lua/lua-plugin-init.c b/lua/lua-plugin-init.c
index 3965452..ca4fb7e 100644
--- a/lua/lua-plugin-init.c
+++ b/lua/lua-plugin-init.c
@@ -66,44 +66,54 @@ static gboolean ime_is_white_space(const char c){
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){
+#define IME_TRIM_PRECHECK \
+ if (NULL == s){ \
+ lua_pushliteral(L, ""); \
+ return 1; \
+ }
+
+
+static int ime_push_string(lua_State* L, const char * s,
+ int start, int end){
+ if (start >= end ){
lua_pushliteral(L, "");
return 1;
}
+ lua_pushlstring(L, s + start, end -start);
+ return 1;
+}
+
+static int ime_trim_string_left(lua_State* L){
+ size_t l; int start, end;
+ const char * s = luaL_checklstring(L, 1, &l);
+
+ IME_TRIM_PRECHECK;
start = 0; end = strlen(s);
while( ime_is_white_space(s[start])){
start++;
}
- lua_pushlstring(L, s + start, end - start);
- return 1;
+ return ime_push_string(L, s, start, end);
}
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;
- }
+
+ IME_TRIM_PRECHECK;
start = 0; end = strlen(s);
while( ime_is_white_space(s[end - 1])){
end--;
}
- lua_pushlstring(L, s + start, end -start);
- return 1;
+
+ return ime_push_string(L, s, start, end);
}
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;
- }
+
+ IME_TRIM_PRECHECK;
start = 0; end = strlen(s);
while( ime_is_white_space(s[start])){
start++;
@@ -111,12 +121,8 @@ static int ime_trim_string(lua_State* L){
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;
+
+ return ime_push_string(L, s, start, end);
}