-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathPatternMatchingUtility.cs
More file actions
39 lines (34 loc) · 1.29 KB
/
PatternMatchingUtility.cs
File metadata and controls
39 lines (34 loc) · 1.29 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
namespace Microsoft.ComponentDetection.Common;
using System;
using System.Collections.Generic;
using System.Linq;
public static class PatternMatchingUtility
{
public delegate bool FilePatternMatcher(ReadOnlySpan<char> span);
public static FilePatternMatcher GetFilePatternMatcher(IEnumerable<string> patterns)
{
var matchers = patterns.Select<string, FilePatternMatcher>(pattern => pattern switch
{
_ when pattern.StartsWith('*') && pattern.EndsWith('*') =>
pattern.Length <= 2
? _ => true
: span => span.Contains(pattern.AsSpan(1, pattern.Length - 2), StringComparison.Ordinal),
_ when pattern.StartsWith('*') =>
span => span.EndsWith(pattern.AsSpan(1), StringComparison.Ordinal),
_ when pattern.EndsWith('*') =>
span => span.StartsWith(pattern.AsSpan(0, pattern.Length - 1), StringComparison.Ordinal),
_ => span => span.Equals(pattern.AsSpan(), StringComparison.Ordinal),
}).ToList();
return span =>
{
foreach (var matcher in matchers)
{
if (matcher(span))
{
return true;
}
}
return false;
};
}
}