summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2017-12-28 11:38:59 +0800
committerPeng Wu <alexepico@gmail.com>2017-12-28 11:48:13 +0800
commit9a44d8280120a80d53a240cc63347e0a3dbadbae (patch)
treebbc0fe0f80243a343d7b7a8a23c3c4f630b6305a
parent3eb8b2d5b4afec1da2e2525fe0c383c2e7f07a50 (diff)
downloadlibpinyin-9a44d8280120a80d53a240cc63347e0a3dbadbae.zip
libpinyin-9a44d8280120a80d53a240cc63347e0a3dbadbae.tar.gz
libpinyin-9a44d8280120a80d53a240cc63347e0a3dbadbae.tar.xz
write is_valid_zhuyin method
-rw-r--r--src/storage/chewing_key.cpp9
-rw-r--r--src/storage/chewing_key.h1
-rw-r--r--src/storage/zhuyin_parser2.cpp19
3 files changed, 29 insertions, 0 deletions
diff --git a/src/storage/chewing_key.cpp b/src/storage/chewing_key.cpp
index 953b369..102cce5 100644
--- a/src/storage/chewing_key.cpp
+++ b/src/storage/chewing_key.cpp
@@ -35,6 +35,15 @@ gint _ChewingKey::get_table_index() {
return index == -1 ? 0 : index;
}
+bool _ChewingKey::is_valid_zhuyin() {
+ assert(m_initial < CHEWING_NUMBER_OF_INITIALS);
+ assert(m_middle < CHEWING_NUMBER_OF_MIDDLES);
+ assert(m_final < CHEWING_NUMBER_OF_FINALS);
+ assert(m_tone < CHEWING_NUMBER_OF_TONES);
+
+ return valid_zhuyin_table[((m_initial * CHEWING_NUMBER_OF_MIDDLES + m_middle) * CHEWING_NUMBER_OF_FINALS + m_final) * CHEWING_NUMBER_OF_TONES + m_tone];
+}
+
gchar * _ChewingKey::get_pinyin_string() {
assert(m_tone < CHEWING_NUMBER_OF_TONES);
gint index = get_table_index();
diff --git a/src/storage/chewing_key.h b/src/storage/chewing_key.h
index 7faf144..7ec4dfb 100644
--- a/src/storage/chewing_key.h
+++ b/src/storage/chewing_key.h
@@ -65,6 +65,7 @@ struct _ChewingKey
public:
gint get_table_index();
+ bool is_valid_zhuyin();
/* Note: the return value should be freed by g_free. */
gchar * get_pinyin_string();
diff --git a/src/storage/zhuyin_parser2.cpp b/src/storage/zhuyin_parser2.cpp
index 9ae7fbd..99ae4d2 100644
--- a/src/storage/zhuyin_parser2.cpp
+++ b/src/storage/zhuyin_parser2.cpp
@@ -250,6 +250,9 @@ int ZhuyinSimpleParser2::parse(pinyin_option_t options,
if (0 == i) /* no more possible chewings. */
break;
+ if (!key.is_valid_zhuyin())
+ break;
+
key_rest.m_raw_begin = parsed_len; key_rest.m_raw_end = parsed_len + i;
parsed_len += i;
@@ -437,6 +440,9 @@ int ZhuyinDiscreteParser2::parse(pinyin_option_t options,
if (0 == i) /* no more possible chewings. */
break;
+ if (!key.is_valid_zhuyin())
+ break;
+
key_rest.m_raw_begin = parsed_len; key_rest.m_raw_end = parsed_len + i;
parsed_len += i;
@@ -741,6 +747,9 @@ int ZhuyinDaChenCP26Parser2::parse(pinyin_option_t options,
if (0 == i) /* no more possible chewings. */
break;
+ if (!key.is_valid_zhuyin())
+ break;
+
key_rest.m_raw_begin = parsed_len; key_rest.m_raw_end = parsed_len + i;
parsed_len += i;
@@ -762,6 +771,9 @@ int ZhuyinDaChenCP26Parser2::parse(pinyin_option_t options,
break;
}
+ if (!key.is_valid_zhuyin())
+ return parsed_len;
+
if (i > 0) { /* found one */
key_rest.m_raw_begin = parsed_len; key_rest.m_raw_end = parsed_len + i;
parsed_len += i;
@@ -911,6 +923,13 @@ int ZhuyinDirectParser2::parse(pinyin_option_t options,
next = i;
if (parse_one_key(options, key, str + cur, next - cur)) {
+#if 0
+ /* as direct parser handles data source,
+ assume the data is correct when loading. */
+ if (!key.is_valid_zhuyin())
+ return parsed_len;
+#endif
+
key_rest.m_raw_begin = cur; key_rest.m_raw_end = next;
/* save the pinyin. */