WIP: funcky wildcard pattern match

This commit is contained in:
Ray 2021-10-27 17:10:56 +01:00
parent 0c01ab92c1
commit 788a9ea107

View File

@ -170,33 +170,7 @@ namespace RandomFileRunner.IO
public static bool MatchFileSearchPattern(string pattern, string subject) public static bool MatchFileSearchPattern(string pattern, string subject)
{ {
if (pattern.Contains(';')) if (string.IsNullOrWhiteSpace(pattern))
{
string[] parts = pattern.Split(';');
for (int i=0; i<parts.Length; i++)
{
if (string.IsNullOrWhiteSpace(parts[i]))
{
continue;
}
if (MatchWildcard(parts[i].Trim(), subject))
{
return true;
}
}
return false;
}
else
{
return MatchWildcard(pattern, subject);
}
}
public static bool MatchWildcard(string wildcardPattern, string subject)
{
if (string.IsNullOrWhiteSpace(wildcardPattern))
{ {
return false; return false;
} }
@ -245,31 +219,57 @@ namespace RandomFileRunner.IO
return true; return true;
}; };
int wildcardCount = wildcardPattern.Count(x => x.Equals('*')); Func<string, string, bool> matchAllPattern = (pattern, subject) =>
{
int wildcardCount = pattern.Count(x => x.Equals('*'));
if (wildcardCount <= 0) if (wildcardCount <= 0)
{ {
return subject.Equals(wildcardPattern, StringComparison.CurrentCultureIgnoreCase); return subject.Equals(pattern, StringComparison.CurrentCultureIgnoreCase);
} }
else if (wildcardCount == 1) else if (wildcardCount == 1)
{ {
string newWildcardPattern = wildcardPattern.Replace("*", ""); string newWildcardPattern = pattern.Replace("*", "");
if (wildcardPattern.StartsWith("*")) if (pattern.StartsWith("*"))
{ {
return subject.EndsWith(newWildcardPattern, StringComparison.CurrentCultureIgnoreCase); return subject.EndsWith(newWildcardPattern, StringComparison.CurrentCultureIgnoreCase);
} }
else if (wildcardPattern.EndsWith("*")) else if (pattern.EndsWith("*"))
{ {
return subject.StartsWith(newWildcardPattern, StringComparison.CurrentCultureIgnoreCase); return subject.StartsWith(newWildcardPattern, StringComparison.CurrentCultureIgnoreCase);
} }
else else
{ {
return matchPattern(wildcardPattern, subject); return matchPattern(pattern, subject);
} }
} }
else else
{ {
return matchPattern(wildcardPattern, subject); return matchPattern(pattern, subject);
}
};
if (pattern.Contains(';'))
{
string[] parts = pattern.Split(';');
for (int i=0; i<parts.Length; i++)
{
if (string.IsNullOrWhiteSpace(parts[i]))
{
continue;
}
if (matchAllPattern(parts[i].Trim(), subject))
{
return true;
}
}
return false;
}
else
{
return matchAllPattern(pattern, subject);
} }
} }