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