Skip to content

Commit 5f29e1e

Browse files
committed
Implemented wildcard_match_any() and wildcard_match_all() with tests.
1 parent 26113bd commit 5f29e1e

File tree

3 files changed

+123
-5
lines changed

3 files changed

+123
-5
lines changed

src/bin2cpp/wildcard.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,32 @@ namespace bin2cpp
140140
return match;
141141
}
142142

143+
bool wildcard_match_any(const std::string& value, const std::vector<std::string>& patterns)
144+
{
145+
bool result = false;
146+
std::vector<std::string> captures;
147+
for ( size_t i = 0; i < patterns.size(); i++ )
148+
{
149+
const std::string& pattern = patterns[i];
150+
bool match = wildcard_match(value, pattern, captures);
151+
if ( match )
152+
return true;
153+
}
154+
return result;
155+
}
156+
157+
bool wildcard_match_all(const std::string& value, const std::vector<std::string>& patterns)
158+
{
159+
bool result = true;
160+
std::vector<std::string> captures;
161+
for ( size_t i = 0; i < patterns.size(); i++ )
162+
{
163+
const std::string& pattern = patterns[i];
164+
bool match = wildcard_match(value, pattern, captures);
165+
if ( !match )
166+
return false;
167+
}
168+
return result;
169+
}
170+
143171
}; //bin2cpp

src/bin2cpp/wildcard.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace bin2cpp
4242
/// -'[charlist]' matches any character in the provided set.
4343
/// -'[a-z]', '[A-Z]', '[0-9]' match characters in respective ranges.
4444
/// -'[a-zA-Z0-9]' matches any alphanumeric character.
45-
///< / remarks>
45+
///</remarks>
4646
///<param name="value">The file path, value or string to match.</param>
4747
///<param name="pattern">The pattern containing wildcards.</param>
4848
///<param name="captures">The captured value of wildcard within the input value. The number of captures matches the number of wildcard in the pattern.</param>
@@ -60,7 +60,7 @@ namespace bin2cpp
6060
/// -'[charlist]' matches any character in the provided set.
6161
/// -'[a-z]', '[A-Z]', '[0-9]' match characters in respective ranges.
6262
/// -'[a-zA-Z0-9]' matches any alphanumeric character.
63-
///< / remarks>
63+
///</remarks>
6464
///<param name="value">The file path, value or string to match.</param>
6565
///<param name="pattern">The pattern containing wildcards.</param>
6666
///<returns>Returns true if the value matches the pattern, otherwise false.</returns>
@@ -70,6 +70,22 @@ namespace bin2cpp
7070
return wildcard_match(value, pattern, tmp_captures);
7171
}
7272

73+
///<summary>
74+
///Checks if a given value matches at least one of the given patterns.
75+
///</summary>
76+
///<param name="value">The file path, value or string to match.</param>
77+
///<param name="patterns">The list of patterns containing wildcards.</param>
78+
///<returns>Returns true if the value matches the any pattern, otherwise false.</returns>
79+
bool wildcard_match_any(const std::string& value, const std::vector<std::string>& patterns);
80+
81+
///<summary>
82+
///Checks if a given value matches at of the given patterns.
83+
///</summary>
84+
///<param name="value">The file path, value or string to match.</param>
85+
///<param name="patterns">The list of patterns containing wildcards.</param>
86+
///<returns>Returns true if the value matches the any pattern, otherwise false.</returns>
87+
bool wildcard_match_all(const std::string& value, const std::vector<std::string>& patterns);
88+
7389
}; //bin2cpp
7490

7591
#endif //BIN2CPP_COMMON_H

test/bin2cpp_unittest/TestWildcard.cpp

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ std::string to_boolean_str(bool value)
4141

4242
TEST_F(TestWildcard, testBasicExamples)
4343
{
44-
struct TESTVALUE
44+
struct TEST_HELPER
4545
{
4646
const char* value;
4747
const char* pattern;
4848
bool expected_result;
4949
std::vector<std::string> expected_captures;
5050
};
51-
static const TESTVALUE test_values[] = {
51+
static const TEST_HELPER test_values[] = {
5252
// ============================== matches ==============================
5353
// ?
5454
{"a", "?", true, {"a"}},
@@ -102,13 +102,87 @@ TEST_F(TestWildcard, testBasicExamples)
102102

103103
for ( size_t i = 0; i < num_test_values; i++ )
104104
{
105-
const TESTVALUE& t = test_values[i];
105+
const TEST_HELPER& t = test_values[i];
106106

107107
std::vector<std::string> actual_captures;
108108
bool actual_result = bin2cpp::wildcard_match(t.value, t.pattern, actual_captures);
109109

110110
ASSERT_EQ(actual_result, t.expected_result) << "Test fail with test_values[" << i << "]. The match between value '" << t.value << "' and pattern '" << t.pattern << "' is supposed to return '" << to_boolean_str(t.expected_result) << "' but it actually retuned '" << to_boolean_str(actual_result) << "'.";
111111
ASSERT_EQ(actual_captures, t.expected_captures) << "Test fail with test_values[" << i << "]. The match between value '" << t.value << "' and pattern '" << t.pattern << "' is has returned '" << to_boolean_str(t.expected_result) << "' but the expected captures does not match.";
112112
}
113+
}
114+
115+
TEST_F(TestWildcard, testWildcardMatchAny)
116+
{
117+
const std::string path = "path/to/a/file.jpg";
118+
std::vector<std::string> patterns;
119+
120+
{
121+
// test for no matches
122+
patterns.clear();
123+
patterns.push_back("idonotmatch");
124+
patterns.push_back("idonotmatcheither");
125+
126+
ASSERT_FALSE(bin2cpp::wildcard_match_any(path, patterns));
127+
}
128+
129+
{
130+
// test matches from first element
131+
patterns.clear();
132+
patterns.push_back("*.jpg");
133+
patterns.push_back("idonotmatch");
134+
135+
ASSERT_TRUE(bin2cpp::wildcard_match_any(path, patterns));
136+
}
137+
138+
{
139+
// test matches from second element
140+
patterns.clear();
141+
patterns.push_back("idonotmatch");
142+
patterns.push_back("*.jpg");
113143

144+
ASSERT_TRUE(bin2cpp::wildcard_match_any(path, patterns));
145+
}
114146
}
147+
148+
TEST_F(TestWildcard, testWildcardMatchAll)
149+
{
150+
const std::string path = "path/to/a/file.jpg";
151+
std::vector<std::string> patterns;
152+
153+
{
154+
// test for no matches
155+
patterns.clear();
156+
patterns.push_back("idonotmatch");
157+
patterns.push_back("idonotmatcheither");
158+
159+
ASSERT_FALSE(bin2cpp::wildcard_match_all(path, patterns));
160+
}
161+
162+
{
163+
// test matches from first element
164+
patterns.clear();
165+
patterns.push_back("*.jpg");
166+
patterns.push_back("idonotmatch");
167+
168+
ASSERT_FALSE(bin2cpp::wildcard_match_all(path, patterns));
169+
}
170+
171+
{
172+
// test matches from second element
173+
patterns.clear();
174+
patterns.push_back("idonotmatch");
175+
patterns.push_back("*.jpg");
176+
177+
ASSERT_FALSE(bin2cpp::wildcard_match_all(path, patterns));
178+
}
179+
180+
{
181+
// test matches from all elements
182+
patterns.clear();
183+
patterns.push_back("*/to/*");
184+
patterns.push_back("*.jpg");
185+
186+
ASSERT_TRUE(bin2cpp::wildcard_match_all(path, patterns));
187+
}
188+
}

0 commit comments

Comments
 (0)