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