From da0c48d52aad4541d38d45ad3490b151029b7f26 Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Wed, 21 Dec 2011 12:20:18 +0800 Subject: re-factor code --- src/libpinyin.ver | 2 +- src/pinyin.cpp | 6 +++--- src/pinyin.h | 6 +++--- src/storage/chewing_key.h | 4 ++++ src/storage/pinyin_parser2.cpp | 30 ++++++++++++++++++------------ src/storage/pinyin_parser2.h | 2 +- 6 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/libpinyin.ver b/src/libpinyin.ver index 1b6b718..88ccb2e 100644 --- a/src/libpinyin.ver +++ b/src/libpinyin.ver @@ -24,7 +24,7 @@ LIBPINYIN { pinyin_translate_token; pinyin_train; pinyin_reset; - _ZNK6pinyin9PinyinKey*; + _ZN6pinyin10ChewingKey*; local: *; diff --git a/src/pinyin.cpp b/src/pinyin.cpp index 8467570..1097b7f 100644 --- a/src/pinyin.cpp +++ b/src/pinyin.cpp @@ -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 diff --git a/src/pinyin.h b/src/pinyin.h index 3c53356..8778715 100644 --- a/src/pinyin.h +++ b/src/pinyin.h @@ -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); diff --git a/src/storage/chewing_key.h b/src/storage/chewing_key.h index 64f61b0..721f566 100644 --- a/src/storage/chewing_key.h +++ b/src/storage/chewing_key.h @@ -120,6 +120,10 @@ struct ChewingKeyRest m_raw_begin = 0; m_raw_end = 0; } + + guint16 length() { + return m_raw_end - m_raw_begin; + } }; }; diff --git a/src/storage/pinyin_parser2.cpp b/src/storage/pinyin_parser2.cpp index a762909..bab9d8a 100644 --- a/src/storage/pinyin_parser2.cpp +++ b/src/storage/pinyin_parser2.cpp @@ -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; } diff --git a/src/storage/pinyin_parser2.h b/src/storage/pinyin_parser2.h index 9988889..d651554 100644 --- a/src/storage/pinyin_parser2.h +++ b/src/storage/pinyin_parser2.h @@ -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; }; -- cgit