1 // Copyright 2003 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;
12 public JSRegexp(Object arg0, Object arg1) throws JSExn {
13 if(arg0 instanceof JSRegexp) {
14 JSRegexp r = (JSRegexp) arg0;
15 this.global = r.global;
17 this.lastIndex = r.lastIndex;
19 String pattern = (String)arg0;
22 if(arg1 != null) sFlags = (String)arg1;
23 if(sFlags == null) sFlags = "";
24 for(int i=0;i<sFlags.length();i++) {
25 switch(sFlags.charAt(i)) {
26 case 'i': flags |= RE.REG_ICASE; break;
27 case 'm': flags |= RE.REG_MULTILINE; break;
28 case 'g': global = true; break;
29 default: throw new JSExn("Invalid flag in regexp \"" + sFlags.charAt(i) + "\"");
32 re = newRE(pattern,flags);
33 put("source", pattern);
34 put("global", B(global));
35 put("ignoreCase", B(flags & RE.REG_ICASE));
36 put("multiline", B(flags & RE.REG_MULTILINE));
40 public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
45 String s = (String)a0;
46 int start = global ? lastIndex : 0;
47 if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
48 REMatch match = re.getMatch(s,start);
49 if(global) lastIndex = match == null ? s.length() : match.getEndIndex();
50 return match == null ? null : matchToExecResult(match,re,s);
53 String s = (String)a0;
54 if (!global) return B(re.getMatch(s) != null);
55 int start = global ? lastIndex : 0;
56 if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
57 REMatch match = re.getMatch(s,start);
58 lastIndex = match != null ? s.length() : match.getEndIndex();
59 return B(match != null);
61 case "toString": return toString(a0);
62 case "stringMatch": return stringMatch(a0,a1);
63 case "stringSearch": return stringSearch(a0,a1);
69 case "stringReplace": return stringReplace(a0, a1,a2);
74 return super.callMethod(method, a0, a1, a2, rest, nargs);
77 public Object get(Object key) throws JSExn {
79 case "exec": return METHOD;
80 case "test": return METHOD;
81 case "toString": return METHOD;
82 case "lastIndex": return N(lastIndex);
84 return super.get(key);
87 public void put(Object key, Object value) throws JSExn {
88 if(key.equals("lastIndex")) lastIndex = JS.toNumber(value).intValue();
92 private static Object matchToExecResult(REMatch match, RE re, String s) {
95 ret.put("index", N(match.getStartIndex()));
97 int n = re.getNumSubs();
98 ret.put("length", N(n+1));
99 ret.put("0",match.toString());
100 for(int i=1;i<=n;i++) ret.put(Integer.toString(i),match.toString(i));
103 throw new Error("this should never happen");
107 public String toString() {
109 StringBuffer sb = new StringBuffer();
111 sb.append(get("source"));
113 if(global) sb.append('g');
114 if(Boolean.TRUE.equals(get("ignoreCase"))) sb.append('i');
115 if(Boolean.TRUE.equals(get("multiline"))) sb.append('m');
116 return sb.toString();
118 throw new Error("this should never happen");
122 public static Object stringMatch(Object o, Object arg0) throws JSExn {
123 String s = o.toString();
125 JSRegexp regexp = null;
126 if(arg0 instanceof JSRegexp) {
127 regexp = (JSRegexp) arg0;
130 re = newRE(arg0.toString(),0);
134 REMatch match = re.getMatch(s);
135 return matchToExecResult(match,re,s);
137 if(!regexp.global) return regexp.callMethod("exec", s, null, null, null, 1);
139 JSArray ret = new JSArray();
140 REMatch[] matches = re.getAllMatches(s);
141 for(int i=0;i<matches.length;i++) ret.addElement(matches[i].toString());
142 regexp.lastIndex = matches.length > 0 ? matches[matches.length-1].getEndIndex() : s.length();
146 public static Object stringSearch(Object o, Object arg0) throws JSExn {
147 String s = o.toString();
148 RE re = arg0 instanceof JSRegexp ? ((JSRegexp)arg0).re : newRE(arg0.toString(),0);
149 REMatch match = re.getMatch(s);
150 return match == null ? N(-1) : N(match.getStartIndex());
153 public static Object stringReplace(Object o, Object arg0, Object arg1) throws JSExn {
154 String s = o.toString();
156 JS replaceFunc = null;
157 String replaceString = null;
158 JSRegexp regexp = null;
159 if(arg0 instanceof JSRegexp) {
160 regexp = (JSRegexp) arg0;
163 re = newRE(arg0.toString(),0);
165 if(arg1 instanceof JS)
166 replaceFunc = (JS) arg1;
168 replaceString = arg1.toString();
170 if(regexp != null && regexp.global) {
171 matches = re.getAllMatches(s);
173 if(matches.length > 0)
174 regexp.lastIndex = matches[matches.length-1].getEndIndex();
176 regexp.lastIndex = s.length();
179 REMatch match = re.getMatch(s);
181 matches = new REMatch[]{ match };
183 matches = new REMatch[0];
186 StringBuffer sb = new StringBuffer(s.length());
188 char[] sa = s.toCharArray();
189 for(int i=0;i<matches.length;i++) {
190 REMatch match = matches[i];
191 sb.append(sa,pos,match.getStartIndex()-pos);
192 pos = match.getEndIndex();
193 if(replaceFunc != null) {
194 int n = (regexp == null ? 0 : re.getNumSubs());
196 Object[] rest = new Object[numArgs - 3];
197 Object a0 = match.toString();
200 for(int j=1;j<=n;j++)
202 case 1: a1 = match.toString(j); break;
203 case 2: a2 = match.toString(j); break;
204 default: rest[j - 3] = match.toString(j); break;
208 a1 = N(match.getStartIndex());
212 a2 = N(match.getStartIndex());
216 rest[rest.length - 2] = N(match.getStartIndex());
217 rest[rest.length - 1] = s;
220 // note: can't perform pausing operations in here
221 sb.append((String)replaceFunc.call(a0, a1, a2, rest, numArgs));
224 sb.append(mySubstitute(match,replaceString,s));
227 int end = matches.length == 0 ? 0 : matches[matches.length-1].getEndIndex();
228 sb.append(sa,end,sa.length-end);
229 return sb.toString();
232 private static String mySubstitute(REMatch match, String s, String source) {
233 StringBuffer sb = new StringBuffer();
236 for(i=0;i<s.length()-1;i++) {
245 case '0': case '1': case '2': case '3': case '4':
246 case '5': case '6': case '7': case '8': case '9':
247 if(i < s.length()-1 && (c2 = s.charAt(i+1)) >= '0' && c2 <= '9') {
248 n = (c - '0') * 10 + (c2 - '0');
254 sb.append(match.toString(n));
257 sb.append('$'); break;
259 sb.append(match.toString()); break;
261 sb.append(source.substring(0,match.getStartIndex())); break;
263 sb.append(source.substring(match.getEndIndex())); break;
269 if(i < s.length()) sb.append(s.charAt(i));
270 return sb.toString();
274 public static Object stringSplit(String s, Object arg0, Object arg1, int nargs) {
275 int limit = nargs < 2 ? Integer.MAX_VALUE : JS.toInt(arg1);
276 if(limit < 0) limit = Integer.MAX_VALUE;
277 if(limit == 0) return new JSArray();
280 JSRegexp regexp = null;
282 JSArray ret = new JSArray();
285 if(arg0 instanceof JSRegexp) {
286 regexp = (JSRegexp) arg0;
289 sep = arg0.toString();
292 // special case this for speed. additionally, the code below doesn't properly handle
293 // zero length strings
294 if(sep != null && sep.length()==0) {
295 int len = s.length();
296 for(int i=0;i<len;i++)
297 ret.addElement(s.substring(i,i+1));
301 OUTER: while(p < s.length()) {
303 REMatch m = re.getMatch(s,p);
304 if(m == null) break OUTER;
305 boolean zeroLength = m.getStartIndex() == m.getEndIndex();
306 ret.addElement(s.substring(p,zeroLength ? m.getStartIndex()+1 : m.getStartIndex()));
307 p = zeroLength ? p + 1 : m.getEndIndex();
309 for(int i=1;i<=re.getNumSubs();i++) {
310 ret.addElement(m.toString(i));
311 if(ret.length() == limit) break OUTER;
315 int x = s.indexOf(sep,p);
316 if(x == -1) break OUTER;
317 ret.addElement(s.substring(p,x));
318 p = x + sep.length();
320 if(ret.length() == limit) break;
322 if(p < s.length() && ret.length() != limit)
323 ret.addElement(s.substring(p));
327 public static RE newRE(String pattern, int flags) throws JSExn {
329 return new RE(pattern,flags,RESyntax.RE_SYNTAX_PERL5);
330 } catch(REException e) {
331 throw new JSExn(e.toString());
335 public String typeName() { return "regexp"; }