update pinyin parsers interface
authorPeng Wu <alexepico@gmail.com>
Wed, 16 Nov 2011 06:45:18 +0000 (14:45 +0800)
committerPeng Wu <alexepico@gmail.com>
Wed, 16 Nov 2011 06:48:10 +0000 (14:48 +0800)
src/storage/pinyin_parser2.cpp
src/storage/pinyin_parser2.h

index 6ba15bd..1663ad2 100644 (file)
@@ -119,9 +119,9 @@ static bool compare_less_than(const pinyin_index_item_t & lhs,
     return 0 > strcmp(lhs.m_pinyin_input, rhs.m_pinyin_input);
 }
 
-int FullPinyinParser2::parse_one_key (guint32 options, ChewingKey & key,
-                                      ChewingKeyRest & key_rest,
-                                      const char * pinyin, int len) const {
+bool FullPinyinParser2::parse_one_key (guint32 options, ChewingKey & key,
+                                       ChewingKeyRest & key_rest,
+                                       const char * pinyin, int len) const {
     /* "'" are not accepted in parse_one_key. */
     assert(NULL == strchr(pinyin, '\''));
     gchar * input = g_strndup(pinyin, len);
@@ -144,7 +144,8 @@ int FullPinyinParser2::parse_one_key (guint32 options, ChewingKey & key,
     pinyin_index_item_t item;
     memset(&item, 0, sizeof(item));
 
-    for (; parsed_len > 0; --parsed_len) {
+    /* Note: optimize here? */
+    for (; parsed_len >= len - 1; --parsed_len) {
         input[parsed_len] = '\0';
         item.m_pinyin_input = input;
         std_lite::pair<const pinyin_index_item_t *,
@@ -153,9 +154,9 @@ int FullPinyinParser2::parse_one_key (guint32 options, ChewingKey & key,
             (pinyin_index, pinyin_index + G_N_ELEMENTS(pinyin_index),
              item, compare_less_than);
 
-        guint16 len = range.second - range.first;
-        assert (len <= 1);
-        if ( len == 1 ) {
+        guint16 range_len = range.second - range.first;
+        assert (range_len <= 1);
+        if ( range_len == 1 ) {
             const pinyin_index_item_t * index = range.first;
 
             if (!check_pinyin_options(options, index))
@@ -179,7 +180,7 @@ int FullPinyinParser2::parse_one_key (guint32 options, ChewingKey & key,
 
     key_rest.m_raw_begin = 0; key_rest.m_raw_end = parsed_len;
     g_free(input);
-    return parsed_len;
+    return parsed_len == len;
 }
 
 
index 1ac47c0..2065119 100644 (file)
@@ -87,9 +87,9 @@ public:
      * @param str snput string in UTF-8 encoding, in most case this string is just a plain ASCII string.
      * @param len the length of str, in number of chars rather than bytes.
      *
-     * @return the number of chars were actually used.
+     * @return whether the entire string is parsed as one key.
      */
-    virtual int parse_one_key (guint32 options, ChewingKey & key, ChewingKeyRest & key_rest, const char *str, int len) const = 0;
+    virtual bool parse_one_key (guint32 options, ChewingKey & key, ChewingKeyRest & key_rest, const char *str, int len) const = 0;
 
     /**
      * @brief Translate the source string into a set of ChewingKeys.
@@ -124,7 +124,7 @@ public:
         g_array_free(m_parse_steps, TRUE);
     }
 
-    virtual int parse_one_key (guint32 options, ChewingKey & key, ChewingKeyRest & key_rest, const char *str, int len) const;
+    virtual bool parse_one_key (guint32 options, ChewingKey & key, ChewingKeyRest & key_rest, const char *str, int len) const;
 
     virtual int parse (guint32 options, ChewingKeyVector & keys, ChewingKeyRestVector & key_rests, const char *str, int len) const;
 };
@@ -139,7 +139,7 @@ class DoublePinyinParser2 : public PinyinParser2
 public:
     virtual ~DoublePinyinParser2 () {}
 
-    virtual int parse_one_key (guint32 options, ChewingKey & key, ChewingKeyRest & key_rest, const char *str, int len) const;
+    virtual bool parse_one_key (guint32 options, ChewingKey & key, ChewingKeyRest & key_rest, const char *str, int len) const;
 
     virtual int parse (guint32 options, ChewingKeyVector & keys, ChewingKeyRestVector & key_rests, const char *str, int len) const;
 
@@ -169,7 +169,7 @@ class ChewingParser2 : public PinyinParser2
 public:
     virtual ~ChewingParser2 () {}
 
-    virtual int parse_one_key (guint32 options, ChewingKey & key, ChewingKeyRest & key_rest, const char *str, int len) const;
+    virtual bool parse_one_key (guint32 options, ChewingKey & key, ChewingKeyRest & key_rest, const char *str, int len) const;
 
     virtual int parse (guint32 options, ChewingKeyVector & keys, ChewingKeyRestVector & key_rests, const char *str, int len) const;