Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ namespace rtps {
using reader_map_helper = utilities::collections::map_size_helper<GUID_t, SubscriptionMatchedStatus>;
using writer_map_helper = utilities::collections::map_size_helper<GUID_t, PublicationMatchedStatus>;

static bool is_partition_empty(
const fastdds::dds::Partition_t& partition)
{
return partition.size() <= 1 && 0 == strlen(partition.name());
}

EDP::EDP(
PDP* p,
RTPSParticipantImpl* part)
Expand Down Expand Up @@ -816,7 +810,7 @@ bool EDP::valid_matching(
for (auto rnameit = rdata->m_qos.m_partition.begin();
rnameit != rdata->m_qos.m_partition.end(); ++rnameit)
{
if (is_partition_empty(*rnameit))
if (StringMatching::matchString("", rnameit->name()))
{
matched = true;
break;
Expand All @@ -828,7 +822,7 @@ bool EDP::valid_matching(
for (auto wnameit = wdata->m_qos.m_partition.begin();
wnameit != wdata->m_qos.m_partition.end(); ++wnameit)
{
if (is_partition_empty(*wnameit))
if (StringMatching::matchString(wnameit->name(), ""))
{
matched = true;
break;
Expand Down
82 changes: 24 additions & 58 deletions src/cpp/utils/StringMatching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <algorithm>
#include <regex>
#elif defined(_WIN32)
#include <cstring>
#include "Shlwapi.h"
#else
#include <fnmatch.h>
Expand All @@ -46,7 +47,7 @@ StringMatching::~StringMatching()
}

#if defined(__cplusplus_winrt)
void replace_all(
static void replace_all(
std::string& subject,
const std::string& search,
const std::string& replace)
Expand All @@ -59,7 +60,7 @@ void replace_all(
}
}

bool StringMatching::matchPattern(
static bool do_match_pattern(
const char* pattern,
const char* str)
{
Expand All @@ -71,87 +72,52 @@ bool StringMatching::matchPattern(

std::regex path_regex(path);
std::smatch spec_match;
if (std::regex_match(spec, spec_match, path_regex))
{
return true;
}

return false;
return std::regex_match(spec, spec_match, path_regex);
}

bool StringMatching::matchString(
const char* str1,
const char* str2)
#elif defined(_WIN32)
static bool do_match_pattern(
const char* pattern,
const char* str)
{
if (StringMatching::matchPattern(str1, str2))
// An empty pattern only matches an empty string
if (strlen(pattern) == 0)
{
return true;
return strlen(str) == 0;
}

if (StringMatching::matchPattern(str2, str1))
// An empty string also matches a pattern of "*"
if (strlen(str) == 0)
{
return true;
return strcmp(pattern, "*") == 0;
}

return false;
// Leave rest of cases to PathMatchSpecA
return PathMatchSpecA(str, pattern);
}

#elif defined(_WIN32)
bool StringMatching::matchPattern(
#else
static bool do_match_pattern(
const char* pattern,
const char* str)
{
if (PathMatchSpecA(str, pattern))
{
return true;
}
return false;
return fnmatch(pattern, str, FNM_NOESCAPE) == 0;
}

bool StringMatching::matchString(
const char* str1,
const char* str2)
{
if (PathMatchSpecA(str1, str2))
{
return true;
}
if (PathMatchSpecA(str2, str1))
{
return true;
}
return false;
}
#endif // if defined(__cplusplus_winrt)

#else
bool StringMatching::matchPattern(
const char* pattern,
const char* str)
{
if (fnmatch(pattern, str, FNM_NOESCAPE) == 0)
{
return true;
}
return false;
return do_match_pattern(pattern, str);
}

bool StringMatching::matchString(
const char* str1,
const char* str2)
{
if (fnmatch(str1, str2, FNM_NOESCAPE) == 0)
{
return true;
}
if (fnmatch(str2, str1, FNM_NOESCAPE) == 0)
{
return true;
}
return false;
return do_match_pattern(str1, str2) || do_match_pattern(str2, str1);
}

#endif // if defined(__cplusplus_winrt)

} // namespace rtps
} /* namespace rtps */
} /* namespace eprosima */
} // namespace fastrtps
} // namespace eprosima
8 changes: 8 additions & 0 deletions test/unittest/utils/StringMatchingTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ TEST_F(StringMatchingTests, patterns_with_wildcards)
ASSERT_FALSE(StringMatching::matchString(path, pattern9));
}

TEST_F(StringMatchingTests, empty_string)
{
ASSERT_TRUE(StringMatching::matchString("", ""));
ASSERT_TRUE(StringMatching::matchString("", "*"));
ASSERT_FALSE(StringMatching::matchString("", "?"));
ASSERT_FALSE(StringMatching::matchString("", "a"));
}


int main(
int argc,
Expand Down
Loading