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