obfuz/Editor/Utils/NameMatcher.cs

42 lines
1.0 KiB
C#
Raw Normal View History

2025-04-18 12:15:47 +08:00
using System.Text.RegularExpressions;
2025-05-04 19:24:14 +08:00
namespace Obfuz.Utils
2025-04-18 12:15:47 +08:00
{
public class NameMatcher
{
private readonly string _str;
private readonly Regex _regex;
public string NameOrPattern => _str;
public NameMatcher(string nameOrPattern)
{
2025-04-18 13:26:31 +08:00
if (string.IsNullOrEmpty(nameOrPattern))
{
nameOrPattern = "*";
}
2025-04-18 12:15:47 +08:00
_str = nameOrPattern;
2025-04-19 11:47:05 +08:00
_regex = nameOrPattern.Contains("*") || nameOrPattern.Contains("?") ? new Regex(WildcardToRegex(nameOrPattern)) : null;
2025-04-18 12:15:47 +08:00
}
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;
}
}
}
}