re-factor code
authorPeng Wu <alexepico@gmail.com>
Wed, 21 Dec 2011 04:20:18 +0000 (12:20 +0800)
committerPeng Wu <alexepico@gmail.com>
Wed, 21 Dec 2011 04:20:18 +0000 (12:20 +0800)
src/libpinyin.ver
src/pinyin.cpp
src/pinyin.h
src/storage/chewing_key.h
src/storage/pinyin_parser2.cpp
src/storage/pinyin_parser2.h

index 1b6b718..88ccb2e 100644 (file)
@@ -24,7 +24,7 @@ LIBPINYIN {
         pinyin_translate_token;
         pinyin_train;
         pinyin_reset;
-        _ZNK6pinyin9PinyinKey*;
+        _ZN6pinyin10ChewingKey*;
 
     local:
        *;
index 8467570..1097b7f 100644 (file)
@@ -540,9 +540,9 @@ bool pinyin_get_candidates(pinyin_instance_t * instance,
     return true;
 }
 
-guint8 pinyin_choose_candidate(pinyin_instance_t * instance,
-                               size_t offset,
-                               phrase_token_t token){
+int pinyin_choose_candidate(pinyin_instance_t * instance,
+                            size_t offset,
+                            phrase_token_t token){
     pinyin_context_t * & context = instance->m_context;
 
     guint8 len = context->m_pinyin_lookup->add_constraint
index 3c53356..8778715 100644 (file)
@@ -88,9 +88,9 @@ bool pinyin_get_candidates(pinyin_instance_t * instance,
                            size_t offset,
                            TokenVector candidates);
 
-guint8 pinyin_choose_candidate(pinyin_instance_t * instance,
-                               size_t offset,
-                               phrase_token_t token);
+int pinyin_choose_candidate(pinyin_instance_t * instance,
+                            size_t offset,
+                            phrase_token_t token);
 
 bool pinyin_clear_constraint(pinyin_instance_t * instance,
                              size_t offset);
index 64f61b0..721f566 100644 (file)
@@ -120,6 +120,10 @@ struct ChewingKeyRest
         m_raw_begin = 0;
         m_raw_end = 0;
     }
+
+    guint16 length() {
+        return m_raw_end - m_raw_begin;
+    }
 };
 
 };
index a762909..bab9d8a 100644 (file)
@@ -662,12 +662,12 @@ bool DoublePinyinParser2::set_scheme(DoublePinyinScheme scheme) {
 
 /* the chewing string must be freed with g_free. */
 static bool search_chewing_symbols(const chewing_symbol_item_t * symbol_table,
-                                   const char key, char ** chewing) {
+                                   const char key, const char ** chewing) {
     *chewing = NULL;
     /* just iterate the table, as we only have < 50 items. */
     while (symbol_table->m_input != '\0') {
         if (symbol_table->m_input == key) {
-            *chewing = g_strdup(symbol_table->m_chewing);
+            *chewing = symbol_table->m_chewing;
             return true;
         }
         symbol_table ++;
@@ -705,12 +705,11 @@ bool ChewingParser2::parse_one_key(pinyin_option_t options,
     }
 
     int i;
-    gchar * chewing = NULL, * onechar = NULL;
+    gchar * chewing = NULL; const char * onechar = NULL;
 
     /* probe the possible chewing map in the rest of str. */
     for (i = 0; i < symbols_len; ++i) {
         if (!search_chewing_symbols(m_symbol_table, str[i], &onechar)) {
-            g_free(onechar);
             g_free(chewing);
             return false;
         }
@@ -722,7 +721,6 @@ bool ChewingParser2::parse_one_key(pinyin_option_t options,
             chewing = g_strconcat(chewing, onechar, NULL);
             g_free(tmp);
         }
-        g_free(onechar);
     }
 
     /* search the chewing in the chewing index table. */
@@ -748,7 +746,7 @@ int ChewingParser2::parse(pinyin_option_t options, ChewingKeyVector & keys,
     int maximum_len = 0; int i;
     /* probe the longest possible chewing string. */
     for (i = 0; i < len; ++i) {
-        if (!in_chewing_scheme(str[i]))
+        if (!in_chewing_scheme(str[i], NULL))
             break;
     }
     maximum_len = i;
@@ -806,13 +804,21 @@ bool ChewingParser2::set_scheme(ChewingScheme scheme) {
 }
 
 
-bool ChewingParser2::in_chewing_scheme(const char key) const {
-    gchar * chewing = NULL;
+bool ChewingParser2::in_chewing_scheme(const char key, const char ** symbol)
+ const {
+    const gchar * chewing = NULL;
     char tone = CHEWING_ZERO_TONE;
 
-    bool retval = search_chewing_symbols(m_symbol_table, key, &chewing) ||
-        search_chewing_tones(m_tone_table, key, &tone);
-    g_free(chewing);
+    if (search_chewing_symbols(m_symbol_table, key, &chewing)) {
+        if (symbol)
+            *symbol = chewing;
+        return true;
+    }
 
-    return retval;
+    if (search_chewing_tones(m_tone_table, key, &tone)) {
+        if (symbol)
+            *symbol = chewing_tone_table[tone];
+    }
+
+    return false;
 }
index 9988889..d651554 100644 (file)
@@ -213,7 +213,7 @@ public:
 
 public:
     bool set_scheme(ChewingScheme scheme);
-    bool in_chewing_scheme(const char key) const;
+    bool in_chewing_scheme(const char key, const char ** symbol) const;
 };