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;
10 public JSRegexp(Object arg0, Object arg1) throws JSExn {
11 if(arg0 instanceof JSRegexp) {
12 JSRegexp r = (JSRegexp) arg0;
13 this.global = r.global;
15 this.lastIndex = r.lastIndex;
17 String pattern = (String)arg0;
20 if(arg1 != null) sFlags = (String)arg1;
21 if(sFlags == null) sFlags = "";
22 for(int i=0;i<sFlags.length();i++) {
23 switch(sFlags.charAt(i)) {
24 case 'i': flags |= GnuRegexp.RE.REG_ICASE; break;
25 case 'm': flags |= GnuRegexp.RE.REG_MULTILINE; break;
26 case 'g': global = true; break;
27 default: throw new JSExn("Invalid flag in regexp \"" + sFlags.charAt(i) + "\"");
30 re = newRE(pattern,flags);
31 put("source", pattern);
32 put("global", B(global));
33 put("ignoreCase", B(flags & GnuRegexp.RE.REG_ICASE));
34 put("multiline", B(flags & GnuRegexp.RE.REG_MULTILINE));
38 public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
43 String s = (String)a0;
44 int start = global ? lastIndex : 0;
45 if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
46 GnuRegexp.REMatch match = re.getMatch(s,start);
47 if(global) lastIndex = match == null ? s.length() : match.getEndIndex();
48 return match == null ? null : matchToExecResult(match,re,s);
51 String s = (String)a0;
52 if (!global) return B(re.getMatch(s) != null);
53 int start = global ? lastIndex : 0;
54 if(start < 0 || start >= s.length()) { lastIndex = 0; return null; }
55 GnuRegexp.REMatch match = re.getMatch(s,start);
56 lastIndex = match != null ? s.length() : match.getEndIndex();
57 return B(match != null);
59 case "toString": return toString(a0);
60 case "stringMatch": return stringMatch(a0,a1);
61 case "stringSearch": return stringSearch(a0,a1);
67 case "stringReplace": return stringReplace(a0, a1,a2);
72 return super.callMethod(method, a0, a1, a2, rest, nargs);
75 public Object get(Object key) throws JSExn {
77 case "exec": return METHOD;
78 case "test": return METHOD;
79 case "toString": return METHOD;
80 case "lastIndex": return N(lastIndex);
82 return super.get(key);
85 public void put(Object key, Object value) throws JSExn {
86 if(key.equals("lastIndex")) lastIndex = JS.toNumber(value).intValue();
90 private static Object matchToExecResult(GnuRegexp.REMatch match, GnuRegexp.RE re, String s) {
93 ret.put("index", N(match.getStartIndex()));
95 int n = re.getNumSubs();
96 ret.put("length", N(n+1));
97 ret.put("0",match.toString());
98 for(int i=1;i<=n;i++) ret.put(Integer.toString(i),match.toString(i));
101 throw new Error("this should never happen");
105 public String toString() {
107 StringBuffer sb = new StringBuffer();
109 sb.append(get("source"));
111 if(global) sb.append('g');
112 if(Boolean.TRUE.equals(get("ignoreCase"))) sb.append('i');
113 if(Boolean.TRUE.equals(get("multiline"))) sb.append('m');
114 return sb.toString();
116 throw new Error("this should never happen");
120 public static Object stringMatch(Object o, Object arg0) throws JSExn {
121 String s = o.toString();
123 JSRegexp regexp = null;
124 if(arg0 instanceof JSRegexp) {
125 regexp = (JSRegexp) arg0;
128 re = newRE(arg0.toString(),0);
132 GnuRegexp.REMatch match = re.getMatch(s);
133 return matchToExecResult(match,re,s);
135 if(!regexp.global) return regexp.callMethod("exec", s, null, null, null, 1);
137 JSArray ret = new JSArray();
138 GnuRegexp.REMatch[] matches = re.getAllMatches(s);
139 for(int i=0;i<matches.length;i++) ret.addElement(matches[i].toString());
140 regexp.lastIndex = matches.length > 0 ? matches[matches.length-1].getEndIndex() : s.length();
144 public static Object stringSearch(Object o, Object arg0) throws JSExn {
145 String s = o.toString();
146 GnuRegexp.RE re = arg0 instanceof JSRegexp ? ((JSRegexp)arg0).re : newRE(arg0.toString(),0);
147 GnuRegexp.REMatch match = re.getMatch(s);
148 return match == null ? N(-1) : N(match.getStartIndex());
151 public static Object stringReplace(Object o, Object arg0, Object arg1) throws JSExn {
152 String s = o.toString();
154 JSFunction replaceFunc = null;
155 String replaceString = null;
156 JSRegexp regexp = null;
157 if(arg0 instanceof JSRegexp) {
158 regexp = (JSRegexp) arg0;
161 re = newRE(arg0.toString(),0);
163 if(arg1 instanceof JSFunction)
164 replaceFunc = (JSFunction) arg1;
166 replaceString = JS.toString(arg1.toString());
167 GnuRegexp.REMatch[] matches;
168 if(regexp != null && regexp.global) {
169 matches = re.getAllMatches(s);
171 if(matches.length > 0)
172 regexp.lastIndex = matches[matches.length-1].getEndIndex();
174 regexp.lastIndex = s.length();
177 GnuRegexp.REMatch match = re.getMatch(s);
179 matches = new GnuRegexp.REMatch[]{ match };
181 matches = new GnuRegexp.REMatch[0];
184 StringBuffer sb = new StringBuffer(s.length());
186 char[] sa = s.toCharArray();
187 for(int i=0;i<matches.length;i++) {
188 GnuRegexp.REMatch match = matches[i];
189 sb.append(sa,pos,match.getStartIndex()-pos);
190 pos = match.getEndIndex();
191 if(replaceFunc != null) {
192 int n = (regexp == null ? 0 : re.getNumSubs());
194 Object[] rest = new Object[numArgs - 3];
195 Object a0 = match.toString();
198 for(int j=1;j<=n;j++)
200 case 1: a1 = match.toString(j); break;
201 case 2: a2 = match.toString(j); break;
202 default: rest[j - 3] = match.toString(j); break;
206 a1 = N(match.getStartIndex());
210 a2 = N(match.getStartIndex());
214 rest[rest.length - 2] = N(match.getStartIndex());
215 rest[rest.length - 1] = s;
218 // note: can't perform pausing operations in here
219 sb.append((String)replaceFunc.call(a0, a1, a2, rest, numArgs));
222 sb.append(mySubstitute(match,replaceString,s));
225 int end = matches.length == 0 ? 0 : matches[matches.length-1].getEndIndex();
226 sb.append(sa,end,sa.length-end);
227 return sb.toString();
230 private static String mySubstitute(GnuRegexp.REMatch match, String s, String source) {
231 StringBuffer sb = new StringBuffer();
234 for(i=0;i<s.length()-1;i++) {
243 case '0': case '1': case '2': case '3': case '4':
244 case '5': case '6': case '7': case '8': case '9':
245 if(i < s.length()-1 && (c2 = s.charAt(i+1)) >= '0' && c2 <= '9') {
246 n = (c - '0') * 10 + (c2 - '0');
252 sb.append(match.toString(n));
255 sb.append('$'); break;
257 sb.append(match.toString()); break;
259 sb.append(source.substring(0,match.getStartIndex())); break;
261 sb.append(source.substring(match.getEndIndex())); break;
267 if(i < s.length()) sb.append(s.charAt(i));
268 return sb.toString();
272 public static Object stringSplit(String s, Object arg0, Object arg1, int nargs) {
273 int limit = nargs < 2 ? Integer.MAX_VALUE : JS.toInt(arg1);
274 if(limit < 0) limit = Integer.MAX_VALUE;
275 if(limit == 0) return new JSArray();
277 GnuRegexp.RE re = null;
278 JSRegexp regexp = null;
280 JSArray ret = new JSArray();
283 if(arg0 instanceof JSRegexp) {
284 regexp = (JSRegexp) arg0;
287 sep = arg0.toString();
290 // special case this for speed. additionally, the code below doesn't properly handle
291 // zero length strings
292 if(sep != null && sep.length()==0) {
293 int len = s.length();
294 for(int i=0;i<len;i++)
295 ret.addElement(s.substring(i,i+1));
299 OUTER: while(p < s.length()) {
301 GnuRegexp.REMatch m = re.getMatch(s,p);
302 if(m == null) break OUTER;
303 boolean zeroLength = m.getStartIndex() == m.getEndIndex();
304 ret.addElement(s.substring(p,zeroLength ? m.getStartIndex()+1 : m.getStartIndex()));
305 p = zeroLength ? p + 1 : m.getEndIndex();
307 for(int i=1;i<=re.getNumSubs();i++) {
308 ret.addElement(m.toString(i));
309 if(ret.length() == limit) break OUTER;
313 int x = s.indexOf(sep,p);
314 if(x == -1) break OUTER;
315 ret.addElement(s.substring(p,x));
316 p = x + sep.length();
318 if(ret.length() == limit) break;
320 if(p < s.length() && ret.length() != limit)
321 ret.addElement(s.substring(p));
325 public static GnuRegexp.RE newRE(String pattern, int flags) throws JSExn {
327 return new GnuRegexp.RE(pattern,flags,GnuRegexp.RESyntax.RE_SYNTAX_PERL5);
328 } catch(GnuRegexp.REException e) {
329 throw new JSExn(e.toString());
333 public String typeName() { return "regexp"; }