diff options
author | Peng Wu <alexepico@gmail.com> | 2013-03-11 10:44:21 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2013-03-11 10:44:21 +0800 |
commit | 6a448d5b60e5c9f7240d4149c3a81a0df439d275 (patch) | |
tree | b3fc3e4c87453207dbb3731822e6a62372c2ec5e | |
parent | ab4961ecf5ea99b08454c011295687153d75cc04 (diff) | |
download | ibus-libpinyin-6a448d5b60e5c9f7240d4149c3a81a0df439d275.tar.gz ibus-libpinyin-6a448d5b60e5c9f7240d4149c3a81a0df439d275.tar.xz ibus-libpinyin-6a448d5b60e5c9f7240d4149c3a81a0df439d275.zip |
re-factor ExtEditor
-rw-r--r-- | src/Makefile.am | 6 | ||||
-rw-r--r-- | src/PYExtEditor.cc | 121 |
2 files changed, 114 insertions, 13 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index fe986fc..bf65ded 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -49,9 +49,6 @@ ibus_engine_libpinyin_c_sources = \ PYPinyinProperties.cc \ PYPunctEditor.cc \ PYSimpTradConverter.cc \ - PYDynamicSpecialPhrase.cc \ - PYSpecialPhrase.cc \ - PYSpecialPhraseTable.cc \ $(NULL) ibus_engine_libpinyin_h_sources = \ PYBus.h \ @@ -71,9 +68,6 @@ ibus_engine_libpinyin_h_sources = \ PYRegex.h \ PYSignal.h \ PYSimpTradConverter.h \ - PYSpecialPhrase.h \ - PYDynamicSpecialPhrase.h \ - PYSpecialPhraseTable.h \ PYString.h \ PYText.h \ PYTypes.h \ diff --git a/src/PYExtEditor.cc b/src/PYExtEditor.cc index 8178fb1..20898e9 100644 --- a/src/PYExtEditor.cc +++ b/src/PYExtEditor.cc @@ -29,14 +29,123 @@ extern "C" { #include "PYPointer.h" #include "PYLookupTable.h" -#include "PYDynamicSpecialPhrase.h" - #include "PYEditor.h" #include "PYExtEditor.h" namespace PY { +static const char * numbers [2][10] = { + {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖",}, + {"〇", "一", "二", "三", "四", "五", "六", "七", "八", "九",}, +}; + +struct unit_t{ + const char * unit_zh_name; // Chinese Character + const int digits; // Position in string. + const bool persist; // Whether to force eating zero and force inserting into result string. +}; + +static unit_t units_simplified[] ={ + {"兆", 12, true}, + {"亿", 8, true}, + {"万", 4, true}, + {"千", 3, false}, + {"百", 2, false}, + {"十", 1, false}, + {"", 0, true}, +}; + +static unit_t units_traditional[] ={ + {"兆", 12, true}, + {"亿", 8, true}, + {"万", 4, true}, + {"仟", 3, false}, + {"佰", 2, false}, + {"拾", 1, false}, + {"", 0, true}, +}; + + +static const std::string +simplest_cn_number(gint64 num) +{ + std::string result = ""; + if ( num == 0 ) + result = numbers[1][0]; + while (num > 0) { + int remains = num % 10; + num = num / 10; + result = std::string ( numbers[1][remains] ) + result; + } + + return result; +} + +static inline const std::string +translate_to_longform(gint64 num, const char * number[10], unit_t units[]) +{ + std::string result = ""; + int cur_pos = -1; + bool eat_zero = false; + + while (num > 0) { + int remains = num % 10; + num = num / 10; + cur_pos ++; + std::string unit = ""; + int pos = cur_pos; + size_t i = 6; + while ( pos > 0 ) { + for ( i = 0; i < 7; ++i) { + pos = pos % units[i].digits; + if ( pos == 0 ) + break; + } + } + + if ( units[i].persist ) { + result = std::string (units[i].unit_zh_name) + result; + eat_zero = true; + } + + if ( remains == 0){ + if ( eat_zero ) continue; + + result = std::string (number[0]) + result; + eat_zero = true; + continue; + }else{ + eat_zero = false; + } + + if (num == 0 && remains == 1 && i == 5) + result = std::string (units[i].unit_zh_name) + result; + else if (units[i].persist) + result = std::string (number[remains]) + result; + else + result = std::string (number[remains]) + std::string (units[i].unit_zh_name) + result; + } + + return result; +} + +static const std::string +simplified_number(gint64 num) +{ + return translate_to_longform(num, numbers[1], units_simplified); +} + +static const std::string +traditional_number(gint64 num) +{ + if ( 0 == num ) + return numbers[0][0]; + return translate_to_longform(num, numbers[0], units_traditional); +} + + + /* Write digit/alpha/none Label generator here. * foreach (results): 1, from get_retval; 2..n from get_retvals. */ @@ -715,26 +824,24 @@ ExtEditor::fillChineseNumber(gint64 num) { clearLookupTable(); - DynamicSpecialPhrase phrase ("", 0); - if ( LABEL_LIST_NUMBERS == m_mode) { for ( int i = 1; i <= 10; ++i ) m_lookup_table.setLabel ( i - 1, Text (i - 1 + 'a') ); } - std::string result = phrase.simplified_number(num); + std::string result = simplified_number(num); if ( !result.empty() ){ Text text(result); m_lookup_table.appendCandidate(text); } - result = phrase.traditional_number(num); + result = traditional_number(num); if ( !result.empty() ){ Text text(result); m_lookup_table.appendCandidate(text); } - result = phrase.simplest_cn_number(num); + result = simplest_cn_number(num); if ( !result.empty() ){ Text text(result); m_lookup_table.appendCandidate(text); |