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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
//
// assembly: System
// namespace: System.Text.RegularExpressions
// file: regex.cs
//
// author: Dan Lewis (dlewis@gmx.co.uk)
// (c) 2002
using System;
using System.Text;
using System.Collections;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.Serialization;
using RegularExpression = System.Text.RegularExpressions.Syntax.RegularExpression;
using Parser = System.Text.RegularExpressions.Syntax.Parser;
namespace System.Text.RegularExpressions {
public delegate string MatchEvaluator (Match match);
[Flags]
public enum RegexOptions {
None = 0x000,
IgnoreCase = 0x001,
Multiline = 0x002,
ExplicitCapture = 0x004,
Compiled = 0x008,
Singleline = 0x010,
IgnorePatternWhitespace = 0x020,
RightToLeft = 0x040,
ECMAScript = 0x100
}
public class Regex : ISerializable {
public static void CompileToAssembly
(RegexCompilationInfo[] regexes, AssemblyName aname)
{
throw new Exception ("Not implemented.");
}
public static void CompileToAssembly
(RegexCompilationInfo[] regexes, AssemblyName aname,
CustomAttributeBuilder[] attribs)
{
throw new Exception ("Not implemented.");
}
public static void CompileToAssembly
(RegexCompilationInfo[] regexes, AssemblyName aname,
CustomAttributeBuilder[] attribs, string resourceFile)
{
throw new Exception ("Not implemented.");
}
public static string Escape (string str) {
return Parser.Escape (str);
}
public static string Unescape (string str) {
return Parser.Unescape (str);
}
public static bool IsMatch (string input, string pattern) {
Regex re = new Regex (pattern, RegexOptions.None);
return re.IsMatch (input);
}
public static Match Match (string input, string pattern) {
return Regex.Match (input, pattern, RegexOptions.None);
}
public static Match Match (string input, string pattern, RegexOptions options) {
Regex re = new Regex (pattern, options);
return re.Match (input);
}
public static string Replace
(string input, string pattern, MatchEvaluator evaluator)
{
return Regex.Replace (input, pattern, evaluator, RegexOptions.None);
}
public static string Replace
(string input, string pattern, MatchEvaluator evaluator,
RegexOptions options)
{
Regex re = new Regex (pattern, options);
return re.Replace (input, evaluator);
}
public static string Replace
(string input, string pattern, string replacement)
{
return Regex.Replace (input, pattern, replacement, RegexOptions.None);
}
public static string Replace
(string input, string pattern, string replacement,
RegexOptions options)
{
Regex re = new Regex (pattern, options);
return re.Replace (input, pattern);
}
public static string[] Split (string input, string pattern) {
return Regex.Split (input, pattern, RegexOptions.None);
}
public static string[] Split (string input, string pattern, RegexOptions options) {
Regex re = new Regex (input, options);
return re.Split (input);
}
// private
private static FactoryCache cache = new FactoryCache (200); // TODO put some meaningful number here
// constructors
protected Regex () {
// XXX what's this constructor for?
}
public Regex (string pattern) : this (pattern, RegexOptions.None) {
}
public Regex (string pattern, RegexOptions options) {
this.pattern = pattern;
this.options = options;
this.factory = cache.Lookup (pattern, options);
if (this.factory == null) {
// parse and install group mapping
Parser psr = new Parser ();
RegularExpression re = psr.ParseRegularExpression (pattern, options);
this.group_count = re.GroupCount;
this.mapping = psr.GetMapping ();
// compile
ICompiler cmp;
if ((options & RegexOptions.Compiled) != 0)
throw new Exception ("Not implemented.");
//cmp = new CILCompiler ();
else
cmp = new PatternCompiler ();
re.Compile (cmp, RightToLeft);
// install machine factory and add to pattern cache
this.factory = cmp.GetMachineFactory ();
cache.Add (pattern, options, this.factory);
}
}
// public instance properties
public RegexOptions Options {
get { return options; }
}
public bool RightToLeft {
get { return (options & RegexOptions.RightToLeft) != 0; }
}
// public instance methods
public string[] GetGroupNames () {
string[] names = new string[mapping.Count];
mapping.Keys.CopyTo (names, 0);
return names;
}
public int[] GetGroupNumbers () {
int[] numbers = new int[mapping.Count];
mapping.Values.CopyTo (numbers, 0);
return numbers;
}
public string GroupNameFromNumber (int i) {
if (i >= group_count)
return "";
foreach (string name in mapping.Keys) {
if ((int)mapping[name] == i)
return name;
}
return "";
}
public int GroupNumberFromName (string name) {
if (mapping.Contains (name))
return (int)mapping[name];
return -1;
}
// match methods
public bool IsMatch (string input) {
return IsMatch (input, 0);
}
public bool IsMatch (string input, int startat) {
return Match (input, startat).Success;
}
public Match Match (string input) {
return Match (input, 0);
}
public Match Match (string input, int startat) {
return CreateMachine ().Scan (this, input, startat, input.Length);
}
public Match Match (string input, int startat, int length) {
return CreateMachine ().Scan (this, input, startat, startat + length);
}
public MatchCollection Matches (string input) {
return Matches (input, 0);
}
public MatchCollection Matches (string input, int startat) {
MatchCollection ms = new MatchCollection ();
Match m = Match (input, startat);
while (m.Success) {
ms.Add (m);
m = m.NextMatch ();
}
return ms;
}
// replace methods
public string Replace (string input, MatchEvaluator evaluator) {
return Replace (input, evaluator, Int32.MaxValue, 0);
}
public string Replace (string input, MatchEvaluator evaluator, int count) {
return Replace (input, evaluator, count, 0);
}
public string Replace (string input, MatchEvaluator evaluator, int count, int startat)
{
StringBuilder result = new StringBuilder ();
int ptr = startat;
Match m = Match (input, startat);
while (m.Success && count -- > 0) {
result.Append (input.Substring (ptr, m.Index - ptr));
result.Append (evaluator (m));
ptr = m.Index + m.Length;
m = m.NextMatch ();
}
result.Append (input.Substring (ptr));
return result.ToString ();
}
public string Replace (string input, string replacement) {
return Replace (input, replacement, Int32.MaxValue, 0);
}
public string Replace (string input, string replacement, int count) {
return Replace (input, replacement, count, 0);
}
public string Replace (string input, string replacement, int count, int startat) {
ReplacementEvaluator ev = new ReplacementEvaluator (this, replacement);
return Replace (input, new MatchEvaluator (ev.Evaluate), count, startat);
}
// split methods
public string[] Split (string input) {
return Split (input, Int32.MaxValue, 0);
}
public string[] Split (string input, int count) {
return Split (input, count, 0);
}
public string[] Split (string input, int count, int startat) {
ArrayList splits = new ArrayList ();
if (count == 0)
count = Int32.MaxValue;
int ptr = startat;
Match m = Match (input, startat);
while (count -- > 0 && m.Success) {
splits.Add (input.Substring (ptr, m.Index - ptr));
ptr = m.Index + m.Length;
}
if (count > 0)
splits.Add (input.Substring (ptr));
string[] result = new string[splits.Count];
splits.CopyTo (result);
return result;
}
// object methods
public override string ToString () {
return pattern;
}
// ISerializable interface
public void GetObjectData (SerializationInfo info, StreamingContext context) {
throw new Exception ("Not implemented.");
}
// internal
internal int GroupCount {
get { return group_count; }
}
// private
private IMachine CreateMachine () {
return factory.NewInstance ();
}
private string pattern;
private RegexOptions options;
private IMachineFactory factory;
private IDictionary mapping;
private int group_count;
}
public class RegexCompilationInfo {
public RegexCompilationInfo (string pattern, RegexOptions options, string name, string full_namespace, bool is_public) {
this.pattern = pattern;
this.options = options;
this.name = name;
this.full_namespace = full_namespace;
this.is_public = is_public;
}
public bool IsPublic {
get { return is_public; }
set { is_public = value; }
}
public string Name {
get { return name; }
set { name = value; }
}
public string Namespace {
get { return full_namespace; }
set { full_namespace = value; }
}
public RegexOptions Options {
get { return options; }
set { options = value; }
}
public string Pattern {
get { return pattern; }
set { pattern = value; }
}
// private
private string pattern, name, full_namespace;
private RegexOptions options;
private bool is_public;
}
}
|