-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariants.cs
194 lines (173 loc) · 4.39 KB
/
Variants.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sylphe.Utils
{
public static class Variants
{
/// <summary>
/// Generate variants of an input string based on markup:
/// the vertical bar | separates variants, brackets [...]
/// limit the scope of variants, and brackets with only one
/// variant (i.e., no vertical bar) denote an optional part.
/// Empty variants are not emitted. Brackets can be nested,
/// but should be balanced. Invalid markup may result in
/// unexpected output, but never raises an error.
/// </summary>
/// <example>
/// "foo|bar" expands into the two strings "foo" and "bar"
/// "ba[r|z]" expands into the two strings "bar" and "baz"
/// "qu[u]x" expands into "quux" and "qux"
/// "foo|ba[r|z[aar]]|quux" expands into "foo", "bar", "bazaar", "baz", "quux"
/// "[foo|]" expands to "foo" (the empty string is not emitted)
/// </example>
public static IEnumerable<string> Expand(string text)
{
if (string.IsNullOrEmpty(text))
{
return Enumerable.Empty<string>();
}
int len = text.Length;
var down = new int[len];
var side = new int[len];
int pipe = Parse(text, down, side);
var acc = new StringBuilder();
var stk = new Stack<Entry>();
if (pipe > 0) // 1st top-level pipe
{
stk.Push(new Entry(pipe+1, 0));
}
stk.Push(new Entry(0, 0));
return Traverse(text, down, side, stk, acc);
}
/// <summary>
/// Fill the <paramref name="down"/> and <paramref name="side"/>
/// arrays with indices into <paramref name="text"/>.
/// </summary>
private static int Parse(string text, int[] down, IList<int> side)
{
// posn: 0 1 2 3 4 5 6 7 8 9 A B
// text: a [ b [ c ] | d | e ] $
// down: : B : 6 : : B : B : : one after closing bracket
// down: 1 : 3 : 5 6 : 8 : A B down[i] > i for all i
// side: - 6 - - - - 8 - - - - index of next pipe
int len = text.Length;
var stk = new Stack<int>();
int firstTopLevelPipe = -1;
for (int i = 0; i < len; i++)
{
switch(text[i])
{
case '[':
stk.Push(i); // remember bracket position
break;
case '|':
if (stk.Count > 0)
{
int j = stk.Peek();
side[j] = i;
}
else
{
firstTopLevelPipe = i; // next[-1] = i
}
stk.Push(i); // remember pipe position
break;
case ']':
while (stk.Count > 0)
{
int j = stk.Pop();
down[j] = i+1;
if (text[j] == '[') break;
}
down[i] = i+1;
break;
default:
down[i] = i+1;
break;
}
}
// Implicitly close unmatched opening brackets:
while (stk.Count > 0)
{
int j = stk.Pop();
down[j] = len;
}
return firstTopLevelPipe;
}
/// <summary>
/// Traverse the DAG defined by the <paramref name="down"/>
/// and <paramref name="side"/> indices, yield variant strings.
/// </summary>
private static IEnumerable<string> Traverse(
string text, int[] down, int[] side, Stack<Entry> stk, StringBuilder acc)
{
int len = text.Length;
while (stk.Count > 0)
{
var entry = stk.Pop();
int index = entry.Index;
int reset = entry.Reset;
acc.Length = reset; // truncate
while (index < len)
{
switch (text[index])
{
case '[':
reset = acc.Length;
if (side[index] > 0) // two or more variants
{
stk.Push(new Entry(side[index]+1, reset));
index += 1;
}
else // optional part: treat "[x]" as "[|x]"
{
stk.Push(new Entry(index+1, reset));
index = down[index];
}
break;
case '|':
if (side[index] > 0)
{
stk.Push(new Entry(side[index]+1, reset));
stk.Exchange(); // ensure variant ordering
}
index = down[index];
break;
case ']':
index += 1;
break;
default:
acc.Append(text[index]);
index += 1;
break;
}
}
if (acc.Length > 0)
{
yield return acc.ToString();
}
}
}
/// <summary>
/// Exchange the top two stack entries.
/// </summary>
private static void Exchange<T>(this Stack<T> stack)
{
var a = stack.Pop();
var b = stack.Pop();
stack.Push(a);
stack.Push(b);
}
private struct Entry
{
public readonly int Index;
public readonly int Reset;
public Entry(int index, int reset)
{
Index = index;
Reset = reset;
}
}
}
}