/
PatternContextLinear.cs
101 lines (83 loc) · 3.08 KB
/
PatternContextLinear.cs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using Microsoft.Extensions.FileSystemGlobbing.Abstractions;
namespace Microsoft.Extensions.FileSystemGlobbing.Internal.PatternContexts
{
public abstract class PatternContextLinear
: PatternContext<PatternContextLinear.FrameData>
{
public PatternContextLinear(ILinearPattern pattern)
{
ThrowHelper.ThrowIfNull(pattern);
Pattern = pattern;
}
public override PatternTestResult Test(FileInfoBase file)
{
if (IsStackEmpty())
{
throw new InvalidOperationException(SR.CannotTestFile);
}
if (!Frame.IsNotApplicable && IsLastSegment() && TestMatchingSegment(file.Name))
{
return PatternTestResult.Success(CalculateStem(file));
}
return PatternTestResult.Failed;
}
public override void PushDirectory(DirectoryInfoBase directory)
{
// copy the current frame
FrameData frame = Frame;
if (IsStackEmpty() || Frame.IsNotApplicable)
{
// when the stack is being initialized
// or no change is required.
}
else if (!TestMatchingSegment(directory.Name))
{
// nothing down this path is affected by this pattern
frame.IsNotApplicable = true;
}
else
{
// Determine this frame's contribution to the stem (if any)
IPathSegment segment = Pattern.Segments[Frame.SegmentIndex];
if (frame.InStem || segment.CanProduceStem)
{
frame.InStem = true;
frame.StemItems.Add(directory.Name);
}
// directory matches segment, advance position in pattern
frame.SegmentIndex++;
}
PushDataFrame(frame);
}
public struct FrameData
{
public bool IsNotApplicable;
public int SegmentIndex;
public bool InStem;
private IList<string>? _stemItems;
public IList<string> StemItems => _stemItems ??= new List<string>();
public string? Stem => _stemItems == null ? null : string.Join("/", _stemItems);
}
protected ILinearPattern Pattern { get; }
protected bool IsLastSegment()
{
return Frame.SegmentIndex == Pattern.Segments.Count - 1;
}
protected bool TestMatchingSegment(string value)
{
if (Frame.SegmentIndex >= Pattern.Segments.Count)
{
return false;
}
return Pattern.Segments[Frame.SegmentIndex].Match(value);
}
protected string CalculateStem(FileInfoBase matchedFile)
{
return MatcherContext.CombinePath(Frame.Stem, matchedFile.Name);
}
}
}