1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
7 /** A JavaScript regular expression object */
8 public class JSRegexp extends JS.Immutable {
9 private static final JS.Method METHOD = new JS.Method();
11 private boolean global;
12 private GnuRegexp.RE re;
13 private int lastIndex;
18 public JSRegexp(JS arg0, JS arg1) throws JSExn {
19 if(arg0 instanceof JSRegexp) {
20 JSRegexp r = (JSRegexp) arg0;
21 this.global = r.global;
23 this.lastIndex = r.lastIndex;
24 this.pattern = r.pattern;
27 String pattern = JSU.toString(arg0);
30 if(arg1 != null) sFlags = JSU.toString(arg1);
31 if(sFlags == null) sFlags = "";
32 for(int i=0;i<sFlags.length();i++) {
33 switch(sFlags.charAt(i)) {
34 case 'i': flags |= GnuRegexp.RE.REG_ICASE; break;
35 case 'm': flags |= GnuRegexp.RE.REG_MULTILINE; break;
36 case 'g': global = true; break;
37 default: throw new JSExn("Invalid flag in regexp \"" + sFlags.charAt(i) + "\"");
40 re = newRE(pattern,flags);
41 this.pattern = JSU.S(pattern);
46 public JS call(JS method, JS[] args) throws JSExn {
49 //#switch(JSU.str(method))
51 String s = JSU.toString(args[0]);
52 int start = global ? lastIndex : 0;
53 if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
54 GnuRegexp.REMatch match = re.getMatch(s,start);
55 if(global) lastIndex = match == null ? s.length() : match.getEndIndex();
56 return match == null ? null : matchToExecResult(match,re,s);
59 String s = JSU.toString(args[0]);
60 if (!global) return JSU.B(re.getMatch(s) != null);
61 int start = global ? lastIndex : 0;
62 if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
63 GnuRegexp.REMatch match = re.getMatch(s,start);
64 lastIndex = match != null ? s.length() : match.getEndIndex();
65 return JSU.B(match != null);
67 case "toString": return JSU.S(args[0].coerceToString());
72 //#switch(JSU.str(method))
73 case "stringMatch": return stringMatch(args[0], args[1]);
74 case "stringSearch": return stringSearch(args[0], args[1]);
79 //#switch(JSU.str(method))
80 case "stringReplace": return stringReplace(args[0], args[1], args[2]);
85 return super.call(method, args);
88 public JS get(JS key) throws JSExn {
89 //#switch(JSU.str(key))
90 case "exec": return METHOD;
91 case "test": return METHOD;
92 case "toString": return METHOD;
93 case "lastIndex": return JSU.N(lastIndex);
94 case "source": return pattern;
95 case "global": return JSU.B(global);
96 case "ignoreCase": return JSU.B(flags & GnuRegexp.RE.REG_ICASE);
97 case "multiline": return JSU.B(flags & GnuRegexp.RE.REG_MULTILINE);
99 return super.get(key);
102 public void put(JS key, JS value) throws JSExn {
103 if(JSU.isString(key)) {
104 if(JSU.toString(key).equals("lastIndex")) {
105 lastIndex = JSU.toInt(value);
109 super.put(key,value);
112 private static JS matchToExecResult(GnuRegexp.REMatch match, GnuRegexp.RE re, String s) {
113 if (match == null) return null;
115 JS ret = new JS.Obj();
116 ret.put(JSU.S("index"), JSU.N(match.getStartIndex()));
117 ret.put(JSU.S("input"), JSU.S(s));
118 int n = re.getNumSubs();
119 ret.put(JSU.S("length"), JSU.N(n+1));
120 ret.put(JSU.ZERO, JSU.S(match.toString()));
121 for(int i=1;i<=n;i++) ret.put(JSU.N(i),JSU.S(match.toString(i)));
124 throw new Error("this should never happen");
128 public String coerceToString() {
129 StringBuffer sb = new StringBuffer();
133 if(global) sb.append('g');
134 if((flags & GnuRegexp.RE.REG_ICASE) != 0) sb.append('i');
135 if((flags & GnuRegexp.RE.REG_MULTILINE) != 0) sb.append('m');
136 return sb.toString();
139 private static final JS[] execarg = new JS[1];
140 static JS stringMatch(JS o, JS arg0) throws JSExn {
141 String s = JSU.toString(o);
143 JSRegexp regexp = null;
144 if(arg0 instanceof JSRegexp) {
145 regexp = (JSRegexp) arg0;
148 re = newRE(JSU.toString(arg0),0);
152 GnuRegexp.REMatch match = re.getMatch(s);
153 return matchToExecResult(match,re,s);
157 if(!regexp.global) return regexp.call(JSU.S("exec"), execarg);
158 } finally { execarg[0] = null; }
160 GnuRegexp.REMatch[] matches = re.getAllMatches(s);
161 JSArray ret = new JSArray(matches.length);
162 for(int i=0;i<matches.length;i++) ret.add(JSU.S(matches[i].toString()));
163 regexp.lastIndex = matches.length > 0 ? matches[matches.length-1].getEndIndex() : s.length();
167 static JS stringSearch(JS o, JS arg0) throws JSExn {
168 String s = JSU.toString(o);
169 GnuRegexp.RE re = arg0 instanceof JSRegexp ? ((JSRegexp)arg0).re : newRE(JSU.toString(arg0),0);
170 GnuRegexp.REMatch match = re.getMatch(s);
171 return match == null ? JSU.N(-1) : JSU.N(match.getStartIndex());
174 static JS stringReplace(JS o, JS arg0, JS arg1) throws JSExn {
175 String s = JSU.toString(o);
177 JSFunction replaceFunc = null;
178 String replaceString = null;
179 JSRegexp regexp = null;
180 if(arg0 instanceof JSRegexp) {
181 regexp = (JSRegexp) arg0;
184 re = newRE(arg0.toString(),0);
186 if(arg1 instanceof JSFunction)
187 replaceFunc = (JSFunction) arg1;
189 replaceString = JSU.toString(arg1);
190 GnuRegexp.REMatch[] matches;
191 if(regexp != null && regexp.global) {
192 matches = re.getAllMatches(s);
194 if(matches.length > 0)
195 regexp.lastIndex = matches[matches.length-1].getEndIndex();
197 regexp.lastIndex = s.length();
200 GnuRegexp.REMatch match = re.getMatch(s);
202 matches = new GnuRegexp.REMatch[]{ match };
204 matches = new GnuRegexp.REMatch[0];
207 StringBuffer sb = new StringBuffer(s.length());
209 char[] sa = s.toCharArray();
210 for(int i=0;i<matches.length;i++) {
211 GnuRegexp.REMatch match = matches[i];
212 sb.append(sa,pos,match.getStartIndex()-pos);
213 pos = match.getEndIndex();
214 if(replaceFunc != null) {
215 int n = (regexp == null ? 0 : re.getNumSubs());
217 JS[] args = new JS[3 + n];
218 args[0] = JSU.S(match.toString());
221 for(int j=1;j<=n;j++) args[j] = JSU.S(match.toString(j));
222 args[args.length - 2] = JSU.N(match.getStartIndex());
223 args[args.length - 1] = JSU.S(s);
225 // note: can't perform pausing operations in here
226 sb.append(JSU.toString(replaceFunc.call(null, args)));
229 sb.append(mySubstitute(match,replaceString,s));
232 int end = matches.length == 0 ? 0 : matches[matches.length-1].getEndIndex();
233 sb.append(sa,end,sa.length-end);
234 return JSU.S(sb.toString());
237 private static String mySubstitute(GnuRegexp.REMatch match, String s, String source) {
238 StringBuffer sb = new StringBuffer();
241 for(i=0;i<s.length()-1;i++) {
250 case '0': case '1': case '2': case '3': case '4':
251 case '5': case '6': case '7': case '8': case '9':
252 if(i < s.length()-1 && (c2 = s.charAt(i+1)) >= '0' && c2 <= '9') {
253 n = (c - '0') * 10 + (c2 - '0');
259 sb.append(match.toString(n));
262 sb.append('$'); break;
264 sb.append(match.toString()); break;
266 sb.append(source.substring(0,match.getStartIndex())); break;
268 sb.append(source.substring(match.getEndIndex())); break;
274 if(i < s.length()) sb.append(s.charAt(i));
275 return sb.toString();
279 static JS stringSplit(JS s_, JS arg0, JS arg1, int nargs) throws JSExn {
280 String s = JSU.toString(s_);
281 int limit = nargs < 2 ? Integer.MAX_VALUE : JSU.toInt(arg1);
282 if(limit < 0) limit = Integer.MAX_VALUE;
283 if(limit == 0) return new JSArray(0);
285 GnuRegexp.RE re = null;
286 JSRegexp regexp = null;
288 JSArray ret = new JSArray();
291 if(arg0 instanceof JSRegexp) {
292 regexp = (JSRegexp) arg0;
295 sep = JSU.toString(arg0);
298 // special case this for speed. additionally, the code below doesn't properly handle
299 // zero length strings
300 if(sep != null && sep.length()==0) {
301 int len = s.length();
302 for(int i=0;i<len;i++)
303 ret.add(JSU.S(s.substring(i,i+1)));
307 OUTER: while(p < s.length()) {
309 GnuRegexp.REMatch m = re.getMatch(s,p);
310 if(m == null) break OUTER;
311 boolean zeroLength = m.getStartIndex() == m.getEndIndex();
312 ret.add(JSU.S(s.substring(p,zeroLength ? m.getStartIndex()+1 : m.getStartIndex())));
313 p = zeroLength ? p + 1 : m.getEndIndex();
315 for(int i=1;i<=re.getNumSubs();i++) {
316 ret.add(JSU.S(m.toString(i)));
317 if(ret.size() == limit) break OUTER;
321 int x = s.indexOf(sep,p);
322 if(x == -1) break OUTER;
323 ret.add(JSU.S(s.substring(p,x)));
324 p = x + sep.length();
326 if(ret.size() == limit) break;
328 if(p < s.length() && ret.size() != limit)
329 ret.add(JSU.S(s.substring(p)));
333 public static GnuRegexp.RE newRE(String pattern, int flags) throws JSExn {
335 return new GnuRegexp.RE(pattern,flags,GnuRegexp.RESyntax.RE_SYNTAX_PERL5);
336 } catch(GnuRegexp.REException e) {
337 throw new JSExn(e.toString());