-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
TpnSectionHeader.cs
175 lines (145 loc) · 5.66 KB
/
TpnSectionHeader.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// 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 System.Linq;
using System.Text.RegularExpressions;
namespace Microsoft.DotNet.Build.Tasks
{
public class TpnSectionHeader
{
private static readonly char[] SectionSeparatorChars = { '-', '=' };
private static readonly Regex NumberListPrefix = new Regex(@"^[0-9]+\.\t(?<name>.*)$");
public static bool IsSeparatorLine(string line)
{
return line.Length > 2 && line.All(c => SectionSeparatorChars.Contains(c));
}
public static IEnumerable<TpnSectionHeader> ParseAll(string[] lines)
{
// A separator line can't represent a section if it's on the first or last few lines.
for (int i = 1; i < lines.Length - 2; i++)
{
string lineAbove = lines[i - 1].Trim();
string line = lines[i].Trim();
string lineBelow = lines[i + 1].Trim();
if (line.Length > 2 &&
IsSeparatorLine(line) &&
string.IsNullOrEmpty(lineBelow))
{
// 'line' is a separator line. Check around to see what kind it is.
if (string.IsNullOrEmpty(lineAbove))
{
var header = ParseSeparatedOrNull(lines, i);
if (header != null)
{
yield return header;
}
}
else
{
var header = ParseUnderlined(lines, i);
yield return header;
}
}
var numberedHeader = ParseNumberedOrNull(lines, i);
if (numberedHeader != null)
{
yield return numberedHeader;
}
}
}
public string Name { get; set; }
public string SeparatorLine { get; set; }
public TpnSectionHeaderFormat Format { get; set; }
public int StartLine { get; set; }
public int LineLength { get; set; }
public string SingleLineName => Name.Replace('\n', ' ').Replace('\r', ' ');
public override string ToString()
{
switch (Format)
{
case TpnSectionHeaderFormat.Separated:
return
SeparatorLine + Environment.NewLine +
Environment.NewLine +
Name;
case TpnSectionHeaderFormat.Underlined:
return
Name + Environment.NewLine +
SeparatorLine;
case TpnSectionHeaderFormat.Numbered:
return SeparatorLine;
default:
throw new ArgumentOutOfRangeException();
}
}
private static TpnSectionHeader ParseSeparatedOrNull(string[] lines, int i)
{
string[] nameLines = lines
.Skip(i + 2)
.TakeWhile(s => !string.IsNullOrWhiteSpace(s))
.Select(s => s.Trim())
.ToArray();
string name = string.Join(Environment.NewLine, nameLines);
// If there's a separator line as the last line in the name, this line doesn't indicate
// a section. It needs to be handled by ParseUnderlined instead.
if (nameLines.Any(IsSeparatorLine))
{
if (nameLines.Take(nameLines.Length - 1).Any(IsSeparatorLine))
{
throw new ArgumentException(
$"Separator line detected inside name '{name}'");
}
}
else
{
return new TpnSectionHeader
{
Name = name,
SeparatorLine = lines[i],
Format = TpnSectionHeaderFormat.Separated,
StartLine = i,
LineLength = 2 + nameLines.Length
};
}
return null;
}
private static TpnSectionHeader ParseUnderlined(string[] lines, int i)
{
string[] nameLines = lines
.Take(i)
.Reverse()
.TakeWhile(s => !string.IsNullOrWhiteSpace(s))
.Reverse()
.Select(s => s.Trim())
.ToArray();
int nameStartLine = i - nameLines.Length;
return new TpnSectionHeader
{
Name = string.Join(Environment.NewLine, nameLines),
SeparatorLine = lines[i],
Format = TpnSectionHeaderFormat.Underlined,
StartLine = nameStartLine,
LineLength = nameLines.Length + 1
};
}
private static TpnSectionHeader ParseNumberedOrNull(string[] lines, int i)
{
Match numberListMatch;
if (string.IsNullOrWhiteSpace(lines[i - 1]) &&
string.IsNullOrWhiteSpace(lines[i + 1]) &&
(numberListMatch = NumberListPrefix.Match(lines[i])).Success)
{
return new TpnSectionHeader
{
Name = numberListMatch.Groups["name"].Value,
SeparatorLine = lines[i],
Format = TpnSectionHeaderFormat.Numbered,
StartLine = i,
LineLength = 1
};
}
return null;
}
}
}