Fix bug in double pinyin
authorPeng Huang <shawn.p.huang@gmail.com>
Wed, 17 Mar 2010 11:28:52 +0000 (19:28 +0800)
committerPeng Huang <shawn.p.huang@gmail.com>
Wed, 17 Mar 2010 11:28:52 +0000 (19:28 +0800)
src/DoublePinyinEditor.cc
src/DoublePinyinEditor.h
src/PinyinEditor.cc

index ff697d6..a3e717e 100644 (file)
@@ -23,15 +23,19 @@ _id (gint ch)
     }
 }
 
+#define ID_TO_SHENG(id) \
+    double_pinyin_map[Config::doublePinyinSchema ()].sheng[id]
+#define ID_TO_YUNS(id) \
+    double_pinyin_map[Config::doublePinyinSchema ()].yun[id]
+
 inline const Pinyin *
-DoublePinyinEditor::isPinyin (gchar i)
+DoublePinyinEditor::isPinyin (gint i)
 {
     if ((Config::option () & PINYIN_INCOMPLETE_PINYIN) == 0) {
         return NULL;
     }
 
-    gint schema = Config::doublePinyinSchema ();
-    gint sheng = double_pinyin_map[schema].sheng[_id (i)];
+    gint sheng = ID_TO_SHENG (i);
 
     if (sheng == PINYIN_ID_VOID) {
         return NULL;
@@ -41,12 +45,11 @@ DoublePinyinEditor::isPinyin (gchar i)
 }
 
 inline const Pinyin *
-DoublePinyinEditor::isPinyin (gchar i, gchar j)
+DoublePinyinEditor::isPinyin (gint i, gint j)
 {
     const Pinyin *pinyin = NULL;
-    gint schema = Config::doublePinyinSchema ();
-    gint sheng = double_pinyin_map[schema].sheng[_id (i)];
-    const gint *yun = double_pinyin_map[schema].yun[_id (j)];
+    gint sheng = ID_TO_SHENG (i);
+    const gint *yun = ID_TO_YUNS (j);
 
     if (sheng == PINYIN_ID_VOID || yun[0] == PINYIN_ID_VOID)
         return pinyin;
@@ -67,15 +70,21 @@ gboolean
 DoublePinyinEditor::insert (gint ch)
 {
     const Pinyin *pinyin;
+    gint id;
     /* is full */
     if (G_UNLIKELY (m_text.length () >= MAX_PINYIN_LEN))
         return FALSE;
 
-    if (_id (ch) < 0) {
+    id = _id (ch);
+    if (id == -1) {
         /* it is not availidate ch */
         return FALSE;
     }
 
+    if (G_UNLIKELY (m_text.isEmpty () && ID_TO_SHENG(id) == PINYIN_ID_VOID)) {
+        return FALSE;
+    }
+
     m_text.insert (m_cursor++, ch);
 
     if (m_cursor > m_pinyin_len + 2) {
@@ -86,7 +95,7 @@ DoublePinyinEditor::insert (gint ch)
 
     do {
         if (m_cursor == m_pinyin_len + 2) {
-            if ((pinyin = isPinyin (m_text[m_cursor - 2], ch)) == NULL) {
+            if ((pinyin = isPinyin (_id (m_text[m_cursor - 2]), id)) == NULL) {
                 updatePreeditText ();
                 updateAuxiliaryText ();
                 return TRUE;
@@ -100,7 +109,7 @@ DoublePinyinEditor::insert (gint ch)
                 pinyin = m_pinyin.back ();
                 if (pinyin->flags & PINYIN_INCOMPLETE_PINYIN) {
                     /* prev pinyin is incomplete */
-                    if ((pinyin = isPinyin (m_text[m_cursor - 2], ch)) != NULL) {
+                    if ((pinyin = isPinyin (_id (m_text[m_cursor - 2]), id)) != NULL) {
                         m_pinyin.pop ();
                         m_pinyin.append (pinyin, m_pinyin_len - 1, 2);
                         m_pinyin_len = m_cursor;
@@ -108,7 +117,7 @@ DoublePinyinEditor::insert (gint ch)
                     }
                 }
             }
-            if ((pinyin = isPinyin (ch)) == NULL) {
+            if ((pinyin = isPinyin (id)) == NULL) {
                 updatePreeditText ();
                 updateAuxiliaryText ();
                 return TRUE;
@@ -145,7 +154,7 @@ DoublePinyinEditor::removeCharBefore (void)
         m_pinyin_len = m_cursor;
     }
     else {
-        if ((pinyin = isPinyin (m_text[m_cursor - 1])) != NULL) {
+        if ((pinyin = isPinyin (_id (m_text[m_cursor - 1]))) != NULL) {
             m_pinyin.append (pinyin, m_cursor - 1, 1);
             m_pinyin_len = m_cursor;
         }
@@ -236,7 +245,7 @@ DoublePinyinEditor::moveCursorLeft (void)
         m_pinyin_len -= 1;
     }
     else {
-        if ((pinyin = isPinyin (m_text[m_cursor - 1])) != NULL) {
+        if ((pinyin = isPinyin (_id (m_text[m_cursor - 1]))) != NULL) {
             m_pinyin.append (pinyin, m_cursor - 1, 1);
             m_pinyin_len = m_cursor;
         }
@@ -266,7 +275,8 @@ DoublePinyinEditor::moveCursorRight (void)
     do {
         const Pinyin *pinyin = NULL;
         if (m_cursor == m_pinyin_len + 2) {
-            if ((pinyin = isPinyin (m_text[m_cursor - 2], m_text[m_cursor - 1])) == NULL) {
+            if ((pinyin = isPinyin (_id (m_text[m_cursor - 2]),
+                                    _id (m_text[m_cursor - 1]))) == NULL) {
                 updatePreeditText ();
                 updateAuxiliaryText ();
                 return TRUE;
@@ -280,7 +290,8 @@ DoublePinyinEditor::moveCursorRight (void)
                 pinyin = m_pinyin.back ();
                 if (pinyin->flags & PINYIN_INCOMPLETE_PINYIN) {
                     /* prev pinyin is incomplete */
-                    if ((pinyin = isPinyin (m_text[m_cursor - 2], m_text[m_cursor -1])) != NULL) {
+                    if ((pinyin = isPinyin (_id (m_text[m_cursor - 2]),
+                                            _id (m_text[m_cursor -1]))) != NULL) {
                         m_pinyin.pop ();
                         m_pinyin.append (pinyin, m_pinyin_len - 1, 2);
                         m_pinyin_len = m_cursor;
@@ -288,7 +299,7 @@ DoublePinyinEditor::moveCursorRight (void)
                     }
                 }
             }
-            if ((pinyin = isPinyin (m_text[m_cursor - 1])) == NULL) {
+            if ((pinyin = isPinyin (_id (m_text[m_cursor - 1]))) == NULL) {
                 updatePreeditText ();
                 updateAuxiliaryText ();
                 return TRUE;
@@ -416,12 +427,12 @@ DoublePinyinEditor::updatePinyin (void)
         const Pinyin *pinyin = NULL;
 
         if (m_pinyin_len == m_cursor - 1) {
-            pinyin = isPinyin (m_text[m_pinyin_len]);
+            pinyin = isPinyin (_id (m_text[m_pinyin_len]));
         }
         else {
             pinyin = isPinyin (m_text[m_pinyin_len], m_text[m_pinyin_len + 1]);
             if (pinyin == NULL)
-                pinyin =  isPinyin (m_text[m_pinyin_len]);
+                pinyin =  isPinyin ( _id (m_text[m_pinyin_len]));
         }
         if (pinyin == NULL)
             break;
index cd7ab74..b577960 100644 (file)
@@ -30,8 +30,8 @@ public:
 
 private:
     void updatePinyin (void);
-    const Pinyin *isPinyin (gchar i, gchar j);
-    const Pinyin *isPinyin (gchar i);
+    const Pinyin *isPinyin (gint i, gint j);
+    const Pinyin *isPinyin (gint i);
 
 };
 
index 6cf387d..81413cc 100644 (file)
@@ -39,8 +39,7 @@ PinyinEditor::processPinyin (guint keyval, guint keycode, guint modifiers)
     if (G_UNLIKELY (CMSHM_FILTER(modifiers) != 0))
         return m_text ? TRUE : FALSE;
 
-    insert (keyval);
-    return TRUE;
+    return insert (keyval);
 }
 
 /**
@@ -103,8 +102,7 @@ PinyinEditor::processPunct (guint keyval, guint keycode, guint modifiers)
 
     switch (keyval) {
     case IBUS_apostrophe:
-        insert (keyval);
-        return TRUE;
+        return insert (keyval);
     case IBUS_comma:
         if (Config::commaPeriodPage ()) {
             pageUp ();