Skip to content

Commit 663d3b4

Browse files
authored
Merge pull request #2321 from joto/wildcard-match-fixes
Wildcard match fixes
2 parents da5a1d5 + 1d693eb commit 663d3b4

File tree

4 files changed

+47
-33
lines changed

4 files changed

+47
-33
lines changed

src/tagtransform-c.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ bool c_tagtransform_t::check_key(std::vector<taginfo> const &infos,
112112
//go through the actual tags found on the item and keep the ones in the export list
113113
for (auto const &info : infos) {
114114
if (info.flags & FLAG_DELETE) {
115-
if (wildMatch(info.name.c_str(), k)) {
115+
if (wild_match(info.name.c_str(), k)) {
116116
return false;
117117
}
118118
} else if (std::strcmp(info.name.c_str(), k) == 0) {

src/wildcmp.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,37 @@
1515
* Case sensitive wild card match with a string.
1616
* * matches any string or no character.
1717
* ? matches any single character.
18-
* anything else etc must match the character exactly.
18+
* anything else must match the character exactly.
1919
*
2020
* Returns if a match was found.
2121
*/
22-
bool wildMatch(char const *first, char const *second)
22+
bool wild_match(char const *expr, char const *str) noexcept
2323
{
24-
// Code borrowed from
24+
// Code based on
2525
// http://www.geeksforgeeks.org/wildcard-character-matching/
26-
if (*first == '\0' && *second == '\0') {
26+
if (*expr == '\0' && *str == '\0') {
2727
return true;
2828
}
2929

30-
if (*first == '*' && *(first + 1) != '\0' && *second == '\0') {
30+
if (*expr == '*') {
31+
while (*(expr + 1) == '*') {
32+
++expr;
33+
}
34+
}
35+
36+
if (*expr == '*' && *(expr + 1) != '\0' && *str == '\0') {
3137
return false;
3238
}
3339

34-
if (*first == '?' || *first == *second) {
35-
return wildMatch(first + 1, second + 1);
40+
if (*expr == '?' || *expr == *str) {
41+
if (*str == '\0') {
42+
return false;
43+
}
44+
return wild_match(expr + 1, str + 1);
3645
}
3746

38-
if (*first == '*') {
39-
return wildMatch(first + 1, second) || wildMatch(first, second + 1);
47+
if (*expr == '*') {
48+
return wild_match(expr + 1, str) || wild_match(expr, str + 1);
4049
}
4150

4251
return false;

src/wildcmp.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
* For a full list of authors see the git log.
1111
*/
1212

13-
bool wildMatch(char const *wildCard, char const *string);
13+
bool wild_match(char const *expr, char const *str) noexcept;
1414

1515
#endif // OSM2PGSQL_WILDCMP_HPP

tests/test-wildcard-match.cpp

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,31 @@
1313

1414
TEST_CASE("Wildcard matching", "[NoDB]")
1515
{
16-
CHECK(wildMatch("fhwieurwe", "fhwieurwe"));
17-
CHECK_FALSE(wildMatch("fhwieurwe", "fhwieurw"));
18-
CHECK_FALSE(wildMatch("fhwieurw", "fhwieurwe"));
19-
CHECK(wildMatch("*", "foo"));
20-
CHECK_FALSE(wildMatch("r*", "foo"));
21-
CHECK(wildMatch("r*", "roo"));
22-
CHECK(wildMatch("*bar", "Hausbar"));
23-
CHECK_FALSE(wildMatch("*bar", "Haustar"));
24-
CHECK(wildMatch("*", ""));
25-
CHECK(wildMatch("kin*la", "kinla"));
26-
CHECK(wildMatch("kin*la", "kinLLla"));
27-
CHECK(wildMatch("kin*la", "kinlalalala"));
28-
CHECK_FALSE(wildMatch("kin*la", "kinlaa"));
29-
CHECK_FALSE(wildMatch("kin*la", "ki??laa"));
30-
CHECK(wildMatch("1*2*3", "123"));
31-
CHECK(wildMatch("1*2*3", "1xX23"));
32-
CHECK(wildMatch("1*2*3", "12y23"));
33-
CHECK_FALSE(wildMatch("1*2*3", "12"));
34-
CHECK(wildMatch("bo??f", "boxxf"));
35-
CHECK_FALSE(wildMatch("bo??f", "boxf"));
36-
CHECK(wildMatch("?5?", "?5?"));
37-
CHECK(wildMatch("?5?", "x5x"));
16+
CHECK(wild_match("fhwieurwe", "fhwieurwe"));
17+
CHECK_FALSE(wild_match("fhwieurwe", "fhwieurw"));
18+
CHECK_FALSE(wild_match("fhwieurw", "fhwieurwe"));
19+
CHECK(wild_match("*", "foo"));
20+
CHECK(wild_match("**", "foo"));
21+
CHECK_FALSE(wild_match("r*", "foo"));
22+
CHECK(wild_match("r*", "roo"));
23+
CHECK(wild_match("*bar", "Hausbar"));
24+
CHECK_FALSE(wild_match("*bar", "Haustar"));
25+
CHECK(wild_match("*", ""));
26+
CHECK(wild_match("**", ""));
27+
CHECK(wild_match("kin*la", "kinla"));
28+
CHECK(wild_match("kin*la", "kinLLla"));
29+
CHECK(wild_match("kin*la", "kinlalalala"));
30+
CHECK(wild_match("kin**la", "kinlalalala"));
31+
CHECK_FALSE(wild_match("kin*la", "kinlaa"));
32+
CHECK_FALSE(wild_match("kin*la", "ki??laa"));
33+
CHECK(wild_match("1*2*3", "123"));
34+
CHECK(wild_match("1*2*3", "1xX23"));
35+
CHECK(wild_match("1*2*3", "12y23"));
36+
CHECK_FALSE(wild_match("1*2*3", "12"));
37+
CHECK(wild_match("bo??f", "boxxf"));
38+
CHECK_FALSE(wild_match("bo??f", "boxf"));
39+
CHECK(wild_match("?5?", "?5?"));
40+
CHECK(wild_match("?5?", "x5x"));
41+
CHECK_FALSE(wild_match("?abc", ""));
42+
CHECK_FALSE(wild_match("?", ""));
3843
}

0 commit comments

Comments
 (0)