In .:
[mono.git] / mcs / class / System / System.Text.RegularExpressions / replace.cs
1 //
2 // assembly:    System
3 // namespace:   System.Text.RegularExpressions
4 // file:        replace.cs
5 //
6 // author:      Dan Lewis (dlewis@gmx.co.uk)
7 //              (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;
31 using System.Text;
32 using System.Collections;
33
34 using Parser = System.Text.RegularExpressions.Syntax.Parser;
35
36 namespace System.Text.RegularExpressions {
37
38         class ReplacementEvaluator {
39                 public static string Evaluate (string replacement, Match match) {
40                         ReplacementEvaluator ev = new ReplacementEvaluator (match.Regex, replacement);
41                         return ev.Evaluate (match);
42                 }
43
44                 public ReplacementEvaluator (Regex regex, string replacement) {
45                         this.regex = regex;
46                         this.replacement = replacement;
47                         this.pieces = null;
48                         this.n_pieces = 0;
49                         Compile ();
50                 }
51
52                 public string Evaluate (Match match) 
53                 {
54                         StringBuilder sb = new StringBuilder ();
55                         EvaluateAppend (match, sb);
56                         return sb.ToString ();
57                 }
58
59                 public void EvaluateAppend (Match match, StringBuilder sb)
60                 {
61                         int i = 0, k, count;
62
63                         if (n_pieces == 0) {
64                                 sb.Append (replacement);
65                                 return;
66                         }
67
68                         while (i < n_pieces) {
69                                 k = pieces [i++];
70                                 if (k >= 0) {
71                                         count = pieces [i++];
72                                         sb.Append (replacement, k, count);
73                                 } else if (k < -3) {
74                                         Group group = match.Groups [-(k + 4)];
75                                         sb.Append (group.Text, group.Index, group.Length);
76                                 } else if (k == -1) {
77                                         sb.Append (match.Text);
78                                 } else if (k == -2) {
79                                         sb.Append (match.Text, 0, match.Index);
80                                 } else { // k == -3
81                                         int matchend = match.Index + match.Length;
82                                         sb.Append (match.Text, matchend, match.Text.Length - matchend);
83                                 } 
84                         }
85                 }
86
87                 void Ensure (int size)
88                 {
89                         int new_size;
90                         if (pieces == null) {
91                                 new_size = 4;
92                                 if (new_size < size)
93                                         new_size = size;
94                                 pieces = new int [new_size];
95                         } else if (size >= pieces.Length) {
96                                 new_size = pieces.Length + (pieces.Length >> 1);
97                                 if (new_size < size)
98                                         new_size = size;
99                                 int [] new_pieces = new int [new_size];
100                                 Array.Copy (pieces, new_pieces, n_pieces);
101                                 pieces = new_pieces;
102                         }
103                 }
104
105                 void AddFromReplacement (int start, int end)
106                 {
107                         if (start == end)
108                                 return;
109                         Ensure (n_pieces + 2);
110                         pieces [n_pieces++] = start;
111                         pieces [n_pieces++] = end - start;
112                 }
113
114                 void AddInt (int i)
115                 {
116                         Ensure (n_pieces + 1);
117                         pieces [n_pieces++] = i;
118                 }
119
120                 // private
121                 private void Compile () {
122                         replacement = Parser.Unescape (replacement);
123
124                         int anchor = 0, ptr = 0, saveptr;
125                         char c;
126                         while (ptr < replacement.Length) {
127                                 c = replacement [ptr++];
128
129                                 if (c != '$')
130                                         continue;
131
132                                 // If the '$' was the last character, just emit it as is
133                                 if (ptr == replacement.Length)
134                                         break;
135
136                                 // If we saw a '$$'
137                                 if (replacement [ptr] == '$') {
138                                         // Everthing from 'anchor' upto and including the first '$' is copied from the replacement string
139                                         AddFromReplacement (anchor, ptr);
140                                         // skip over the second '$'.
141                                         anchor = ++ptr;
142                                         continue;
143                                 }
144
145                                 saveptr = ptr - 1;
146
147                                 int from_match = CompileTerm (ref ptr);
148
149                                 // We couldn't recognize the term following the '$'.  Just treat it as a literal.
150                                 // 'ptr' has already been advanced, no need to rewind it back
151                                 if (from_match >= 0)
152                                         continue;
153
154                                 AddFromReplacement (anchor, saveptr);
155                                 AddInt (from_match);
156                                 anchor = ptr;
157                         }
158
159                         // If we never needed to advance anchor, it means the result is the whole replacement string.
160                         // We optimize that case by never allocating the pieces array.
161                         if (anchor != 0)
162                                 AddFromReplacement (anchor, ptr);
163                 }
164
165                 private int CompileTerm (ref int ptr) {
166                         char c = replacement [ptr];
167
168                         if (Char.IsDigit (c)) {         // numbered group
169                                 int n = Parser.ParseDecimal (replacement, ref ptr);
170                                 if (n < 0 || n > regex.GroupCount)
171                                         return 0;
172                                 
173                                 return -n - 4;
174                         }
175                         
176                         ++ ptr;
177
178                         switch (c) {
179                         case '{': {                     // named group
180                                 string name;
181                                 int n = -1;
182
183                                 try {
184                                         // The parser is written such that there are few explicit range checks
185                                         // and depends on 'IndexOutOfRangeException' being thrown.
186
187                                         if (Char.IsDigit (replacement [ptr])) {
188                                                 n = Parser.ParseDecimal (replacement, ref ptr);
189                                                 name = "";
190                                         } else {
191                                                 name = Parser.ParseName (replacement, ref ptr);
192                                         }
193                                 } catch (IndexOutOfRangeException) {
194                                         ptr = replacement.Length;
195                                         return 0;
196                                 }
197
198                                 if (ptr == replacement.Length || replacement[ptr] != '}' || name == null)
199                                         return 0;
200                                 ++ptr;                  // Swallow the '}'
201
202                                 if (name != "")
203                                         n = regex.GroupNumberFromName (name);
204
205                                 if (n < 0 || n > regex.GroupCount)
206                                         return 0;
207
208                                 return -n - 4;
209                         }
210
211                         case '&':                       // entire match.  Value should be same as $0
212                                 return -4;
213
214                         case '`':                       // text before match
215                                 return -2;
216
217                         case '\'':                      // text after match
218                                 return -3;
219
220                         case '+':                       // last group
221                                 return -regex.GroupCount - 4;
222
223                         case '_':                       // entire text
224                                 return -1;
225
226                         default:
227                                 return 0;
228                         }
229                 }
230
231                 private Regex regex;
232                 int n_pieces;
233                 private int [] pieces;
234                 string replacement;
235         }
236 }