Flutter Engine
 
Loading...
Searching...
No Matches
ascii_trie.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
8
9namespace fml {
12
13namespace {
14void Add(TrieNodePtr* trie, const char* entry) {
15 int ch = entry[0];
17 if (ch != 0) {
18 if (!*trie) {
19 *trie = std::make_unique<TrieNode>();
20 }
21 // NOLINTNEXTLINE(clang-analyzer-security.ArrayBound)
22 Add(&(*trie)->children[ch], entry + 1);
23 }
24}
25
26TrieNodePtr MakeTrie(const std::vector<std::string>& entries) {
27 TrieNodePtr result;
28 for (const std::string& entry : entries) {
29 Add(&result, entry.c_str());
30 }
31 return result;
32}
33} // namespace
34
35void AsciiTrie::Fill(const std::vector<std::string>& entries) {
36 node_ = MakeTrie(entries);
37}
38
39bool AsciiTrie::Query(TrieNode* trie, const char* query) {
40 FML_DCHECK(trie);
41 const char* char_position = query;
42 TrieNode* trie_position = trie;
43 TrieNode* child = nullptr;
44 int ch;
45 while ((ch = *char_position) && (child = trie_position->children[ch].get())) {
46 char_position++;
47 trie_position = child;
48 }
49 return !child && trie_position != trie;
50}
51} // namespace fml
std::unique_ptr< TrieNode > TrieNodePtr
Definition ascii_trie.h:18
static const int kMaxAsciiValue
The max Ascii value.
Definition ascii_trie.h:20
bool Query(const char *argument)
Returns true if argument is prefixed by the contents of the trie.
Definition ascii_trie.h:26
void Fill(const std::vector< std::string > &entries)
Clear and insert all the entries into the trie.
Definition ascii_trie.cc:35
#define FML_DCHECK(condition)
Definition logging.h:122
AsciiTrie::TrieNodePtr TrieNodePtr
Definition ascii_trie.cc:11
AsciiTrie::TrieNode TrieNode
Definition ascii_trie.cc:10
TrieNodePtr children[kMaxAsciiValue]
Definition ascii_trie.h:31