obfuz/Editor/NameMatcher.cs

38 lines
914 B
C#
Raw Normal View History

2025-04-18 12:15:47 +08:00
using System.Text.RegularExpressions;
namespace Obfuz
{
public class NameMatcher
{
private readonly string _str;
private readonly Regex _regex;
public string NameOrPattern => _str;
public NameMatcher(string nameOrPattern)
{
_str = nameOrPattern;
_regex = nameOrPattern.Contains('*') || nameOrPattern.Contains('?') ? new Regex(WildcardToRegex(nameOrPattern)) : null;
}
public static string WildcardToRegex(string pattern)
{
return "^" + Regex.Escape(pattern).
Replace("\\*", ".*").
Replace("\\?", ".") + "$";
}
public bool IsMatch(string name)
{
if (_regex != null)
{
return _regex.IsMatch(name);
}
else
{
return _str == name;
}
}
}
}