From f754d164d05cc38832837ee75337cd9da8247ca0 Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Thu, 1 Jul 2010 11:46:59 +0800 Subject: [PATCH] re-factor chinese number code. --- src/DynamicSpecialPhrase.cc | 98 ++++++++++++++++++++++++++++++++++++++++++++ src/DynamicSpecialPhrase.h | 5 +++ src/ExtEditor.cc | 99 --------------------------------------------- 3 files changed, 103 insertions(+), 99 deletions(-) diff --git a/src/DynamicSpecialPhrase.cc b/src/DynamicSpecialPhrase.cc index dd1a196..896b9fc 100644 --- a/src/DynamicSpecialPhrase.cc +++ b/src/DynamicSpecialPhrase.cc @@ -173,6 +173,104 @@ DynamicSpecialPhrase::minsec_cn (guint i) return std::string (num[i / 10 + 10]) + num[i % 10]; } +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[] ={ + {"兆", 12, true}, + {"亿", 8, true}, + {"万", 4, true}, + {"千", 3, false}, + {"百", 2, false}, + {"十", 1, false}, + {"", 0, true}, +}; + +inline const std::string +DynamicSpecialPhrase::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]) +{ + 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 < sizeof (units)/ sizeof(units[0]); ++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; +} + +inline const std::string +DynamicSpecialPhrase::simplified_number(gint64 num) +{ + return translate_to_longform(num, numbers[1]); +} + +inline const std::string +DynamicSpecialPhrase::traditional_number(gint64 num) +{ + if ( 0 == num ) + return numbers[0][0]; + return translate_to_longform(num, numbers[0]); +} + inline const std::string DynamicSpecialPhrase::variable (const std::string &name) { diff --git a/src/DynamicSpecialPhrase.h b/src/DynamicSpecialPhrase.h index 0046c04..dbe97fd 100644 --- a/src/DynamicSpecialPhrase.h +++ b/src/DynamicSpecialPhrase.h @@ -46,6 +46,11 @@ public: const std::string minsec_cn (guint i); const std::string variable (const std::string &name); + /* declaration function about Chinese Number. */ + const std::string simplest_cn_number(gint64 num); + const std::string simplified_number(gint64 num); + const std::string traditional_number(gint64 num); + private: std::string m_text; std::tm m_time; diff --git a/src/ExtEditor.cc b/src/ExtEditor.cc index 547b847..3567772 100644 --- a/src/ExtEditor.cc +++ b/src/ExtEditor.cc @@ -34,11 +34,6 @@ extern "C" { namespace PY { -/* forward declaration function about Chinese Number. */ -static std::string translate_to_simplest(gint64 num); -static std::string translate_to_simplified(gint64 num); -static std::string translate_to_traditional(gint64 num); - /* Write digit/alpha/none Label generator here. * foreach (results): 1, from get_retval; 2..n from get_retvals. @@ -782,100 +777,6 @@ ExtEditor::updateAuxiliaryText (void) Editor::updateAuxiliaryText (aux_text, TRUE); } -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[] ={ - {"兆", 12, true}, - {"亿", 8, true}, - {"万", 4, true}, - {"千", 3, false}, - {"百", 2, false}, - {"十", 1, false}, - {"", 0, true}, -}; - -static std::string translate_to_simplest(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 std::string translate_to_longform(gint64 num, const char * number[10]) -{ - 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 < sizeof (units)/ sizeof(units[0]); ++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 std::string translate_to_simplified(gint64 num) -{ - return translate_to_longform(num, numbers[1]); -} - -static std::string translate_to_traditional(gint64 num) -{ - if ( 0 == num ) - return numbers[0][0]; - return translate_to_longform(num, numbers[0]); -} - }; -- 2.7.4