Skip to content

Commit cce6120

Browse files
committed
🔥 Speed up compilation times by not using std::regex
Problem: - std::regex include takes multiple seconds to compile. Solution: - Implement utility::regex_match instead.
1 parent ef80412 commit cce6120

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

include/boost/ut.hpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ export import std;
7575
#include <iostream>
7676
#include <memory>
7777
#include <optional>
78-
#include <regex>
7978
#include <sstream>
8079
#include <stack>
8180
#include <string_view>
@@ -231,6 +230,18 @@ template <class T = std::string_view, class TDelim>
231230
}
232231
return output;
233232
}
233+
constexpr auto regex_match(const char *str, const char *pattern) -> bool {
234+
if (*pattern == '\0' && *str == '\0') return true;
235+
if (*pattern == '\0' && *str != '\0') return false;
236+
if (*str == '\0' && *pattern != '\0') return false;
237+
if (*pattern == '.') {
238+
return regex_match(str+1, pattern+1);
239+
}
240+
if (*pattern == *str) {
241+
return regex_match(str+1, pattern+1);
242+
}
243+
return false;
244+
}
234245
} // namespace utility
235246

236247
namespace reflection {
@@ -2031,10 +2042,10 @@ class runner {
20312042
}
20322043

20332044
if (!detail::cfg::query_pattern.empty()) {
2034-
const static std::regex regex(detail::cfg::query_regex_pattern);
2035-
bool matches = std::regex_match(test.name.data(), regex);
2045+
const static auto regex = detail::cfg::query_regex_pattern;
2046+
bool matches = utility::regex_match(test.name.data(), regex.c_str());
20362047
for (const auto& tag2 : test.tag) {
2037-
matches |= std::regex_match(tag2.data(), regex);
2048+
matches |= utility::regex_match(tag2.data(), regex.c_str());
20382049
}
20392050
if (matches) {
20402051
execute = !detail::cfg::invert_query_pattern;

test/ut/ut.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,21 @@ int main() {
435435
static_assert("fake_cfg" == reflection::type_name<fake_cfg>());
436436
#endif
437437
}
438-
438+
439+
{
440+
static_assert(utility::regex_match("", ""));
441+
static_assert(utility::regex_match("hello", "hello"));
442+
static_assert(utility::regex_match("hello", "h.llo"));
443+
static_assert(utility::regex_match("hello", "he..o"));
444+
static_assert(not utility::regex_match("hello", "hella"));
445+
static_assert(not utility::regex_match("hello", "helao"));
446+
static_assert(not utility::regex_match("hello", "hlllo"));
447+
static_assert(not utility::regex_match("hello", ""));
448+
static_assert(not utility::regex_match("", "hello"));
449+
static_assert(not utility::regex_match("hi", "hello"));
450+
static_assert(not utility::regex_match("hello there", "hello"));
451+
}
452+
439453
{
440454
test_assert(utility::is_match("", ""));
441455
test_assert(utility::is_match("", "*"));

0 commit comments

Comments
 (0)