• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifdef ENABLE_TEXT_ENHANCE
16 #include "include/HyphenTrie.h"
17 
18 namespace skia {
19 namespace textlayout {
insert(const std::string & key,const std::string & value)20 void HyphenTrie::insert(const std::string& key, const std::string& value) {
21     std::shared_ptr<TrieNode> node = root;
22     for (char c : key) {
23         if (node->children.count(c) == 0) {
24             node->children.emplace(c, std::make_shared<TrieNode>());
25         }
26         node = node->children[c];
27     }
28     node->value = value;
29 }
30 
findPartialMatch(const std::string & keyPart)31 std::string HyphenTrie::findPartialMatch(const std::string& keyPart) {
32     std::shared_ptr<TrieNode> node = root;
33     for (char c : keyPart) {
34         if (node->children.find(c) == node->children.end()) {
35             return "";
36         }
37         node = node->children[c];
38     }
39     return collectValues(node);
40 }
41 
collectValues(const std::shared_ptr<TrieNode> & node)42 std::string HyphenTrie::collectValues(const std::shared_ptr<TrieNode>& node) {
43     if (node == nullptr) {
44         return "";
45     }
46     if (!node->value.empty()) {
47         return node->value;
48     }
49     for (const auto& child : node->children) {
50         std::string value = collectValues(child.second);
51         if (!value.empty()) {
52             return value;
53         }
54     }
55     return "";
56 }
57 } // namespace textlayout
58 } // namespace skia
59 #endif // ENABLE_TEXT_ENHANCE
60