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 {
44 if(!JS.isString(method_)) return super.callMethod(method_, a0, a1, a2, rest, nargs);
45 String method = JS.toString(method_);
50 String s = JS.toString(a0);
51 int start = global ? lastIndex : 0;
52 if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
53 REMatch match = re.getMatch(s,start);
54 if(global) lastIndex = match == null ? s.length() : match.getEndIndex();
55 return match == null ? null : matchToExecResult(match,re,s);
58 String s = JS.toString(a0);
59 if (!global) return B(re.getMatch(s) != null);
60 int start = global ? lastIndex : 0;
61 if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
62 REMatch match = re.getMatch(s,start);
63 lastIndex = match != null ? s.length() : match.getEndIndex();
64 return B(match != null);
66 case "toString": return JS.S(a0.coerceToString());
67 case "stringMatch": return stringMatch(a0,a1);
68 case "stringSearch": return stringSearch(a0,a1);
74 case "stringReplace": return stringReplace(a0, a1,a2);
79 return super.callMethod(method_, a0, a1, a2, rest, nargs);
82 public JS get(JS key_) throws JSExn {
83 if(!JS.isString(key_)) return super.get(key_);
84 String key = JS.toString(key_);
86 case "exec": return METHOD;
87 case "test": return METHOD;
88 case "toString": return METHOD;
89 case "lastIndex": return N(lastIndex);
90 case "source": return pattern;
91 case "global": return JS.B(global);
92 case "ignoreCase": return B(flags & RE.REG_ICASE);
93 case "multiline": return B(flags & RE.REG_MULTILINE);
95 return super.get(key_);
98 public void put(JS key, JS value) throws JSExn {
99 if(JS.isString(key)) {
100 if(JS.toString(key).equals("lastIndex")) {
101 lastIndex = JS.toInt(value);
105 super.put(key,value);
108 private static JS matchToExecResult(REMatch match, RE re, String s) {
111 ret.put(JS.S("index"), N(match.getStartIndex()));
112 ret.put(JS.S("input"),JS.S(s));
113 int n = re.getNumSubs();
114 ret.put(JS.S("length"), N(n+1));
115 ret.put(ZERO,JS.S(match.toString()));
116 for(int i=1;i<=n;i++) ret.put(JS.N(i),JS.S(match.toString(i)));
119 throw new Error("this should never happen");
123 String coerceToString() {
124 StringBuffer sb = new StringBuffer();
128 if(global) sb.append('g');
129 if((flags & RE.REG_ICASE) != 0) sb.append('i');
130 if((flags & RE.REG_MULTILINE) != 0) sb.append('m');
131 return sb.toString();
134 public static JS stringMatch(JS o, JS arg0) throws JSExn {
135 String s = JS.toString(o);
137 JSRegexp regexp = null;
138 if(arg0 instanceof JSRegexp) {
139 regexp = (JSRegexp) arg0;
142 re = newRE(JS.toString(arg0),0);
146 REMatch match = re.getMatch(s);
147 return matchToExecResult(match,re,s);
149 if(!regexp.global) return regexp.callMethod(JS.S("exec"), o, null, null, null, 1);
151 JSArray ret = new JSArray();
152 REMatch[] matches = re.getAllMatches(s);
153 for(int i=0;i<matches.length;i++) ret.addElement(JS.S(matches[i].toString()));
154 regexp.lastIndex = matches.length > 0 ? matches[matches.length-1].getEndIndex() : s.length();
158 public static JS stringSearch(JS o, JS arg0) throws JSExn {
159 String s = JS.toString(o);
160 RE re = arg0 instanceof JSRegexp ? ((JSRegexp)arg0).re : newRE(JS.toString(arg0),0);
161 REMatch match = re.getMatch(s);
162 return match == null ? N(-1) : N(match.getStartIndex());
165 public static JS stringReplace(JS o, JS arg0, JS arg1) throws JSExn {
166 String s = JS.toString(o);
168 JSFunction replaceFunc = null;
169 String replaceString = null;
170 JSRegexp regexp = null;
171 if(arg0 instanceof JSRegexp) {
172 regexp = (JSRegexp) arg0;
175 re = newRE(arg0.toString(),0);
177 if(arg1 instanceof JSFunction)
178 replaceFunc = (JSFunction) arg1;
180 replaceString = JS.toString(arg1);
182 if(regexp != null && regexp.global) {
183 matches = re.getAllMatches(s);
185 if(matches.length > 0)
186 regexp.lastIndex = matches[matches.length-1].getEndIndex();
188 regexp.lastIndex = s.length();
191 REMatch match = re.getMatch(s);
193 matches = new REMatch[]{ match };
195 matches = new REMatch[0];
198 StringBuffer sb = new StringBuffer(s.length());
200 char[] sa = s.toCharArray();
201 for(int i=0;i<matches.length;i++) {
202 REMatch match = matches[i];
203 sb.append(sa,pos,match.getStartIndex()-pos);
204 pos = match.getEndIndex();
205 if(replaceFunc != null) {
206 int n = (regexp == null ? 0 : re.getNumSubs());
208 JS[] rest = new JS[numArgs - 3];
209 JS a0 = JS.S(match.toString());
212 for(int j=1;j<=n;j++)
214 case 1: a1 = JS.S(match.toString(j)); break;
215 case 2: a2 = JS.S(match.toString(j)); break;
216 default: rest[j - 3] = JS.S(match.toString(j)); break;
220 a1 = N(match.getStartIndex());
224 a2 = N(match.getStartIndex());
228 rest[rest.length - 2] = N(match.getStartIndex());
229 rest[rest.length - 1] = JS.S(s);
232 // note: can't perform pausing operations in here
233 sb.append(JS.toString(replaceFunc.call(a0, a1, a2, rest, numArgs)));
236 sb.append(mySubstitute(match,replaceString,s));
239 int end = matches.length == 0 ? 0 : matches[matches.length-1].getEndIndex();
240 sb.append(sa,end,sa.length-end);
241 return JS.S(sb.toString());
244 private static String mySubstitute(REMatch match, String s, String source) {
245 StringBuffer sb = new StringBuffer();
248 for(i=0;i<s.length()-1;i++) {
257 case '0': case '1': case '2': case '3': case '4':
258 case '5': case '6': case '7': case '8': case '9':
259 if(i < s.length()-1 && (c2 = s.charAt(i+1)) >= '0' && c2 <= '9') {
260 n = (c - '0') * 10 + (c2 - '0');
266 sb.append(match.toString(n));
269 sb.append('$'); break;
271 sb.append(match.toString()); break;
273 sb.append(source.substring(0,match.getStartIndex())); break;
275 sb.append(source.substring(match.getEndIndex())); break;
281 if(i < s.length()) sb.append(s.charAt(i));
282 return sb.toString();
286 public static JS stringSplit(JS s_, JS arg0, JS arg1, int nargs) throws JSExn {
287 String s = JS.toString(s_);
288 int limit = nargs < 2 ? Integer.MAX_VALUE : JS.toInt(arg1);
289 if(limit < 0) limit = Integer.MAX_VALUE;
290 if(limit == 0) return new JSArray();
293 JSRegexp regexp = null;
295 JSArray ret = new JSArray();
298 if(arg0 instanceof JSRegexp) {
299 regexp = (JSRegexp) arg0;
302 sep = JS.toString(arg0);
305 // special case this for speed. additionally, the code below doesn't properly handle
306 // zero length strings
307 if(sep != null && sep.length()==0) {
308 int len = s.length();
309 for(int i=0;i<len;i++)
310 ret.addElement(JS.S(s.substring(i,i+1)));
314 OUTER: while(p < s.length()) {
316 REMatch m = re.getMatch(s,p);
317 if(m == null) break OUTER;
318 boolean zeroLength = m.getStartIndex() == m.getEndIndex();
319 ret.addElement(JS.S(s.substring(p,zeroLength ? m.getStartIndex()+1 : m.getStartIndex())));
320 p = zeroLength ? p + 1 : m.getEndIndex();
322 for(int i=1;i<=re.getNumSubs();i++) {
323 ret.addElement(JS.S(m.toString(i)));
324 if(ret.length() == limit) break OUTER;
328 int x = s.indexOf(sep,p);
329 if(x == -1) break OUTER;
330 ret.addElement(JS.S(s.substring(p,x)));
331 p = x + sep.length();
333 if(ret.length() == limit) break;
335 if(p < s.length() && ret.length() != limit)
336 ret.addElement(JS.S(s.substring(p)));
340 public static RE newRE(String pattern, int flags) throws JSExn {
342 return new RE(pattern,flags,RESyntax.RE_SYNTAX_PERL5);
343 } catch(REException e) {
344 throw new JSExn(e.toString());