-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLabelFilter.cs
More file actions
42 lines (38 loc) · 1.36 KB
/
Copy pathLabelFilter.cs
File metadata and controls
42 lines (38 loc) · 1.36 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
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace GitImporter
{
public class LabelFilter
{
private readonly List<Regex> _labelsRegexToKeep;
private readonly HashSet<string> _labelsToKeep = new HashSet<string>();
private readonly HashSet<string> _labelsToTrash = new HashSet<string>();
private readonly object _syncroot = new object();
public LabelFilter(IEnumerable<string> labels)
{
_labelsRegexToKeep = new List<Regex>(labels
.Where(l => !string.IsNullOrWhiteSpace(l) && l.ToUpper() != "NONE")
.Select(l => new Regex(l, RegexOptions.Compiled)));
}
public bool ShouldKeep(string label)
{
lock (_syncroot)
{
if (_labelsToTrash.Contains(label))
return false;
if (!_labelsToKeep.Contains(label))
{
// first time seen
if (_labelsRegexToKeep.TrueForAll(r => !r.IsMatch(label)))
{
_labelsToTrash.Add(label);
return false;
}
_labelsToKeep.Add(label);
}
return true;
}
}
}
}