-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNamePatterns.cs
More file actions
69 lines (60 loc) · 1.69 KB
/
Copy pathNamePatterns.cs
File metadata and controls
69 lines (60 loc) · 1.69 KB
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
using System.Text.RegularExpressions;
/// <summary>
/// Name-based pattern matcher for StartsWith / EndsWith / Contains / Regexes.
/// Used by both AdditionalIgnorePatterns (blacklist) and AdditionalIncludePatterns (whitelist).
/// </summary>
internal class NamePatterns
{
private List<Func<string, bool>> _checks = [];
public string[]? StartsWith { get; set; }
public string[]? EndsWith { get; set; }
public string[]? Contains { get; set; }
public string[]? Regexes { get; set; }
internal void Initialize()
{
var checks = new List<Func<string, bool>>();
if (StartsWith != null && StartsWith.Length > 0)
{
var prefixes = StartsWith;
checks.Add(name => prefixes.Any(p => name.StartsWith(p, StringComparison.OrdinalIgnoreCase)));
}
if (EndsWith != null && EndsWith.Length > 0)
{
var suffixes = EndsWith;
checks.Add(name => suffixes.Any(s => name.EndsWith(s, StringComparison.OrdinalIgnoreCase)));
}
if (Contains != null && Contains.Length > 0)
{
var substrings = Contains;
checks.Add(name => substrings.Any(c => name.Contains(c, StringComparison.OrdinalIgnoreCase)));
}
if (Regexes != null && Regexes.Length > 0)
{
foreach (var pattern in Regexes)
{
try
{
var regex = new Regex(pattern, RegexOptions.Compiled);
checks.Add(name => regex.IsMatch(name));
}
catch (Exception ex)
{
Console.WriteLine($"[WARNING] Invalid regex pattern '{pattern}': {ex.Message}");
}
}
}
_checks = checks;
}
/// <summary>
/// Returns true if the name matches any of the configured patterns.
/// </summary>
internal bool Matches(string name)
{
foreach (var check in _checks)
{
if (check(name))
return true;
}
return false;
}
}