aboutsummaryrefslogtreecommitdiff
path: root/src/zenutil/wildcard.cpp
blob: 7a44c049842c23d93ac35929a09b511001e2cbbf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright Epic Games, Inc. All Rights Reserved.

#include <zencore/string.h>
#include <zenutil/wildcard.h>

#if ZEN_WITH_TESTS
#	include <zencore/testing.h>
#endif	// ZEN_WITH_TESTS

namespace zen {

bool
MatchWildcard(std::string_view::const_iterator WildcardIt,
			  std::string_view::const_iterator WildcardEnd,
			  std::string_view::const_iterator StringIt,
			  std::string_view::const_iterator StringEnd)
{
	for (; WildcardIt != WildcardEnd; WildcardIt++)
	{
		switch (*WildcardIt)
		{
			case '?':
				if (StringIt == StringEnd)
				{
					return false;
				}
				StringIt++;
				break;
			case '*':
				{
					if ((WildcardIt + 1) == WildcardEnd)
					{
						return true;
					}
					size_t Max = std::distance(StringIt, StringEnd);
					for (size_t i = 0; i < Max; i++)
					{
						if (MatchWildcard(WildcardIt + 1, WildcardEnd, StringIt + i, StringEnd))
						{
							return true;
						}
					}
					return false;
				}
			default:
				if (StringIt == StringEnd)
				{
					return false;
				}
				if (*StringIt != *WildcardIt)
				{
					return false;
				}
				++StringIt;
				break;
		}
	}
	return StringIt == StringEnd;
}

bool
MatchWildcard(std::string_view Wildcard, std::string_view String, bool CaseSensitive)
{
	if (CaseSensitive)
	{
		return MatchWildcard(begin(Wildcard), end(Wildcard), begin(String), end(String));
	}
	else
	{
		std::string		 LowercaseWildcard = ToLower(Wildcard);
		std::string		 LowercaseString   = ToLower(String);
		std::string_view LowercaseWildcardView(LowercaseWildcard);
		std::string_view LowercaseStringView(LowercaseString);
		return MatchWildcard(begin(LowercaseWildcardView),
							 end(LowercaseWildcardView),
							 begin(LowercaseStringView),
							 end(LowercaseStringView));
	}
}

bool
IncludePath(std::span<const std::string> IncludeWildcards,
			std::span<const std::string> ExcludeWildcards,
			const std::string&			 Path,
			bool						 CaseSensitive)
{
	bool IncludePath = true;
	if (!IncludeWildcards.empty())
	{
		IncludePath = false;
		for (const std::string& IncludeWildcard : IncludeWildcards)
		{
			if (MatchWildcard(IncludeWildcard, Path, CaseSensitive))
			{
				IncludePath = true;
				break;
			}
		}
		if (!IncludePath)
		{
			return false;
		}
	}
	for (const std::string& ExcludeWildcard : ExcludeWildcards)
	{
		if (MatchWildcard(ExcludeWildcard, Path, CaseSensitive))
		{
			return false;
		}
	}
	return true;
}

#if ZEN_WITH_TESTS

void
wildcard_forcelink()
{
}

TEST_CASE("Wildcard")
{
	CHECK(MatchWildcard("*.*", "normal.txt", true));
	CHECK(MatchWildcard("*.*", "normal.txt", false));

	CHECK(!MatchWildcard("*.*", "normal", true));
	CHECK(!MatchWildcard("*.*", "normal", false));

	CHECK(MatchWildcard("*", "hey/normal", true));
	CHECK(MatchWildcard("*", "hey/normal", false));

	CHECK(MatchWildcard("hey/*.txt", "hey/normal.txt", true));
	CHECK(MatchWildcard("*/?ormal.txt", "hey/normal.txt", true));
	CHECK(!MatchWildcard("*/?rmal.txt", "hey/normal.txt", true));
	CHECK(MatchWildcard("*/?ormal.*", "hey/normal.txt", true));
	CHECK(MatchWildcard("*/?ormal", "hey/normal", true));

	CHECK(MatchWildcard("hey/*.txt", "hey/normaL.txt", false));
	CHECK(MatchWildcard("*/?ormal.TXT", "hey/normal.txt", false));
	CHECK(MatchWildcard("*/?ORMAL.*", "hey/normal.txt", false));
	CHECK(MatchWildcard("*/?ormal", "HEY/normal", false));

	CHECK(!MatchWildcard("hey/*.txt", "heY/normal.txt", true));
	CHECK(!MatchWildcard("*/?ormal.TXT", "hey/normal.txt", true));
	CHECK(!MatchWildcard("*/?ORMAL.*", "hey/normal.txt", true));
	CHECK(!MatchWildcard("*/?ormal", "hey/normaL", true));

	CHECK(MatchWildcard("*.dll", "dir/path.dll", true));
	CHECK(!MatchWildcard("*.dll", "dir/path.d", true));
	CHECK(!MatchWildcard("*.d", "dir/path.dll", true));
	CHECK(MatchWildcard("*.d", "dir/path.d", true));
}

#endif
}  // namespace zen