From fb3a4831ffc38392cf0a821134604035db259fa6 Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Thu, 13 Mar 2014 14:20:56 +0800 Subject: extend in_chewing_scheme method --- src/storage/pinyin_parser2.cpp | 159 +++++++++++++++++++++++++---------------- src/storage/pinyin_parser2.h | 6 +- 2 files changed, 100 insertions(+), 65 deletions(-) diff --git a/src/storage/pinyin_parser2.cpp b/src/storage/pinyin_parser2.cpp index f82c0d2..ac0f659 100644 --- a/src/storage/pinyin_parser2.cpp +++ b/src/storage/pinyin_parser2.cpp @@ -515,6 +515,35 @@ static bool search_chewing_tones(const chewing_tone_item_t * tone_table, return false; } +static int search_chewing_symbols2(const chewing_symbol_item_t * symbol_table, + const char key, + const char ** first, + const char ** second) { + int num = 0; + *first = NULL; *second = NULL; + + /* just iterate the table, as we only have < 50 items. */ + while (symbol_table->m_input != '\0') { + if (symbol_table->m_input == key) { + ++num; + if (NULL == *first) { + *first = symbol_table->m_chewing; + } else { + *second = symbol_table->m_chewing; + } + } + + /* search done */ + if (symbol_table->m_input > key) + break; + + symbol_table++; + } + + assert(0 <= num && num <= 2); + return num; +} + #if 0 bool ChewingSimpleParser2::parse_one_key(pinyin_option_t options, ChewingKey & key, @@ -726,67 +755,53 @@ bool ChewingDiscreteParser2::set_scheme(ChewingScheme scheme) { bool ChewingDiscreteParser2::in_chewing_scheme(pinyin_option_t options, const char key, - const char ** symbol) const { - const gchar * chewing = NULL; + gchar ** & symbols) const { + symbols = NULL; + GPtrArray * array = g_ptr_array_new(); + + const gchar * first = NULL, * second = NULL; unsigned char tone = CHEWING_ZERO_TONE; - if (search_chewing_symbols(m_initial_table, key, &chewing)) { - if (symbol) - *symbol = chewing; - return true; + if (search_chewing_symbols2(m_initial_table, key, &first, &second)) { + if (first) + g_ptr_array_add(array, g_strdup(first)); + if (second) + g_ptr_array_add(array, g_strdup(second)); } - if (search_chewing_symbols(m_middle_table, key, &chewing)) { - if (symbol) - *symbol = chewing; - return true; + if (search_chewing_symbols2(m_middle_table, key, &first, &second)) { + if (first) + g_ptr_array_add(array, g_strdup(first)); + if (second) + g_ptr_array_add(array, g_strdup(second)); } - if (search_chewing_symbols(m_final_table, key, &chewing)) { - if (symbol) - *symbol = chewing; - return true; + if (search_chewing_symbols2(m_final_table, key, &first, &second)) { + if (first) + g_ptr_array_add(array, g_strdup(first)); + if (second) + g_ptr_array_add(array, g_strdup(second)); } if (!(options & USE_TONE)) - return false; + goto end; if (search_chewing_tones(m_tone_table, key, &tone)) { - if (symbol) - *symbol = chewing_tone_table[tone]; - return true; + g_ptr_array_add(array, g_strdup(chewing_tone_table[tone])); } - return false; -} - -static int search_chewing_symbols2(const chewing_symbol_item_t * symbol_table, - const char key, - const char ** first, - const char ** second) { - int num = 0; - *first = NULL; *second = NULL; +end: + assert(array->len <= 2); - /* just iterate the table, as we only have < 50 items. */ - while (symbol_table->m_input != '\0') { - if (symbol_table->m_input == key) { - ++num; - if (NULL == *first) { - *first = symbol_table->m_chewing; - } else { - *second = symbol_table->m_chewing; - } - } - - /* search done */ - if (symbol_table->m_input > key) - break; - - symbol_table++; + if (array->len) { + g_ptr_array_add(array, NULL); + /* must be freed by g_strfreev. */ + symbols = (gchar **) g_ptr_array_free(array, FALSE); + return true; } - assert(0 <= num && num <= 2); - return num; + g_ptr_array_free(array, TRUE); + return false; } ChewingDaChenCP26Parser2::ChewingDaChenCP26Parser2() { @@ -1054,37 +1069,57 @@ int ChewingDaChenCP26Parser2::parse(pinyin_option_t options, bool ChewingDaChenCP26Parser2::in_chewing_scheme(pinyin_option_t options, const char key, - const char ** symbol) const { - const gchar * chewing = NULL; + gchar ** & symbols) const { + symbols = NULL; + GPtrArray * array = g_ptr_array_new(); + + const gchar * first = NULL, * second = NULL; unsigned char tone = CHEWING_ZERO_TONE; - if (search_chewing_symbols(m_initial_table, key, &chewing)) { - if (symbol) - *symbol = chewing; - return true; + if (search_chewing_symbols2(m_initial_table, key, &first, &second)) { + if (first) + g_ptr_array_add(array, g_strdup(first)); + if (second) + g_ptr_array_add(array, g_strdup(second)); } - if (search_chewing_symbols(m_middle_table, key, &chewing)) { - if (symbol) - *symbol = chewing; - return true; + if (search_chewing_symbols2(m_middle_table, key, &first, &second)) { + if (first) + g_ptr_array_add(array, g_strdup(first)); + if (second) + g_ptr_array_add(array, g_strdup(second)); } - if (search_chewing_symbols(m_final_table, key, &chewing)) { - if (symbol) - *symbol = chewing; - return true; + if (search_chewing_symbols2(m_final_table, key, &first, &second)) { + if (first) + g_ptr_array_add(array, g_strdup(first)); + if (second) + g_ptr_array_add(array, g_strdup(second)); + } + + /* handles for "i" */ + if ('i' == key) { + g_ptr_array_add(array, g_strdup("ㄧㄚ")); } if (!(options & USE_TONE)) - return false; + goto end; if (search_chewing_tones(m_tone_table, key, &tone)) { - if (symbol) - *symbol = chewing_tone_table[tone]; + g_ptr_array_add(array, g_strdup(chewing_tone_table[tone])); + } + +end: + assert(array->len <= 3); + + if (array->len) { + g_ptr_array_add(array, NULL); + /* must be freed by g_strfreev. */ + symbols = (gchar **) g_ptr_array_free(array, FALSE); return true; } + g_ptr_array_free(array, TRUE); return false; } diff --git a/src/storage/pinyin_parser2.h b/src/storage/pinyin_parser2.h index f44c3d3..82edbb6 100644 --- a/src/storage/pinyin_parser2.h +++ b/src/storage/pinyin_parser2.h @@ -167,7 +167,7 @@ public: * Check whether the input character is in the chewing keyboard mapping. * */ - virtual bool in_chewing_scheme(pinyin_option_t options, const char key, const char ** symbol) const = 0; + virtual bool in_chewing_scheme(pinyin_option_t options, const char key, gchar ** & symbols) const = 0; }; /** @@ -216,7 +216,7 @@ public: public: bool set_scheme(ChewingScheme scheme); - virtual bool in_chewing_scheme(pinyin_option_t options, const char key, const char ** symbol) const; + virtual bool in_chewing_scheme(pinyin_option_t options, const char key, gchar ** & symbols) const; }; @@ -240,7 +240,7 @@ public: virtual int parse(pinyin_option_t options, ChewingKeyVector & keys, ChewingKeyRestVector & key_rests, const char *str, int len) const; public: - virtual bool in_chewing_scheme(pinyin_option_t options, const char key, const char ** symbol) const; + virtual bool in_chewing_scheme(pinyin_option_t options, const char key, gchar ** & symbols) const; }; -- cgit