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