summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2011-11-16 14:45:18 +0800
committerPeng Wu <alexepico@gmail.com>2011-11-16 14:48:10 +0800
commit9fa01b9621b7cdc102f7efda35fac5260cbf622d (patch)
tree23d4e91971e282e7b0de337a4b49dc520a487466
parent5af041d0ecb0eeff49be8c35a2223e8458a93120 (diff)
downloadlibpinyin-9fa01b9621b7cdc102f7efda35fac5260cbf622d.tar.gz
libpinyin-9fa01b9621b7cdc102f7efda35fac5260cbf622d.tar.xz
libpinyin-9fa01b9621b7cdc102f7efda35fac5260cbf622d.zip
update pinyin parsers interface
-rw-r--r--src/storage/pinyin_parser2.cpp17
-rw-r--r--src/storage/pinyin_parser2.h10
2 files changed, 14 insertions, 13 deletions
diff --git a/src/storage/pinyin_parser2.cpp b/src/storage/pinyin_parser2.cpp
index 6ba15bd..1663ad2 100644
--- a/src/storage/pinyin_parser2.cpp
+++ b/src/storage/pinyin_parser2.cpp
@@ -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;
}
diff --git a/src/storage/pinyin_parser2.h b/src/storage/pinyin_parser2.h
index 1ac47c0..2065119 100644
--- a/src/storage/pinyin_parser2.h
+++ b/src/storage/pinyin_parser2.h
@@ -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;