diff options
author | Peng Wu <alexepico@gmail.com> | 2015-12-24 13:08:39 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2015-12-24 15:45:15 +0800 |
commit | 3da54d43273d44be2861192c8ed1c573efe537c7 (patch) | |
tree | 79d3e705d91870cf20b425f84642f704442843eb | |
parent | 7b790c9dbf5d93882be92feff98b8e2aa6b429c3 (diff) | |
download | libpinyin-3da54d43273d44be2861192c8ed1c573efe537c7.tar.gz libpinyin-3da54d43273d44be2861192c8ed1c573efe537c7.tar.xz libpinyin-3da54d43273d44be2861192c8ed1c573efe537c7.zip |
simplify ZhuyinDaChenCP26Parser2
-rw-r--r-- | src/storage/zhuyin_parser2.cpp | 114 |
1 files changed, 42 insertions, 72 deletions
diff --git a/src/storage/zhuyin_parser2.cpp b/src/storage/zhuyin_parser2.cpp index 9047947..d55d92e 100644 --- a/src/storage/zhuyin_parser2.cpp +++ b/src/storage/zhuyin_parser2.cpp @@ -544,6 +544,22 @@ ZhuyinDaChenCP26Parser2::ZhuyinDaChenCP26Parser2() { m_tone_table = chewing_dachen_cp26_tones; } +static int count_same_chars(const char * str, int len) { + assert(len > 0); + + int count = 0; + const char cur_char = str[0]; + + for (int i = 0; i < len; ++i) { + if (cur_char != str[i]) + break; + ++count; + } + + assert(count >= 1); + return count; +} + bool ZhuyinDaChenCP26Parser2::parse_one_key(pinyin_option_t options, ChewingKey & key, const char *str, int len) const { @@ -581,26 +597,19 @@ bool ZhuyinDaChenCP26Parser2::parse_one_key(pinyin_option_t options, if (0 == len) return false; - int i; int choice; + int choice; int count; /* probe initial */ do { ch = input[index]; + count = count_same_chars(input + index, len - index); if (search_chewing_symbols2(m_initial_table, ch, &first, &second)) { - index ++; + index += count; if (NULL == second) { initial = first; break; } else { - choice = 0; - /* zero out the same char */ - for (i = index; i < len; ++i) { - if (input[i] == ch) { - input[i] = '\0'; - choice ++; - } - } - choice = choice % 2; + choice = (count - 1) % 2; if (0 == choice) initial = first; if (1 == choice) @@ -609,12 +618,6 @@ bool ZhuyinDaChenCP26Parser2::parse_one_key(pinyin_option_t options, } } while (0); - /* skip zeros */ - for (; index < len; index ++) { - if ('\0' != input[index]) - break; - } - if (index == len) goto probe; @@ -622,57 +625,37 @@ bool ZhuyinDaChenCP26Parser2::parse_one_key(pinyin_option_t options, /* probe middle */ do { ch = input[index]; + count = count_same_chars(input + index, len - index); /* handle 'u' */ if ('u' == ch) { - index ++; - choice = 0; - /* zero out the same char */ - for (i = index; i < len; ++i) { - if ('u' == input[i]) { - input[i] = '\0'; - choice ++; - } - choice = choice % 3; - if (0 == choice) - middle = "ㄧ"; - if (1 == choice) - final = "ㄚ"; - if (2 == choice) { - middle = "ㄧ"; - final = "ㄚ"; - } + choice = (count - 1) % 3; + if (0 == choice) + middle = "ㄧ"; + if (1 == choice) + final = "ㄚ"; + if (2 == choice) { + middle = "ㄧ"; + final = "ㄚ"; } } /* handle 'm' */ if ('m' == ch) { - index ++; - choice = 0; - /* zero out the same char */ - for (i = index; i < len; ++i) { - if ('m' == input[i]) { - input[i] = '\0'; - choice ++; - } - choice = choice % 2; - if (0 == choice) - middle = "ㄩ"; - if (1 == choice) - final = "ㄡ"; - } + choice = (count - 1) % 2; + if (0 == choice) + middle = "ㄩ"; + if (1 == choice) + final = "ㄡ"; + } + /* handle 'j' */ + if ('j' == ch) { + middle = "ㄨ"; } if (search_chewing_symbols2(m_middle_table, ch, &first, &second)) { + index += count; assert(NULL == second); - index ++; - middle = first; } } while(0); - /* skip zeros */ - for (; index < len; index ++) { - if ('\0' != input[index]) - break; - } - if (index == len) goto probe; @@ -683,21 +666,14 @@ bool ZhuyinDaChenCP26Parser2::parse_one_key(pinyin_option_t options, break; ch = input[index]; + count = count_same_chars(input + index, len - index); if (search_chewing_symbols2(m_final_table, ch, &first, &second)) { - index ++; + index += count; if (NULL == second) { final = first; break; } else { - choice = 0; - /* zero out the same char */ - for (i = index; i < len; ++i) { - if (input[i] == ch) { - input[i] = '\0'; - choice ++; - } - } - choice = choice % 2; + choice = (count - 1) % 2; if (0 == choice) final = first; if (1 == choice) @@ -706,12 +682,6 @@ bool ZhuyinDaChenCP26Parser2::parse_one_key(pinyin_option_t options, } } while(0); - /* skip zeros */ - for (; index < len; index ++) { - if ('\0' != input[index]) - break; - } - if (index == len) goto probe; |