summaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2012-07-16 13:24:15 +0800
committerPeng Wu <alexepico@gmail.com>2012-07-16 13:24:15 +0800
commit6a422d70a223a059393432ff45f7c1c3b1b44ae0 (patch)
tree709e23c2f0c5fb2b5884e15cb9fb142d07346e65 /lua
parent9634aef4f33bca66f1634f305860131c22c722ac (diff)
downloadibus-libpinyin-6a422d70a223a059393432ff45f7c1c3b1b44ae0.tar.gz
ibus-libpinyin-6a422d70a223a059393432ff45f7c1c3b1b44ae0.tar.xz
ibus-libpinyin-6a422d70a223a059393432ff45f7c1c3b1b44ae0.zip
write utf8/utf16 conversion
Diffstat (limited to 'lua')
-rw-r--r--lua/lua-plugin-init.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/lua/lua-plugin-init.c b/lua/lua-plugin-init.c
index 021883a..d45916a 100644
--- a/lua/lua-plugin-init.c
+++ b/lua/lua-plugin-init.c
@@ -327,6 +327,44 @@ static int ime_trim_string(lua_State* L){
return ime_push_string(L, s, start, end);
}
+static int ime_utf8_to_utf16(lua_State* L){
+ size_t l;
+ const char * s = luaL_checklstring(L, 1, &l);
+
+ luaL_Buffer buf;
+ luaL_buffinit(L, &buf);
+
+ glong written = 0;
+ gunichar2 * str = g_utf8_to_utf16(s, l, NULL, &written, NULL);
+
+ /* not includes trailing-zero */
+ luaL_addlstring(&buf, str, written * sizeof(gunichar2));
+ luaL_pushresult(&buf);
+
+ g_free(str);
+ lua_remove(L, 1);
+ return 1;
+}
+
+static int ime_utf16_to_utf8(lua_State* L){
+ size_t l;
+ const gunichar2 * s = luaL_checklstring(L, 1, &l);
+
+ luaL_Buffer buf;
+ luaL_buffinit(L, &buf);
+
+ glong written = 0;
+ gchar * str = g_utf16_to_utf8(s, l / sizeof(gunichar2),
+ NULL, &written, NULL );
+
+ /* not includes trailing-zero */
+ luaL_addlstring(&buf, str, written * sizeof(gchar));
+ luaL_pushresult(&buf);
+
+ g_free(str);
+ lua_remove(L, 1);
+ return 1;
+}
static const luaL_Reg imelib[] = {
{"get_last_commit", ime_get_last_commit},
@@ -340,6 +378,8 @@ static const luaL_Reg imelib[] = {
{"trim_string_left", ime_trim_string_left},
{"trim_string_right", ime_trim_string_right},
{"trim_string", ime_trim_string},
+ {"utf16_to_utf8", ime_utf16_to_utf8},
+ {"utf8_to_utf16", ime_utf8_to_utf16},
{NULL, NULL}
};