5 // author: Dan Lewis (dlewis@gmx.co.uk)
\r
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
\r
11 // Permission is hereby granted, free of charge, to any person obtaining
\r
12 // a copy of this software and associated documentation files (the
\r
13 // "Software"), to deal in the Software without restriction, including
\r
14 // without limitation the rights to use, copy, modify, merge, publish,
\r
15 // distribute, sublicense, and/or sell copies of the Software, and to
\r
16 // permit persons to whom the Software is furnished to do so, subject to
\r
17 // the following conditions:
\r
19 // The above copyright notice and this permission notice shall be
\r
20 // included in all copies or substantial portions of the Software.
\r
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
32 using System.Collections;
\r
33 using System.Collections.Specialized;
\r
35 namespace System.Text.RegularExpressions
\r
37 abstract class BaseMachine : IMachine
\r
39 internal delegate void MatchAppendEvaluator (Match match, StringBuilder sb);
\r
41 public virtual string Replace (Regex regex, string input, string replacement, int count, int startat)
\r
43 ReplacementEvaluator ev = new ReplacementEvaluator (regex, replacement);
\r
44 if (regex.RightToLeft)
\r
45 return RTLReplace (regex, input, new MatchEvaluator (ev.Evaluate), count, startat);
\r
47 return LTRReplace (regex, input, new MatchAppendEvaluator (ev.EvaluateAppend), count, startat, ev.NeedsGroupsOrCaptures);
\r
50 virtual public string [] Split (Regex regex, string input, int count, int startat)
\r
52 ArrayList splits = new ArrayList ();
\r
54 count = Int32.MaxValue;
\r
58 while (--count > 0) {
\r
62 m = regex.Match (input, ptr);
\r
67 if (regex.RightToLeft)
\r
68 splits.Add (input.Substring (m.Index + m.Length, ptr - m.Index - m.Length));
\r
70 splits.Add (input.Substring (ptr, m.Index - ptr));
\r
72 int gcount = m.Groups.Count;
\r
73 for (int gindex = 1; gindex < gcount; gindex++) {
\r
74 Group grp = m.Groups [gindex];
\r
76 splits.Add (input.Substring (grp.Index, grp.Length));
\r
79 if (regex.RightToLeft)
\r
82 ptr = m.Index + m.Length;
\r
86 if (regex.RightToLeft && ptr >= 0)
\r
87 splits.Add (input.Substring (0, ptr));
\r
88 if (!regex.RightToLeft && ptr <= input.Length)
\r
89 splits.Add (input.Substring (ptr));
\r
91 return (string []) splits.ToArray (typeof (string));
\r
94 virtual public Match Scan (Regex regex, string text, int start, int end)
\r
96 throw new NotImplementedException ("Scan method must be implemented in derived classes");
\r
99 virtual public string Result (string replacement, Match match)
\r
101 return ReplacementEvaluator.Evaluate (replacement, match);
\r
104 internal string LTRReplace (Regex regex, string input, MatchAppendEvaluator evaluator, int count, int startat) {
\r
105 return LTRReplace (regex, input, evaluator, count, startat, true);
\r
108 internal string LTRReplace (Regex regex, string input, MatchAppendEvaluator evaluator, int count, int startat, bool needs_groups_or_captures)
\r
110 this.needs_groups_or_captures = needs_groups_or_captures;
\r
112 Match m = Scan (regex, input, startat, input.Length);
\r
116 StringBuilder result = new StringBuilder (input.Length);
\r
118 int counter = count;
\r
120 result.Append (input, 0, ptr);
\r
124 if (counter-- <= 0)
\r
127 throw new SystemException ("how");
\r
128 result.Append (input, ptr, m.Index - ptr);
\r
129 evaluator (m, result);
\r
131 ptr = m.Index + m.Length;
\r
132 m = m.NextMatch ();
\r
133 } while (m.Success);
\r
135 result.Append (input, ptr, input.Length - ptr);
\r
137 return result.ToString ();
\r
140 internal string RTLReplace (Regex regex, string input, MatchEvaluator evaluator, int count, int startat)
\r
142 Match m = Scan (regex, input, startat, input.Length);
\r
147 int counter = count;
\r
149 var pieces = new System.Collections.Generic.List<string> ();
\r
151 StringCollection pieces = new StringCollection ();
\r
154 pieces.Add (input.Substring (ptr));
\r
158 if (counter-- <= 0)
\r
160 if (m.Index + m.Length > ptr)
\r
161 throw new SystemException ("how");
\r
162 pieces.Add (input.Substring (m.Index + m.Length, ptr - m.Index - m.Length));
\r
163 pieces.Add (evaluator (m));
\r
166 m = m.NextMatch ();
\r
167 } while (m.Success);
\r
169 StringBuilder result = new StringBuilder ();
\r
171 result.Append (input, 0, ptr);
\r
172 for (int i = pieces.Count; i > 0; )
\r
173 result.Append (pieces [--i]);
\r
177 return result.ToString ();
\r
180 // Specify whenever Match objects created by this machine need to be fully
\r
181 // built. If false, these can be omitted, avoiding some memory allocations and
\r
182 // processing time.
\r
183 protected bool needs_groups_or_captures = true;
\r