From 3f4ee7e103cf12ed2aae4c088d0449c4a042b688 Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Thu, 23 Jun 2016 15:08:12 +0800 Subject: write pinyin_get_character_offset function --- src/pinyin.cpp | 17 +++++++++++++++++ src/pinyin.h | 14 ++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/pinyin.cpp b/src/pinyin.cpp index 47e0320..e3f1581 100644 --- a/src/pinyin.cpp +++ b/src/pinyin.cpp @@ -2814,6 +2814,23 @@ bool pinyin_get_right_character_offset(pinyin_instance_t * instance, return true; } +bool pinyin_get_character_offset(pinyin_instance_t * instance, + size_t offset, + size_t * plength) { + PhoneticKeyMatrix & matrix = instance->m_matrix; + MatchResults results = instance->m_match_results; + _check_offset(matrix, offset); + + size_t length = 0; + for (size_t i = 0; i < offset; ++i) { + phrase_token_t token = g_array_index(results, phrase_token_t, i); + if (null_token != token) + ++length; + } + + *plength = length; + return true; +} #if 0 bool pinyin_get_raw_full_pinyin(pinyin_instance_t * instance, diff --git a/src/pinyin.h b/src/pinyin.h index b15925a..de1be7c 100644 --- a/src/pinyin.h +++ b/src/pinyin.h @@ -898,6 +898,20 @@ bool pinyin_get_right_character_offset(pinyin_instance_t * instance, size_t offset, size_t * right); +/** + * pinyin_get_character_offset: + * @instance: the pinyin instance. + * @offset: the lookup offset. + * @length: the character offset. + * @returns: whether the get operation is successful. + * + * Get the character offset from the lookup offset. + * + */ +bool pinyin_get_character_offset(pinyin_instance_t * instance, + size_t offset, + size_t * length); + #if 0 /** * pinyin_get_raw_full_pinyin: -- cgit