Skip to content

Commit 541c304

Browse files
MiguelCompanymergify[bot]
authored andcommitted
Allow empty partition list to match against "*" (#5989)
* Refs #23623. Always use StringMatching. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> * Refs #23623. Refactor StringMatching. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> * Refs #23623. Regression unit test.. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> * Refs #23623. Fix StringMatching on Windows. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> * Refs #23624. Avoid unnecessary conditionals. Signed-off-by: Miguel Company <miguelcompany@eprosima.com> --------- Signed-off-by: Miguel Company <miguelcompany@eprosima.com> (cherry picked from commit 27ce90d) # Conflicts: # src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp # src/cpp/utils/StringMatching.cpp
1 parent 28529b7 commit 541c304

File tree

3 files changed

+47
-59
lines changed

3 files changed

+47
-59
lines changed

src/cpp/rtps/builtin/discovery/endpoint/EDP.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,21 @@ namespace rtps {
6767
using reader_map_helper = utilities::collections::map_size_helper<GUID_t, SubscriptionMatchedStatus>;
6868
using writer_map_helper = utilities::collections::map_size_helper<GUID_t, PublicationMatchedStatus>;
6969

70+
<<<<<<< HEAD
7071
static bool is_partition_empty(
7172
const fastdds::dds::Partition_t& partition)
7273
{
7374
return partition.size() <= 1 && 0 == strlen(partition.name());
75+
=======
76+
static bool is_same_type(
77+
const dds::xtypes::TypeInformation& t1,
78+
const dds::xtypes::TypeInformation& t2)
79+
{
80+
return (dds::xtypes::TK_NONE != t1.complete().typeid_with_size().type_id()._d()
81+
&& t1.complete().typeid_with_size() == t2.complete().typeid_with_size())
82+
|| (dds::xtypes::TK_NONE != t1.minimal().typeid_with_size().type_id()._d()
83+
&& t1.minimal().typeid_with_size() == t2.minimal().typeid_with_size());
84+
>>>>>>> 27ce90d9 (Allow empty partition list to match against `"*"` (#5989))
7485
}
7586

7687
EDP::EDP(
@@ -816,7 +827,7 @@ bool EDP::valid_matching(
816827
for (auto rnameit = rdata->m_qos.m_partition.begin();
817828
rnameit != rdata->m_qos.m_partition.end(); ++rnameit)
818829
{
819-
if (is_partition_empty(*rnameit))
830+
if (StringMatching::matchString("", rnameit->name()))
820831
{
821832
matched = true;
822833
break;
@@ -828,7 +839,7 @@ bool EDP::valid_matching(
828839
for (auto wnameit = wdata->m_qos.m_partition.begin();
829840
wnameit != wdata->m_qos.m_partition.end(); ++wnameit)
830841
{
831-
if (is_partition_empty(*wnameit))
842+
if (StringMatching::matchString(wnameit->name(), ""))
832843
{
833844
matched = true;
834845
break;

src/cpp/utils/StringMatching.cpp

Lines changed: 26 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <algorithm>
2626
#include <regex>
2727
#elif defined(_WIN32)
28+
#include <cstring>
2829
#include "Shlwapi.h"
2930
#else
3031
#include <fnmatch.h>
@@ -46,7 +47,7 @@ StringMatching::~StringMatching()
4647
}
4748

4849
#if defined(__cplusplus_winrt)
49-
void replace_all(
50+
static void replace_all(
5051
std::string& subject,
5152
const std::string& search,
5253
const std::string& replace)
@@ -59,7 +60,7 @@ void replace_all(
5960
}
6061
}
6162

62-
bool StringMatching::matchPattern(
63+
static bool do_match_pattern(
6364
const char* pattern,
6465
const char* str)
6566
{
@@ -71,87 +72,55 @@ bool StringMatching::matchPattern(
7172

7273
std::regex path_regex(path);
7374
std::smatch spec_match;
74-
if (std::regex_match(spec, spec_match, path_regex))
75-
{
76-
return true;
77-
}
78-
79-
return false;
75+
return std::regex_match(spec, spec_match, path_regex);
8076
}
8177

82-
bool StringMatching::matchString(
83-
const char* str1,
84-
const char* str2)
78+
#elif defined(_WIN32)
79+
static bool do_match_pattern(
80+
const char* pattern,
81+
const char* str)
8582
{
86-
if (StringMatching::matchPattern(str1, str2))
83+
// An empty pattern only matches an empty string
84+
if (strlen(pattern) == 0)
8785
{
88-
return true;
86+
return strlen(str) == 0;
8987
}
90-
91-
if (StringMatching::matchPattern(str2, str1))
88+
// An empty string also matches a pattern of "*"
89+
if (strlen(str) == 0)
9290
{
93-
return true;
91+
return strcmp(pattern, "*") == 0;
9492
}
95-
96-
return false;
93+
// Leave rest of cases to PathMatchSpecA
94+
return PathMatchSpecA(str, pattern);
9795
}
9896

99-
#elif defined(_WIN32)
100-
bool StringMatching::matchPattern(
97+
#else
98+
static bool do_match_pattern(
10199
const char* pattern,
102100
const char* str)
103101
{
104-
if (PathMatchSpecA(str, pattern))
105-
{
106-
return true;
107-
}
108-
return false;
102+
return fnmatch(pattern, str, FNM_NOESCAPE) == 0;
109103
}
110104

111-
bool StringMatching::matchString(
112-
const char* str1,
113-
const char* str2)
114-
{
115-
if (PathMatchSpecA(str1, str2))
116-
{
117-
return true;
118-
}
119-
if (PathMatchSpecA(str2, str1))
120-
{
121-
return true;
122-
}
123-
return false;
124-
}
105+
#endif // if defined(__cplusplus_winrt)
125106

126-
#else
107+
<<<<<<< HEAD
108+
} // namespace rtps
109+
=======
127110
bool StringMatching::matchPattern(
128111
const char* pattern,
129112
const char* str)
130113
{
131-
if (fnmatch(pattern, str, FNM_NOESCAPE) == 0)
132-
{
133-
return true;
134-
}
135-
return false;
114+
return do_match_pattern(pattern, str);
136115
}
137116

138117
bool StringMatching::matchString(
139118
const char* str1,
140119
const char* str2)
141120
{
142-
if (fnmatch(str1, str2, FNM_NOESCAPE) == 0)
143-
{
144-
return true;
145-
}
146-
if (fnmatch(str2, str1, FNM_NOESCAPE) == 0)
147-
{
148-
return true;
149-
}
150-
return false;
121+
return do_match_pattern(str1, str2) || do_match_pattern(str2, str1);
151122
}
152123

153-
#endif // if defined(__cplusplus_winrt)
154-
155-
} // namespace rtps
124+
>>>>>>> 27ce90d9 (Allow empty partition list to match against `"*"` (#5989))
156125
} /* namespace rtps */
157126
} /* namespace eprosima */

test/unittest/utils/StringMatchingTests.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ TEST_F(StringMatchingTests, patterns_with_wildcards)
5555
ASSERT_FALSE(StringMatching::matchString(path, pattern9));
5656
}
5757

58+
TEST_F(StringMatchingTests, empty_string)
59+
{
60+
ASSERT_TRUE(StringMatching::matchString("", ""));
61+
ASSERT_TRUE(StringMatching::matchString("", "*"));
62+
ASSERT_FALSE(StringMatching::matchString("", "?"));
63+
ASSERT_FALSE(StringMatching::matchString("", "a"));
64+
}
65+
5866

5967
int main(int argc, char **argv)
6068
{

0 commit comments

Comments
 (0)