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
2 changes: 1 addition & 1 deletion src/tagtransform-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ bool c_tagtransform_t::check_key(std::vector<taginfo> const &infos,
//go through the actual tags found on the item and keep the ones in the export list
for (auto const &info : infos) {
if (info.flags & FLAG_DELETE) {
if (wildMatch(info.name.c_str(), k)) {
if (wild_match(info.name.c_str(), k)) {
return false;
}
} else if (std::strcmp(info.name.c_str(), k) == 0) {
Expand Down
27 changes: 18 additions & 9 deletions src/wildcmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,37 @@
* Case sensitive wild card match with a string.
* * matches any string or no character.
* ? matches any single character.
* anything else etc must match the character exactly.
* anything else must match the character exactly.
*
* Returns if a match was found.
*/
bool wildMatch(char const *first, char const *second)
bool wild_match(char const *expr, char const *str) noexcept
{
// Code borrowed from
// Code based on
// http://www.geeksforgeeks.org/wildcard-character-matching/
if (*first == '\0' && *second == '\0') {
if (*expr == '\0' && *str == '\0') {
return true;
}

if (*first == '*' && *(first + 1) != '\0' && *second == '\0') {
if (*expr == '*') {
while (*(expr + 1) == '*') {
++expr;
}
}

if (*expr == '*' && *(expr + 1) != '\0' && *str == '\0') {
return false;
}

if (*first == '?' || *first == *second) {
return wildMatch(first + 1, second + 1);
if (*expr == '?' || *expr == *str) {
if (*str == '\0') {
return false;
}
return wild_match(expr + 1, str + 1);
}

if (*first == '*') {
return wildMatch(first + 1, second) || wildMatch(first, second + 1);
if (*expr == '*') {
return wild_match(expr + 1, str) || wild_match(expr, str + 1);
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion src/wildcmp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
* For a full list of authors see the git log.
*/

bool wildMatch(char const *wildCard, char const *string);
bool wild_match(char const *expr, char const *str) noexcept;

#endif // OSM2PGSQL_WILDCMP_HPP
49 changes: 27 additions & 22 deletions tests/test-wildcard-match.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,31 @@

TEST_CASE("Wildcard matching", "[NoDB]")
{
CHECK(wildMatch("fhwieurwe", "fhwieurwe"));
CHECK_FALSE(wildMatch("fhwieurwe", "fhwieurw"));
CHECK_FALSE(wildMatch("fhwieurw", "fhwieurwe"));
CHECK(wildMatch("*", "foo"));
CHECK_FALSE(wildMatch("r*", "foo"));
CHECK(wildMatch("r*", "roo"));
CHECK(wildMatch("*bar", "Hausbar"));
CHECK_FALSE(wildMatch("*bar", "Haustar"));
CHECK(wildMatch("*", ""));
CHECK(wildMatch("kin*la", "kinla"));
CHECK(wildMatch("kin*la", "kinLLla"));
CHECK(wildMatch("kin*la", "kinlalalala"));
CHECK_FALSE(wildMatch("kin*la", "kinlaa"));
CHECK_FALSE(wildMatch("kin*la", "ki??laa"));
CHECK(wildMatch("1*2*3", "123"));
CHECK(wildMatch("1*2*3", "1xX23"));
CHECK(wildMatch("1*2*3", "12y23"));
CHECK_FALSE(wildMatch("1*2*3", "12"));
CHECK(wildMatch("bo??f", "boxxf"));
CHECK_FALSE(wildMatch("bo??f", "boxf"));
CHECK(wildMatch("?5?", "?5?"));
CHECK(wildMatch("?5?", "x5x"));
CHECK(wild_match("fhwieurwe", "fhwieurwe"));
CHECK_FALSE(wild_match("fhwieurwe", "fhwieurw"));
CHECK_FALSE(wild_match("fhwieurw", "fhwieurwe"));
CHECK(wild_match("*", "foo"));
CHECK(wild_match("**", "foo"));
CHECK_FALSE(wild_match("r*", "foo"));
CHECK(wild_match("r*", "roo"));
CHECK(wild_match("*bar", "Hausbar"));
CHECK_FALSE(wild_match("*bar", "Haustar"));
CHECK(wild_match("*", ""));
CHECK(wild_match("**", ""));
CHECK(wild_match("kin*la", "kinla"));
CHECK(wild_match("kin*la", "kinLLla"));
CHECK(wild_match("kin*la", "kinlalalala"));
CHECK(wild_match("kin**la", "kinlalalala"));
CHECK_FALSE(wild_match("kin*la", "kinlaa"));
CHECK_FALSE(wild_match("kin*la", "ki??laa"));
CHECK(wild_match("1*2*3", "123"));
CHECK(wild_match("1*2*3", "1xX23"));
CHECK(wild_match("1*2*3", "12y23"));
CHECK_FALSE(wild_match("1*2*3", "12"));
CHECK(wild_match("bo??f", "boxxf"));
CHECK_FALSE(wild_match("bo??f", "boxf"));
CHECK(wild_match("?5?", "?5?"));
CHECK(wild_match("?5?", "x5x"));
CHECK_FALSE(wild_match("?abc", ""));
CHECK_FALSE(wild_match("?", ""));
}