1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
6 /** A JavaScript regular expression object */
7 public class JSRegexp extends JS {
8 private boolean global;
10 private int lastIndex;
15 public JSRegexp(JS arg0, JS arg1) throws JSExn {
16 if(arg0 instanceof JSRegexp) {
17 JSRegexp r = (JSRegexp) arg0;
18 this.global = r.global;
20 this.lastIndex = r.lastIndex;
21 this.pattern = pattern;
24 String pattern = JS.toString(arg0);
27 if(arg1 != null) sFlags = JS.toString(arg1);
28 if(sFlags == null) sFlags = "";
29 for(int i=0;i<sFlags.length();i++) {
30 switch(sFlags.charAt(i)) {
31 case 'i': flags |= RE.REG_ICASE; break;
32 case 'm': flags |= RE.REG_MULTILINE; break;
33 case 'g': global = true; break;
34 default: throw new JSExn("Invalid flag in regexp \"" + sFlags.charAt(i) + "\"");
37 re = newRE(pattern,flags);
38 this.pattern = JS.S(pattern);
43 public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
48 String s = JS.toString(a0);
49 int start = global ? lastIndex : 0;
50 if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
51 REMatch match = re.getMatch(s,start);
52 if(global) lastIndex = match == null ? s.length() : match.getEndIndex();
53 return match == null ? null : matchToExecResult(match,re,s);
56 String s = JS.toString(a0);
57 if (!global) return B(re.getMatch(s) != null);
58 int start = global ? lastIndex : 0;
59 if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
60 REMatch match = re.getMatch(s,start);
61 lastIndex = match != null ? s.length() : match.getEndIndex();
62 return B(match != null);
64 case "toString": return JS.S(a0.coerceToString());
65 case "stringMatch": return stringMatch(a0,a1);
66 case "stringSearch": return stringSearch(a0,a1);
72 case "stringReplace": return stringReplace(a0, a1,a2);
77 return super.callMethod(method, a0, a1, a2, rest, nargs);
80 public JS get(JS key) throws JSExn {
82 case "exec": return METHOD;
83 case "test": return METHOD;
84 case "toString": return METHOD;
85 case "lastIndex": return N(lastIndex);
86 case "source": return pattern;
87 case "global": return JS.B(global);
88 case "ignoreCase": return B(flags & RE.REG_ICASE);
89 case "multiline": return B(flags & RE.REG_MULTILINE);
91 return super.get(key);
94 public void put(JS key, JS value) throws JSExn {
95 if(JS.isString(key)) {
96 if(JS.toString(key).equals("lastIndex")) {
97 lastIndex = JS.toInt(value);
101 super.put(key,value);
104 private static JS matchToExecResult(REMatch match, RE re, String s) {
107 ret.put(JS.S("index"), N(match.getStartIndex()));
108 ret.put(JS.S("input"),JS.S(s));
109 int n = re.getNumSubs();
110 ret.put(JS.S("length"), N(n+1));
111 ret.put(ZERO,JS.S(match.toString()));
112 for(int i=1;i<=n;i++) ret.put(JS.N(i),JS.S(match.toString(i)));
115 throw new Error("this should never happen");
119 String coerceToString() {
120 StringBuffer sb = new StringBuffer();
124 if(global) sb.append('g');
125 if((flags & RE.REG_ICASE) != 0) sb.append('i');
126 if((flags & RE.REG_MULTILINE) != 0) sb.append('m');
127 return sb.toString();
130 static JS stringMatch(JS o, JS arg0) throws JSExn {
131 String s = JS.toString(o);
133 JSRegexp regexp = null;
134 if(arg0 instanceof JSRegexp) {
135 regexp = (JSRegexp) arg0;
138 re = newRE(JS.toString(arg0),0);
142 REMatch match = re.getMatch(s);
143 return matchToExecResult(match,re,s);
145 if(!regexp.global) return regexp.callMethod(JS.S("exec"), o, null, null, null, 1);
147 JSArray ret = new JSArray();
148 REMatch[] matches = re.getAllMatches(s);
149 for(int i=0;i<matches.length;i++) ret.addElement(JS.S(matches[i].toString()));
150 regexp.lastIndex = matches.length > 0 ? matches[matches.length-1].getEndIndex() : s.length();
154 static JS stringSearch(JS o, JS arg0) throws JSExn {
155 String s = JS.toString(o);
156 RE re = arg0 instanceof JSRegexp ? ((JSRegexp)arg0).re : newRE(JS.toString(arg0),0);
157 REMatch match = re.getMatch(s);
158 return match == null ? N(-1) : N(match.getStartIndex());
161 static JS stringReplace(JS o, JS arg0, JS arg1) throws JSExn {
162 String s = JS.toString(o);
164 JSFunction replaceFunc = null;
165 String replaceString = null;
166 JSRegexp regexp = null;
167 if(arg0 instanceof JSRegexp) {
168 regexp = (JSRegexp) arg0;
171 re = newRE(arg0.toString(),0);
173 if(arg1 instanceof JSFunction)
174 replaceFunc = (JSFunction) arg1;
176 replaceString = JS.toString(arg1);
178 if(regexp != null && regexp.global) {
179 matches = re.getAllMatches(s);
181 if(matches.length > 0)
182 regexp.lastIndex = matches[matches.length-1].getEndIndex();
184 regexp.lastIndex = s.length();
187 REMatch match = re.getMatch(s);
189 matches = new REMatch[]{ match };
191 matches = new REMatch[0];
194 StringBuffer sb = new StringBuffer(s.length());
196 char[] sa = s.toCharArray();
197 for(int i=0;i<matches.length;i++) {
198 REMatch match = matches[i];
199 sb.append(sa,pos,match.getStartIndex()-pos);
200 pos = match.getEndIndex();
201 if(replaceFunc != null) {
202 int n = (regexp == null ? 0 : re.getNumSubs());
204 JS[] rest = new JS[numArgs - 3];
205 JS a0 = JS.S(match.toString());
208 for(int j=1;j<=n;j++)
210 case 1: a1 = JS.S(match.toString(j)); break;
211 case 2: a2 = JS.S(match.toString(j)); break;
212 default: rest[j - 3] = JS.S(match.toString(j)); break;
216 a1 = N(match.getStartIndex());
220 a2 = N(match.getStartIndex());
224 rest[rest.length - 2] = N(match.getStartIndex());
225 rest[rest.length - 1] = JS.S(s);
228 // note: can't perform pausing operations in here
229 sb.append(JS.toString(replaceFunc.call(a0, a1, a2, rest, numArgs)));
232 sb.append(mySubstitute(match,replaceString,s));
235 int end = matches.length == 0 ? 0 : matches[matches.length-1].getEndIndex();
236 sb.append(sa,end,sa.length-end);
237 return JS.S(sb.toString());
240 private static String mySubstitute(REMatch match, String s, String source) {
241 StringBuffer sb = new StringBuffer();
244 for(i=0;i<s.length()-1;i++) {
253 case '0': case '1': case '2': case '3': case '4':
254 case '5': case '6': case '7': case '8': case '9':
255 if(i < s.length()-1 && (c2 = s.charAt(i+1)) >= '0' && c2 <= '9') {
256 n = (c - '0') * 10 + (c2 - '0');
262 sb.append(match.toString(n));
265 sb.append('$'); break;
267 sb.append(match.toString()); break;
269 sb.append(source.substring(0,match.getStartIndex())); break;
271 sb.append(source.substring(match.getEndIndex())); break;
277 if(i < s.length()) sb.append(s.charAt(i));
278 return sb.toString();
282 static JS stringSplit(JS s_, JS arg0, JS arg1, int nargs) throws JSExn {
283 String s = JS.toString(s_);
284 int limit = nargs < 2 ? Integer.MAX_VALUE : JS.toInt(arg1);
285 if(limit < 0) limit = Integer.MAX_VALUE;
286 if(limit == 0) return new JSArray();
289 JSRegexp regexp = null;
291 JSArray ret = new JSArray();
294 if(arg0 instanceof JSRegexp) {
295 regexp = (JSRegexp) arg0;
298 sep = JS.toString(arg0);
301 // special case this for speed. additionally, the code below doesn't properly handle
302 // zero length strings
303 if(sep != null && sep.length()==0) {
304 int len = s.length();
305 for(int i=0;i<len;i++)
306 ret.addElement(JS.S(s.substring(i,i+1)));
310 OUTER: while(p < s.length()) {
312 REMatch m = re.getMatch(s,p);
313 if(m == null) break OUTER;
314 boolean zeroLength = m.getStartIndex() == m.getEndIndex();
315 ret.addElement(JS.S(s.substring(p,zeroLength ? m.getStartIndex()+1 : m.getStartIndex())));
316 p = zeroLength ? p + 1 : m.getEndIndex();
318 for(int i=1;i<=re.getNumSubs();i++) {
319 ret.addElement(JS.S(m.toString(i)));
320 if(ret.length() == limit) break OUTER;
324 int x = s.indexOf(sep,p);
325 if(x == -1) break OUTER;
326 ret.addElement(JS.S(s.substring(p,x)));
327 p = x + sep.length();
329 if(ret.length() == limit) break;
331 if(p < s.length() && ret.length() != limit)
332 ret.addElement(JS.S(s.substring(p)));
336 private static RE newRE(String pattern, int flags) throws JSExn {
338 return new RE(pattern,flags,RESyntax.RE_SYNTAX_PERL5);
339 } catch(REException e) {
340 throw new JSExn(e.toString());