[re]-merged in Brians stuff
[org.ibex.js.git] / src / org / ibex / js / JSRegexp.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.ibex.js;
3
4 /** A JavaScript regular expression object */
5 public class JSRegexp extends JS {
6     private boolean global;
7     private GnuRegexp.RE re;
8     private int lastIndex;
9
10     private JS pattern;
11     private int flags;
12     
13     public JSRegexp(JS arg0, JS arg1) throws JSExn {
14         if(arg0 instanceof JSRegexp) {
15             JSRegexp r = (JSRegexp) arg0;
16             this.global = r.global;
17             this.re = r.re;
18             this.lastIndex = r.lastIndex;
19             this.pattern = pattern;
20             this.flags = flags;
21         } else {
22             String pattern = JS.toString(arg0);
23             String sFlags = null;
24             int flags = 0;
25             if(arg1 != null) sFlags = JS.toString(arg1);
26             if(sFlags == null) sFlags = "";
27             for(int i=0;i<sFlags.length();i++) {
28                 switch(sFlags.charAt(i)) {
29                     case 'i': flags |= GnuRegexp.RE.REG_ICASE; break;
30                     case 'm': flags |= GnuRegexp.RE.REG_MULTILINE; break;
31                     case 'g': global = true; break;
32                     default: throw new JSExn("Invalid flag in regexp \"" + sFlags.charAt(i) + "\"");
33                 }
34             }
35             re = newRE(pattern,flags);
36             this.pattern = JS.S(pattern);
37             this.flags = flags;
38         }
39     }
40
41     public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
42         switch(nargs) {
43             case 1: {
44                 //#switch(JS.toString(method))
45                 case "exec": {
46                     String s = JS.toString(a0);
47                     int start = global ? lastIndex : 0;
48                     if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
49                     GnuRegexp.REMatch match = re.getMatch(s,start);
50                     if(global) lastIndex = match == null ? s.length() : match.getEndIndex();
51                     return match == null ? null : matchToExecResult(match,re,s);
52                 }
53                 case "test": {
54                     String s = JS.toString(a0);
55                     if (!global) return B(re.getMatch(s) != null);
56                     int start = global ? lastIndex : 0;
57                     if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
58                     GnuRegexp.REMatch match = re.getMatch(s,start);
59                     lastIndex = match != null ? s.length() : match.getEndIndex();
60                     return B(match != null);
61                 }
62                 case "toString": return JS.S(a0.coerceToString());
63                 case "stringMatch": return stringMatch(a0,a1);
64                 case "stringSearch": return stringSearch(a0,a1);
65                 //#end
66                 break;
67             }
68             case 2: {
69                 //#switch(JS.toString(method))
70                 case "stringReplace": return stringReplace(a0, a1,a2);
71                 //#end
72                 break;
73             }
74         }
75         return super.callMethod(method, a0, a1, a2, rest, nargs);
76     }
77     
78     public JS get(JS key) throws JSExn {
79         //#switch(JS.toString(key))
80         case "exec": return METHOD;
81         case "test": return METHOD;
82         case "toString": return METHOD;
83         case "lastIndex": return N(lastIndex);
84         case "source": return pattern;
85         case "global": return JS.B(global);
86         case "ignoreCase": return B(flags & GnuRegexp.RE.REG_ICASE);
87         case "multiline": return B(flags & GnuRegexp.RE.REG_MULTILINE);
88         //#end
89         return super.get(key);
90     }
91     
92     public void put(JS key, JS value) throws JSExn {
93         if(JS.isString(key)) {
94             if(JS.toString(key).equals("lastIndex")) {
95                 lastIndex = JS.toInt(value);
96                 return;
97             }
98         }
99         super.put(key,value);
100     }
101   
102     private static JS matchToExecResult(GnuRegexp.REMatch match, GnuRegexp.RE re, String s) {
103         try {
104             JS ret = new JS.O();
105             ret.put(JS.S("index"), N(match.getStartIndex()));
106             ret.put(JS.S("input"),JS.S(s));
107             int n = re.getNumSubs();
108             ret.put(JS.S("length"), N(n+1));
109             ret.put(ZERO,JS.S(match.toString()));
110             for(int i=1;i<=n;i++) ret.put(JS.N(i),JS.S(match.toString(i)));
111             return ret;
112         } catch (JSExn e) {
113             throw new Error("this should never happen");
114         }
115     }
116     
117     String coerceToString() {
118         StringBuffer sb = new StringBuffer();
119         sb.append('/');
120         sb.append(pattern);
121         sb.append('/');
122         if(global) sb.append('g');
123         if((flags & GnuRegexp.RE.REG_ICASE) != 0) sb.append('i');
124         if((flags & GnuRegexp.RE.REG_MULTILINE) != 0) sb.append('m');
125         return sb.toString();
126     }
127     
128     static JS stringMatch(JS o, JS arg0) throws JSExn {
129         String s = JS.toString(o);
130         GnuRegexp.RE re;
131         JSRegexp regexp = null;
132         if(arg0 instanceof JSRegexp) {
133             regexp = (JSRegexp) arg0;
134             re = regexp.re;
135         } else {
136             re = newRE(JS.toString(arg0),0);
137         }
138         
139         if(regexp == null) {
140             GnuRegexp.REMatch match = re.getMatch(s);
141             return matchToExecResult(match,re,s);
142         }
143         if(!regexp.global) return regexp.callMethod(JS.S("exec"), o, null, null, null, 1);
144         
145         JSArray ret = new JSArray();
146         GnuRegexp.REMatch[] matches = re.getAllMatches(s);
147         for(int i=0;i<matches.length;i++) ret.addElement(JS.S(matches[i].toString()));
148         regexp.lastIndex = matches.length > 0 ? matches[matches.length-1].getEndIndex() : s.length();
149         return ret;
150     }
151     
152     static JS stringSearch(JS o, JS arg0) throws JSExn  {
153         String s = JS.toString(o);
154         GnuRegexp.RE re = arg0 instanceof JSRegexp ? ((JSRegexp)arg0).re : newRE(JS.toString(arg0),0);
155         GnuRegexp.REMatch match = re.getMatch(s);
156         return match == null ? N(-1) : N(match.getStartIndex());
157     }
158     
159     static JS stringReplace(JS o, JS arg0, JS arg1) throws JSExn {
160         String s = JS.toString(o);
161         GnuRegexp.RE re;
162         JSFunction replaceFunc = null;
163         String replaceString = null;
164         JSRegexp regexp = null;
165         if(arg0 instanceof JSRegexp) {
166             regexp = (JSRegexp) arg0;
167             re = regexp.re;
168         } else {
169             re = newRE(arg0.toString(),0);
170         }
171         if(arg1 instanceof JSFunction)
172             replaceFunc = (JSFunction) arg1;
173         else
174             replaceString = JS.toString(arg1);
175         GnuRegexp.REMatch[] matches;
176         if(regexp != null && regexp.global) {
177             matches = re.getAllMatches(s);
178             if(regexp != null) {
179                 if(matches.length > 0)
180                     regexp.lastIndex = matches[matches.length-1].getEndIndex();
181                 else
182                     regexp.lastIndex = s.length();
183             }
184         } else {
185             GnuRegexp.REMatch match = re.getMatch(s);
186             if(match != null)
187                 matches = new GnuRegexp.REMatch[]{ match };
188             else
189                 matches = new GnuRegexp.REMatch[0];
190         }
191         
192         StringBuffer sb = new StringBuffer(s.length());
193         int pos = 0;
194         char[] sa = s.toCharArray();
195         for(int i=0;i<matches.length;i++) {
196             GnuRegexp.REMatch match = matches[i];
197             sb.append(sa,pos,match.getStartIndex()-pos);
198             pos = match.getEndIndex();
199             if(replaceFunc != null) {
200                 int n = (regexp == null ? 0 : re.getNumSubs());
201                 int numArgs = 3 + n;
202                 JS[] rest = new JS[numArgs - 3];
203                 JS a0 = JS.S(match.toString());
204                 JS a1 = null;
205                 JS a2 = null;
206                 for(int j=1;j<=n;j++)
207                     switch(j) {
208                         case 1: a1 = JS.S(match.toString(j)); break;
209                         case 2: a2 = JS.S(match.toString(j)); break;
210                         default: rest[j - 3] = JS.S(match.toString(j)); break;
211                     }
212                 switch(numArgs) {
213                     case 3:
214                         a1 = N(match.getStartIndex());
215                         a2 = JS.S(s);
216                         break;
217                     case 4:
218                         a2 = N(match.getStartIndex());
219                         rest[0] = JS.S(s);
220                         break;
221                     default:
222                         rest[rest.length - 2] = N(match.getStartIndex());
223                         rest[rest.length - 1] = JS.S(s);
224                 }
225
226                 // note: can't perform pausing operations in here
227                 sb.append(JS.toString(replaceFunc.call(a0, a1, a2, rest, numArgs)));
228
229             } else {
230                 sb.append(mySubstitute(match,replaceString,s));
231             }
232         }
233         int end = matches.length == 0 ? 0 : matches[matches.length-1].getEndIndex();
234         sb.append(sa,end,sa.length-end);
235         return JS.S(sb.toString());
236     }
237     
238     private static String mySubstitute(GnuRegexp.REMatch match, String s, String source) {
239         StringBuffer sb = new StringBuffer();
240         int i,n;
241         char c,c2;
242         for(i=0;i<s.length()-1;i++) {
243            c = s.charAt(i);
244             if(c != '$') {
245                 sb.append(c);
246                 continue;
247             }
248             i++;
249             c = s.charAt(i);
250             switch(c) {
251                 case '0': case '1': case '2': case '3': case '4':
252                 case '5': case '6': case '7': case '8': case '9':
253                     if(i < s.length()-1 && (c2 = s.charAt(i+1)) >= '0' && c2 <= '9') {
254                         n = (c - '0') * 10 + (c2 - '0');
255                         i++;
256                     } else {
257                         n = c - '0';
258                     }
259                     if(n > 0)
260                         sb.append(match.toString(n));
261                     break;
262                 case '$':
263                     sb.append('$'); break;
264                 case '&':
265                     sb.append(match.toString()); break;
266                 case '`':
267                     sb.append(source.substring(0,match.getStartIndex())); break;
268                 case '\'':
269                     sb.append(source.substring(match.getEndIndex())); break;
270                 default:
271                     sb.append('$');
272                     sb.append(c);
273             }
274         }
275         if(i < s.length()) sb.append(s.charAt(i));
276         return sb.toString();
277     }
278                     
279     
280     static JS stringSplit(JS s_, JS arg0, JS arg1, int nargs) throws JSExn {
281         String s = JS.toString(s_);
282         int limit = nargs < 2 ? Integer.MAX_VALUE : JS.toInt(arg1);
283         if(limit < 0) limit = Integer.MAX_VALUE;
284         if(limit == 0) return new JSArray();
285         
286         GnuRegexp.RE re = null;
287         JSRegexp regexp = null;
288         String sep = null;
289         JSArray ret = new JSArray();
290         int p = 0;
291         
292         if(arg0 instanceof JSRegexp) {
293             regexp = (JSRegexp) arg0;
294             re = regexp.re;
295         } else {
296             sep = JS.toString(arg0);
297         }
298         
299         // special case this for speed. additionally, the code below doesn't properly handle
300         // zero length strings
301         if(sep != null && sep.length()==0) {
302             int len = s.length();
303             for(int i=0;i<len;i++)
304                 ret.addElement(JS.S(s.substring(i,i+1)));
305             return ret;
306         }
307         
308         OUTER: while(p < s.length()) {
309             if(re != null) {
310                 GnuRegexp.REMatch m = re.getMatch(s,p);
311                 if(m == null) break OUTER;
312                 boolean zeroLength = m.getStartIndex() == m.getEndIndex();
313                 ret.addElement(JS.S(s.substring(p,zeroLength ? m.getStartIndex()+1 : m.getStartIndex())));
314                 p = zeroLength ? p + 1 : m.getEndIndex();
315                 if(!zeroLength) {
316                     for(int i=1;i<=re.getNumSubs();i++) {
317                         ret.addElement(JS.S(m.toString(i)));
318                         if(ret.length() == limit) break OUTER;
319                     }
320                 }
321             } else {
322                 int x = s.indexOf(sep,p);
323                 if(x == -1) break OUTER;
324                 ret.addElement(JS.S(s.substring(p,x)));
325                 p = x + sep.length();
326             }
327             if(ret.length() == limit) break;
328         }
329         if(p < s.length() && ret.length() != limit)
330             ret.addElement(JS.S(s.substring(p)));
331         return ret;
332     }
333    
334     public static GnuRegexp.RE newRE(String pattern, int flags) throws JSExn {
335         try {
336             return new GnuRegexp.RE(pattern,flags,GnuRegexp.RESyntax.RE_SYNTAX_PERL5);
337         } catch(GnuRegexp.REException e) {
338             throw new JSExn(e.toString());
339         }
340     }
341
342 }