diff options
author | Copybara-Service <copybara-worker@google.com> | 2022-02-08 14:51:54 (GMT) |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-02-08 14:51:54 (GMT) |
commit | 504eb989534f26033db3edce41a8b3d883cc8e52 (patch) | |
tree | 161091f9ad39fd37622723fbe1e80f13f598a396 /googletest | |
parent | 43efa0a4efd40c78b9210d15373112081899a97c (diff) | |
parent | d6841c040d63b6e2bc716e365d1c60d26f23b1f5 (diff) | |
download | googletest-504eb989534f26033db3edce41a8b3d883cc8e52.zip googletest-504eb989534f26033db3edce41a8b3d883cc8e52.tar.gz googletest-504eb989534f26033db3edce41a8b3d883cc8e52.tar.bz2 |
Merge pull request #3746 from IYP-Programer-Yeah:use-constant-time-lookup-for-exact-match
PiperOrigin-RevId: 427179775
Change-Id: I9928be2421d559acf0e0f03643ce0b856b63f737
Diffstat (limited to 'googletest')
-rw-r--r-- | googletest/src/gtest.cc | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 46c3e7f..4775fbc 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -50,6 +50,7 @@ #include <map> #include <ostream> // NOLINT #include <sstream> +#include <unordered_set> #include <vector> #include "gtest/gtest-assertion-result.h" @@ -727,6 +728,11 @@ static bool PatternMatchesString(const std::string& name_str, namespace { +bool IsGlobPattern(const std::string& pattern) { + return std::any_of(pattern.begin(), pattern.end(), + [](const char c) { return c == '?' || c == '*'; }); +} + class UnitTestFilter { public: UnitTestFilter() = default; @@ -734,13 +740,24 @@ class UnitTestFilter { // Constructs a filter from a string of patterns separated by `:`. explicit UnitTestFilter(const std::string& filter) { // By design "" filter matches "" string. - SplitString(filter, ':', &patterns_); + std::vector<std::string> all_patterns; + SplitString(filter, ':', &all_patterns); + const auto exact_match_patterns_begin = std::partition( + all_patterns.begin(), all_patterns.end(), &IsGlobPattern); + + glob_patterns_.reserve(exact_match_patterns_begin - all_patterns.begin()); + std::move(all_patterns.begin(), exact_match_patterns_begin, + std::inserter(glob_patterns_, glob_patterns_.begin())); + std::move( + exact_match_patterns_begin, all_patterns.end(), + std::inserter(exact_match_patterns_, exact_match_patterns_.begin())); } // Returns true if and only if name matches at least one of the patterns in // the filter. bool MatchesName(const std::string& name) const { - return std::any_of(patterns_.begin(), patterns_.end(), + return exact_match_patterns_.count(name) > 0 || + std::any_of(glob_patterns_.begin(), glob_patterns_.end(), [&name](const std::string& pattern) { return PatternMatchesString( name, pattern.c_str(), @@ -749,7 +766,8 @@ class UnitTestFilter { } private: - std::vector<std::string> patterns_; + std::vector<std::string> glob_patterns_; + std::unordered_set<std::string> exact_match_patterns_; }; class PositiveAndNegativeUnitTestFilter { |