This repository was archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRegularExpressionSearchFilter.cs
More file actions
79 lines (67 loc) · 2.17 KB
/
RegularExpressionSearchFilter.cs
File metadata and controls
79 lines (67 loc) · 2.17 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
70
71
72
73
74
75
76
77
78
79
/****
* CocoaLogViewer
* Copyright (C) 2020-2021 Yigty.ORG; all rights reserved.
* Copyright (C) 2020-2021 Takym.
*
* distributed under the MIT License.
****/
using System;
using System.Text.RegularExpressions;
using Covid19Radar.LogViewer.Models;
namespace Covid19Radar.LogViewer.SearchFilters
{
internal sealed class RegularExpressionSearchFilter : ISearchFilter
{
internal static readonly RegularExpressionSearchFilter _inst1 = new(
"regexs",
(model, pattern, expected) =>
MatchCore(model, model.OriginalMessage, pattern, expected) ||
MatchCore(model, model.TransformedMessage, pattern, expected)
);
internal static readonly RegularExpressionSearchFilter _inst2 = new(
"regexo",
(model, pattern, expected) =>
MatchCore(model, model.OriginalMessage, pattern, expected)
);
internal static readonly RegularExpressionSearchFilter _inst3 = new(
"regext",
(model, pattern, expected) =>
MatchCore(model, model.TransformedMessage, pattern, expected)
);
private readonly Func<LogDataModel, string, SearchFilterNode[], bool> _match;
public string Key { get; }
private RegularExpressionSearchFilter(string key, Func<LogDataModel, string, SearchFilterNode[], bool> match)
{
_match = match;
this.Key = key;
}
public bool Match(SearchFilterNode value, LogDataModel model)
{
if (value is SearchFilterNodeList nodeList && nodeList.Nodes.Count >= 2 &&
nodeList.Nodes[0] is TokenNode patternNode) {
string pattern = patternNode.Token.GetText();
var expected = new SearchFilterNode[nodeList.Nodes.Count - 1];
for (int i = 0; i < expected.Length; ++i) {
expected[i] = nodeList.Nodes[i + 1];
}
return _match(model, pattern, expected);
} else {
return false;
}
}
private static bool MatchCore(LogDataModel model, string input, string pattern, SearchFilterNode[] expected)
{
string[] vs = Regex.Split(input, pattern);
if (vs.Length != expected.Length) {
return false;
}
for (int i = 0; i < vs.Length; ++i) {
string msg = vs[i];
if (!expected[i].Match(model with { OriginalMessage = msg, TransformedMessage = msg })) {
return false;
}
}
return true;
}
}
}