Flutter Engine
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 
5 #include "flutter/fml/ascii_trie.h"
6 
7 #include "flutter/fml/logging.h"
8 
9 namespace fml {
12 
13 namespace {
14 void 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  Add(&(*trie)->children[ch], entry + 1);
22  }
23 }
24 
25 TrieNodePtr MakeTrie(const std::vector<std::string>& entries) {
26  TrieNodePtr result;
27  for (const std::string& entry : entries) {
28  Add(&result, entry.c_str());
29  }
30  return result;
31 }
32 } // namespace
33 
34 void AsciiTrie::Fill(const std::vector<std::string>& entries) {
35  node_ = MakeTrie(entries);
36 }
37 
38 bool AsciiTrie::Query(TrieNode* trie, const char* query) {
39  FML_DCHECK(trie);
40  const char* char_position = query;
41  TrieNode* trie_position = trie;
42  TrieNode* child = nullptr;
43  int ch;
44  while ((ch = *char_position) && (child = trie_position->children[ch].get())) {
45  char_position++;
46  trie_position = child;
47  }
48  return !child && trie_position != trie;
49 }
50 } // namespace fml
#define FML_DCHECK(condition)
Definition: logging.h:86
TrieNodePtr children[kMaxAsciiValue]
Definition: ascii_trie.h:31
Definition: ascii_trie.cc:9
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
AsciiTrie::TrieNode TrieNode
Definition: ascii_trie.cc:10
void Fill(const std::vector< std::string > &entries)
Clear and insert all the entries into the trie.
Definition: ascii_trie.cc:34
std::unique_ptr< TrieNode > TrieNodePtr
Definition: ascii_trie.h:17
AsciiTrie::TrieNodePtr TrieNodePtr
Definition: ascii_trie.cc:11