forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made it so you can whitelist what events you want to listen to (flutt…
- Loading branch information
Showing
11 changed files
with
279 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
#include "flutter/fml/ascii_trie.h" | ||
#include "flutter/fml/logging.h" | ||
|
||
namespace fml { | ||
typedef AsciiTrie::TrieNode TrieNode; | ||
typedef AsciiTrie::TrieNodePtr TrieNodePtr; | ||
|
||
namespace { | ||
void Add(TrieNodePtr* trie, const char* entry) { | ||
int ch = entry[0]; | ||
FML_DCHECK(ch < AsciiTrie::kMaxAsciiValue); | ||
if (ch != 0) { | ||
if (!*trie) { | ||
*trie = std::make_unique<TrieNode>(); | ||
} | ||
Add(&(*trie)->children[ch], entry + 1); | ||
} | ||
} | ||
|
||
TrieNodePtr MakeTrie(const std::vector<std::string>& entries) { | ||
TrieNodePtr result; | ||
for (const std::string& entry : entries) { | ||
Add(&result, entry.c_str()); | ||
} | ||
return result; | ||
} | ||
} // namespace | ||
|
||
void AsciiTrie::Fill(const std::vector<std::string>& entries) { | ||
node_ = MakeTrie(entries); | ||
} | ||
|
||
bool AsciiTrie::Query(TrieNode* trie, const char* query) { | ||
FML_DCHECK(trie); | ||
const char* char_position = query; | ||
TrieNode* trie_position = trie; | ||
TrieNode* child; | ||
int ch; | ||
while ((ch = *char_position) && (child = trie_position->children[ch].get())) { | ||
char_position++; | ||
trie_position = child; | ||
} | ||
return !child && trie_position != trie; | ||
} | ||
} // namespace fml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_FML_ASCIITRIE_H_ | ||
#define FLUTTER_FML_ASCIITRIE_H_ | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
namespace fml { | ||
|
||
/// A trie for looking for ASCII prefixes. | ||
class AsciiTrie { | ||
public: | ||
struct TrieNode; | ||
typedef std::unique_ptr<TrieNode> TrieNodePtr; | ||
/// The max Ascii value. | ||
static const int kMaxAsciiValue = 128; | ||
|
||
/// Clear and insert all the entries into the trie. | ||
void Fill(const std::vector<std::string>& entries); | ||
|
||
/// Returns true if \p argument is prefixed by the contents of the trie. | ||
inline bool Query(const char* argument) { | ||
return !node_ || Query(node_.get(), argument); | ||
} | ||
|
||
struct TrieNode { | ||
TrieNodePtr children[kMaxAsciiValue]; | ||
}; | ||
|
||
private: | ||
static bool Query(TrieNode* trie, const char* query); | ||
TrieNodePtr node_; | ||
}; | ||
} // namespace fml | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "flutter/fml/ascii_trie.h" | ||
#include "gtest/gtest.h" | ||
|
||
using fml::AsciiTrie; | ||
|
||
TEST(AsciiTableTest, Simple) { | ||
AsciiTrie trie; | ||
auto entries = std::vector<std::string>{"foo"}; | ||
trie.Fill(entries); | ||
ASSERT_TRUE(trie.Query("foobar")); | ||
ASSERT_FALSE(trie.Query("google")); | ||
} | ||
|
||
TEST(AsciiTableTest, ExactMatch) { | ||
AsciiTrie trie; | ||
auto entries = std::vector<std::string>{"foo"}; | ||
trie.Fill(entries); | ||
ASSERT_TRUE(trie.Query("foo")); | ||
} | ||
|
||
TEST(AsciiTableTest, Empty) { | ||
AsciiTrie trie; | ||
ASSERT_TRUE(trie.Query("foo")); | ||
} | ||
|
||
TEST(AsciiTableTest, MultipleEntries) { | ||
AsciiTrie trie; | ||
auto entries = std::vector<std::string>{"foo", "bar"}; | ||
trie.Fill(entries); | ||
ASSERT_TRUE(trie.Query("foozzz")); | ||
ASSERT_TRUE(trie.Query("barzzz")); | ||
} |
Oops, something went wrong.