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