| 23 | using namespace opencc; |
| 24 | |
| 25 | Optional<const DictEntry*> Dict::MatchPrefix(const char* word, |
| 26 | size_t wordLen) const { |
| 27 | long len = static_cast<long>((std::min)(KeyMaxLength(), wordLen)); |
| 28 | std::string wordTrunc = UTF8Util::TruncateUTF8(word, len); |
| 29 | const char* wordTruncPtr = wordTrunc.c_str() + len; |
| 30 | for (; len > 0;) { |
| 31 | wordTrunc.resize(static_cast<size_t>(len)); |
| 32 | wordTruncPtr = wordTrunc.c_str() + len; |
| 33 | const Optional<const DictEntry*>& result = Match(wordTrunc.c_str()); |
| 34 | if (!result.IsNull()) { |
| 35 | return result; |
| 36 | } |
| 37 | len -= static_cast<long>(UTF8Util::PrevCharLength(wordTruncPtr)); |
| 38 | } |
| 39 | return Optional<const DictEntry*>::Null(); |
| 40 | } |
| 41 | |
| 42 | std::vector<const DictEntry*> Dict::MatchAllPrefixes(const char* word, |
| 43 | size_t wordLen) const { |