• 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 OHOS_SUPPORT
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 {
22     std::shared_ptr<TrieNode> node = root;
23     for (char c : key) {
24         if (node->children.count(c) == 0) {
25             node->children.emplace(c, std::make_shared<TrieNode>());
26         }
27         node = node->children[c];
28     }
29     node->value = value;
30 }
31 
findPartialMatch(const std::string & keyPart)32 std::string HyphenTrie::findPartialMatch(const std::string& keyPart)
33 {
34     std::shared_ptr<TrieNode> node = root;
35     for (char c : keyPart) {
36         if (node->children.find(c) == node->children.end()) {
37             return "";
38         }
39         node = node->children[c];
40     }
41     return collectValues(node);
42 }
43 
collectValues(const std::shared_ptr<TrieNode> & node)44 std::string HyphenTrie::collectValues(const std::shared_ptr<TrieNode>& node)
45 {
46     if (node == nullptr) {
47         return "";
48     }
49     if (!node->value.empty()) {
50         return node->value;
51     }
52     for (const auto& child : node->children) {
53         std::string value = collectValues(child.second);
54         if (!value.empty()) {
55             return value;
56         }
57     }
58     return "";
59 }
60 } // namespace textlayout
61 } // namespace skia
62 #endif
63