Debian Codegraph @

src/Dict.cpp

raw symbols SCIP
1 /*
2 * Open Chinese Convert
3 *
4 * Copyright 2010-2026 Carbo Kuo and contributors
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include <algorithm>
20
21 #include "Dict.hpp"
22
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 {
44 std::vector<const DictEntry*> matchedLengths;
45 long len = static_cast<long>((std::min)(KeyMaxLength(), wordLen));
46 std::string wordTrunc = UTF8Util::TruncateUTF8(word, len);
47 const char* wordTruncPtr = wordTrunc.c_str() + len;
48 for (; len > 0;
49 len -= static_cast<long>(UTF8Util::PrevCharLength(wordTruncPtr))) {
50 wordTrunc.resize(static_cast<size_t>(len));
51 wordTruncPtr = wordTrunc.c_str() + len;
52 const Optional<const DictEntry*>& result = Match(wordTrunc.c_str());
53 if (!result.IsNull()) {
54 matchedLengths.push_back(result.Get());
55 }
56 }
57 return matchedLengths;
58 }