6 |
// author: Dan Lewis (dlewis@gmx.co.uk) |
// author: Dan Lewis (dlewis@gmx.co.uk) |
7 |
// (c) 2002 |
// (c) 2002 |
8 |
|
|
9 |
|
// |
10 |
|
// Permission is hereby granted, free of charge, to any person obtaining |
11 |
|
// a copy of this software and associated documentation files (the |
12 |
|
// "Software"), to deal in the Software without restriction, including |
13 |
|
// without limitation the rights to use, copy, modify, merge, publish, |
14 |
|
// distribute, sublicense, and/or sell copies of the Software, and to |
15 |
|
// permit persons to whom the Software is furnished to do so, subject to |
16 |
|
// the following conditions: |
17 |
|
// |
18 |
|
// The above copyright notice and this permission notice shall be |
19 |
|
// included in all copies or substantial portions of the Software. |
20 |
|
// |
21 |
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
22 |
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
23 |
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
24 |
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
25 |
|
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
26 |
|
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
27 |
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
28 |
|
// |
29 |
|
|
30 |
using System; |
using System; |
31 |
using System.Text; |
using System.Text; |
32 |
using System.Collections; |
using System.Collections; |
44 |
|
|
45 |
public delegate string MatchEvaluator (Match match); |
public delegate string MatchEvaluator (Match match); |
46 |
|
|
47 |
|
delegate void MatchAppendEvaluator (Match match, StringBuilder sb); |
48 |
|
|
49 |
[Flags] |
[Flags] |
50 |
public enum RegexOptions { |
public enum RegexOptions { |
51 |
None = 0x000, |
None = 0x000, |
60 |
CultureInvariant = 0x200 |
CultureInvariant = 0x200 |
61 |
} |
} |
62 |
|
|
|
#if CONFIG_SERIALIZATION |
|
63 |
[Serializable] |
[Serializable] |
64 |
public class Regex : ISerializable { |
public class Regex : ISerializable { |
|
#else |
|
|
public class Regex { |
|
|
#endif // CONFIG_SERIALIZATION |
|
|
#if CONFIG_SERIALIZATION_EMIT |
|
65 |
public static void CompileToAssembly |
public static void CompileToAssembly |
66 |
(RegexCompilationInfo[] regexes, AssemblyName aname) |
(RegexCompilationInfo[] regexes, AssemblyName aname) |
67 |
{ |
{ |
72 |
(RegexCompilationInfo[] regexes, AssemblyName aname, |
(RegexCompilationInfo[] regexes, AssemblyName aname, |
73 |
CustomAttributeBuilder[] attribs) |
CustomAttributeBuilder[] attribs) |
74 |
{ |
{ |
75 |
Regex.CompileToAssembly(regexes, aname, attribs, null); |
Regex.CompileToAssembly(regexes, aname, attribs, null); |
76 |
} |
} |
77 |
|
|
78 |
|
[TODO] |
79 |
public static void CompileToAssembly |
public static void CompileToAssembly |
80 |
(RegexCompilationInfo[] regexes, AssemblyName aname, |
(RegexCompilationInfo[] regexes, AssemblyName aname, |
81 |
CustomAttributeBuilder[] attribs, string resourceFile) |
CustomAttributeBuilder[] attribs, string resourceFile) |
82 |
{ |
{ |
83 |
throw new Exception ("Not fully implemented."); |
throw new NotImplementedException (); |
84 |
// TODO : Make use of attribs and resourceFile parameters |
// TODO : Make use of attribs and resourceFile parameters |
85 |
/* |
/* |
86 |
AssemblyBuilder asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (aname, AssemblyBuilderAccess.RunAndSave); |
AssemblyBuilder asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (aname, AssemblyBuilderAccess.RunAndSave); |
110 |
asmBuilder.Save(aname.Name); |
asmBuilder.Save(aname.Name); |
111 |
*/ |
*/ |
112 |
} |
} |
|
#endif // CONFIG_REFLECTION_EMIT |
|
113 |
|
|
114 |
public static string Escape (string str) { |
public static string Escape (string str) { |
115 |
return Parser.Escape (str); |
return Parser.Escape (str); |
233 |
} |
} |
234 |
} |
} |
235 |
|
|
236 |
#if CONFIG_SERIALIZATION |
private Regex (SerializationInfo info, StreamingContext context) : |
|
protected Regex (SerializationInfo info, StreamingContext context) : |
|
237 |
this (info.GetString ("pattern"), |
this (info.GetString ("pattern"), |
238 |
(RegexOptions) info.GetValue ("roptions", typeof (RegexOptions))) { |
(RegexOptions) info.GetValue ("options", typeof (RegexOptions))) { |
239 |
} |
} |
|
#endif // CONFIG_SERIALIZATION |
|
240 |
|
|
241 |
|
// fixes public API signature |
242 |
|
~Regex () |
243 |
|
{ |
244 |
|
} |
245 |
|
|
246 |
// public instance properties |
// public instance properties |
247 |
|
|
326 |
} |
} |
327 |
|
|
328 |
public MatchCollection Matches (string input, int startat) { |
public MatchCollection Matches (string input, int startat) { |
|
MatchCollection ms = new MatchCollection (); |
|
329 |
Match m = Match (input, startat); |
Match m = Match (input, startat); |
330 |
while (m.Success) { |
return new MatchCollection (m); |
|
ms.Add (m); |
|
|
m = m.NextMatch (); |
|
|
} |
|
|
|
|
|
return ms; |
|
331 |
} |
} |
332 |
|
|
333 |
// replace methods |
// replace methods |
346 |
return Replace (input, evaluator, count, 0); |
return Replace (input, evaluator, count, 0); |
347 |
} |
} |
348 |
|
|
349 |
|
class Adapter |
350 |
|
{ |
351 |
|
MatchEvaluator ev; |
352 |
|
public Adapter (MatchEvaluator ev) { this.ev = ev; } |
353 |
|
public void Evaluate (Match m, StringBuilder sb) { sb.Append (ev (m)); } |
354 |
|
} |
355 |
|
|
356 |
public string Replace (string input, MatchEvaluator evaluator, int count, int startat) |
public string Replace (string input, MatchEvaluator evaluator, int count, int startat) |
357 |
{ |
{ |
358 |
|
Adapter a = new Adapter (evaluator); |
359 |
|
return Replace (input, new MatchAppendEvaluator (a.Evaluate), count, startat); |
360 |
|
} |
361 |
|
|
362 |
|
string Replace (string input, MatchAppendEvaluator evaluator, int count, int startat) |
363 |
|
{ |
364 |
StringBuilder result = new StringBuilder (); |
StringBuilder result = new StringBuilder (); |
365 |
int ptr = startat; |
int ptr = startat; |
366 |
|
int counter = count; |
367 |
|
|
368 |
|
result.Append (input, 0, ptr); |
369 |
|
|
370 |
Match m = Match (input, startat); |
Match m = Match (input, startat); |
371 |
while (m.Success && count -- > 0) { |
while (m.Success) { |
372 |
result.Append (input.Substring (ptr, m.Index - ptr)); |
if (count != -1) |
373 |
result.Append (evaluator (m)); |
if(counter -- <= 0) |
374 |
|
break; |
375 |
|
result.Append (input, ptr, m.Index - ptr); |
376 |
|
evaluator (m, result); |
377 |
|
|
378 |
ptr = m.Index + m.Length; |
ptr = m.Index + m.Length; |
379 |
m = m.NextMatch (); |
m = m.NextMatch (); |
380 |
} |
} |
381 |
result.Append (input.Substring (ptr)); |
|
382 |
|
if (ptr == 0) |
383 |
|
return input; |
384 |
|
|
385 |
|
result.Append (input, ptr, input.Length - ptr); |
386 |
|
|
387 |
return result.ToString (); |
return result.ToString (); |
388 |
} |
} |
403 |
|
|
404 |
public string Replace (string input, string replacement, int count, int startat) { |
public string Replace (string input, string replacement, int count, int startat) { |
405 |
ReplacementEvaluator ev = new ReplacementEvaluator (this, replacement); |
ReplacementEvaluator ev = new ReplacementEvaluator (this, replacement); |
406 |
return Replace (input, new MatchEvaluator (ev.Evaluate), count, startat); |
return Replace (input, new MatchAppendEvaluator (ev.EvaluateAppend), count, startat); |
407 |
} |
} |
408 |
|
|
409 |
// split methods |
// split methods |
428 |
count = Int32.MaxValue; |
count = Int32.MaxValue; |
429 |
|
|
430 |
int ptr = startat; |
int ptr = startat; |
431 |
|
Match m = null; |
432 |
while (--count > 0) { |
while (--count > 0) { |
433 |
Match m = Match (input, ptr); |
if (m != null) |
434 |
|
m = m.NextMatch (); |
435 |
|
else |
436 |
|
m = Match (input, ptr); |
437 |
|
|
438 |
if (!m.Success) |
if (!m.Success) |
439 |
break; |
break; |
440 |
|
|
472 |
} |
} |
473 |
|
|
474 |
// MS undocummented method |
// MS undocummented method |
475 |
|
[TODO] |
476 |
protected void InitializeReferences() { |
protected void InitializeReferences() { |
477 |
throw new Exception ("Not implemented."); |
throw new NotImplementedException (); |
478 |
} |
} |
479 |
|
|
480 |
|
[TODO] |
481 |
protected bool UseOptionC(){ |
protected bool UseOptionC(){ |
482 |
throw new Exception ("Not implemented."); |
throw new NotImplementedException (); |
483 |
} |
} |
484 |
|
|
485 |
|
[TODO] |
486 |
protected bool UseOptionR(){ |
protected bool UseOptionR(){ |
487 |
throw new Exception ("Not implemented."); |
throw new NotImplementedException (); |
488 |
} |
} |
489 |
|
|
490 |
// object methods |
// object methods |
493 |
return pattern; |
return pattern; |
494 |
} |
} |
495 |
|
|
|
#if CONFIG_SERIALIZATION |
|
496 |
// ISerializable interface |
// ISerializable interface |
497 |
void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context) { |
void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context) { |
498 |
info.AddValue ("pattern", this.ToString (), typeof (string)); |
info.AddValue ("pattern", this.ToString (), typeof (string)); |
499 |
info.AddValue ("roptions", this.Options, typeof (RegexOptions)); |
info.AddValue ("options", this.Options, typeof (RegexOptions)); |
500 |
} |
} |
|
#endif // CONFIG_SERIALIZATION |
|
501 |
|
|
502 |
// internal |
// internal |
503 |
|
|
522 |
protected internal RegexOptions roptions; |
protected internal RegexOptions roptions; |
523 |
|
|
524 |
// MS undocumented members |
// MS undocumented members |
525 |
|
[TODO] |
526 |
protected internal System.Collections.Hashtable capnames; |
protected internal System.Collections.Hashtable capnames; |
527 |
protected internal System.Collections.Hashtable cap; |
[TODO] |
528 |
|
protected internal System.Collections.Hashtable caps; |
529 |
|
[TODO] |
530 |
protected internal int capsize; |
protected internal int capsize; |
531 |
protected internal string[] caplist; |
[TODO] |
532 |
|
protected internal string[] capslist; |
533 |
|
[TODO] |
534 |
protected internal RegexRunnerFactory factory; |
protected internal RegexRunnerFactory factory; |
535 |
} |
} |
536 |
|
|
537 |
[Serializable] |
[Serializable] |
538 |
public class RegexCompilationInfo { |
public class RegexCompilationInfo { |
539 |
public RegexCompilationInfo (string pattern, RegexOptions options, string name, string full_namespace, bool is_public) { |
public RegexCompilationInfo (string pattern, RegexOptions options, string name, string nspace, bool isPublic) |
540 |
|
{ |
541 |
this.pattern = pattern; |
this.pattern = pattern; |
542 |
this.options = options; |
this.options = options; |
543 |
this.name = name; |
this.name = name; |
544 |
this.full_namespace = full_namespace; |
this.nspace = nspace; |
545 |
this.is_public = is_public; |
this.isPublic = isPublic; |
546 |
} |
} |
547 |
|
|
548 |
public bool IsPublic { |
public bool IsPublic { |
549 |
get { return is_public; } |
get { return isPublic; } |
550 |
set { is_public = value; } |
set { isPublic = value; } |
551 |
} |
} |
552 |
|
|
553 |
public string Name { |
public string Name { |
556 |
} |
} |
557 |
|
|
558 |
public string Namespace { |
public string Namespace { |
559 |
get { return full_namespace; } |
get { return nspace; } |
560 |
set { full_namespace = value; } |
set { nspace = value; } |
561 |
} |
} |
562 |
|
|
563 |
public RegexOptions Options { |
public RegexOptions Options { |
572 |
|
|
573 |
// private |
// private |
574 |
|
|
575 |
private string pattern, name, full_namespace; |
private string pattern, name, nspace; |
576 |
private RegexOptions options; |
private RegexOptions options; |
577 |
private bool is_public; |
private bool isPublic; |
578 |
} |
} |
579 |
} |
} |