1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
4 /** A JavaScript regular expression object */
5 public class JSRegexp extends JS {
6 private boolean global;
7 private GnuRegexp.RE re;
13 public JSRegexp(JS arg0, JS arg1) throws JSExn {
14 if(arg0 instanceof JSRegexp) {
15 JSRegexp r = (JSRegexp) arg0;
16 this.global = r.global;
18 this.lastIndex = r.lastIndex;
19 this.pattern = pattern;
22 String pattern = JS.toString(arg0);
25 if(arg1 != null) sFlags = JS.toString(arg1);
26 if(sFlags == null) sFlags = "";
27 for(int i=0;i<sFlags.length();i++) {
28 switch(sFlags.charAt(i)) {
29 case 'i': flags |= GnuRegexp.RE.REG_ICASE; break;
30 case 'm': flags |= GnuRegexp.RE.REG_MULTILINE; break;
31 case 'g': global = true; break;
32 default: throw new JSExn("Invalid flag in regexp \"" + sFlags.charAt(i) + "\"");
35 re = newRE(pattern,flags);
36 this.pattern = JS.S(pattern);
41 public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
44 //#switch(JS.toString(method))
46 String s = JS.toString(a0);
47 int start = global ? lastIndex : 0;
48 if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
49 GnuRegexp.REMatch match = re.getMatch(s,start);
50 if(global) lastIndex = match == null ? s.length() : match.getEndIndex();
51 return match == null ? null : matchToExecResult(match,re,s);
54 String s = JS.toString(a0);
55 if (!global) return B(re.getMatch(s) != null);
56 int start = global ? lastIndex : 0;
57 if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
58 GnuRegexp.REMatch match = re.getMatch(s,start);
59 lastIndex = match != null ? s.length() : match.getEndIndex();
60 return B(match != null);
62 case "toString": return JS.S(a0.coerceToString());
63 case "stringMatch": return stringMatch(a0,a1);
64 case "stringSearch": return stringSearch(a0,a1);
69 //#switch(JS.toString(method))
70 case "stringReplace": return stringReplace(a0, a1,a2);
75 return super.callMethod(method, a0, a1, a2, rest, nargs);
78 public JS get(JS key) throws JSExn {
79 //#switch(JS.toString(key))
80 case "exec": return METHOD;
81 case "test": return METHOD;
82 case "toString": return METHOD;
83 case "lastIndex": return N(lastIndex);
84 case "source": return pattern;
85 case "global": return JS.B(global);
86 case "ignoreCase": return B(flags & GnuRegexp.RE.REG_ICASE);
87 case "multiline": return B(flags & GnuRegexp.RE.REG_MULTILINE);
89 return super.get(key);
92 public void put(JS key, JS value) throws JSExn {
93 if(JS.isString(key)) {
94 if(JS.toString(key).equals("lastIndex")) {
95 lastIndex = JS.toInt(value);
102 private static JS matchToExecResult(GnuRegexp.REMatch match, GnuRegexp.RE re, String s) {
105 ret.put(JS.S("index"), N(match.getStartIndex()));
106 ret.put(JS.S("input"),JS.S(s));
107 int n = re.getNumSubs();
108 ret.put(JS.S("length"), N(n+1));
109 ret.put(ZERO,JS.S(match.toString()));
110 for(int i=1;i<=n;i++) ret.put(JS.N(i),JS.S(match.toString(i)));
113 throw new Error("this should never happen");
117 String coerceToString() {
118 StringBuffer sb = new StringBuffer();
122 if(global) sb.append('g');
123 if((flags & GnuRegexp.RE.REG_ICASE) != 0) sb.append('i');
124 if((flags & GnuRegexp.RE.REG_MULTILINE) != 0) sb.append('m');
125 return sb.toString();
128 static JS stringMatch(JS o, JS arg0) throws JSExn {
129 String s = JS.toString(o);
131 JSRegexp regexp = null;
132 if(arg0 instanceof JSRegexp) {
133 regexp = (JSRegexp) arg0;
136 re = newRE(JS.toString(arg0),0);
140 GnuRegexp.REMatch match = re.getMatch(s);
141 return matchToExecResult(match,re,s);
143 if(!regexp.global) return regexp.callMethod(JS.S("exec"), o, null, null, null, 1);
145 JSArray ret = new JSArray();
146 GnuRegexp.REMatch[] matches = re.getAllMatches(s);
147 for(int i=0;i<matches.length;i++) ret.addElement(JS.S(matches[i].toString()));
148 regexp.lastIndex = matches.length > 0 ? matches[matches.length-1].getEndIndex() : s.length();
152 static JS stringSearch(JS o, JS arg0) throws JSExn {
153 String s = JS.toString(o);
154 GnuRegexp.RE re = arg0 instanceof JSRegexp ? ((JSRegexp)arg0).re : newRE(JS.toString(arg0),0);
155 GnuRegexp.REMatch match = re.getMatch(s);
156 return match == null ? N(-1) : N(match.getStartIndex());
159 static JS stringReplace(JS o, JS arg0, JS arg1) throws JSExn {
160 String s = JS.toString(o);
162 JSFunction replaceFunc = null;
163 String replaceString = null;
164 JSRegexp regexp = null;
165 if(arg0 instanceof JSRegexp) {
166 regexp = (JSRegexp) arg0;
169 re = newRE(arg0.toString(),0);
171 if(arg1 instanceof JSFunction)
172 replaceFunc = (JSFunction) arg1;
174 replaceString = JS.toString(arg1);
175 GnuRegexp.REMatch[] matches;
176 if(regexp != null && regexp.global) {
177 matches = re.getAllMatches(s);
179 if(matches.length > 0)
180 regexp.lastIndex = matches[matches.length-1].getEndIndex();
182 regexp.lastIndex = s.length();
185 GnuRegexp.REMatch match = re.getMatch(s);
187 matches = new GnuRegexp.REMatch[]{ match };
189 matches = new GnuRegexp.REMatch[0];
192 StringBuffer sb = new StringBuffer(s.length());
194 char[] sa = s.toCharArray();
195 for(int i=0;i<matches.length;i++) {
196 GnuRegexp.REMatch match = matches[i];
197 sb.append(sa,pos,match.getStartIndex()-pos);
198 pos = match.getEndIndex();
199 if(replaceFunc != null) {
200 int n = (regexp == null ? 0 : re.getNumSubs());
202 JS[] rest = new JS[numArgs - 3];
203 JS a0 = JS.S(match.toString());
206 for(int j=1;j<=n;j++)
208 case 1: a1 = JS.S(match.toString(j)); break;
209 case 2: a2 = JS.S(match.toString(j)); break;
210 default: rest[j - 3] = JS.S(match.toString(j)); break;
214 a1 = N(match.getStartIndex());
218 a2 = N(match.getStartIndex());
222 rest[rest.length - 2] = N(match.getStartIndex());
223 rest[rest.length - 1] = JS.S(s);
226 // note: can't perform pausing operations in here
227 sb.append(JS.toString(replaceFunc.call(a0, a1, a2, rest, numArgs)));
230 sb.append(mySubstitute(match,replaceString,s));
233 int end = matches.length == 0 ? 0 : matches[matches.length-1].getEndIndex();
234 sb.append(sa,end,sa.length-end);
235 return JS.S(sb.toString());
238 private static String mySubstitute(GnuRegexp.REMatch match, String s, String source) {
239 StringBuffer sb = new StringBuffer();
242 for(i=0;i<s.length()-1;i++) {
251 case '0': case '1': case '2': case '3': case '4':
252 case '5': case '6': case '7': case '8': case '9':
253 if(i < s.length()-1 && (c2 = s.charAt(i+1)) >= '0' && c2 <= '9') {
254 n = (c - '0') * 10 + (c2 - '0');
260 sb.append(match.toString(n));
263 sb.append('$'); break;
265 sb.append(match.toString()); break;
267 sb.append(source.substring(0,match.getStartIndex())); break;
269 sb.append(source.substring(match.getEndIndex())); break;
275 if(i < s.length()) sb.append(s.charAt(i));
276 return sb.toString();
280 static JS stringSplit(JS s_, JS arg0, JS arg1, int nargs) throws JSExn {
281 String s = JS.toString(s_);
282 int limit = nargs < 2 ? Integer.MAX_VALUE : JS.toInt(arg1);
283 if(limit < 0) limit = Integer.MAX_VALUE;
284 if(limit == 0) return new JSArray();
286 GnuRegexp.RE re = null;
287 JSRegexp regexp = null;
289 JSArray ret = new JSArray();
292 if(arg0 instanceof JSRegexp) {
293 regexp = (JSRegexp) arg0;
296 sep = JS.toString(arg0);
299 // special case this for speed. additionally, the code below doesn't properly handle
300 // zero length strings
301 if(sep != null && sep.length()==0) {
302 int len = s.length();
303 for(int i=0;i<len;i++)
304 ret.addElement(JS.S(s.substring(i,i+1)));
308 OUTER: while(p < s.length()) {
310 GnuRegexp.REMatch m = re.getMatch(s,p);
311 if(m == null) break OUTER;
312 boolean zeroLength = m.getStartIndex() == m.getEndIndex();
313 ret.addElement(JS.S(s.substring(p,zeroLength ? m.getStartIndex()+1 : m.getStartIndex())));
314 p = zeroLength ? p + 1 : m.getEndIndex();
316 for(int i=1;i<=re.getNumSubs();i++) {
317 ret.addElement(JS.S(m.toString(i)));
318 if(ret.length() == limit) break OUTER;
322 int x = s.indexOf(sep,p);
323 if(x == -1) break OUTER;
324 ret.addElement(JS.S(s.substring(p,x)));
325 p = x + sep.length();
327 if(ret.length() == limit) break;
329 if(p < s.length() && ret.length() != limit)
330 ret.addElement(JS.S(s.substring(p)));
334 public static GnuRegexp.RE newRE(String pattern, int flags) throws JSExn {
336 return new GnuRegexp.RE(pattern,flags,GnuRegexp.RESyntax.RE_SYNTAX_PERL5);
337 } catch(GnuRegexp.REException e) {
338 throw new JSExn(e.toString());