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(Object arg0, Object arg1) throws JS.Exn {
11 if(arg0 instanceof Regexp) {
12 Regexp r = (Regexp) 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 callMethod(Object method, Array args, boolean checkOnly) throws JS.Exn {
39 if (method.equals("exec")) {
40 if (checkOnly) return Boolean.TRUE;
41 return exec(args);
42 } else if (method.equals("test")) {
43 if (checkOnly) return Boolean.TRUE;
44 return test(args);
45 } else if (method.equals("toString")) {
46 if (checkOnly) return Boolean.TRUE;
47 return toString();
48 }
49 if (checkOnly) return Boolean.FALSE;
50 return null;
51 }
52
53
54 public Object get(Object key) { return _get(key); }
55 public Object put(Object key,Object value) { _put(key,value); return null; }
56
57 public Object _get(Object key) {
58 if(key.equals("lastIndex")) return new Integer(lastIndex);
59 return super.get(key);
60 }
61
62 public void _put(Object key, Object value) {
63 if(key.equals("lastIndex")) lastIndex = JS.toNumber(value).intValue();
64 super.put(key,value);
65 }
66
67 private Object exec(String s) throws JS.Exn {
68 int start = global ? lastIndex : 0;
69 if(start < 0 || start >= s.length()) {
70 lastIndex = 0;
71 return null;
72 }
73
74 REMatch match = re.getMatch(s,start);
75 if(global)
76 lastIndex = match == null ? s.length() : match.getEndIndex();
77 if(match == null)
78 return null;
79 else
80 return matchToExecResult(match,re,s);
81 }
82
83 private static Object matchToExecResult(REMatch match, RE re, String s) {
84 JS.Obj ret = new JS.Obj();
85 ret.put("index",new Integer(match.getStartIndex()));
86 ret.put("input",s);
87 int n = re.getNumSubs();
88 ret.put("length",new Integer(n+1));
89 ret.put("0",match.toString());
90 for(int i=1;i<=n;i++)
91 ret.put(Integer.toString(i),match.toString(i));
92 return ret;
93 }
94
95
96 private Object exec(JS.Array args) throws JS.Exn {
97 if(args.length() < 1) throw new JS.Exn("Not enough args to exec");
98 String s = args.elementAt(0).toString();
99 return exec(s);
100 }
101
102 private Object test(JS.Array args) throws JS.Exn {
103 if(args.length() < 1) throw new JS.Exn("Not enough args to match");
104 String s = args.elementAt(0).toString();
105
106 if(global) {
107 int start = global ? lastIndex : 0;
108 if(start < 0 || start >= s.length()) {
109 lastIndex = 0;
110 return null;
111 }
112
113 REMatch match = re.getMatch(s,start);
114 lastIndex = match != null ? s.length() : match.getEndIndex();
115 return wrapBool(match != null);
116 } else {
117 return wrapBool(re.getMatch(s) != null);
118 }
119 }
120
121 public String toString() {
122 StringBuffer sb = new StringBuffer();
123 sb.append('/');
124 sb.append(_get("source"));
125 sb.append('/');
126 if(global) sb.append('g');
127 if(Boolean.TRUE.equals(_get("ignoreCase"))) sb.append('i');
128 if(Boolean.TRUE.equals(_get("multiline"))) sb.append('m');
129 return sb.toString();
130 }
131
132 public static Object stringMatch(Object o, JS.Array args) throws JS.Exn {
133 if(args.length() < 1) throw new JS.Exn("not enough args to match");
134 Object arg0 = args.elementAt(0);
135 String s = o.toString();
136 RE re;
137 Regexp regexp = null;
138 if(arg0 instanceof Regexp) {
139 regexp = (Regexp) arg0;
140 re = regexp.re;
141 } else {
142 re = newRE(arg0.toString(),0);
143 }
144
145 if(regexp == null) {
146 REMatch match = re.getMatch(s);
147 return matchToExecResult(match,re,s);
148 }
149 if(!regexp.global)
150 return regexp.exec(s);
151
152 JS.Array ret = new JS.Array();
153 REMatch[] matches = re.getAllMatches(s);
154 for(int i=0;i<matches.length;i++)
155 ret.addElement(matches[i].toString());
156 if(matches.length > 0)
157 regexp.lastIndex = matches[matches.length-1].getEndIndex();
158 else
159 regexp.lastIndex = s.length();
160 return ret;
161 }
162
163 public static Object stringSearch(Object o, JS.Array args) throws JS.Exn {
164 if(args.length() < 1) throw new JS.Exn("not enough args to match");
165 Object arg0 = args.elementAt(0);
166 String s = o.toString();
167 RE re;
168 if(arg0 instanceof Regexp)
169 re = ((Regexp)arg0).re;
170 else
171 re = newRE(arg0.toString(),0);
172 REMatch match = re.getMatch(s);
173 if(match == null) return new Integer(-1);
174 return new Integer(match.getStartIndex());
175 }
176
177 public static Object stringReplace(Object o, JS.Array args) throws JS.Exn {
178 if(args.length() < 2) throw new JS.Exn("not enough args to replace");
179 Object arg0 = args.elementAt(0);
180 Object arg1 = args.elementAt(1);
181 String s = o.toString();
182 RE re;
183 JS.Callable replaceFunc = null;
184 String replaceString = null;
185 Regexp regexp = null;
186 if(arg0 instanceof Regexp) {
187 regexp = (Regexp) arg0;
188 re = regexp.re;
189 } else {
190 re = newRE(arg0.toString(),0);
191 }
192 if(arg1 instanceof JS.Callable)
193 replaceFunc = (JS.Callable) arg1;
194 else
195 replaceString = arg1.toString();
196 REMatch[] matches;
197 if(regexp != null && regexp.global) {
198 matches = re.getAllMatches(s);
199 if(regexp != null) {
200 if(matches.length > 0)
201 regexp.lastIndex = matches[matches.length-1].getEndIndex();
202 else
203 regexp.lastIndex = s.length();
204 }
205 } else {
206 REMatch match = re.getMatch(s);
207 if(match != null)
208 matches = new REMatch[]{ match };
209 else
210 matches = new REMatch[0];
211 }
212
213 StringBuffer sb = new StringBuffer(s.length());
214 int pos = 0;
215 char[] sa = s.toCharArray();
216 for(int i=0;i<matches.length;i++) {
217 REMatch match = matches[i];
218 sb.append(sa,pos,match.getStartIndex()-pos);
219 pos = match.getEndIndex();
220 if(replaceFunc != null) {
221 JS.Array a = new JS.Array();
222 a.addElement(match.toString());
223 if(regexp != null) {
224 int n = re.getNumSubs();
225 for(int j=1;j<=n;j++)
226 a.addElement(match.toString(j));
227 }
228 a.addElement(new Integer(match.getStartIndex()));
229 a.addElement(s);
230 Object ret = replaceFunc.call(a);
231 sb.append(ret.toString());
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 intintn;
244 charchar2;
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,JS.Array args) {
284 String s = o.toString();
285 if(args.length() < 1 || args.elementAt(0) == null || s.length() == 0) {
286 JS.Array ret = new JS.Array();
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 JS.Array();
295
296 RE re = null;
297 Regexp regexp = null;
298 String sep = null;
299 JS.Array ret = new JS.Array();
300 int p = 0;
301
302 if(arg0 instanceof Regexp) {
303 regexp = (Regexp) arg0;
304 re = regexp.re;
305 } else {
306 sep = arg0.toString();
307 }
308
309
310
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 }
362