-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewLineConverter.cs
216 lines (191 loc) · 7.59 KB
/
NewLineConverter.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System.Collections.Generic;
namespace AndroidUITestFramework
{
class NewLineConverter
{
System.IO.TextWriter textWriter;
LinkedList<char> buffer;
List<(NewLineDetector from, string to)> conversions;
List<(NewLineDetector from, string to)> eliminated;
private static object LOCK = new();
public NewLineConverter(System.IO.TextWriter textWriter)
{
this.textWriter = textWriter;
conversions = new List<(NewLineDetector from, string to)>();
eliminated = new List<(NewLineDetector from, string to)>();
buffer = new LinkedList<char>();
}
public void addConversion(string from, string to)
{
lock (LOCK)
{
NewLineDetector conversion = new(textWriter, from);
conversions.Add((conversion, to));
conversions.Sort((a, b) =>
{
string sa = a.from.NewLine?.Invoke();
int NL_A = sa != null ? sa.Length : 0;
string sb = b.from.NewLine?.Invoke();
int NL_B = sb != null ? sb.Length : 0;
return Comparer<int>.Default.Compare(NL_A, NL_B);
});
}
}
bool flushing_input = false;
public (string str, bool isNewLine) processNext(char c)
{
lock (LOCK)
{
if (flushing_input)
{
// prevent recursion
flushing_input = false;
return ("" + c, c == '\r');
}
if (conversions.Count == 0)
{
// we have no available conversions
return ("" + c, false);
}
bool bufferScan = false;
char firstCharacter = c;
while (true)
{
// loop throuch each detectore
// keep detecting until all detectors are exhausted
bool matched = false;
bool isPartialMatch = false;
string replacement = null;
foreach ((NewLineDetector newLineDetector, string to) x in conversions)
{
if (eliminated.Contains(x))
{
// this detector has been eliminated
continue;
}
if (x.newLineDetector.processNext(firstCharacter))
{
// we got a complete match
matched = true;
replacement = "" + x.to;
break;
}
if (x.newLineDetector.getIndex() != 0)
{
// we got a partial match
isPartialMatch = true;
continue;
}
else
{
// we got no match, eliminate
eliminated.Add(x);
continue;
}
}
if (matched)
{
if (!bufferScan)
{
// we got a match, clear the buffer
buffer.Clear();
eliminated.Clear();
}
else
{
// we got a match, flush it later
}
}
else if (isPartialMatch)
{
if (!bufferScan)
{
// we got a partial match
buffer.AddLast(firstCharacter);
// nothing should be done while we have a partial match
return (null, false);
}
else
{
// we got a partial match
// do nothing and advance to next character
}
}
else
{
// we got no matches
eliminated.Clear();
if (!bufferScan && buffer.Count != 0)
{
buffer.AddLast(firstCharacter);
}
}
if (!bufferScan || !isPartialMatch)
{
// if !bufferScan
// we got a match, or we didnt get any matches at all
// if bufferScan
// we got a match or we didnt get any matches at all
// we cannot reset state during partial match in a bufferScan
// reset detector states
foreach ((NewLineDetector newLineDetector_, _) in conversions)
{
newLineDetector_.reset();
}
}
if (matched)
{
if (!bufferScan)
{
// we got a match, return its replacement
return (replacement, true);
}
else
{
// we got a match
// flush our replacement to text writer
textWriter.Write(replacement);
}
}
if (bufferScan)
{
// save first character
char saved = firstCharacter;
// remove first character to proceed to the next
buffer.RemoveFirst();
// return early if we can
if (buffer.Count == 0)
{
// buffer has become empty, we can no longer match anything
return ("" + saved, false);
}
else
{
// we still have input in the buffer
// flush our input to text writer
flushing_input = true;
textWriter.Write(saved);
// set our input to the first character
firstCharacter = buffer.First.Value;
continue;
}
}
else
{
// at this point, we didnt get a match
if (buffer.Count == 0)
{
// buffer is empty, no matches whatsoever
return ("" + firstCharacter, false);
}
// rescan the buffer
bufferScan = true;
// set our input to the first character
firstCharacter = buffer.First.Value;
continue;
}
}
}
}
}
}