Skip to content

Commit 63bae2b

Browse files
committed
Match regular expressions to string_view
1 parent e9ee31c commit 63bae2b

File tree

4 files changed

+81
-11
lines changed

4 files changed

+81
-11
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Support matching regular expressions to std::string view
2+
13
* Added adapter for QA Systems Cantata. Thank you Andreas Schätti
24

35
v44 2023-04-10

include/trompeloeil.hpp

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,28 +2555,50 @@ template <typename T>
25552555
class string_helper // a vastly simplified string_view type of class
25562556
{
25572557
public:
2558+
template <
2559+
typename S,
2560+
typename = decltype(std::declval<const char*&>() = std::declval<S>().data()),
2561+
typename = decltype(std::declval<S>().length())
2562+
>
25582563
string_helper(
2559-
std::string const& s)
2564+
const S& s)
25602565
noexcept
2561-
: str(s.c_str())
2566+
: begin_(s.data())
2567+
, end_(begin_ + s.length())
25622568
{}
25632569

25642570
constexpr
25652571
string_helper(
25662572
char const* s)
25672573
noexcept
2568-
: str(s)
2569-
{}
2574+
: begin_(s)
2575+
, end_(s ? begin_ + strlen(s) : nullptr)
2576+
{
2577+
}
25702578

2571-
char const*
2572-
c_str()
2579+
constexpr
2580+
explicit
2581+
operator bool() const
2582+
{
2583+
return begin_;
2584+
}
2585+
constexpr
2586+
char const *
2587+
begin()
2588+
const
2589+
{
2590+
return begin_;
2591+
}
2592+
constexpr
2593+
char const *
2594+
end()
25732595
const
2574-
noexcept
25752596
{
2576-
return str;
2597+
return end_;
25772598
}
25782599
private:
2579-
char const* str;
2600+
char const* begin_;
2601+
char const* end_;
25802602
};
25812603

25822604
regex_check(
@@ -2593,8 +2615,7 @@ template <typename T>
25932615
T const&)
25942616
const
25952617
{
2596-
return str.c_str()
2597-
&& std::regex_search(str.c_str(), re, match_type);
2618+
return str && std::regex_search(str.begin(), str.end(), re, match_type);
25982619
}
25992620

26002621
private:

test/compiling_tests.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,9 @@ class mock_str
401401
MAKE_MOCK1(str, void(std::string));
402402
MAKE_MOCK1(overload, void(char const*));
403403
MAKE_MOCK1(overload, void(std::string const&));
404+
#if defined(__cpp_lib_string_view)
405+
MAKE_MOCK1(string_view, void(std::string_view));
406+
#endif
404407
};
405408

406409
class C_ptr

test/compiling_tests_11.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,6 +2918,50 @@ TEST_CASE_METHOD(
29182918

29192919
// tests of parameter matching using typed matcher re
29202920

2921+
#if TROMPELOEIL_TEST_REGEX_FAILURES && defined(__cpp_lib_string_view)
2922+
TEST_CASE_METHOD(
2923+
Fixture,
2924+
"C++11: call to string_view function matching regex is not reported",
2925+
"[C++11][C++14][matching][matchers][re]")
2926+
{
2927+
{
2928+
mock_str obj;
2929+
REQUIRE_CALL_V(obj, string_view(trompeloeil::re("mid")));
2930+
char str[] = "pre mid post";
2931+
obj.string_view(str);
2932+
}
2933+
REQUIRE(reports.empty());
2934+
}
2935+
#endif
2936+
#if TROMPELOEIL_TEST_REGEX_FAILURES && defined(__cpp_lib_string_view)
2937+
TEST_CASE_METHOD(
2938+
Fixture,
2939+
"C++11: call to string_view function with non-matching string to regex is reported",
2940+
"[C++11][C++14][matching][matchers][re]")
2941+
{
2942+
mock_str obj;
2943+
REQUIRE_CALL_V(obj, string_view(trompeloeil::re("mid")));
2944+
try
2945+
{
2946+
char str[] = "abcde";
2947+
obj.string_view(str);
2948+
FAIL("did not throw");
2949+
}
2950+
catch (reported)
2951+
{
2952+
REQUIRE(reports.size() == 1U);
2953+
auto& msg = reports.front().msg;
2954+
INFO("msg=" << msg);
2955+
auto re = R":(No match for call of string_view with signature void\(std::string_view\) with.
2956+
param _1 == abcde
2957+
2958+
Tried obj.string_view\(trompeloeil::re\("mid"\)\) at [A-Za-z0-9_ ./:\]*:[0-9]*.*
2959+
Expected _1 matching regular expression /mid/):";
2960+
REQUIRE(is_match(msg, re));
2961+
}
2962+
}
2963+
#endif
2964+
29212965
#if TROMPELOEIL_TEST_REGEX_FAILURES
29222966

29232967
TEST_CASE_METHOD(

0 commit comments

Comments
 (0)